Skip to content

Commit e7ea292

Browse files
author
Deepika
committed
Update udp test cases to verify network socket statistics
Cases Updated are: 1. UDPSOCKET_ECHOTEST_NONBLOCK 2. UDPSOCKET_OPEN_CLOSE_REPEAT 3. UDPSOCKET_OPEN_LIMIT
1 parent 09b4bc0 commit e7ea292

File tree

5 files changed

+54
-0
lines changed

5 files changed

+54
-0
lines changed

TESTS/netsocket/udp/main.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ namespace {
3737
NetworkInterface *net;
3838
}
3939

40+
#if defined(MBED_NW_STATS_ENABLED)
41+
mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT] = {0};
42+
#endif
43+
4044
NetworkInterface *get_interface()
4145
{
4246
return net;
@@ -76,6 +80,15 @@ void fill_tx_buffer_ascii(char *buff, size_t len)
7680
}
7781
}
7882

83+
int fetch_stats()
84+
{
85+
#if defined(MBED_NW_STATS_ENABLED)
86+
return SocketStats::mbed_stats_socket_get_each(&udp_stats[0], MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT);
87+
#else
88+
return 0;
89+
#endif
90+
}
91+
7992
// Test setup
8093
utest::v1::status_t greentea_setup(const size_t number_of_cases)
8194
{

TESTS/netsocket/udp/udp_tests.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
NetworkInterface *get_interface();
2222
void drop_bad_packets(UDPSocket &sock, int orig_timeout);
2323
void fill_tx_buffer_ascii(char *buff, size_t len);
24+
int fetch_stats(void);
25+
26+
#if defined(MBED_NW_STATS_ENABLED)
27+
extern mbed_stats_socket_t udp_stats[MBED_CONF_NSAPI_SOCKET_STATS_MAX_COUNT];
28+
#endif
2429

2530
/*
2631
* Test cases

TESTS/netsocket/udp/udpsocket_echotest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ void udpsocket_echotest_nonblock_receiver(void *receive_bytes)
121121

122122
void UDPSOCKET_ECHOTEST_NONBLOCK()
123123
{
124+
#if defined(MBED_NW_STATS_ENABLED)
125+
int j = 0;
126+
int count = fetch_stats();
127+
for (; j < count; j++) {
128+
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
129+
}
130+
#endif
131+
124132
SocketAddress udp_addr;
125133
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &udp_addr);
126134
udp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
@@ -174,6 +182,14 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
174182
}
175183
}
176184
free(stack_mem);
185+
186+
#if defined(MBED_NW_STATS_ENABLED)
187+
TEST_ASSERT_EQUAL(1, fetch_stats());
188+
TEST_ASSERT_EQUAL(NSAPI_UDP, udp_stats[0].proto);
189+
TEST_ASSERT(udp_stats[0].sent_bytes != 0);
190+
TEST_ASSERT(udp_stats[0].recv_bytes != 0);
191+
#endif
192+
177193
// Packet loss up to 30% tolerated
178194
if (packets_sent > 0) {
179195
double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);

TESTS/netsocket/udp/udpsocket_open_close_repeat.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ using namespace utest::v1;
2626

2727
void UDPSOCKET_OPEN_CLOSE_REPEAT()
2828
{
29+
int count = fetch_stats();
30+
for (int j = 0; j < count; j++) {
31+
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
32+
}
33+
2934
UDPSocket *sock = new UDPSocket;
3035
if (!sock) {
3136
TEST_FAIL();
@@ -36,4 +41,9 @@ void UDPSOCKET_OPEN_CLOSE_REPEAT()
3641
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
3742
}
3843
delete sock;
44+
45+
count = fetch_stats();
46+
for (int j = 0; j < count; j++) {
47+
TEST_ASSERT_EQUAL(SOCK_CLOSED, udp_stats[j].state);
48+
}
3949
}

TESTS/netsocket/udp/udpsocket_open_limit.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "UDPSocket.h"
2222
#include "unity/unity.h"
2323
#include "utest.h"
24+
#include "SocketStats.h"
2425

2526
using namespace utest::v1;
2627

@@ -70,6 +71,15 @@ void UDPSOCKET_OPEN_LIMIT()
7071
break;
7172
}
7273

74+
int count = fetch_stats();
75+
int open_count = 0;
76+
for (int j = 0; j < count; j++) {
77+
if ((udp_stats[j].state == SOCK_OPEN) && (udp_stats[j].proto == NSAPI_UDP)) {
78+
open_count++;
79+
}
80+
}
81+
TEST_ASSERT(open_count >= 3);
82+
7383
UDPSocketItem *tmp;
7484
for (UDPSocketItem *it = socket_list_head; it;) {
7585
++open_sockets[i];

0 commit comments

Comments
 (0)