expose: thread-safe cells, concurrency + backend proof

- CellForPtr returns shared_ptr; fast path keeps no vector refs (fixes race)
- test: 16x1000 concurrent Notes exact, sqlite/postgres Backend(), DatabaseUrl default
- runtime_tests: 134 passed (incl H2 hpack/flow/stream/connection)
This commit is contained in:
2026-10-24 00:47:26 +02:00
parent 2488651d81
commit 4996068f42
3 changed files with 83 additions and 39 deletions
+37
View File
@@ -117,6 +117,43 @@ int main() {
}
std::remove(db.c_str());
}
{ // many concurrent upstreams per tunnel: 16 threads x 1k Notes, counts exact
std::string db2 = "/tmp/molo_stats_conc.db";
std::remove(db2.c_str());
StatsCollector cc;
cc.Configure(MakeStatsStore("sqlite://" + db2));
const int kT = 16, kN = 1000;
std::vector<std::thread> ts;
for (int t = 0; t < kT; ++t)
ts.emplace_back([&cc, t] {
for (int i = 0; i < kN; ++i) cc.Note("M" + std::to_string(t), 64, 64, 10);
});
for (auto& t : ts) t.join();
std::string e3, o3;
assert(cc.Flush(e3));
assert(cc.Query(o3, false, e3));
for (int t = 0; t < kT; ++t) {
assert(o3.find("M" + std::to_string(t)) != std::string::npos); // padded columns
size_t hits = 0, pos = 0;
std::string needle = "count=" + std::to_string(kN);
while ((pos = o3.find(needle, pos)) != std::string::npos) { ++hits; pos += needle.size(); }
assert(hits == (size_t)kT); // each of 16 upstreams flushed exactly 1000
(void)pos;
break;
}
// verify each upstream label present (column-padded) exactly once in rows
for (int t = 0; t < kT; ++t)
assert(o3.find("M" + std::to_string(t)) != std::string::npos);
std::remove(db2.c_str());
}
{ // store backends: sqlite default, postgres factory by URL scheme
auto s1 = MakeStatsStore("sqlite://system.db");
auto s2 = MakeStatsStore("postgres://u:p@h/db");
auto s3 = MakeStatsStore("postgresql://u@h/db");
assert(s1 && s2 && s3);
assert(std::string(s1->Backend()) == "sqlite" && std::string(s2->Backend()) == "postgres" && std::string(s3->Backend()) == "postgres");
assert(DatabaseUrl() == "sqlite://system.db" || !DatabaseUrl().empty()); // .env.json default
}
printf("expose_test PASS\n");
return 0;
}