Skip to content

Commit 1b04172

Browse files
Veijo PesonenSeppo Takalo
authored andcommitted
Fixes Greentea UDP test cases
udpsocket_echotest.cpp udpsocket_echotest_burst.cpp
1 parent 4fa57e3 commit 1b04172

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

TESTS/netsocket/udp/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static void _ifdown() {
5555
printf("MBED: ifdown\n");
5656
}
5757

58-
void drop_bad_packets(UDPSocket& sock) {
58+
void drop_bad_packets(UDPSocket& sock, int orig_timeout) {
5959
nsapi_error_t err;
6060
sock.set_timeout(0);
6161
while (true) {
@@ -64,6 +64,7 @@ void drop_bad_packets(UDPSocket& sock) {
6464
break;
6565
}
6666
}
67+
sock.set_timeout(orig_timeout);
6768
}
6869

6970
void fill_tx_buffer_ascii(char *buff, size_t len)

TESTS/netsocket/udp/udp_tests.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#define UDP_TESTS_H
2020

2121
NetworkInterface* get_interface();
22-
void drop_bad_packets(UDPSocket& sock);
22+
void drop_bad_packets(UDPSocket& sock, int orig_timeout);
2323
void fill_tx_buffer_ascii(char *buff, size_t len);
2424

2525
/*

TESTS/netsocket/udp/udpsocket_echotest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ void udpsocket_echotest_nonblock_receiver(void *receive_bytes)
9090
recvd = sock.recvfrom(NULL, rx_buffer, expt2recv);
9191
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
9292
wait_ms(WAIT2RECV_TIMEOUT);
93+
--retry_cnt;
9394
continue;
9495
} else if (recvd == expt2recv) {
9596
break;
9697
}
9798
}
9899

99100
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, expt2recv));
100-
drop_bad_packets(sock);
101-
sock.set_blocking(false);
101+
drop_bad_packets(sock, -1); // timeout equivalent to set_blocking(false)
102102

103103
tx_sem.release();
104104
}
@@ -136,6 +136,7 @@ void test_udpsocket_echotest_nonblock()
136136
if (osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
137137
continue;
138138
}
139+
--retry_cnt;
139140
} else if (sent != pkt_s) {
140141
printf("[Round#%02d - Sender] error, returned %d\n", s_idx, sent);
141142
continue;

TESTS/netsocket/udp/udpsocket_echotest_burst.cpp

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#define SIGNAL_SIGIO 0x1
2727
#define SIGIO_TIMEOUT 5000 //[ms]
28+
#define RECV_TIMEOUT 1 //[s]
2829

2930
namespace
3031
{
@@ -56,78 +57,86 @@ void free_tx_buffers() {
5657
}
5758
}
5859

60+
static void _sigio_handler(osThreadId id) {
61+
osSignalSet(id, SIGNAL_SIGIO);
62+
}
63+
5964
void test_udpsocket_echotest_burst()
6065
{
6166
SocketAddress udp_addr;
6267
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
6368
udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
6469

6570
UDPSocket sock;
71+
const int TIMEOUT = 5000; // [ms]
6672
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
67-
sock.set_timeout(5000);
73+
sock.set_timeout(TIMEOUT);
74+
sock.sigio(callback(_sigio_handler, Thread::gettid()));
6875

6976
// TX buffers to be preserved for comparison
7077
prepare_tx_buffers();
7178

79+
int bt_total = 0;
7280
int ok_bursts = 0;
7381
int pkg_fail = 0;
74-
SocketAddress temp_addr;
7582
int recvd = 0;
76-
int bt_total = 0;
83+
int recv_timeout = RECV_TIMEOUT;;
84+
SocketAddress temp_addr;
7785
for (int i = 0; i < BURST_CNT; i++) {
7886
for (int x = 0; x < BURST_PKTS; x++) {
7987
TEST_ASSERT_EQUAL(tx_buffers[x].len, sock.sendto(udp_addr, tx_buffers[x].payload, tx_buffers[x].len));
8088
}
8189

82-
recvd = 0;
8390
bt_total = 0;
91+
recvd = 0;
8492
for (int j = 0; j < BURST_PKTS; j++) {
8593
recvd = sock.recvfrom(&temp_addr, rx_buffer, 500);
86-
if (recvd < 0) {
87-
pkg_fail++;
94+
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
95+
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
96+
pkg_fail += BURST_PKTS-j;
97+
break;
98+
}
99+
} else if (recvd < 0) {
100+
pkg_fail += BURST_PKTS-j; // Assume all the following packets of the burst to be lost
88101
printf("[%02d] network error %d\n", i, recvd);
89-
continue;
102+
wait(recv_timeout);
103+
recv_timeout *= 2; // Back off,
104+
break;
90105
} else if (temp_addr != udp_addr) {
91106
printf("[%02d] packet from wrong address\n", i);
107+
--j;
92108
continue;
93109
}
94110

111+
recv_timeout = recv_timeout > RECV_TIMEOUT ? recv_timeout/2 : RECV_TIMEOUT;
112+
95113
// Packets might arrive unordered
96114
for (int k = 0; k < BURST_PKTS; k++) {
97115
if (tx_buffers[k].len == recvd &&
98116
(memcmp(tx_buffers[k].payload, rx_buffer, recvd) == 0)) {
99117
bt_total += recvd;
100-
goto PKT_OK;
101118
}
102119
}
103-
pkg_fail++;
104-
break;
105-
PKT_OK:
106-
continue;
107120
}
108121

109122
if (bt_total == RECV_TOTAL) {
110123
ok_bursts++;
111124
} else {
112-
drop_bad_packets(sock);
125+
drop_bad_packets(sock, TIMEOUT);
113126
printf("[%02d] burst failure\n", i);
114127
}
115128
}
116129

117130
free_tx_buffers();
118131

119-
// Packet loss up to 10% tolerated
120-
TEST_ASSERT_INT_WITHIN((BURST_CNT*BURST_PKTS/10), BURST_CNT*BURST_PKTS, BURST_CNT*BURST_PKTS-pkg_fail);
121-
// 90% of the bursts need to be successful
122-
TEST_ASSERT_INT_WITHIN((BURST_CNT/10), BURST_CNT, ok_bursts);
132+
// Packet loss up to 1/4 tolerated
133+
TEST_ASSERT_INT_WITHIN((BURST_CNT*BURST_PKTS/4), BURST_CNT*BURST_PKTS, BURST_CNT*BURST_PKTS-pkg_fail);
134+
// 3/4 of the bursts need to be successful
135+
TEST_ASSERT_INT_WITHIN((BURST_CNT/4), BURST_CNT, ok_bursts);
123136

124137
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
125138
}
126139

127-
static void _sigio_handler(osThreadId id) {
128-
osSignalSet(id, SIGNAL_SIGIO);
129-
}
130-
131140
void test_udpsocket_echotest_burst_nonblock()
132141
{
133142
SocketAddress udp_addr;
@@ -188,7 +197,7 @@ void test_udpsocket_echotest_burst_nonblock()
188197
if (bt_total == RECV_TOTAL) {
189198
ok_bursts++;
190199
} else {
191-
drop_bad_packets(sock);
200+
drop_bad_packets(sock, -1); // timeout equivalent to set_blocking(false)
192201
sock.set_blocking(false);
193202
}
194203
}

0 commit comments

Comments
 (0)