Skip to content

Commit d5cbd91

Browse files
d-a-vdevyte
authored andcommitted
device test for #4516 (WiFiClient leaking) (#4549)
* device test for #4516 (WiFiClient leaking) * simply count the number of connections that do not change heap * remove unused heapLost * + python server (to not depend on gateway's opened tcp port) * remove old&bad define * fix another old address
1 parent 42f824b commit d5cbd91

File tree

2 files changed

+154
-0
lines changed

2 files changed

+154
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <Arduino.h>
2+
#include <BSTest.h>
3+
#include <test_config.h>
4+
#include <ESP8266WiFi.h>
5+
6+
extern "C" {
7+
#include "user_interface.h"
8+
}
9+
10+
BS_ENV_DECLARE();
11+
12+
// no need for #include
13+
struct tcp_pcb;
14+
extern struct tcp_pcb* tcp_tw_pcbs;
15+
extern "C" void tcp_abort (struct tcp_pcb* pcb);
16+
17+
void tcpCleanup (void)
18+
{
19+
while (tcp_tw_pcbs)
20+
tcp_abort(tcp_tw_pcbs);
21+
}
22+
23+
void setup()
24+
{
25+
Serial.begin(115200);
26+
Serial.setDebugOutput(true);
27+
WiFi.persistent(false);
28+
WiFi.mode(WIFI_STA);
29+
WiFi.begin(STA_SSID, STA_PASS);
30+
while (WiFi.status() != WL_CONNECTED) {
31+
delay(500);
32+
}
33+
BS_RUN(Serial);
34+
}
35+
36+
TEST_CASE("WiFi release ClientContext", "[clientcontext]")
37+
{
38+
#define MAXLOOPS 50
39+
#define SUCCESS_GOAL 10
40+
#define srv SERVER_IP
41+
42+
WiFiClient client;
43+
44+
Serial.print(srv);
45+
46+
// look for reachable port on gateway
47+
int port;
48+
for (port = 8266; port <= 8285; port++)
49+
if (client.connect(srv, port))
50+
{
51+
client.stop();
52+
break;
53+
}
54+
if (port > 8285)
55+
port = 0;
56+
57+
Serial.printf(":%d\r\n", port);
58+
59+
int loops = 0;
60+
int success = 0;
61+
62+
if (port)
63+
{
64+
tcpCleanup();
65+
int heapStart = ESP.getFreeHeap();
66+
int minHeap = heapStart / 2;
67+
int heap = heapStart;
68+
Serial.printf("heap: %d\r\n", heap);
69+
70+
while (success < SUCCESS_GOAL && ++loops <= MAXLOOPS && (int)ESP.getFreeHeap() > minHeap)
71+
if (client.connect(srv, port))
72+
{
73+
client.stop();
74+
tcpCleanup();
75+
int newHeap = (int)ESP.getFreeHeap();
76+
Serial.printf("%03d %5d %d\r\n", loops, newHeap, newHeap - heap);
77+
if (newHeap - heap == 0)
78+
success++;
79+
heap = newHeap;
80+
}
81+
82+
Serial.printf("heap: %d\r\n"
83+
"loops: %d\r\nstable-loops: %d\r\n",
84+
ESP.getFreeHeap(),
85+
loops,
86+
success);
87+
}
88+
89+
REQUIRE(success >= SUCCESS_GOAL);
90+
}
91+
92+
void loop()
93+
{
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from mock_decorators import setup, teardown
2+
from flask import Flask, request
3+
from threading import Thread
4+
import socket
5+
import select
6+
import sys
7+
import os
8+
9+
@setup('WiFi release ClientContext')
10+
def setup_tcpsrv(e):
11+
12+
global thread
13+
14+
app = Flask(__name__)
15+
16+
def run():
17+
18+
global running
19+
20+
running = False
21+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
22+
for port in range(8266, 8285 + 1):
23+
try:
24+
print >>sys.stderr, 'trying port', port
25+
server_address = ("0.0.0.0", port)
26+
sock.bind(server_address)
27+
sock.listen(1)
28+
running = True
29+
break
30+
except Exception:
31+
print >>sys.stderr, 'busy'
32+
if not running:
33+
return
34+
print >>sys.stderr, 'starting up on %s port %s' % server_address
35+
print >>sys.stderr, 'waiting for connections'
36+
while running:
37+
print >>sys.stderr, 'loop'
38+
readable, writable, errored = select.select([sock], [], [], 1.0)
39+
if readable:
40+
connection, client_address = sock.accept()
41+
try:
42+
print >>sys.stderr, 'client connected:', client_address
43+
finally:
44+
print >>sys.stderr, 'close'
45+
connection.shutdown(socket.SHUT_RDWR)
46+
connection.close()
47+
48+
thread = Thread(target=run)
49+
thread.start()
50+
51+
@teardown('WiFi release ClientContext')
52+
def teardown_tcpsrv(e):
53+
54+
global thread
55+
global running
56+
57+
print >>sys.stderr, 'closing'
58+
running = False
59+
thread.join()
60+
return 0

0 commit comments

Comments
 (0)