Skip to content

Commit 4937918

Browse files
Veijo PesonenCruz Monrreal II
authored andcommitted
Makes Greentea TCP test cases to timeout less in connection errors
Made to prevent timeout if a single test case fails. The goal is that each test case might wait only half of the remaining time reserved for running TCP test cases.
1 parent 98134a4 commit 4937918

10 files changed

+174
-44
lines changed

TESTS/netsocket/tcp/main.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ using namespace utest::v1;
3232
namespace
3333
{
3434
NetworkInterface* net;
35+
Timer tc_bucket; // Timer to limit a test cases run time
3536
}
3637

3738
char tcp_global::rx_buffer[RX_BUFF_SIZE];
@@ -66,24 +67,32 @@ static void _ifdown() {
6667
printf("MBED: ifdown\n");
6768
}
6869

69-
void tcpsocket_connect_to_echo_srv(TCPSocket& sock) {
70+
nsapi_error_t tcpsocket_connect_to_echo_srv(TCPSocket& sock) {
7071
SocketAddress tcp_addr;
7172

7273
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
7374
tcp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
7475

75-
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
76-
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(tcp_addr));
76+
nsapi_error_t err = sock.open(get_interface());
77+
if (err != NSAPI_ERROR_OK) {
78+
return err;
79+
}
80+
81+
return sock.connect(tcp_addr);
7782
}
7883

79-
void tcpsocket_connect_to_discard_srv(TCPSocket& sock) {
84+
nsapi_error_t tcpsocket_connect_to_discard_srv(TCPSocket& sock) {
8085
SocketAddress tcp_addr;
8186

8287
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
8388
tcp_addr.set_port(9);
8489

85-
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
86-
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(tcp_addr));
90+
nsapi_error_t err = sock.open(get_interface());
91+
if (err != NSAPI_ERROR_OK) {
92+
return err;
93+
}
94+
95+
return sock.connect(tcp_addr);
8796
}
8897

8998
void fill_tx_buffer_ascii(char *buff, size_t len)
@@ -93,16 +102,23 @@ void fill_tx_buffer_ascii(char *buff, size_t len)
93102
}
94103
}
95104

105+
int split2half_rmng_tcp_test_time()
106+
{
107+
return (tcp_global::TESTS_TIMEOUT-tc_bucket.read())/2;
108+
}
109+
96110
// Test setup
97111
utest::v1::status_t greentea_setup(const size_t number_of_cases)
98112
{
99-
GREENTEA_SETUP(480, "default_auto");
113+
GREENTEA_SETUP(tcp_global::TESTS_TIMEOUT, "default_auto");
100114
_ifup();
115+
tc_bucket.start();
101116
return greentea_test_setup_handler(number_of_cases);
102117
}
103118

104119
void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure)
105120
{
121+
tc_bucket.stop();
106122
_ifdown();
107123
return greentea_test_teardown_handler(passed, failed, failure);
108124
}

TESTS/netsocket/tcp/tcp_tests.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@
2121
NetworkInterface* get_interface();
2222
void drop_bad_packets(TCPSocket& sock, int orig_timeout);
2323
void fill_tx_buffer_ascii(char *buff, size_t len);
24-
void tcpsocket_connect_to_echo_srv(TCPSocket& sock);
25-
void tcpsocket_connect_to_discard_srv(TCPSocket& sock);
24+
nsapi_error_t tcpsocket_connect_to_echo_srv(TCPSocket& sock);
25+
nsapi_error_t tcpsocket_connect_to_discard_srv(TCPSocket& sock);
26+
27+
/**
28+
* Single testcase might take only half of the remaining execution time
29+
*/
30+
int split2half_rmng_tcp_test_time(); // [s]
2631

2732
namespace tcp_global
2833
{
34+
static const int TESTS_TIMEOUT = 480;
2935
static const int TCP_OS_STACK_SIZE = 1024;
3036

3137
static const int RX_BUFF_SIZE = 1220;

TESTS/netsocket/tcp/tcpsocket_echotest.cpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ namespace
3737
1100,1200};
3838
TCPSocket sock;
3939
Semaphore tx_sem(0, 1);
40+
41+
Timer tc_exec_time;
42+
int time_allotted;
4043
}
4144

