Skip to content

Refactor socket stats to reduce boiler plate #9959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion TESTS/netsocket/tcp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,38 @@ void greentea_teardown(const size_t passed, const size_t failed, const failure_t
return greentea_test_teardown_handler(passed, failed, failure);
}

utest::v1::status_t greentea_case_setup_handler_tcp(const Case *const source, const size_t index_of_case)
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
return greentea_case_setup_handler(source, index_of_case);
}

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)
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
return greentea_case_teardown_handler(source, passed, failed, failure);
}

static void test_failure_handler(const failure_t failure)
{
UTEST_LOG_FUNCTION();
if (failure.location == LOCATION_TEST_SETUP || failure.location == LOCATION_TEST_TEARDOWN) {
verbose_test_failure_handler(failure);
GREENTEA_TESTSUITE_RESULT(false);
while (1) ;
}
}


Case cases[] = {
Case("TCPSOCKET_ECHOTEST", TCPSOCKET_ECHOTEST),
Expand Down Expand Up @@ -178,7 +210,16 @@ Case cases[] = {
Case("TCPSOCKET_ENDPOINT_CLOSE", TCPSOCKET_ENDPOINT_CLOSE),
};

Specification specification(greentea_setup, cases, greentea_teardown, greentea_continue_handlers);
handlers_t tcp_test_case_handlers = {
default_greentea_test_setup_handler,
greentea_test_teardown_handler,
test_failure_handler,
greentea_case_setup_handler_tcp,
greentea_case_teardown_handler_tcp,
greentea_case_failure_continue_handler
};

Specification specification(greentea_setup, cases, greentea_teardown, tcp_test_case_handlers);

int main()
{
Expand Down
14 changes: 0 additions & 14 deletions TESTS/netsocket/tcp/tcpsocket_bind_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_BIND_ADDRESS()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif

TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -48,11 +41,4 @@ void TCPSOCKET_BIND_ADDRESS()
}

delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
14 changes: 0 additions & 14 deletions TESTS/netsocket/tcp/tcpsocket_bind_address_invalid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_BIND_ADDRESS_INVALID()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif

TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -54,11 +47,4 @@ void TCPSOCKET_BIND_ADDRESS_INVALID()
}

delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
14 changes: 0 additions & 14 deletions TESTS/netsocket/tcp/tcpsocket_bind_address_null.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_BIND_ADDRESS_NULL()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif

TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -47,11 +40,4 @@ void TCPSOCKET_BIND_ADDRESS_NULL()
}

delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
14 changes: 0 additions & 14 deletions TESTS/netsocket/tcp/tcpsocket_bind_address_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_BIND_ADDRESS_PORT()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif

TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -47,11 +40,4 @@ void TCPSOCKET_BIND_ADDRESS_PORT()
}

delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
14 changes: 0 additions & 14 deletions TESTS/netsocket/tcp/tcpsocket_bind_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_BIND_PORT()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif

TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -47,11 +40,4 @@ void TCPSOCKET_BIND_PORT()
}

delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
14 changes: 0 additions & 14 deletions TESTS/netsocket/tcp/tcpsocket_bind_port_fail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_BIND_PORT_FAIL()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif

TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -57,11 +50,4 @@ void TCPSOCKET_BIND_PORT_FAIL()

delete sock;
delete sock2;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
14 changes: 0 additions & 14 deletions TESTS/netsocket/tcp/tcpsocket_bind_unopened.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_BIND_UNOPENED()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif

TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -46,11 +39,4 @@ void TCPSOCKET_BIND_UNOPENED()
}

delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
14 changes: 0 additions & 14 deletions TESTS/netsocket/tcp/tcpsocket_bind_wrong_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_BIND_WRONG_TYPE()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif

TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -56,11 +49,4 @@ void TCPSOCKET_BIND_WRONG_TYPE()
}

delete sock;

#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
10 changes: 2 additions & 8 deletions TESTS/netsocket/tcp/tcpsocket_echotest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,6 @@ void tcpsocket_echotest_nonblock_receive()

void TCPSOCKET_ECHOTEST_NONBLOCK()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int j = 0;
int count = fetch_stats();
for (; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
tc_exec_time.start();
time_allotted = split2half_rmng_tcp_test_time(); // [s]

Expand Down Expand Up @@ -180,7 +173,8 @@ void TCPSOCKET_ECHOTEST_NONBLOCK()
bytes2send -= sent;
}
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
int count = fetch_stats();
int j;
for (j = 0; j < count; j++) {
if ((tcp_stats[j].state == SOCK_OPEN) && (tcp_stats[j].proto == NSAPI_TCP)) {
break;
Expand Down
12 changes: 0 additions & 12 deletions TESTS/netsocket/tcp/tcpsocket_open_close_repeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_OPEN_CLOSE_REPEAT()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -42,10 +36,4 @@ void TCPSOCKET_OPEN_CLOSE_REPEAT()
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->close());
}
delete sock;
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
13 changes: 0 additions & 13 deletions TESTS/netsocket/tcp/tcpsocket_open_destruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_OPEN_DESTRUCT()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif

for (int i = 0; i < 100; i++) {
TCPSocket *sock = new TCPSocket;
if (!sock) {
Expand All @@ -41,10 +34,4 @@ void TCPSOCKET_OPEN_DESTRUCT()
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock->open(NetworkInterface::get_default_instance()));
delete sock;
}
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
12 changes: 0 additions & 12 deletions TESTS/netsocket/tcp/tcpsocket_open_twice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ using namespace utest::v1;

void TCPSOCKET_OPEN_TWICE()
{
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
int count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
TCPSocket *sock = new TCPSocket;
if (!sock) {
TEST_FAIL();
Expand All @@ -41,10 +35,4 @@ void TCPSOCKET_OPEN_TWICE()
TEST_ASSERT_EQUAL(NSAPI_ERROR_PARAMETER, sock->open(NetworkInterface::get_default_instance()));

delete sock;
#if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED
count = fetch_stats();
for (int j = 0; j < count; j++) {
TEST_ASSERT_EQUAL(SOCK_CLOSED, tcp_stats[j].state);
}
#endif
}
Loading