Skip to content

Commit 0eb1749

Browse files
committed
Fix bad test print and move locks for printf into macro.
A message of "Failed to close socket" was always being printed, not just when the socket failed to close. This patch fixes this in addition to a simplifying the call to a thread safe printf.
1 parent 35999be commit 0eb1749

File tree

1 file changed

+19
-17
lines changed
  • features/FEATURE_LWIP/TESTS/mbedmicro-net/udp_echo_parallel

1 file changed

+19
-17
lines changed

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ SocketAddress udp_addr;
3434
Mutex iomutex;
3535
char uuid[GREENTEA_UUID_LENGTH] = {0};
3636

37+
// Thread safe printf macro
38+
#define TS_PRINTF(...) {\
39+
iomutex.lock();\
40+
printf(__VA_ARGS__);\
41+
iomutex.unlock();\
42+
}
43+
3744
// NOTE: assuming that "id" stays in the single digits
3845
//
3946
// Creates a buffer that first contains the thread's id.
@@ -64,7 +71,6 @@ void prep_buffer(unsigned int id, char *uuid, char *tx_buffer, size_t tx_size) {
6471
}
6572
}
6673

67-
6874
// Each echo class is in charge of one parallel transaction
6975
class Echo {
7076
private:
@@ -105,36 +111,26 @@ class Echo {
105111
prep_buffer(id, uuid, tx_buffer, sizeof(tx_buffer));
106112
int ret = sock.sendto(udp_addr, tx_buffer, sizeof(tx_buffer));
107113
if (ret >= 0) {
108-
iomutex.lock();
109-
printf("[ID:%01u][%02u] sent %d bytes - %.*s \n", id, i, ret, ret, tx_buffer);
110-
iomutex.unlock();
114+
TS_PRINTF("[ID:%01u][%02u] sent %d bytes - %.*s \n", id, i, ret, ret, tx_buffer);
111115
} else {
112-
iomutex.lock();
113-
printf("[ID:%01u][%02u] Network error %d\n", id, i, ret);
114-
iomutex.unlock();
116+
TS_PRINTF("[ID:%01u][%02u] Network error %d\n", id, i, ret);
115117
continue;
116118
}
117119

118120
SocketAddress temp_addr;
119121
ret = sock.recvfrom(&temp_addr, rx_buffer, sizeof(rx_buffer));
120122
if (ret >= 0) {
121-
iomutex.lock();
122-
printf("[ID:%01u][%02u] recv %d bytes - %.*s \n", id, i, ret, ret, tx_buffer);
123-
iomutex.unlock();
123+
TS_PRINTF("[ID:%01u][%02u] recv %d bytes - %.*s \n", id, i, ret, ret, tx_buffer);
124124
} else {
125-
iomutex.lock();
126-
printf("[ID:%01u][%02u] Network error %d\n", id, i, ret);
127-
iomutex.unlock();
125+
TS_PRINTF("[ID:%01u][%02u] Network error %d\n", id, i, ret);
128126
continue;
129127
}
130128

131129
if ((temp_addr == udp_addr &&
132130
ret == sizeof(tx_buffer) &&
133131
memcmp(rx_buffer, tx_buffer, sizeof(rx_buffer)) == 0)) {
134132
success += 1;
135-
iomutex.lock();
136-
printf("[ID:%01u][%02u] success #%d\n", id, i, success);
137-
iomutex.unlock();
133+
TS_PRINTF("[ID:%01u][%02u] success #%d\n", id, i, success);
138134
continue;
139135
}
140136

@@ -151,9 +147,15 @@ class Echo {
151147

152148
result = success == ECHO_LOOPS;
153149

150+
if (result) {
151+
TS_PRINTF("[ID:%01u] Succeeded all %d times!\n", id, success);
152+
} else {
153+
TS_PRINTF("[ID:%01u] Only succeeded %d times out of a required %d.\n", id, success, ECHO_LOOPS);
154+
}
155+
154156
err = sock.close();
155-
printf("[ID:%01u] Failed to close socket!\n", id);
156157
if (err) {
158+
TS_PRINTF("[ID:%01u] Failed to close socket!\n", id);
157159
result = false;
158160
}
159161
}

0 commit comments

Comments
 (0)