Skip to content

Commit f962854

Browse files
bridadanadbridge
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 89be202 commit f962854

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,69 +86,94 @@ 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
int main() {
99-
GREENTEA_SETUP(60, "udp_echo");
130+
char uuid[48] = {0};
131+
GREENTEA_SETUP_UUID(60, "udp_echo", uuid, 48);
132+
printf("Got a uuid of %s\r\n", uuid);
133+
size_t uuid_len = strlen(uuid);
100134

101135
Echo echoers[MBED_CFG_UDP_CLIENT_ECHO_THREADS];
102136

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

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

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

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

117-
greentea_send_kv("host_port", " ");
118-
greentea_parse_kv(recv_key, portbuf, sizeof(recv_key), sizeof(ipbuf));
119-
sscanf(portbuf, "%u", &port);
153+
greentea_send_kv("host_ip", " ");
154+
greentea_parse_kv(recv_key, ipbuf, sizeof(recv_key), sizeof(ipbuf));
120155

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

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

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

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

0 commit comments

Comments
 (0)