|
| 1 | +// MIT License |
| 2 | +// |
| 3 | +// Copyright (c) 2026 Ioannis Kaliakatsos |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +#include "database.h" |
| 24 | + |
| 25 | +#include <gtest/gtest.h> |
| 26 | + |
| 27 | +#include <atomic> |
| 28 | +#include <cstdint> |
| 29 | +#include <set> |
| 30 | +#include <thread> |
| 31 | +#include <vector> |
| 32 | + |
| 33 | +#include "person.h" |
| 34 | + |
| 35 | +using namespace sqlite_reflection; |
| 36 | + |
| 37 | +// These tests exercise the shared singleton connection from many threads at once. |
| 38 | +// Before the connection access was serialized (issue #9), concurrent writers raced |
| 39 | +// on a single sqlite3* and each operation's BEGIN/COMMIT interleaved, producing |
| 40 | +// "cannot start a transaction within a transaction" failures and intermittent |
| 41 | +// crashes. They deterministically pass only once access is serialized. |
| 42 | +class ConcurrencyTest : public ::testing::Test { |
| 43 | + void SetUp() override { |
| 44 | + Database::Initialize(""); |
| 45 | + } |
| 46 | + |
| 47 | + void TearDown() override { |
| 48 | + Database::Finalize(); |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | +TEST_F(ConcurrencyTest, ConcurrentAutoIncrementInsertsAllSucceedWithUniqueIds) { |
| 53 | + const auto& db = Database::Instance(); |
| 54 | + |
| 55 | + constexpr int kThreads = 8; |
| 56 | + constexpr int kInsertsPerThread = 100; |
| 57 | + constexpr int kExpected = kThreads * kInsertsPerThread; |
| 58 | + |
| 59 | + std::atomic<int> failures{0}; |
| 60 | + std::vector<std::thread> workers; |
| 61 | + workers.reserve(kThreads); |
| 62 | + for (int t = 0; t < kThreads; ++t) { |
| 63 | + workers.emplace_back([&db, &failures] { |
| 64 | + for (int i = 0; i < kInsertsPerThread; ++i) { |
| 65 | + try { |
| 66 | + Person p{L"john", L"doe", 30}; |
| 67 | + db.SaveAutoIncrement(p); |
| 68 | + } catch (...) { |
| 69 | + failures.fetch_add(1, std::memory_order_relaxed); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + for (auto& w : workers) { |
| 75 | + w.join(); |
| 76 | + } |
| 77 | + |
| 78 | + // No operation may fail (e.g. with a nested-transaction error). |
| 79 | + EXPECT_EQ(0, failures.load()); |
| 80 | + |
| 81 | + // Every insert must be persisted exactly once. |
| 82 | + const auto all = db.FetchAll<Person>(); |
| 83 | + EXPECT_EQ(kExpected, static_cast<int>(all.size())); |
| 84 | + |
| 85 | + // Every row must have received a distinct, database-assigned id. |
| 86 | + std::set<int64_t> ids; |
| 87 | + for (const auto& person : all) { |
| 88 | + ids.insert(person.id); |
| 89 | + } |
| 90 | + EXPECT_EQ(kExpected, static_cast<int>(ids.size())); |
| 91 | +} |
| 92 | + |
| 93 | +TEST_F(ConcurrencyTest, ConcurrentReadsAndWritesDoNotThrow) { |
| 94 | + const auto& db = Database::Instance(); |
| 95 | + |
| 96 | + constexpr int kWriters = 4; |
| 97 | + constexpr int kReaders = 4; |
| 98 | + constexpr int kInsertsPerWriter = 100; |
| 99 | + constexpr int kReadsPerReader = 100; |
| 100 | + constexpr int kExpected = kWriters * kInsertsPerWriter; |
| 101 | + |
| 102 | + std::atomic<int> failures{0}; |
| 103 | + std::vector<std::thread> workers; |
| 104 | + workers.reserve(kWriters + kReaders); |
| 105 | + |
| 106 | + for (int t = 0; t < kWriters; ++t) { |
| 107 | + workers.emplace_back([&db, &failures] { |
| 108 | + for (int i = 0; i < kInsertsPerWriter; ++i) { |
| 109 | + try { |
| 110 | + Person p{L"jane", L"roe", 41}; |
| 111 | + db.SaveAutoIncrement(p); |
| 112 | + } catch (...) { |
| 113 | + failures.fetch_add(1, std::memory_order_relaxed); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + for (int t = 0; t < kReaders; ++t) { |
| 120 | + workers.emplace_back([&db, &failures] { |
| 121 | + for (int i = 0; i < kReadsPerReader; ++i) { |
| 122 | + try { |
| 123 | + // Reading concurrently with writers must not crash or throw. |
| 124 | + volatile auto count = db.FetchAll<Person>().size(); |
| 125 | + (void)count; |
| 126 | + } catch (...) { |
| 127 | + failures.fetch_add(1, std::memory_order_relaxed); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + for (auto& w : workers) { |
| 134 | + w.join(); |
| 135 | + } |
| 136 | + |
| 137 | + EXPECT_EQ(0, failures.load()); |
| 138 | + |
| 139 | + const auto all = db.FetchAll<Person>(); |
| 140 | + EXPECT_EQ(kExpected, static_cast<int>(all.size())); |
| 141 | +} |
0 commit comments