4245
static void _sigio_handler(osThreadId id) {
@@ -45,7 +48,10 @@ static void _sigio_handler(osThreadId id) {
4548

4649
void TCPSOCKET_ECHOTEST()
4750
{
48-
tcpsocket_connect_to_echo_srv(sock);
51+
if (tcpsocket_connect_to_echo_srv(sock) != NSAPI_ERROR_OK) {
52+
TEST_FAIL();
53+
return;
54+
}
4955

5056
int recvd;
5157
int sent;
@@ -57,6 +63,8 @@ void TCPSOCKET_ECHOTEST()
5763
if (sent < 0) {
5864
printf("[Round#%02d] network error %d\n", x, sent);
5965
TEST_FAIL();
66+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
67+
return;
6068
}
6169

6270
int bytes2recv = sent;
@@ -65,6 +73,8 @@ void TCPSOCKET_ECHOTEST()
6573
if (recvd < 0) {
6674
printf("[Round#%02d] network error %d\n", x, recvd);
6775
TEST_FAIL();
76+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
77+
return;
6878
}
6979
bytes2recv -= recvd;
7080
}
@@ -80,10 +90,15 @@ void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)
8090
while (bytes2recv) {
8191
recvd = sock.recv(&(tcp_global::rx_buffer[*(int*)receive_bytes-bytes2recv]), bytes2recv);
8292
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
93+
if (tc_exec_time.read() >= time_allotted) {
94+
TEST_FAIL();
95+
break;
96+
}
8397
wait(1);
8498
continue;
8599
} else if (recvd < 0) {
86100
TEST_FAIL();
101+
break;
87102
}
88103
bytes2recv -= recvd;
89104
}
@@ -99,6 +114,9 @@ void tcpsocket_echotest_nonblock_receiver(void *receive_bytes)
99114

100115
void TCPSOCKET_ECHOTEST_NONBLOCK()
101116
{
117+
tc_exec_time.start();
118+
time_allotted = split2half_rmng_tcp_test_time(); // [s]
119+
102120
tcpsocket_connect_to_echo_srv(sock);
103121
sock.set_blocking(false);
104122
sock.sigio(callback(_sigio_handler, Thread::gettid()));
@@ -124,19 +142,30 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
124142
while (bytes2send > 0) {
125143
sent = sock.send(&(tcp_global::tx_buffer[pkt_s-bytes2send]), bytes2send);
126144
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
127-
TEST_ASSERT_NOT_EQUAL(osEventTimeout, osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status);
145+
if (tc_exec_time.read() >= time_allotted ||
146+
osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
147+
thread->terminate();
148+
delete thread;
149+
TEST_FAIL();
150+
goto END;
151+
}
128152
continue;
129153
} else if (sent <= 0) {
130154
printf("[Sender#%02d] network error %d\n", s_idx, sent);
155+
thread->terminate();
156+
delete thread;
131157
TEST_FAIL();
158+
goto END;
132159
}
133160
bytes2send -= sent;
134161
}
135162
printf("[Sender#%02d] bytes sent: %d\n", s_idx, pkt_s);
136-
tx_sem.wait();
163+
tx_sem.wait(split2half_rmng_tcp_test_time());
137164
thread->join();
138165
delete thread;
139166
}
167+
END:
168+
tc_exec_time.stop();
140169
free(stack_mem);
141170
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
142171
}

TESTS/netsocket/tcp/tcpsocket_echotest_burst.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ void TCPSOCKET_ECHOTEST_BURST()
5757
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
5858
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
5959
TEST_FAIL();
60+
goto END;
6061
}
6162
continue;
6263
} else if (sent < 0) {
6364
printf("[%02d] network error %d\n", i, sent);
6465
TEST_FAIL();
66+
goto END;
6567
}
6668
bt_left -= sent;
6769
}
@@ -79,10 +81,12 @@ void TCPSOCKET_ECHOTEST_BURST()
7981
if (bt_left != 0) {
8082
drop_bad_packets(sock, 0);
8183
TEST_FAIL();
84+
goto END;
8285
}
8386

