Skip to content

Commit 35f064f

Browse files
author
Mika Leppänen
committed
Changed TCP socket test to use shared buffers
Changed RX and TX buffers used in TCP socket tests to global variables to conserve memory.
1 parent 9095b03 commit 35f064f

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

TESTS/netsocket/tcp/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ namespace
3434
NetworkInterface* net;
3535
}
3636

37+
char tcp_global::rx_buffer[RX_BUFF_SIZE];
38+
char tcp_global::tx_buffer[TX_BUFF_SIZE];
39+
3740
NetworkInterface* get_interface()
3841
{
3942
return net;

TESTS/netsocket/tcp/tcp_tests.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ void fill_tx_buffer_ascii(char *buff, size_t len);
2424
void tcpsocket_connect_to_echo_srv(TCPSocket& sock);
2525
void tcpsocket_connect_to_discard_srv(TCPSocket& sock);
2626

27+
namespace tcp_global
28+
{
29+
static const int TCP_OS_STACK_SIZE = 1024;
30+
31+
static const int RX_BUFF_SIZE = 1220;
32+
static const int TX_BUFF_SIZE = 1220;
33+
34+
extern char rx_buffer[RX_BUFF_SIZE];
35+
extern char tx_buffer[TX_BUFF_SIZE];
36+
}
37+
2738
/*
2839
* Test cases
2940
*/

TESTS/netsocket/tcp/tcpsocket_echotest.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ namespace
3131
static const int SIGIO_TIMEOUT = 5000; //[ms]
3232

3333
static const int BUFF_SIZE = 1200;
34-
char rx_buffer[BUFF_SIZE] = {0};
35-
char tx_buffer[BUFF_SIZE] = {0};
36-
3734
static const int PKTS = 22;
3835
static const int pkt_sizes[PKTS] = {1,2,3,4,5,6,7,8,9,10, \
3936
100,200,300,400,500,600,700,800,900,1000,\
@@ -54,24 +51,24 @@ void TCPSOCKET_ECHOTEST()
5451
int sent;
5552
int x = 0;
5653
for (int pkt_s = pkt_sizes[x]; x < PKTS; pkt_s = pkt_sizes[x++]) {
57-
fill_tx_buffer_ascii(tx_buffer, BUFF_SIZE);
54+
fill_tx_buffer_ascii(tcp_global::tx_buffer, BUFF_SIZE);
5855

59-
sent = sock.send(tx_buffer, pkt_s);
56+
sent = sock.send(tcp_global::tx_buffer, pkt_s);
6057
if (sent < 0) {
6158
printf("[Round#%02d] network error %d\n", x, sent);
6259
TEST_FAIL();
6360
}
6461

6562
int bytes2recv = sent;
6663
while (bytes2recv) {
67-
recvd = sock.recv(&(rx_buffer[sent-bytes2recv]), bytes2recv);
64+
recvd = sock.recv(&(tcp_global::rx_buffer[sent-bytes2recv]), bytes2recv);
6865
if (recvd < 0) {
6966
printf("[Round#%02d] network error %d\n", x, recvd);
7067
TEST_FAIL();
7168
}
7269
bytes2recv -= recvd;
7370
}
74-
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, sent));
71+
TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, sent));
7572
}
7673
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
7774
}
@@ -81,7 +78,7 @@ void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)
8178
int bytes2recv = *(int*)receive_bytes;
8279
int recvd;
8380
while (bytes2recv) {
84-
recvd = sock.recv(&(rx_buffer[*(int*)receive_bytes-bytes2recv]), bytes2recv);
81+
recvd = sock.recv(&(tcp_global::rx_buffer[*(int*)receive_bytes-bytes2recv]), bytes2recv);
8582
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
8683
wait(1);
8784
continue;
@@ -91,7 +88,7 @@ void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)
9188
bytes2recv -= recvd;
9289
}
9390

94-
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, *(int*)receive_bytes));
91+
TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, *(int*)receive_bytes));
9592

9693
static int round = 0;
9794
printf("[Recevr#%02d] bytes received: %d\n", round++, *(int*)receive_bytes);
@@ -110,22 +107,22 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
110107
int sent;
111108
int s_idx = 0;
112109
Thread *thread;
113-
unsigned char *stack_mem = (unsigned char *)malloc(OS_STACK_SIZE);
110+
unsigned char *stack_mem = (unsigned char *)malloc(tcp_global::TCP_OS_STACK_SIZE);
114111
TEST_ASSERT_NOT_NULL(stack_mem);
115112

