Skip to content

Commit db8a018

Browse files
authored
Merge pull request #9959 from michalpasztamobica/refactor_socket_stats_usage
Refactor socket stats to reduce boiler plate
2 parents 4a2985a + b7ed4b5 commit db8a018

31 files changed

+136
-354
lines changed

TESTS/netsocket/tcp/main.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,38 @@ void greentea_teardown(const size_t passed, const size_t failed, const failure_t
149149
return greentea_test_teardown_handler(passed, failed, failure);
150150
}
151151

152+
utest::v1::status_t greentea_case_setup_handler_tcp(const Case *const source, const size_t index_of_case)
153+
{
154+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
155+
int count = fetch_stats();
156+
for (int j = 0; j < count; j++) {
157+
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
158+
}
159+
#endif
160+
return greentea_case_setup_handler(source, index_of_case);
161+
}
162+
163+
utest::v1::status_t greentea_case_teardown_handler_tcp(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
164+
{
165+
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
166+
int count = fetch_stats();
167+
for (int j = 0; j < count; j++) {
168+
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
169+
}
170+
#endif
171+
return greentea_case_teardown_handler(source, passed, failed, failure);
172+
}
173+
174+
static void test_failure_handler(const failure_t failure)
175+
{
176+
UTEST_LOG_FUNCTION();
177+
if (failure.location == LOCATION_TEST_SETUP || failure.location == LOCATION_TEST_TEARDOWN) {
178+
verbose_test_failure_handler(failure);
179+
GREENTEA_TESTSUITE_RESULT(false);
180+
while (1) ;
181+
}
182+
}
183+
152184

153185
Case cases[] = {
154186
Case("TCPSOCKET_ECHOTEST", TCPSOCKET_ECHOTEST),
@@ -178,7 +210,16 @@ Case cases[] = {
178210
Case("TCPSOCKET_ENDPOINT_CLOSE", TCPSOCKET_ENDPOINT_CLOSE),
179211
};
180212

181-
Specification specification(greentea_setup, cases, greentea_teardown, greentea_continue_handlers);
213+
handlers_t tcp_test_case_handlers = {
214+
default_greentea_test_setup_handler,
215+
greentea_test_teardown_handler,
216+
test_failure_handler,
217+
greentea_case_setup_handler_tcp,
218+
greentea_case_teardown_handler_tcp,
219+
greentea_case_failure_continue_handler
220+
};
221+
222+
Specification specification(greentea_setup, cases, greentea_teardown, tcp_test_case_handlers);
182223

183224
int main()
184225
{

TESTS/netsocket/tcp/tcpsocket_bind_address.cpp

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

2727
void TCPSOCKET_BIND_ADDRESS()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
35-
3629
TCPSocket *sock = new TCPSocket;
3730
if (!sock) {
3831
TEST_FAIL();
@@ -48,11 +41,4 @@ void TCPSOCKET_BIND_ADDRESS()
4841
}
4942

5043
delete sock;
51-
52-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
53-
count = fetch_stats();
54-
for (int j = 0; j < count; j++) {
55-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
56-
}
57-
#endif
5844
}

TESTS/netsocket/tcp/tcpsocket_bind_address_invalid.cpp

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

2727
void TCPSOCKET_BIND_ADDRESS_INVALID()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
35-
3629
TCPSocket *sock = new TCPSocket;
3730
if (!sock) {
3831
TEST_FAIL();
@@ -54,11 +47,4 @@ void TCPSOCKET_BIND_ADDRESS_INVALID()
5447
}
5548

5649
delete sock;
57-
58-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
59-
count = fetch_stats();
60-
for (int j = 0; j < count; j++) {
61-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
62-
}
63-
#endif
6450
}

TESTS/netsocket/tcp/tcpsocket_bind_address_null.cpp

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

2727
void TCPSOCKET_BIND_ADDRESS_NULL()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
35-
3629
TCPSocket *sock = new TCPSocket;
3730
if (!sock) {
3831
TEST_FAIL();
@@ -47,11 +40,4 @@ void TCPSOCKET_BIND_ADDRESS_NULL()
4740
}
4841

4942
delete sock;
50-
51-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
52-
count = fetch_stats();
53-
for (int j = 0; j < count; j++) {
54-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
55-
}
56-
#endif
5743
}

TESTS/netsocket/tcp/tcpsocket_bind_address_port.cpp

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

2727
void TCPSOCKET_BIND_ADDRESS_PORT()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
35-
3629
TCPSocket *sock = new TCPSocket;
3730
if (!sock) {
3831
TEST_FAIL();
@@ -47,11 +40,4 @@ void TCPSOCKET_BIND_ADDRESS_PORT()
4740
}
4841

4942
delete sock;
50-
51-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
52-
count = fetch_stats();
53-
for (int j = 0; j < count; j++) {
54-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
55-
}
56-
#endif
5743
}

TESTS/netsocket/tcp/tcpsocket_bind_port.cpp

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

