|
| 1 | +#include <Arduino.h> |
| 2 | +#include <BSTest.h> |
| 3 | +#include <test_config.h> |
| 4 | +#include <ESP8266WiFi.h> |
| 5 | +#include <Ticker.h> |
| 6 | + |
| 7 | +extern "C" { |
| 8 | +#include "user_interface.h" |
| 9 | +} |
| 10 | + |
| 11 | +BS_ENV_DECLARE(); |
| 12 | + |
| 13 | +// no need for #include |
| 14 | +struct tcp_pcb; |
| 15 | +extern struct tcp_pcb* tcp_tw_pcbs; |
| 16 | +extern "C" void tcp_abort (struct tcp_pcb* pcb); |
| 17 | + |
| 18 | +void tcpCleanup (void) { |
| 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 LOOPS 20 |
| 39 | + |
| 40 | + // will search for a tcp server these gateway's ports |
| 41 | + int ports [] = { 21, 23, 80, 443, 22, |
| 42 | + 0 }; |
| 43 | + WiFiClient client; |
| 44 | + |
| 45 | + Serial.print(WiFi.gatewayIP()); |
| 46 | + |
| 47 | + // look for reachable port on gateway |
| 48 | + int port; |
| 49 | + for (int i = 0; (port = ports[i]); i++) |
| 50 | + if (client.connect(WiFi.gatewayIP(), ports[i])) { |
| 51 | + client.stop(); |
| 52 | + break; |
| 53 | + } |
| 54 | + |
| 55 | + Serial.printf(":%d\r\n", port); |
| 56 | + |
| 57 | + int loops = 0; |
| 58 | + int heapLost = 123456; |
| 59 | + |
| 60 | + if (port) { |
| 61 | + |
| 62 | + tcpCleanup(); |
| 63 | + int heapStart = ESP.getFreeHeap(); |
| 64 | + int minHeap = heapStart / 2; |
| 65 | + int heap = heapStart; |
| 66 | + Serial.printf("heap: %d\r\n", heap); |
| 67 | + |
| 68 | + while (++loops < LOOPS && (int)ESP.getFreeHeap() > minHeap) |
| 69 | + if (client.connect(WiFi.gatewayIP(), port)) { |
| 70 | + client.stop(); |
| 71 | + tcpCleanup(); |
| 72 | + int newHeap = (int)ESP.getFreeHeap(); |
| 73 | + Serial.printf("%03d %5d %d\r\n", loops, newHeap, newHeap - heap); |
| 74 | + heap = newHeap; |
| 75 | + } |
| 76 | + |
| 77 | + heapLost = heapStart - ESP.getFreeHeap(); |
| 78 | + |
| 79 | + Serial.printf("min heap: %d\r\nheap: %d\r\nloops: %d\r\nheapLost: %d\r\n", |
| 80 | + minHeap, |
| 81 | + ESP.getFreeHeap(), |
| 82 | + loops, |
| 83 | + (int)heapLost); |
| 84 | + } |
| 85 | + |
| 86 | + REQUIRE(loops == LOOPS && heapLost <= 0); |
| 87 | +} |
| 88 | + |
| 89 | +void loop() |
| 90 | +{ |
| 91 | +} |
0 commit comments