Skip to content

Commit f04f7c1

Browse files
Veijo PesonenSeppo Takalo
authored andcommitted
Starts adding more TCP and UDP performance tests
Starts splitting Greentea test suites to TCP and UDP suites and adds more test cases like tcpsocket_echotest_burst tcpsocket_echotest_burst_nonblock tcpsocket_endpoint_close tcpsocket_recv_100k tcpsocket_recv_100k tcpsocket_recv_timeout tcpsocket_send_timeout tcpsocket_thread_per_socket_safety udpsocket_echotest_burst udpsocket_echotest_burst_nonblock udpsocket_recv_timeout udpsocket_sendto_timeout
1 parent 2f410b0 commit f04f7c1

13 files changed

+1282
-0
lines changed

TESTS/netsocket/tcp/main.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef MBED_CONF_APP_CONNECT_STATEMENT
19+
#error [NOT_SUPPORTED] No network configuration found for this target.
20+
#endif
21+
22+
#ifndef MBED_EXTENDED_TESTS
23+
#error [NOT_SUPPORTED] Pressure tests are not supported by default
24+
#endif
25+
26+
#include "mbed.h"
27+
#include MBED_CONF_APP_HEADER_FILE
28+
#include "greentea-client/test_env.h"
29+
#include "unity/unity.h"
30+
#include "utest.h"
31+
#include "utest/utest_stack_trace.h"
32+
#include "tcp_tests.h"
33+
34+
using namespace utest::v1;
35+
36+
#ifndef MBED_CFG_TCP_CLIENT_ECHO_TIMEOUT
37+
#define MBED_CFG_TCP_CLIENT_ECHO_TIMEOUT 500 //[ms]
38+
#endif
39+
40+
static NetworkInterface* net;
41+
42+
NetworkInterface* get_interface()
43+
{
44+
return net;
45+
}
46+
47+
void drop_bad_packets(TCPSocket& sock) {
48+
nsapi_error_t err;
49+
sock.set_timeout(0);
50+
while (true) {
51+
err = sock.recv(NULL, 0);
52+
if (err == NSAPI_ERROR_WOULD_BLOCK) {
53+
break;
54+
}
55+
}
56+
sock.set_timeout(MBED_CFG_TCP_CLIENT_ECHO_TIMEOUT);
57+
}
58+
59+
static void _ifup() {
60+
net = MBED_CONF_APP_OBJECT_CONSTRUCTION;
61+
nsapi_error_t err = MBED_CONF_APP_CONNECT_STATEMENT;
62+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
63+
printf("MBED: TCPClient IP address is '%s'\n", net->get_ip_address());
64+
}
65+
66+
static void _ifdown() {
67+
net->disconnect();
68+
printf("MBED: ifdown\n");
69+
}
70+
71+
void fill_tx_buffer_ascii(char *buff, size_t len)
72+
{
73+
for (size_t i = 0; i<len; ++i) {
74+
buff[i] = (rand() % 43) + '0';
75+
}
76+
}
77+
78+
// Test setup
79+
utest::v1::status_t greentea_setup(const size_t number_of_cases)
80+
{
81+
GREENTEA_SETUP(240, "default_auto");
82+
_ifup();
83+
return greentea_test_setup_handler(number_of_cases);
84+
}
85+
86+
void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure)
87+
{
88+
_ifdown();
89+
return greentea_test_teardown_handler(passed, failed, failure);
90+
}
91+
92+
93+
Case cases[] = {
94+
Case("Echo burst", test_tcpsocket_echotest_burst),
95+
Case("Echo burst non-block", test_tcpsocket_echotest_burst_nonblock),
96+
Case("Receive 100k from CHARGEN service", test_tcpsocket_recv_100k),
97+
Case("Receive 100k from CHARGEN service non-block", test_tcpsocket_recv_100k_nonblock),
98+
Case("Receive in given time", test_tcpsocket_recv_timeout),
99+
Case("Sending shall not take too long", test_tcpsocket_send_timeout),
100+
Case("Parallel socket thread safety", test_tcpsocket_thread_per_socket_safety),
101+
Case("Endpoint initiated close", test_tcpsocket_endpoint_close),
102+
};
103+
104+
Specification specification(greentea_setup, cases, greentea_teardown);
105+
106+
int main()
107+
{
108+
return !Harness::run(specification);
109+
}

