485 lines
22 KiB
Python
485 lines
22 KiB
Python
#!/usr/bin/env python3
|
|
"""Test MQTT memory admission and its production allocator/connection wiring."""
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
from test_mqtt_transport_results import method
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
|
|
POLICY_PREAMBLE = r'''
|
|
#include <cassert>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <initializer_list>
|
|
#include <algorithm>
|
|
#include "helpers/MQTTConnectionAdmission.h"
|
|
#include "helpers/MQTTTransportHealth.h"
|
|
namespace Admission = MQTTConnectionAdmission;
|
|
using MemoryState = Admission::ClientMemoryState;
|
|
'''
|
|
|
|
HEAP_PREAMBLE = POLICY_PREAMBLE + r'''
|
|
#define ESP_PLATFORM 1
|
|
#define BOARD_HAS_PSRAM 1
|
|
constexpr uint32_t MALLOC_CAP_INTERNAL = 1, MALLOC_CAP_DMA = 2, MALLOC_CAP_SPIRAM = 4;
|
|
static size_t dma_free = 100000, dma_largest = 20000;
|
|
static size_t heap_queries = 0, psram_allocations = 0, internal_allocations = 0;
|
|
static bool psram_fails = false, internal_fails = false;
|
|
static unsigned char psram_storage[4096], internal_storage[4096];
|
|
size_t heap_caps_get_free_size(uint32_t caps) {
|
|
assert(caps == (MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA));
|
|
++heap_queries;
|
|
return dma_free;
|
|
}
|
|
size_t heap_caps_get_largest_free_block(uint32_t caps) {
|
|
assert(caps == (MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA));
|
|
++heap_queries;
|
|
return dma_largest;
|
|
}
|
|
void* heap_caps_calloc(size_t count, size_t size, uint32_t caps) {
|
|
assert(count && size && count <= SIZE_MAX / size);
|
|
if (caps == MALLOC_CAP_SPIRAM) {
|
|
++psram_allocations;
|
|
return psram_fails ? nullptr : psram_storage;
|
|
}
|
|
assert(caps == (MALLOC_CAP_INTERNAL | MALLOC_CAP_DMA));
|
|
++internal_allocations;
|
|
return internal_fails ? nullptr : internal_storage;
|
|
}
|
|
'''
|
|
|
|
CONNECTION_PREAMBLE = HEAP_PREAMBLE + r'''
|
|
#include <atomic>
|
|
constexpr int RUNTIME_MQTT_SLOTS = 6;
|
|
struct { size_t size = 0; size_t getPsramSize() const { return size; } } ESP;
|
|
struct Preset { const char* server_url; };
|
|
struct Client {
|
|
bool initialized = false, started = false;
|
|
bool isInitialized() const { return initialized; }
|
|
bool isStarted() const { return started; }
|
|
};
|
|
struct MQTTBridge {
|
|
struct MQTTSlot {
|
|
bool enabled = true, connected = false;
|
|
Client* client = nullptr;
|
|
const Preset* preset = nullptr;
|
|
char broker_uri[128] = {}, host[64] = "broker";
|
|
uint16_t port = 8883;
|
|
MQTTTransportHealth::State transport_health;
|
|
} _slots[RUNTIME_MQTT_SLOTS];
|
|
std::atomic<bool> _slot_attempt_pending[RUNTIME_MQTT_SLOTS] = {};
|
|
std::atomic<bool> _slot_transport_fault_pending[RUNTIME_MQTT_SLOTS] = {};
|
|
bool hasPendingSlotConnection() const;
|
|
bool canStartSlotConnection(int index) const;
|
|
};
|
|
'''
|
|
|
|
|
|
class MqttConnectionAdmissionTest(unittest.TestCase):
|
|
def compile_run(self, source):
|
|
compiler = shutil.which("g++") or shutil.which("clang++")
|
|
self.assertIsNotNone(compiler, "a host C++ compiler is required")
|
|
with tempfile.TemporaryDirectory(prefix="meshcore-mqtt-admission-") as temporary:
|
|
work = Path(temporary)
|
|
path, binary = work / "admission.cpp", work / "admission"
|
|
path.write_text(source, encoding="utf-8")
|
|
result = subprocess.run(
|
|
[compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror",
|
|
"-I", str(ROOT / "src"), str(path), "-o", str(binary)],
|
|
capture_output=True, text=True, timeout=60)
|
|
self.assertEqual(result.returncode, 0, result.stderr)
|
|
result = subprocess.run([str(binary)], cwd=work, capture_output=True,
|
|
text=True, timeout=10)
|
|
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
|
|
|
|
def test_connection_admission_checks_both_total_and_contiguous_dma_heap(self):
|
|
self.compile_run(POLICY_PREAMBLE + r'''
|
|
int main() {
|
|
assert(Admission::kPsramPublishLargestBytes == 4096);
|
|
assert(Admission::kPsramPublishLargestBytes == Admission::kDmaLargestBytes);
|
|
assert(Admission::kPsramTlsLargestBytes == 16000);
|
|
assert(Admission::kInternalPublishLargestBytes == 8000);
|
|
assert(Admission::kDmaReserveBytes == 16384);
|
|
assert(Admission::kClientTaskStackBytes == 6144);
|
|
assert(Admission::kClientTaskPriority == 5);
|
|
assert(Admission::kColdStartAllowanceBytes == 16384);
|
|
assert(Admission::kRestartAllowanceBytes == 8192);
|
|
assert(Admission::kTlsInternalFreeBytes == 61440);
|
|
assert(Admission::kClientBufferBytes == 896);
|
|
const MemoryState states[] = {MemoryState::Uninitialized, MemoryState::Stopped,
|
|
MemoryState::Started};
|
|
const size_t allowances[] = {Admission::kColdStartAllowanceBytes,
|
|
Admission::kRestartAllowanceBytes, 0};
|
|
const size_t sdk_blocks[] = {Admission::kColdStartLargestBytes,
|
|
Admission::kRestartLargestBytes, Admission::kDmaLargestBytes};
|
|
for (bool psram : {false, true}) {
|
|
const size_t floor = Admission::publishLargestBytes(psram);
|
|
for (bool tls : {false, true}) {
|
|
const size_t record = tls
|
|
? (psram ? Admission::kPsramTlsLargestBytes : Admission::kTlsRecordAllocationBytes) : 0;
|
|
for (bool protect : {false, true}) {
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
const size_t free = std::max(Admission::kDmaReserveBytes + allowances[i],
|
|
tls && !psram ? Admission::kTlsInternalFreeBytes : size_t(0));
|
|
const size_t protected_largest = psram ? std::max(floor, record) + allowances[i]
|
|
: floor + allowances[i] + record;
|
|
const size_t largest = protect ? protected_largest
|
|
: std::max(std::max(floor, sdk_blocks[i]), record);
|
|
// Total and contiguous headroom are independent requirements, for
|
|
// bootstrap, stopped-task restart, and retained-task reconnect.
|
|
assert(!Admission::canStart(psram, tls, states[i], free - 1, 100000, protect));
|
|
assert(!Admission::canStart(psram, tls, states[i], 100000, largest - 1, protect));
|
|
assert(Admission::canStart(psram, tls, states[i], free, largest, protect));
|
|
assert(Admission::canStart(psram, tls, states[i], free + 1, largest + 1, protect));
|
|
}
|
|
assert(!Admission::canStart(psram, tls, static_cast<MemoryState>(255),
|
|
SIZE_MAX, SIZE_MAX, protect));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
''')
|
|
|
|
def test_additional_broker_cannot_spend_live_packet_publishing_headroom(self):
|
|
self.compile_run(POLICY_PREAMBLE + r'''
|
|
int main() {
|
|
for (bool psram : {false, true}) {
|
|
const size_t floor = Admission::publishLargestBytes(psram);
|
|
assert(!Admission::hasPublishHeadroom(psram, floor - 1));
|
|
assert(Admission::hasPublishHeadroom(psram, floor));
|
|
// Previously admitted: 8 KiB largest was enough to start another client,
|
|
// even though publishing on a PSRAM board was already suppressed there.
|
|
assert(!Admission::canStart(psram, false, MemoryState::Uninitialized, 100000, 8192, true));
|
|
assert(!Admission::canStart(psram, true, MemoryState::Stopped, 100000, 8192, true));
|
|
const size_t allowances[] = {16384, 8192, 0};
|
|
const MemoryState states[] = {MemoryState::Uninitialized, MemoryState::Stopped,
|
|
MemoryState::Started};
|
|
for (size_t i = 0; i < 3; ++i) {
|
|
const size_t minimum = floor + allowances[i];
|
|
assert(!Admission::canStart(psram, false, states[i], 100000, minimum - 1, true));
|
|
assert(Admission::canStart(psram, false, states[i], 100000, minimum, true));
|
|
// Spending the complete bounded SDK startup allowance still leaves a
|
|
// block accepted by the exact production packet-publishing policy.
|
|
assert(Admission::hasPublishHeadroom(psram, minimum - allowances[i]));
|
|
}
|
|
}
|
|
}
|
|
''')
|
|
|
|
def test_actual_packet_publish_guard_uses_detected_psram_and_throttles_sampling(self):
|
|
self.compile_run(POLICY_PREAMBLE + r'''
|
|
#include "helpers/MQTTPublishAdmission.h"
|
|
#define ESP_PLATFORM 1
|
|
#define BOARD_HAS_PSRAM 1
|
|
static unsigned long now_ms = 0;
|
|
unsigned long millis() { return now_ms; }
|
|
constexpr uint32_t MALLOC_CAP_DMA = 2;
|
|
static size_t dma_free = Admission::kDmaReserveBytes;
|
|
static size_t dma_largest = Admission::kPsramPublishLargestBytes;
|
|
static size_t dma_queries = 0;
|
|
size_t heap_caps_get_free_size(uint32_t caps) {
|
|
assert(caps == MALLOC_CAP_DMA); ++dma_queries; return dma_free;
|
|
}
|
|
size_t heap_caps_get_largest_free_block(uint32_t caps) {
|
|
assert(caps == MALLOC_CAP_DMA); ++dma_queries; return dma_largest;
|
|
}
|
|
struct {
|
|
size_t psram = 2097152, largest = Admission::kInternalPublishLargestBytes, queries = 0;
|
|
size_t getPsramSize() const { return psram; }
|
|
size_t getMaxAllocHeap() { ++queries; return largest; }
|
|
} ESP;
|
|
struct MQTTBridge {
|
|
MQTTPublishAdmission::State _publish_memory;
|
|
int _skipped_publishes = 0;
|
|
bool hasPublishHeadroom(size_t = 0, size_t = 0, uint8_t = 0);
|
|
};
|
|
''' + method("src/helpers/bridges/MQTTBridge.cpp",
|
|
"bool MQTTBridge::hasPublishHeadroom(") + r'''
|
|
int main() {
|
|
MQTTBridge bridge;
|
|
const uint32_t interval = MQTTPublishAdmission::kSampleIntervalMs;
|
|
assert(bridge.hasPublishHeadroom() && dma_queries == 2 && ESP.queries == 0);
|
|
--dma_free;
|
|
// Admission uses one captured snapshot, including its detected PSRAM flag.
|
|
// A later allocator/PSRAM change does not splice fresh fields into that snapshot.
|
|
ESP.psram = 0;
|
|
assert(bridge.hasPublishHeadroom() && dma_queries == 2 && ESP.queries == 0);
|
|
ESP.psram = 2097152;
|
|
now_ms += interval;
|
|
assert(!bridge.hasPublishHeadroom() && bridge._skipped_publishes == 1 && dma_queries == 4);
|
|
assert(!bridge.hasPublishHeadroom() && bridge._skipped_publishes == 2 && dma_queries == 4);
|
|
++dma_free; --dma_largest; now_ms += interval;
|
|
assert(!bridge.hasPublishHeadroom() && bridge._skipped_publishes == 3 && dma_queries == 6);
|
|
++dma_largest; now_ms += interval;
|
|
assert(bridge.hasPublishHeadroom() && dma_queries == 8);
|
|
assert(!bridge.hasPublishHeadroom(10240, 128, 1)); // Whole QoS1 outbox record.
|
|
assert(bridge.hasPublishHeadroom(10240, 128, 0)); // QoS0 uses fixed SDK chunks.
|
|
assert(dma_queries == 8);
|
|
// Compiling BOARD_HAS_PSRAM does not establish that PSRAM initialized.
|
|
ESP.psram = 0; now_ms += interval;
|
|
assert(bridge.hasPublishHeadroom() && ESP.queries == 1 && dma_queries == 8);
|
|
--ESP.largest; now_ms += interval;
|
|
assert(!bridge.hasPublishHeadroom() && ESP.queries == 2);
|
|
++ESP.largest; now_ms += interval;
|
|
assert(bridge.hasPublishHeadroom() && ESP.queries == 3);
|
|
}
|
|
''')
|
|
|
|
def test_internal_fallback_preserves_dma_reserve_and_rejects_fragmentation(self):
|
|
self.compile_run(POLICY_PREAMBLE + r'''
|
|
int main() {
|
|
assert(Admission::canFallback(1600, 16384 + 1600, 1600));
|
|
assert(!Admission::canFallback(1600, 16384 + 1599, 1600));
|
|
assert(!Admission::canFallback(1600, 100000, 1599));
|
|
assert(!Admission::canFallback(1600, 16383, 100000));
|
|
assert(!Admission::canFallback(1, 0, 100000));
|
|
assert(!Admission::canFallback(0, 100000, 100000));
|
|
assert(!Admission::canFallback(SIZE_MAX, SIZE_MAX, SIZE_MAX));
|
|
assert(Admission::canFallback(SIZE_MAX - 16384, SIZE_MAX, SIZE_MAX));
|
|
}
|
|
''')
|
|
|
|
def test_calloc_size_rejects_zero_and_overflow_without_allocator_calls(self):
|
|
self.compile_run(POLICY_PREAMBLE + r'''
|
|
int main() {
|
|
size_t total = 123;
|
|
assert(!Admission::checkedCallocSize(0, 16, total) && total == 0);
|
|
total = 123;
|
|
assert(!Admission::checkedCallocSize(16, 0, total) && total == 0);
|
|
total = 123;
|
|
assert(!Admission::checkedCallocSize(SIZE_MAX, 2, total) && total == 0);
|
|
assert(Admission::checkedCallocSize(SIZE_MAX, 1, total) && total == SIZE_MAX);
|
|
assert(Admission::checkedCallocSize(1, SIZE_MAX, total) && total == SIZE_MAX);
|
|
assert(Admission::checkedCallocSize(2, SIZE_MAX / 2, total));
|
|
assert(total == SIZE_MAX - 1);
|
|
assert(Admission::checkedCallocSize(4, 896, total) && total == 3584);
|
|
}
|
|
''')
|
|
|
|
def test_explicit_uri_scheme_takes_precedence_over_port(self):
|
|
self.compile_run(POLICY_PREAMBLE + r'''
|
|
int main() {
|
|
assert(Admission::requiresTls("wss://broker:1883/mqtt", 1883));
|
|
assert(Admission::requiresTls("mqtts://broker:1883", 1883));
|
|
assert(!Admission::requiresTls("ws://broker:443/mqtt", 443));
|
|
assert(!Admission::requiresTls("mqtt://broker:8883", 8883));
|
|
assert(Admission::requiresTls("broker", 443));
|
|
assert(Admission::requiresTls("broker", 8883));
|
|
assert(!Admission::requiresTls("broker", 1883));
|
|
assert(!Admission::requiresTls("broker", 9443));
|
|
assert(Admission::requiresTls(nullptr, 443));
|
|
assert(!Admission::requiresTls(nullptr, 0));
|
|
assert(Admission::requiresTls("", 8883));
|
|
}
|
|
''')
|
|
|
|
def test_actual_calloc_fallback_preserves_dma_headroom_and_handles_failure(self):
|
|
source = HEAP_PREAMBLE + method("src/helpers/bridges/MQTTBridge.cpp",
|
|
"static void* psram_calloc(size_t n, size_t size)")
|
|
self.compile_run(source + r'''
|
|
int main() {
|
|
assert(psram_calloc(0, 16) == nullptr);
|
|
assert(psram_calloc(16, 0) == nullptr);
|
|
assert(psram_calloc(SIZE_MAX, 2) == nullptr);
|
|
assert(psram_allocations == 0 && internal_allocations == 0 && heap_queries == 0);
|
|
|
|
// A successful external allocation needs no internal fallback or reserve.
|
|
dma_free = dma_largest = 0;
|
|
assert(psram_calloc(4, 400) == psram_storage);
|
|
assert(psram_allocations == 1 && internal_allocations == 0 && heap_queries == 0);
|
|
psram_fails = true;
|
|
dma_free = 16384 + 1599; dma_largest = 20000;
|
|
assert(psram_calloc(4, 400) == nullptr && internal_allocations == 0);
|
|
dma_free = 100000; dma_largest = 1599;
|
|
assert(psram_calloc(4, 400) == nullptr && internal_allocations == 0);
|
|
dma_free = 16383; dma_largest = 20000;
|
|
assert(psram_calloc(1, 1) == nullptr && internal_allocations == 0);
|
|
|
|
dma_free = 16384 + 1600; dma_largest = 1600;
|
|
assert(psram_calloc(4, 400) == internal_storage && internal_allocations == 1);
|
|
internal_fails = true;
|
|
assert(psram_calloc(4, 400) == nullptr && internal_allocations == 2);
|
|
}
|
|
''')
|
|
|
|
def test_actual_byte_allocators_use_byte_capable_fallback_and_preserve_failed_resize(self):
|
|
source = POLICY_PREAMBLE + r'''
|
|
#include <cstring>
|
|
#define ESP_PLATFORM 1
|
|
#define BOARD_HAS_PSRAM 1
|
|
constexpr uint32_t MALLOC_CAP_INTERNAL=1, MALLOC_CAP_DMA=2,
|
|
MALLOC_CAP_SPIRAM=4, MALLOC_CAP_8BIT=8;
|
|
static bool psram_fails=false, internal_fails=false;
|
|
static size_t dma_free=100000,dma_largest=100000,internal_calls=0,free_calls=0,malloc_calls=0;
|
|
static unsigned char original[32], psram_storage[64], internal_storage[64];
|
|
static void* freed=nullptr;
|
|
size_t heap_caps_get_free_size(uint32_t caps){assert(caps==3);return dma_free;}
|
|
size_t heap_caps_get_largest_free_block(uint32_t caps){assert(caps==3);return dma_largest;}
|
|
size_t heap_caps_get_allocated_size(void*ptr){assert(ptr==original);return sizeof(original);}
|
|
void* heap_caps_malloc(size_t size,uint32_t caps){
|
|
assert(size==32||size==48);++malloc_calls;
|
|
if(caps==MALLOC_CAP_SPIRAM)return psram_fails?nullptr:psram_storage;
|
|
assert(caps==(MALLOC_CAP_INTERNAL|MALLOC_CAP_8BIT));
|
|
++internal_calls;return internal_fails?nullptr:internal_storage;
|
|
}
|
|
void heap_caps_free(void*ptr){++free_calls;freed=ptr;}
|
|
'''
|
|
for signature in ("static void* psram_malloc(size_t size)",
|
|
"static void psram_free(void* ptr)",
|
|
"static void* psram_realloc(void* ptr, size_t new_size)"):
|
|
source += method("src/helpers/bridges/MQTTBridge.cpp", signature)
|
|
self.compile_run(source + r'''
|
|
int main(){
|
|
assert(psram_malloc(0)==nullptr&&malloc_calls==0);
|
|
assert(psram_malloc(32)==psram_storage&&internal_calls==0);
|
|
psram_fails=true;dma_free=16384+31;
|
|
assert(psram_malloc(32)==nullptr&&internal_calls==0);
|
|
dma_free=16384+32;dma_largest=31;
|
|
assert(psram_malloc(32)==nullptr&&internal_calls==0);
|
|
dma_largest=32;assert(psram_malloc(32)==internal_storage&&internal_calls==1);
|
|
memset(original,0xa5,sizeof(original));
|
|
const auto before=malloc_calls;
|
|
dma_free=0;assert(psram_realloc(original,16)==original&&malloc_calls==before);
|
|
assert(psram_realloc(original,48)==nullptr&&free_calls==0);
|
|
for(unsigned char byte:original)assert(byte==0xa5);
|
|
psram_fails=false;
|
|
assert(psram_realloc(original,48)==psram_storage&&freed==original&&free_calls==1);
|
|
assert(!memcmp(original,psram_storage,sizeof(original)));
|
|
psram_fails=true;dma_free=16384+48;dma_largest=48;
|
|
assert(psram_realloc(original,48)==internal_storage&&free_calls==2);
|
|
assert(!memcmp(original,internal_storage,sizeof(original)));
|
|
internal_fails=true;
|
|
assert(psram_realloc(original,48)==nullptr&&free_calls==2);
|
|
for(unsigned char byte:original)assert(byte==0xa5);
|
|
assert(psram_realloc(original,0)==nullptr&&free_calls==3);
|
|
assert(psram_realloc(nullptr,0)==nullptr&&free_calls==3);
|
|
}
|
|
''')
|
|
|
|
def test_actual_preflight_uses_detected_psram_transport_and_dma_caps(self):
|
|
source = CONNECTION_PREAMBLE + method("src/helpers/bridges/MQTTBridge.cpp",
|
|
"bool MQTTBridge::hasPendingSlotConnection() const")
|
|
source += method("src/helpers/bridges/MQTTBridge.cpp",
|
|
"bool MQTTBridge::canStartSlotConnection(int index) const")
|
|
self.compile_run(source + r'''
|
|
int main() {
|
|
MQTTBridge bridge;
|
|
assert(!bridge.canStartSlotConnection(-1));
|
|
assert(!bridge.canStartSlotConnection(RUNTIME_MQTT_SLOTS));
|
|
assert(heap_queries == 0);
|
|
|
|
// BOARD_HAS_PSRAM does not establish that physical PSRAM initialized.
|
|
ESP.size = 0; dma_free = 61440; dma_largest = 17408;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
dma_free = 61439;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
dma_free = 100000; dma_largest = 17407;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
const size_t floor = std::max(Admission::publishLargestBytes(true), Admission::kPsramTlsLargestBytes);
|
|
const size_t bootstrap_cold = std::max(floor, Admission::kColdStartLargestBytes);
|
|
const size_t bootstrap_restart = std::max(floor, Admission::kRestartLargestBytes);
|
|
ESP.size = 2097152; dma_free = Admission::kDmaReserveBytes; dma_largest = bootstrap_cold;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
Client client;
|
|
bridge._slots[0].client = &client;
|
|
assert(!bridge.canStartSlotConnection(0)); // Wrapper allocated; SDK not initialized.
|
|
dma_free = Admission::kDmaReserveBytes + Admission::kColdStartAllowanceBytes - 1;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
dma_free = Admission::kDmaReserveBytes + Admission::kColdStartAllowanceBytes;
|
|
dma_largest = bootstrap_cold - 1;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
dma_largest = bootstrap_cold;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
// An existing connected peer must keep its publication block beside the
|
|
// additional client's cold SDK allocations.
|
|
bridge._slots[1].connected = true;
|
|
dma_largest = floor + Admission::kColdStartAllowanceBytes - 1;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
++dma_largest;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
bridge._slots[1].connected = false;
|
|
|
|
// An initialized but stopped handle needs its task again, not new SDK buffers.
|
|
client.initialized = true; dma_free = 24575; dma_largest = 20000;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
dma_free = Admission::kDmaReserveBytes + Admission::kRestartAllowanceBytes;
|
|
dma_largest = bootstrap_restart - 1;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
dma_largest = bootstrap_restart;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
|
|
// A warm reconnect retains the SDK task/buffers and uses the smaller floor.
|
|
client.started = true; dma_free = Admission::kDmaReserveBytes; dma_largest = floor;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
--dma_largest;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
|
|
// An explicit plaintext URI overrides a TLS port. The effective URI wins
|
|
// over the raw host; a preset's URI wins over both custom fields.
|
|
ESP.size = 0; dma_free = Admission::kDmaReserveBytes;
|
|
dma_largest = Admission::kInternalPublishLargestBytes;
|
|
strcpy(bridge._slots[0].host, "mqtt://broker:8883");
|
|
assert(bridge.canStartSlotConnection(0));
|
|
strcpy(bridge._slots[0].broker_uri, "wss://broker/mqtt");
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
const Preset plaintext{"ws://broker:443/mqtt"};
|
|
bridge._slots[0].preset = &plaintext;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
const Preset tls{"mqtts://broker:1883"};
|
|
bridge._slots[0].preset = &tls;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
ESP.size = 2097152;
|
|
assert(!bridge.canStartSlotConnection(0)); // TLS still needs its incoming-record floor.
|
|
dma_largest = floor;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
|
|
// Reproduce the reporter's failed-state heap after all SDK tasks stopped.
|
|
// One broker must be able to recover instead of waiting forever for a
|
|
// contiguous 24,192-byte block pinned apart by retained SDK buffers.
|
|
client.started = false;
|
|
dma_free = 93024; dma_largest = 20468;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
// Outgoing QoS0 chunks do not bound incoming TLS records. Keep the original
|
|
// additional TLS admission beside a live peer, even though packet publishing
|
|
// itself now uses a separate four-KiB DMA scratch floor.
|
|
bridge._slots[1].connected = true;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
dma_largest = floor + Admission::kRestartAllowanceBytes - 1;
|
|
assert(!bridge.canStartSlotConnection(0));
|
|
++dma_largest;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
// Health reconciliation removing the other failed connection restores
|
|
// bootstrap admission. The target never counts itself as a live peer.
|
|
dma_largest = bootstrap_restart;
|
|
bridge._slots[1].connected = false;
|
|
bridge._slots[0].connected = true;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
bridge._slots[0].connected = false;
|
|
bridge._slots[1].enabled = false;
|
|
bridge._slots[1].connected = true;
|
|
assert(bridge.canStartSlotConnection(0));
|
|
bridge._slots[1].connected = false;
|
|
|
|
// Every pending TLS attempt, including the target's own attempt, blocks all
|
|
// other starts until an event callback clears it. No heap walk is needed.
|
|
for (int pending = 0; pending < RUNTIME_MQTT_SLOTS; ++pending) {
|
|
bridge._slot_attempt_pending[pending].store(true, std::memory_order_release);
|
|
const size_t before = heap_queries;
|
|
for (int index = 0; index < RUNTIME_MQTT_SLOTS; ++index) {
|
|
assert(!bridge.canStartSlotConnection(index));
|
|
}
|
|
assert(heap_queries == before);
|
|
bridge._slot_attempt_pending[pending].store(false, std::memory_order_release);
|
|
assert(bridge.canStartSlotConnection(0));
|
|
}
|
|
}
|
|
''')
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|