116113
for (int pkt_s = pkt_sizes[s_idx]; s_idx < PKTS; ++s_idx) {
117114
pkt_s = pkt_sizes[s_idx];
118115
thread = new Thread(osPriorityNormal,
119-
OS_STACK_SIZE,
116+
tcp_global::TCP_OS_STACK_SIZE,
120117
stack_mem,
121118
"receiver");
122119
TEST_ASSERT_EQUAL(osOK, thread->start(callback(tcpsocket_echotest_nonblock_receiver, &pkt_s)));
123120

124-
fill_tx_buffer_ascii(tx_buffer, pkt_s);
121+
fill_tx_buffer_ascii(tcp_global::tx_buffer, pkt_s);
125122

126123
bytes2send = pkt_s;
127124
while (bytes2send > 0) {
128-
sent = sock.send(&(tx_buffer[pkt_s-bytes2send]), bytes2send);
125+
sent = sock.send(&(tcp_global::tx_buffer[pkt_s-bytes2send]), bytes2send);
129126
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
130127
TEST_ASSERT_NOT_EQUAL(osEventTimeout, osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status);
131128
continue;

TESTS/netsocket/tcp/tcpsocket_echotest_burst.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ namespace
3232

3333
static const int BURST_CNT = 100;
3434
static const int BURST_SIZE = 1220;
35-
char rx_buffer[BURST_SIZE] = {0};
36-
char tx_buffer[BURST_SIZE] = {0};
3735
}
3836

3937
static void _sigio_handler(osThreadId id) {
@@ -47,15 +45,15 @@ void TCPSOCKET_ECHOTEST_BURST()
4745
sock.sigio(callback(_sigio_handler, Thread::gettid()));
4846

4947
// TX buffer to be preserved for comparison
50-
fill_tx_buffer_ascii(tx_buffer, BURST_SIZE);
48+
fill_tx_buffer_ascii(tcp_global::tx_buffer, BURST_SIZE);
5149

5250
int recvd;
5351
int bt_left;
5452
int sent;
5553
for (int i = 0; i < BURST_CNT; i++) {
5654
bt_left = BURST_SIZE;
5755
while (bt_left > 0) {
58-
sent = sock.send(&(tx_buffer[BURST_SIZE-bt_left]), bt_left);
56+
sent = sock.send(&(tcp_global::tx_buffer[BURST_SIZE-bt_left]), bt_left);
5957
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
6058
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
6159
TEST_FAIL();
@@ -70,7 +68,7 @@ void TCPSOCKET_ECHOTEST_BURST()
7068

7169
bt_left = BURST_SIZE;
7270
while (bt_left > 0) {
73-
recvd = sock.recv(&(rx_buffer[BURST_SIZE-bt_left]), BURST_SIZE);
71+
recvd = sock.recv(&(tcp_global::rx_buffer[BURST_SIZE-bt_left]), BURST_SIZE);
7472
if (recvd < 0) {
7573
printf("[%02d] network error %d\n", i, recvd);
7674
break;
@@ -83,7 +81,7 @@ void TCPSOCKET_ECHOTEST_BURST()
8381
TEST_FAIL();
8482
}
8583

86-
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, BURST_SIZE));
84+
TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, BURST_SIZE));
8785
}
8886
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
8987
}
@@ -96,15 +94,15 @@ void TCPSOCKET_ECHOTEST_BURST_NONBLOCK()
9694
sock.sigio(callback(_sigio_handler, Thread::gettid()));
9795

9896
// TX buffer to be preserved for comparison
99-
fill_tx_buffer_ascii(tx_buffer, BURST_SIZE);
97+
fill_tx_buffer_ascii(tcp_global::tx_buffer, BURST_SIZE);
10098

10199
int sent;
102100
int recvd;
103101
int bt_left = 0;
104102
for (int i = 0; i < BURST_CNT; i++) {
105103
bt_left = BURST_SIZE;
106104
while (bt_left > 0) {
107-
sent = sock.send(&(tx_buffer[BURST_SIZE-bt_left]), bt_left);
105+
sent = sock.send(&(tcp_global::tx_buffer[BURST_SIZE-bt_left]), bt_left);
108106
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
109107
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
110108
TEST_FAIL();
@@ -120,7 +118,7 @@ void TCPSOCKET_ECHOTEST_BURST_NONBLOCK()
120118

121119
bt_left = BURST_SIZE;
122120
while (bt_left > 0) {
123-
recvd = sock.recv(&(rx_buffer[BURST_SIZE-bt_left]), BURST_SIZE);
121+
recvd = sock.recv(&(tcp_global::rx_buffer[BURST_SIZE-bt_left]), BURST_SIZE);
124122
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
125123
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
126124
printf("[bt#%02d] packet timeout...", i);
@@ -140,7 +138,7 @@ void TCPSOCKET_ECHOTEST_BURST_NONBLOCK()
140138
TEST_FAIL();
141139
}
142140

143-
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, BURST_SIZE));
141+
TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, BURST_SIZE));
144142
}
145143
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
146144
}

0 commit comments

Comments
 (0)