8487
TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, BURST_SIZE));
8588
}
89+
END:
8690
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
8791
}
8892

@@ -106,15 +110,20 @@ void TCPSOCKET_ECHOTEST_BURST_NONBLOCK()
106110
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
107111
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
108112
TEST_FAIL();
113+
goto END;
109114
}
110115
continue;
111116
} else if (sent < 0) {
112117
printf("[%02d] network error %d\n", i, sent);
113118
TEST_FAIL();
119+
goto END;
114120
}
115121
bt_left -= sent;
116122
}
117-
TEST_ASSERT_EQUAL(0, bt_left);
123+
if (bt_left != 0) {
124+
TEST_FAIL();
125+
goto END;
126+
}
118127

119128
bt_left = BURST_SIZE;
120129
while (bt_left > 0) {
@@ -136,9 +145,11 @@ void TCPSOCKET_ECHOTEST_BURST_NONBLOCK()
136145
printf("network error %d, missing %d bytes from a burst\n", recvd, bt_left);
137146
drop_bad_packets(sock, -1);
138147
TEST_FAIL();
148+
goto END;
139149
}
140150

141151
TEST_ASSERT_EQUAL(0, memcmp(tcp_global::tx_buffer, tcp_global::rx_buffer, BURST_SIZE));
142152
}
153+
END:
143154
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
144155
}

TESTS/netsocket/tcp/tcpsocket_endpoint_close.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,34 @@ static void _sigio_handler(osThreadId id) {
3535
osSignalSet(id, SIGNAL_SIGIO);
3636
}
3737

38-
static void _tcpsocket_connect_to_daytime_srv(TCPSocket& sock) {
38+
static nsapi_error_t _tcpsocket_connect_to_daytime_srv(TCPSocket& sock) {
3939
SocketAddress tcp_addr;
4040

4141
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
4242
tcp_addr.set_port(13);
4343

44-
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
45-
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(tcp_addr));
44+
nsapi_error_t err = sock.open(get_interface());
45+
if (err != NSAPI_ERROR_OK) {
46+
return err;
47+
}
48+
49+
return sock.connect(tcp_addr);
4650
}
4751

4852

4953
void TCPSOCKET_ENDPOINT_CLOSE()
5054
{
5155
static const int MORE_THAN_AVAILABLE = 30;
5256
char buff[MORE_THAN_AVAILABLE];
57+
int time_allotted = split2half_rmng_tcp_test_time(); // [s]
58+
Timer tc_exec_time;
59+
tc_exec_time.start();
5360

5461
TCPSocket sock;
55-
_tcpsocket_connect_to_daytime_srv(sock);
62+
if (_tcpsocket_connect_to_daytime_srv(sock) != NSAPI_ERROR_OK) {
63+
TEST_FAIL();
64+
return;
65+
}
5666
sock.sigio(callback(_sigio_handler, Thread::gettid()));
5767

5868
int recvd = 0;
@@ -61,16 +71,20 @@ void TCPSOCKET_ENDPOINT_CLOSE()
6171
recvd = sock.recv(&(buff[recvd_total]), MORE_THAN_AVAILABLE);
6272
if (recvd_total > 0 && recvd == 0) {
6373
break; // Endpoint closed socket, success
64-
} else if (recvd == 0) {
74+
} else if (recvd <= 0) {
6575
TEST_FAIL();
76+
break;
6677
} else if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
67-
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
78+
if(tc_exec_time.read() >= time_allotted ||
79+
osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
6880
TEST_FAIL();
81+
break;
6982
}
7083
continue;
7184
}
7285
recvd_total += recvd;
7386
TEST_ASSERT(recvd_total < MORE_THAN_AVAILABLE);
7487
}
88+
tc_exec_time.stop();
7589
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
7690
}

0 commit comments

Comments
 (0)