File tree 2 files changed +96
-0
lines changed
tests/device/test_WiFiServer
2 files changed +96
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < Arduino.h>
2
+ #include < ESP8266WiFi.h>
3
+ #include < ESP8266mDNS.h>
4
+ #include < WiFiClient.h>
5
+ #include < BSTest.h>
6
+ #include < test_config.h>
7
+
8
+
9
+ BS_ENV_DECLARE ();
10
+
11
+ void setup ()
12
+ {
13
+ Serial.begin (115200 );
14
+ WiFi.persistent (false );
15
+ WiFi.begin (STA_SSID, STA_PASS);
16
+ while (WiFi.status () != WL_CONNECTED) {
17
+ delay (500 );
18
+ }
19
+ MDNS.begin (" esp8266-wfs-test" );
20
+ BS_RUN (Serial);
21
+ }
22
+
23
+ TEST_CASE (" Simple echo server" , " [WiFiServer]" )
24
+ {
25
+ const uint32_t timeout = 10000 ;
26
+ const uint16_t port = 5000 ;
27
+ const int maxRequests = 5 ;
28
+ const int minRequestLength = 128 ;
29
+ WiFiServer server (port);
30
+ server.begin ();
31
+ auto start = millis ();
32
+
33
+ int replyCount = 0 ;
34
+ while (millis () - start < timeout) {
35
+ delay (50 );
36
+ WiFiClient client = server.available ();
37
+ if (!client) {
38
+ continue ;
39
+ }
40
+ String request = client.readStringUntil (' \n ' );
41
+ CHECK (request.length () >= minRequestLength);
42
+ client.print (request);
43
+ client.print (' \n ' );
44
+ if (++replyCount == maxRequests) {
45
+ break ;
46
+ }
47
+ }
48
+ CHECK (replyCount == maxRequests);
49
+ }
50
+
51
+ void loop ()
52
+ {
53
+ }
54
+
Original file line number Diff line number Diff line change
1
+ from mock_decorators import setup , teardown
2
+ from threading import Thread
3
+ import socket
4
+ import time
5
+
6
+ stop_client_thread = False
7
+ client_thread = None
8
+
9
+ @setup ('Simple echo server' )
10
+ def setup_echo_server (e ):
11
+ global stop_client_thread
12
+ global client_thread
13
+ def echo_client_thread ():
14
+ server_address = socket .gethostbyname ('esp8266-wfs-test.local' )
15
+ count = 0
16
+ while count < 5 and not stop_client_thread :
17
+ sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
18
+ sock .connect ((server_address , 5000 ))
19
+ sock .settimeout (1.0 )
20
+ buf = 'a' * 1023 + '\n '
21
+ sock .sendall (buf )
22
+ data = ''
23
+ retries = 0
24
+ while len (data ) < 1024 and retries < 3 :
25
+ data += sock .recv (1024 )
26
+ retries += 1
27
+ print 'Received {} bytes' .format (len (data ))
28
+ if len (data ) != 1024 :
29
+ raise RuntimeError ('client failed to receive response' )
30
+ count += 1
31
+
32
+ stop_client_thread = False
33
+ client_thread = Thread (target = echo_client_thread )
34
+ client_thread .start ()
35
+
36
+ @teardown ('Simple echo server' )
37
+ def teardown_echo_server (e ):
38
+ global stop_client_thread
39
+ stop_client_thread = True
40
+ client_thread .join ()
41
+
42
+
You can’t perform that action at this time.
0 commit comments