#include #include #include #include #include #include #include #include #include #include #include #include #include namespace { using namespace Void::Sqlite; struct TempPoolDir { std::filesystem::path path; TempPoolDir() { static std::atomic seq{0}; const auto id = seq.fetch_add(1, std::memory_order_relaxed); path = std::filesystem::temp_directory_path() / ("void-sqlite-async-" + std::to_string(::getpid()) + "-" + std::to_string(id)); std::error_code ec; std::filesystem::remove_all(path, ec); std::filesystem::create_directories(path, ec); } ~TempPoolDir() { std::error_code ec; std::filesystem::remove_all(path, ec); } }; Void::Coro::Task run_pool_crud_and_backpressure( AsyncPool& pool, Void::Async::ManualExecutor& exec) { (void)exec; auto init = co_await pool.run_write_void([](Database& db) { return db.exec("CREATE TABLE kv(k INTEGER PRIMARY KEY, v TEXT)"); }); Void::Testing::require(init.has_value()); // Write transaction commit auto tx_ok = co_await pool.run_write_transaction( BeginMode::Immediate, [](Database& db, Transaction&) -> Result { auto r1 = db.exec("INSERT INTO kv VALUES(1, 'one')"); if (!r1) return Void::Result::fail(std::move(r1).error()); auto r2 = db.exec("INSERT INTO kv VALUES(2, 'two')"); if (!r2) return Void::Result::fail(std::move(r2).error()); return 2; }); Void::Testing::require(tx_ok.has_value() && *tx_ok == 2); // Write transaction error rolls back cleanly inside same lease auto tx_err = co_await pool.run_write_transaction( BeginMode::Immediate, [](Database& db, Transaction&) -> Result { auto r1 = db.exec("INSERT INTO kv VALUES(3, 'rolled')"); if (!r1) return Void::Result::fail(std::move(r1).error()); return Void::Result::fail(make_sqlite_error( SqliteErrorCode::constraint, "intentional rollback")); }); Void::Testing::expect(!tx_err.has_value()); // 4 concurrent reads via coroutine calls for (int i = 0; i < 4; ++i) { auto count = co_await pool.run_read([](Database& db) -> Result { auto st = db.prepare("SELECT COUNT(*) FROM kv"); if (!st) return Void::Result::fail(std::move(st).error()); auto s = st->step(); if (!s) return Void::Result::fail(std::move(s).error()); return st->try_column_int(0); }); Void::Testing::require(count.has_value()); Void::Testing::expect_eq(*count, static_cast(2)); } // Oversized input bytes rejected before queueing auto over_bytes = co_await pool.run_read_void( [](Database&) -> Result { return {}; }, {}, 4096); Void::Testing::expect(!over_bytes.has_value()); Void::Testing::expect(over_bytes.error().kind() == Void::Result::ErrorKind::overloaded); } Void::Coro::Task run_heavy_query( AsyncPool& pool, std::stop_token st, Result* out) { Void::Async::OperationOptions op; op.stop = st; *out = co_await pool.run_read( [](Database& db) -> Result { auto stmt = db.prepare( "WITH RECURSIVE cnt(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM cnt WHERE x < 20000000) " "SELECT MAX(x) FROM cnt"); if (!stmt) return Void::Result::fail(std::move(stmt).error()); auto s = stmt->step(); if (!s) return Void::Result::fail(std::move(s).error()); return stmt->try_column_int(0); }, op); } Void::Coro::Task verify_follow_up_reads(AsyncPool& pool) { for (int i = 0; i < 10; ++i) { auto r = co_await pool.run_read( [](Database& db) -> Result { auto st = db.prepare("SELECT 42"); if (!st) return Void::Result::fail(std::move(st).error()); auto has = st->step(); if (!has) return Void::Result::fail(std::move(has).error()); return st->try_column_int(0); }); Void::Testing::require(r.has_value()); Void::Testing::expect_eq(*r, static_cast(42)); } } Void::Coro::Task drive_pool_crud( AsyncPool& pool, Void::Async::ManualExecutor& exec, bool* done) { co_await run_pool_crud_and_backpressure(pool, exec); *done = true; } Void::Coro::Task drive_warmup(AsyncPool& pool, bool* done) { auto r = co_await pool.run_write_void([](Database& db) { return db.exec("CREATE TABLE dummy(id INTEGER)"); }); Void::Testing::require(r.has_value()); *done = true; } Void::Coro::Task drive_heavy_query( AsyncPool& pool, std::stop_token st, Result* out, bool* done) { co_await run_heavy_query(pool, st, out); *done = true; } Void::Coro::Task drive_followups(AsyncPool& pool, bool* done) { co_await verify_follow_up_reads(pool); *done = true; } Void::Testing::Test sqlite_async_pool_readers_writer_transactions_and_backpressure() { TempPoolDir dir; Void::Async::ManualExecutor exec; AsyncPoolOptions opts; opts.path = dir.path / "pool.db"; opts.reader_connections = 4; opts.max_queued_jobs = 8; opts.max_pending_input_bytes = 1024; auto pool_r = AsyncPool::open(opts, &exec); Void::Testing::require(pool_r.has_value()); auto pool = std::move(*pool_r); bool done = false; auto t = drive_pool_crud(pool, exec, &done); t.handle().resume(); while (!done) { if (exec.drain() == 0) { std::this_thread::sleep_for(std::chrono::milliseconds{1}); } } const auto st = pool.stats(); Void::Testing::expect_eq(st.reader_connections, static_cast(4)); Void::Testing::expect(st.completed_jobs >= 7); Void::Testing::expect(st.rejected_jobs >= 1); Void::Testing::require(pool.close_and_join().has_value()); } Void::Testing::Test sqlite_async_pool_cancellation_no_cross_query_interrupt_leak() { TempPoolDir dir; Void::Async::ManualExecutor exec; AsyncPoolOptions opts; opts.path = dir.path / "cancel.db"; opts.reader_connections = 1; // force all follow-up reads onto the exact same connection opts.progress_handler_steps = 32; auto pool_r = AsyncPool::open(opts, &exec); Void::Testing::require(pool_r.has_value()); auto pool = std::move(*pool_r); // Warm up DB with initial table/checkpoint { bool warmup_done = false; auto wt = drive_warmup(pool, &warmup_done); wt.handle().resume(); while (!warmup_done) { if (exec.drain() == 0) std::this_thread::sleep_for(std::chrono::milliseconds{1}); } } std::stop_source ss; Result cancel_res = Void::Result::fail("uninitialized"); bool cancel_done = false; auto rt = drive_heavy_query(pool, ss.get_token(), &cancel_res, &cancel_done); rt.handle().resume(); // Wait until reader is actively running, then interrupt it. for (int i = 0; i < 100 && pool.stats().active_read_jobs == 0 && !cancel_done; ++i) { exec.drain(); std::this_thread::sleep_for(std::chrono::milliseconds{1}); } ss.request_stop(); while (!cancel_done) { if (exec.drain() == 0) std::this_thread::sleep_for(std::chrono::milliseconds{1}); } Void::Testing::expect(!cancel_res.has_value()); Void::Testing::expect(cancel_res.error().kind() == Void::Result::ErrorKind::cancelled); // Immediately run 10 follow-up queries on the same single reader connection; // zero interrupts may leak to subsequent borrowers! bool followup_done = false; auto ft = drive_followups(pool, &followup_done); ft.handle().resume(); while (!followup_done) { if (exec.drain() == 0) std::this_thread::sleep_for(std::chrono::milliseconds{1}); } Void::Testing::require(pool.close_and_join().has_value()); } } // namespace