2727
void TCPSOCKET_BIND_PORT()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
35-
3629
TCPSocket *sock = new TCPSocket;
3730
if (!sock) {
3831
TEST_FAIL();
@@ -47,11 +40,4 @@ void TCPSOCKET_BIND_PORT()
4740
}
4841

4942
delete sock;
50-
51-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
52-
count = fetch_stats();
53-
for (int j = 0; j < count; j++) {
54-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
55-
}
56-
#endif
5743
}

TESTS/netsocket/tcp/tcpsocket_bind_port_fail.cpp

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

2727
void TCPSOCKET_BIND_PORT_FAIL()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
35-
3629
TCPSocket *sock = new TCPSocket;
3730
if (!sock) {
3831
TEST_FAIL();
@@ -57,11 +50,4 @@ void TCPSOCKET_BIND_PORT_FAIL()
5750

5851
delete sock;
5952
delete sock2;
60-
61-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
62-
count = fetch_stats();
63-
for (int j = 0; j < count; j++) {
64-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
65-
}
66-
#endif
6753
}

TESTS/netsocket/tcp/tcpsocket_bind_unopened.cpp

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

2727
void TCPSOCKET_BIND_UNOPENED()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
35-
3629
TCPSocket *sock = new TCPSocket;
3730
if (!sock) {
3831
TEST_FAIL();
@@ -46,11 +39,4 @@ void TCPSOCKET_BIND_UNOPENED()
4639
}
4740

4841
delete sock;
49-
50-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
51-
count = fetch_stats();
52-
for (int j = 0; j < count; j++) {
53-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
54-
}
55-
#endif
5642
}

TESTS/netsocket/tcp/tcpsocket_bind_wrong_type.cpp

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

2727
void TCPSOCKET_BIND_WRONG_TYPE()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
35-
3629
TCPSocket *sock = new TCPSocket;
3730
if (!sock) {
3831
TEST_FAIL();
@@ -56,11 +49,4 @@ void TCPSOCKET_BIND_WRONG_TYPE()
5649
}
5750

5851
delete sock;
59-
60-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
61-
count = fetch_stats();
62-
for (int j = 0; j < count; j++) {
63-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
64-
}
65-
#endif
6652
}

TESTS/netsocket/tcp/tcpsocket_echotest.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,6 @@ void tcpsocket_echotest_nonblock_receive()
127127

128128
void TCPSOCKET_ECHOTEST_NONBLOCK()
129129
{
130-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
131-
int j = 0;
132-
int count = fetch_stats();
133-
for (; j < count; j++) {
134-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
135-
}
136-
#endif
137130
tc_exec_time.start();
138131
time_allotted = split2half_rmng_tcp_test_time(); // [s]
139132

@@ -182,7 +175,8 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
182175
bytes2send -= sent;
183176
}
184177
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
185-
count = fetch_stats();
178+
int count = fetch_stats();
179+
int j;
186180
for (j = 0; j < count; j++) {
187181
if ((tcp_stats[j].state == SOCK_OPEN) && (tcp_stats[j].proto == NSAPI_TCP)) {
188182
break;

TESTS/netsocket/tcp/tcpsocket_open_close_repeat.cpp

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

2727
void TCPSOCKET_OPEN_CLOSE_REPEAT()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
3529
TCPSocket *sock = new TCPSocket;
3630
if (!sock) {
3731
TEST_FAIL();
@@ -42,10 +36,4 @@ void TCPSOCKET_OPEN_CLOSE_REPEAT()
4236
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
4337
}
4438
delete sock;
45-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
46-
count = fetch_stats();
47-
for (int j = 0; j < count; j++) {
48-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
49-
}
50-
#endif
5139
}

TESTS/netsocket/tcp/tcpsocket_open_destruct.cpp

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

2727
void TCPSOCKET_OPEN_DESTRUCT()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
35-
3629
for (int i = 0; i < 100; i++) {
3730
TCPSocket *sock = new TCPSocket;
3831
if (!sock) {
@@ -41,10 +34,4 @@ void TCPSOCKET_OPEN_DESTRUCT()
4134
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
4235
delete sock;
4336
}
44-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
45-
count = fetch_stats();
46-
for (int j = 0; j < count; j++) {
47-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
48-
}
49-
#endif
5037
}

TESTS/netsocket/tcp/tcpsocket_open_twice.cpp

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

2727
void TCPSOCKET_OPEN_TWICE()
2828
{
29-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
30-
int count = fetch_stats();
31-
for (int j = 0; j < count; j++) {
32-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
33-
}
34-
#endif
3529
TCPSocket *sock = new TCPSocket;
3630
if (!sock) {
3731
TEST_FAIL();
@@ -41,10 +35,4 @@ void TCPSOCKET_OPEN_TWICE()
4135
TEST_ASSERT_EQUAL(NSAPI_ERROR_PARAMETER, sock->open(NetworkInterface::get_default_instance()));
4236

4337
delete sock;
44-
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
45-
count = fetch_stats();
46-
for (int j = 0; j < count; j++) {
47-
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
48-
}
49-
#endif
5038
}

0 commit comments

Comments
 (0)