Skip to content

Commit 27a97f9

Browse files
bridadangeky
authored andcommitted
Making udp_echo_parallel test more robust against network issues.
Making the test more forgiven for minor networking issues. Also adding more debug prints to make it easier to see which packets are coming from where.
1 parent 8222627 commit 27a97f9

File tree

1 file changed

+77
-34
lines changed
  • features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel

1 file changed

+77
-34
lines changed

features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel/main.cpp

Lines changed: 77 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,20 @@ EthernetInterface net;
2929
SocketAddress udp_addr;
3030
Mutex iomutex;
3131

32-
void prep_buffer(char *tx_buffer, size_t tx_size) {
33-
for (size_t i=0; i<tx_size; ++i) {
32+
// NOTE: assuming that "id" stays in the single digits
33+
void prep_buffer(int id, char *uuid_buffer, size_t uuid_len, char *tx_buffer, size_t tx_size) {
34+
size_t i = 2;
35+
36+
tx_buffer[0] = '0' + id;
37+
tx_buffer[1] = ' ';
38+
39+
for (; i<uuid_len + 2; ++i) {
40+
tx_buffer[i] = uuid_buffer[i - 2];
41+
}
42+
43+
tx_buffer[i++] = ' ';
44+
45+
for (; i<tx_size; ++i) {
3446
tx_buffer[i] = (rand() % 10) + '0';
3547
}
3648
}
@@ -44,15 +56,21 @@ class Echo {
4456

4557
UDPSocket sock;
4658
Thread thread;
59+
bool result;
60+
int id;
61+
char *uuid_buffer;
62+
size_t uuid_len;
4763

4864
public:
4965
// Limiting stack size to 1k
50-
Echo(): thread(osPriorityNormal, 1024) {
66+
Echo(): thread(osPriorityNormal, 1024), result(false) {
5167
}
5268

53-
void start() {
69+
void start(int id, char *uuid_buffer, size_t uuid_len) {
70+
this->id = id;
71+
this->uuid_buffer = uuid_buffer;
72+
this->uuid_len = uuid_len;
5473
osStatus status = thread.start(callback(this, &Echo::echo));
55-
TEST_ASSERT_EQUAL(osOK, status);
5674
}
5775

5876
void join() {
@@ -68,70 +86,95 @@ class Echo {
6886

6987
sock.set_timeout(MBED_CFG_UDP_CLIENT_ECHO_TIMEOUT);
7088

71-
for (int i = 0; i < ECHO_LOOPS; i++) {
72-
prep_buffer(tx_buffer, sizeof(tx_buffer));
89+
int i = 0;
90+
while(success < ECHO_LOOPS) {
91+
prep_buffer(id, uuid_buffer, uuid_len, tx_buffer, sizeof(tx_buffer));
7392
const int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
7493
iomutex.lock();
75-
printf("[%02d] sent...%d Bytes \n", i, ret);
94+
printf("[ID:%01d][%02d] sent %d Bytes - %.*s\n", id, i, ret, MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE, tx_buffer);
7695
iomutex.unlock();
7796

7897
SocketAddress temp_addr;
7998
const int n = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
8099
iomutex.lock();
81-
printf("[%02d] recv...%d Bytes \n", i, n);
100+
printf("[ID:%01d][%02d] recv %d Bytes - %.*s\n", id, i, ret, MBED_CFG_UDP_CLIENT_ECHO_BUFFER_SIZE, rx_buffer);
82101
iomutex.unlock();
83102

84103
if ((temp_addr == udp_addr &&
85104
n == sizeof(tx_buffer) &&
86105
memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
87106
success += 1;
107+
iomutex.lock();
108+
printf("[ID:%01d][%02d] success #%d\n", id, i, success);
109+
iomutex.unlock();
88110
}
111+
112+
i++;
89113
}
90114

115+
result = success == ECHO_LOOPS;
116+
91117
err = sock.close();
92118
TEST_ASSERT_EQUAL(0, err);
119+
if (err) {
120+
result = false;
121+
}
122+
}
93123

94-
TEST_ASSERT(success > 3*ECHO_LOOPS/4);
124+
bool get_result() {
125+
return result;
95126
}
96127
};
97128

98129
Echo echoers[MBED_CFG_UDP_CLIENT_ECHO_THREADS];
99130

100131

101132
int main() {
102-
GREENTEA_SETUP(60, "udp_echo");
133+
char uuid[48] = {0};
134+
GREENTEA_SETUP_UUID(60, "udp_echo", uuid, 48);
135+
printf("Got a uuid of %s\r\n", uuid);
136+
size_t uuid_len = strlen(uuid);
103137

104138
int err = net.connect();
105139
TEST_ASSERT_EQUAL(0, err);
106-
printf("UDP client IP Address is %s\n", net.get_ip_address());
107140

108-
greentea_send_kv("target_ip", net.get_ip_address());
141+
if (err) {
142+
printf("MBED: failed to connect with an error of %d\r\n", err);
143+
GREENTEA_TESTSUITE_RESULT(false);
144+
} else {
145+
printf("UDP client IP Address is %s\n", net.get_ip_address());
109146

110-
char recv_key[] = "host_port";
111-
char ipbuf[60] = {0};
112-
char portbuf[16] = {0};
113-
unsigned int port = 0;
147+
greentea_send_kv("target_ip", net.get_ip_address());
114148

115-
greentea_send_kv("host_ip", " ");
116-
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
149+
char recv_key[] = "host_port";
150+
char ipbuf[60] = {0};
151+
char portbuf[16] = {0};
152+
unsigned int port = 0;
117153

118-
greentea_send_kv("host_port", " ");
119-
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
120-
sscanf(portbuf, "%u", &port);
154+
greentea_send_kv("host_ip", " ");
155+
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
121156

122-
printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
123-
udp_addr.set_ip_address(ipbuf);
124-
udp_addr.set_port(port);
157+
greentea_send_kv("host_port", " ");
158+
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
159+
sscanf(portbuf, "%u", &port);
125160

126-
// Startup echo threads in parallel
127-
for (int i = 0; i < MBED_CFG_UDP_CLIENT_ECHO_THREADS; i++) {
128-
echoers[i].start();
129-
}
161+
printf("MBED: UDP Server IP address received: %s:%d \n", ipbuf, port);
162+
udp_addr.set_ip_address(ipbuf);
163+
udp_addr.set_port(port);
130164

131-
for (int i = 0; i < MBED_CFG_UDP_CLIENT_ECHO_THREADS; i++) {
132-
echoers[i].join();
133-
}
165+
// Startup echo threads in parallel
166+
for (int i = 0; i < MBED_CFG_UDP_CLIENT_ECHO_THREADS; i++) {
167+
echoers[i].start(i, uuid, uuid_len);
168+
}
134169

135-
net.disconnect();
136-
GREENTEA_TESTSUITE_RESULT(true);
170+
bool result = true;
171+
172+
for (int i = 0; i < MBED_CFG_UDP_CLIENT_ECHO_THREADS; i++) {
173+
echoers[i].join();
174+
result = result && echoers[i].get_result();
175+
}
176+
177+
net.disconnect();
178+
GREENTEA_TESTSUITE_RESULT(result);
179+
}
137180
}

0 commit comments

Comments
 (0)