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