Skip to content

Commit 117eb0b

Browse files
author
deepikabhavnani
committed
Add socketstats stub functions for unittest and addressed reviews
1 parent f6c1a40 commit 117eb0b

File tree

17 files changed

+151
-62
lines changed

17 files changed

+151
-62
lines changed

TESTS/netsocket/udp/udpsocket_echotest.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,26 @@ void UDPSOCKET_ECHOTEST_NONBLOCK()
183183
}
184184
free(stack_mem);
185185

186-
#ifdef MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
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-
193186
// Packet loss up to 30% tolerated
194187
if (packets_sent > 0) {
195188
double loss_ratio = 1 - ((double)packets_recv / (double)packets_sent);
196189
printf("Packets sent: %d, packets received %d, loss ratio %.2lf\r\n", packets_sent, packets_recv, loss_ratio);
197190
TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);
191+
192+
#ifdef MBED_CONF_NSAPI_SOCKET_STATS_ENABLE
193+
count = fetch_stats();
194+
for (j = 0; j < count; j++) {
195+
if ((NSAPI_UDP == udp_stats[j].proto) && (SOCK_OPEN == udp_stats[j].state)) {
196+
TEST_ASSERT(udp_stats[j].sent_bytes != 0);
197+
TEST_ASSERT(udp_stats[j].recv_bytes != 0);
198+
break;
199+
}
200+
}
201+
loss_ratio = 1 - ((double)udp_stats[j].recv_bytes / (double)udp_stats[j].sent_bytes);
202+
printf("Bytes sent: %d, bytes received %d, loss ratio %.2lf\r\n", udp_stats[j].sent_bytes, udp_stats[j].recv_bytes, loss_ratio);
203+
TEST_ASSERT_DOUBLE_WITHIN(TOLERATED_LOSS_RATIO, EXPECTED_LOSS_RATIO, loss_ratio);
204+
205+
#endif
198206
}
199207
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
200208
}

UNITTESTS/features/netsocket/EthernetInterface/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ set(unittest-test-sources
3333
stubs/stoip4_stub.c
3434
stubs/ip4tos_stub.c
3535
stubs/NetworkStack_stub.cpp
36+
stubs/SocketStats_Stub.cpp
3637
)

UNITTESTS/features/netsocket/InternetSocket/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ set(unittest-test-sources
2525
stubs/EventFlags_stub.cpp
2626
stubs/stoip4_stub.c
2727
stubs/ip4tos_stub.c
28+
stubs/SocketStats_Stub.cpp
2829
)

UNITTESTS/features/netsocket/NetworkInterface/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ set(unittest-test-sources
2525
stubs/nsapi_dns_stub.cpp
2626
stubs/EventFlags_stub.cpp
2727
features/netsocket/NetworkInterface/test_NetworkInterface.cpp
28+
stubs/SocketStats_Stub.cpp
2829
)

UNITTESTS/features/netsocket/NetworkStack/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ set(unittest-test-sources
2828
stubs/nsapi_dns_stub.cpp
2929
stubs/EventFlags_stub.cpp
3030
features/netsocket/NetworkStack/test_NetworkStack.cpp
31+
stubs/SocketStats_Stub.cpp
3132
)

UNITTESTS/features/netsocket/TCPServer/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ set(unittest-test-sources
2828
stubs/nsapi_dns_stub.cpp
2929
stubs/EventFlags_stub.cpp
3030
features/netsocket/TCPServer/test_TCPServer.cpp
31+
stubs/SocketStats_Stub.cpp
3132
)

UNITTESTS/features/netsocket/TCPSocket/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ set(unittest-test-sources
2626
stubs/EventFlags_stub.cpp
2727
stubs/stoip4_stub.c
2828
stubs/ip4tos_stub.c
29+
stubs/SocketStats_Stub.cpp
2930
)

UNITTESTS/features/netsocket/UDPSocket/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ set(unittest-test-sources
2626
stubs/nsapi_dns_stub.cpp
2727
stubs/stoip4_stub.c
2828
stubs/ip4tos_stub.c
29+
stubs/SocketStats_Stub.cpp
2930
)

UNITTESTS/stubs/SocketStats_Stub.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "SocketStats.h"
18+
19+
int SocketStats::get_entry_position(const Socket *const reference_id)
20+
{
21+
return 0;
22+
}
23+
24+
size_t SocketStats::mbed_stats_socket_get_each(mbed_stats_socket_t *stats, size_t count)
25+
{
26+
return 0;
27+
}
28+
29+
SocketStats::SocketStats()
30+
{
31+
}
32+
33+
void SocketStats::stats_new_socket_entry(const Socket *const reference_id)
34+
{
35+
return;
36+
}
37+
38+
void SocketStats::stats_update_socket_state(const Socket *const reference_id, socket_state state)
39+
{
40+
return;
41+
}
42+
43+
void SocketStats::stats_update_peer(const Socket *const reference_id, const SocketAddress &peer)
44+
{
45+
return;
46+
}
47+
48+
void SocketStats::stats_update_proto(const Socket *const reference_id, nsapi_protocol_t proto)
49+
{
50+
return;
51+
}
52+
53+
void SocketStats::stats_update_sent_bytes(const Socket *const reference_id, size_t sent_bytes)
54+
{
55+
return;
56+
}
57+
58+
void SocketStats::stats_update_recv_bytes(const Socket *const reference_id, size_t recv_bytes)
59+
{
60+
return;
61+
}

features/netsocket/InternetSocket.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ InternetSocket::InternetSocket()
2424
_readers(0), _writers(0), _pending(0),
2525
_factory_allocated(false)
2626
{
27+
_socket_stats.stats_new_socket_entry(this);
2728
}
2829

2930
InternetSocket::~InternetSocket()
@@ -48,6 +49,7 @@ nsapi_error_t InternetSocket::open(NetworkStack *stack)
4849
return err;
4950
}
5051

52+
_socket_stats.stats_update_socket_state(this, SOCK_OPEN);
5153
_socket = socket;
5254
_event = callback(this, &InternetSocket::event);
5355
_stack->socket_attach(_socket, Callback<void()>::thunk, &_event);
@@ -72,7 +74,7 @@ nsapi_error_t InternetSocket::close()
7274
_socket = 0;
7375
ret = _stack->socket_close(socket);
7476
_stack = 0; // Invalidate the stack pointer - otherwise open() fails.
75-
77+
_socket_stats.stats_update_socket_state(this, SOCK_CLOSED);
7678
// Wakeup anything in a blocking operation
7779
// on this socket
7880
event();

0 commit comments

Comments
 (0)