TESTS/netsocket/tcp/tcp_tests.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef TCP_TESTS_H
19+
#define TCP_TESTS_H
20+
21+
NetworkInterface* get_interface();
22+
void drop_bad_packets(TCPSocket& sock);
23+
void fill_tx_buffer_ascii(char *buff, size_t len);
24+
25+
/*
26+
* Test cases
27+
*/
28+
void test_tcpsocket_echotest_burst();
29+
void test_tcpsocket_echotest_burst_nonblock();
30+
void test_tcpsocket_endpoint_close();
31+
void test_tcpsocket_recv_100k();
32+
void test_tcpsocket_recv_100k_nonblock();
33+
void test_tcpsocket_recv_timeout();
34+
void test_tcpsocket_send_timeout();
35+
void test_tcpsocket_thread_per_socket_safety();
36+
37+
#endif //TCP_TESTS_H
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include MBED_CONF_APP_HEADER_FILE
20+
#include "TCPSocket.h"
21+
#include "greentea-client/test_env.h"
22+
#include "unity/unity.h"
23+
#include "utest.h"
24+
#include "tcp_tests.h"
25+
26+
#define SIGNAL_SIGIO 0x1
27+
#define SIGIO_TIMEOUT 5000 //[ms]
28+
29+
namespace
30+
{
31+
static const int BURST_CNT = 100;
32+
static const int BURST_SIZE = 1220;
33+
char rx_buffer[BURST_SIZE] = {0};
34+
char tx_buffer[BURST_SIZE] = {0};
35+
}
36+
37+
static void _sigio_handler(osThreadId id) {
38+
osSignalSet(id, SIGNAL_SIGIO);
39+
}
40+
41+
void test_tcpsocket_echotest_burst()
42+
{
43+
SocketAddress tcp_addr;
44+
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
45+
tcp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
46+
47+
TCPSocket sock;
48+
sock.open(get_interface());
49+
sock.connect(tcp_addr);
50+
sock.sigio(callback(_sigio_handler, Thread::gettid()));
51+
52+
// TX buffer to be preserved for comparison
53+
fill_tx_buffer_ascii(tx_buffer, BURST_SIZE);
54+
55+
int recvd;
56+
int bt_left;
57+
int sent;
58+
for (int i = 0; i < BURST_CNT; i++) {
59+
bt_left = BURST_SIZE;
60+
while (bt_left > 0) {
61+
sent = sock.send(&(tx_buffer[BURST_SIZE-bt_left]), bt_left);
62+
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
63+
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
64+
TEST_FAIL();
65+
}
66+
continue;
67+
} else if (sent < 0) {
68+
printf("[%02d] network error %d\n", i, sent);
69+
TEST_FAIL();
70+
}
71+
bt_left -= sent;
72+
}
73+
74+
bt_left = BURST_SIZE;
75+
while (bt_left > 0) {
76+
recvd = sock.recv(&(rx_buffer[BURST_SIZE-bt_left]), BURST_SIZE);
77+
if (recvd < 0) {
78+
printf("[%02d] network error %d\n", i, recvd);
79+
break;
80+
}
81+
bt_left -= recvd;
82+
}
83+
84+
if (bt_left != 0) {
85+
drop_bad_packets(sock);
86+
TEST_FAIL();
87+
}
88+
89+
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, BURST_SIZE));
90+
}
91+
sock.close();
92+
}
93+
94+
void test_tcpsocket_echotest_burst_nonblock()
95+
{
96+
SocketAddress tcp_addr;
97+
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
98+
tcp_addr.set_port(MBED_CONF_APP_ECHO_SERVER_PORT);
99+
100+
TCPSocket sock;
101+
sock.open(get_interface());
102+
sock.connect(tcp_addr);
103+
sock.set_blocking(false);
104+
sock.sigio(callback(_sigio_handler, Thread::gettid()));
105+
106+
// TX buffer to be preserved for comparison
107+
fill_tx_buffer_ascii(tx_buffer, BURST_SIZE);
108+
109+
int sent;
110+
int recvd;
111+
int bt_left = 0;
112+
for (int i = 0; i < BURST_CNT; i++) {
113+
bt_left = BURST_SIZE;
114+
while (bt_left > 0) {
115+
sent = sock.send(&(tx_buffer[BURST_SIZE-bt_left]), bt_left);
116+
if (sent == NSAPI_ERROR_WOULD_BLOCK) {
117+
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
118+
TEST_FAIL();
119+
}
120+
continue;
121+
} else if (sent < 0) {
122+
printf("[%02d] network error %d\n", i, sent);
123+
TEST_FAIL();
124+
}
125+
bt_left -= sent;
126+
}
127+
TEST_ASSERT_EQUAL(0, bt_left);
128+
129+
bt_left = BURST_SIZE;
130+
while (bt_left > 0) {
131+
recvd = sock.recv(&(rx_buffer[BURST_SIZE-bt_left]), BURST_SIZE);
132+
if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
133+
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
134+
printf("[bt#%02d] packet timeout...", i);
135+
break;
136+
}
137+
continue;
138+
} else if (recvd < 0) {
139+
printf("[%02d] network error %d\n", i, recvd);
140+
break;
141+
}
142+
bt_left -= recvd;
143+
}
144+
145+
if (bt_left != 0) {
146+
printf("network error %d, missing %d bytes from a burst\n", recvd, bt_left);
147+
drop_bad_packets(sock);
148+
TEST_FAIL();
149+
}
150+
151+
TEST_ASSERT_EQUAL(0, memcmp(tx_buffer, rx_buffer, BURST_SIZE));
152+
}
153+
sock.close();
154+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include MBED_CONF_APP_HEADER_FILE
20+
#include "TCPSocket.h"
21+
#include "greentea-client/test_env.h"
22+
#include "unity/unity.h"
23+
#include "utest.h"
24+
#include "tcp_tests.h"
25+
26+
using namespace utest::v1;
27+
28+
namespace
29+
{
30+
static const int SIGNAL_SIGIO = 0x1;
31+
static const int SIGIO_TIMEOUT = 5000; //[ms]
32+
}
33+
34+
static void _sigio_handler(osThreadId id) {
35+
osSignalSet(id, SIGNAL_SIGIO);
36+
}
37+
38+
static void _tcpsocket_connect_to_daytime_srv(TCPSocket& sock) {
39+
SocketAddress tcp_addr;
40+
41+
get_interface()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
42+
tcp_addr.set_port(13);
43+
44+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.open(get_interface()));
45+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(tcp_addr));
46+
}
47+
48+
49+
void test_tcpsocket_endpoint_close()
50+
{
51+
static const int MORE_THAN_AVAILABLE = 30;
52+
char buff[MORE_THAN_AVAILABLE];
53+
54+
TCPSocket sock;
55+
_tcpsocket_connect_to_daytime_srv(sock);
56+
sock.sigio(callback(_sigio_handler, Thread::gettid()));
57+
58+
int recvd = 0;
59+
int recvd_total = 0;
60+
while (true) {
61+
recvd = sock.recv(&(buff[recvd_total]), MORE_THAN_AVAILABLE);
62+
if (recvd_total > 0 && recvd == 0) {
63+
break; // Endpoint closed socket, success
64+
} else if (recvd == 0) {
65+
TEST_FAIL();
66+
} else if (recvd == NSAPI_ERROR_WOULD_BLOCK) {
67+
if(osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status == osEventTimeout) {
68+
TEST_FAIL();
69+
}
70+
continue;
71+
}
72+
recvd_total += recvd;
73+
TEST_ASSERT(recvd_total < MORE_THAN_AVAILABLE);
74+
}
75+
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
76+
}

0 commit comments

Comments
 (0)