mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
Add sample test for WiFiServer
Mostly an example of writing tests for servers
This commit is contained in:
parent
af3b17c0bb
commit
7a2d2460f3
54
tests/device/test_WiFiServer/test_WiFiServer.ino
Normal file
54
tests/device/test_WiFiServer/test_WiFiServer.ino
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ESP8266mDNS.h>
|
||||||
|
#include <WiFiClient.h>
|
||||||
|
#include <BSTest.h>
|
||||||
|
#include <test_config.h>
|
||||||
|
|
||||||
|
|
||||||
|
BS_ENV_DECLARE();
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Serial.begin(115200);
|
||||||
|
WiFi.persistent(false);
|
||||||
|
WiFi.begin(STA_SSID, STA_PASS);
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(500);
|
||||||
|
}
|
||||||
|
MDNS.begin("esp8266-wfs-test");
|
||||||
|
BS_RUN(Serial);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("Simple echo server", "[WiFiServer]")
|
||||||
|
{
|
||||||
|
const uint32_t timeout = 10000;
|
||||||
|
const uint16_t port = 5000;
|
||||||
|
const int maxRequests = 5;
|
||||||
|
const int minRequestLength = 128;
|
||||||
|
WiFiServer server(port);
|
||||||
|
server.begin();
|
||||||
|
auto start = millis();
|
||||||
|
|
||||||
|
int replyCount = 0;
|
||||||
|
while (millis() - start < timeout) {
|
||||||
|
delay(50);
|
||||||
|
WiFiClient client = server.available();
|
||||||
|
if (!client) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String request = client.readStringUntil('\n');
|
||||||
|
CHECK(request.length() >= minRequestLength);
|
||||||
|
client.print(request);
|
||||||
|
client.print('\n');
|
||||||
|
if (++replyCount == maxRequests) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CHECK(replyCount == maxRequests);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
42
tests/device/test_WiFiServer/test_WiFiServer.py
Normal file
42
tests/device/test_WiFiServer/test_WiFiServer.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
from mock_decorators import setup, teardown
|
||||||
|
from threading import Thread
|
||||||
|
import socket
|
||||||
|
import time
|
||||||
|
|
||||||
|
stop_client_thread = False
|
||||||
|
client_thread = None
|
||||||
|
|
||||||
|
@setup('Simple echo server')
|
||||||
|
def setup_echo_server(e):
|
||||||
|
global stop_client_thread
|
||||||
|
global client_thread
|
||||||
|
def echo_client_thread():
|
||||||
|
server_address = socket.gethostbyname('esp8266-wfs-test.local')
|
||||||
|
count = 0
|
||||||
|
while count < 5 and not stop_client_thread:
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.connect((server_address, 5000))
|
||||||
|
sock.settimeout(1.0)
|
||||||
|
buf = 'a' * 1023 + '\n'
|
||||||
|
sock.sendall(buf)
|
||||||
|
data = ''
|
||||||
|
retries = 0
|
||||||
|
while len(data) < 1024 and retries < 3:
|
||||||
|
data += sock.recv(1024)
|
||||||
|
retries += 1
|
||||||
|
print 'Received {} bytes'.format(len(data))
|
||||||
|
if len(data) != 1024:
|
||||||
|
raise RuntimeError('client failed to receive response')
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
stop_client_thread = False
|
||||||
|
client_thread = Thread(target=echo_client_thread)
|
||||||
|
client_thread.start()
|
||||||
|
|
||||||
|
@teardown('Simple echo server')
|
||||||
|
def teardown_echo_server(e):
|
||||||
|
global stop_client_thread
|
||||||
|
stop_client_thread = True
|
||||||
|
client_thread.join()
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user