Skip to content

Commit 114efcf

Browse files
committed
Adding retries to the NIST test.
A UDP request to the NIST servers can fail to return data due to UDP packet loss. Since packets are not guaranteed with UDP, this is a valid failure and should not be treated as a test failure. The test should retry the request in this case. This commit adds those retries.
1 parent 52658e5 commit 114efcf

File tree

1 file changed

+29
-20
lines changed
  • features/net/FEATURE_IPV4/TESTS/mbedmicro-net/nist_internet_time_service

1 file changed

+29
-20
lines changed

features/net/FEATURE_IPV4/TESTS/mbedmicro-net/nist_internet_time_service/main.cpp

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,64 @@
88
#include "greentea-client/test_env.h"
99

1010
namespace {
11-
//const char *HTTP_SERVER_NAME = "utcnist.colorado.edu";
1211
const char *HTTP_SERVER_NAME = "pool.ntp.org";
1312
const int HTTP_SERVER_PORT = 123;
1413
}
1514

1615

1716
int main() {
18-
GREENTEA_SETUP(20, "default_auto");
17+
GREENTEA_SETUP(60, "default_auto");
1918

2019
bool result = false;
2120
const time_t TIME1970 = 2208988800L;
22-
int ntp_values[12] = {0};
21+
int ntp_send_values[12] = {0};
22+
int ntp_recv_values[12] = {0};
2323

2424
EthernetInterface eth;
25-
//eth.init(); //Use DHCP
2625
eth.connect();
2726
printf("UDP client IP Address is %s\n", eth.get_ip_address());
2827

2928
UDPSocket sock;
3029
sock.open(&eth);
30+
sock.set_timeout(15000);
3131

3232
SocketAddress nist(&eth, HTTP_SERVER_NAME, HTTP_SERVER_PORT);
3333

3434
printf("UDP: NIST server %s address: %s on port %d\r\n", HTTP_SERVER_NAME, nist.get_ip_address(), nist.get_port());
3535

36-
memset(ntp_values, 0x00, sizeof(ntp_values));
37-
ntp_values[0] = '\x1b';
36+
memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
37+
ntp_send_values[0] = '\x1b';
3838

39-
int ret_send = sock.sendto(nist, (void*)ntp_values, sizeof(ntp_values));
40-
printf("UDP: Sent %d Bytes to NTP server \n", ret_send);
39+
while(1) {
40+
memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values));
4141

42-
const int n = sock.recvfrom(&nist, (void*)ntp_values, sizeof(ntp_values));
42+
int ret_send = sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values));
43+
printf("UDP: Sent %d Bytes to NTP server \n", ret_send);
4344

44-
printf("UDP: Recved from NTP server %d Bytes \n", n);
45+
const int n = sock.recvfrom(&nist, (void*)ntp_recv_values, sizeof(ntp_recv_values));
4546

46-
if (n > 0 ) {
47-
result = true;
47+
printf("UDP: Recved from NTP server %d Bytes \n", n);
4848

49-
printf("UDP: Values returned by NTP server: \n");
50-
for (size_t i=0; i < sizeof(ntp_values) / sizeof(ntp_values[0]); ++i) {
51-
printf("\t[%02d] 0x%X", i, ntohl(ntp_values[i]));
49+
if (n > 0) {
50+
result = true;
5251

53-
if (i == 10) {
54-
time_t timestamp = ntohl(ntp_values[i]) - TIME1970;
55-
printf("\tNTP timestamp is %s", ctime(&timestamp));
56-
} else {
57-
printf("\n");
52+
printf("UDP: Values returned by NTP server: \n");
53+
for (size_t i=0; i < sizeof(ntp_recv_values) / sizeof(ntp_recv_values[0]); ++i) {
54+
printf("\t[%02d] 0x%X", i, ntohl(ntp_recv_values[i]));
55+
56+
if (i == 10) {
57+
time_t timestamp = ntohl(ntp_recv_values[i]) - TIME1970;
58+
printf("\tNTP timestamp is %s", ctime(&timestamp));
59+
} else {
60+
printf("\n");
61+
}
5862
}
63+
64+
break;
5965
}
66+
67+
printf("Failed to receive data, retrying in 5 seconds...\n");
68+
wait(5);
6069
}
6170

6271
sock.close();

0 commit comments

Comments
 (0)