|
12 | 12 | * |
13 | 13 | */ |
14 | 14 | #include <cassert> |
15 | | -#include <chrono> |
| 15 | +#include <filesystem> |
16 | 16 | #include <iostream> |
17 | 17 | #include <memory> |
| 18 | +#include <stdexcept> |
| 19 | +#include <utility> |
18 | 20 |
|
19 | 21 | #include <vix/net/NetworkProbe.hpp> |
20 | 22 | #include <vix/sync/outbox/Outbox.hpp> |
|
23 | 25 |
|
24 | 26 | #include "fake_http_transport.hpp" |
25 | 27 |
|
26 | | -static std::int64_t now_ms() |
| 28 | +static void reset_test_dir(const std::filesystem::path &dir) |
27 | 29 | { |
28 | | - using namespace std::chrono; |
29 | | - return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); |
| 30 | + std::error_code ec; |
| 31 | + std::filesystem::remove_all(dir, ec); |
| 32 | + std::filesystem::create_directories(dir, ec); |
30 | 33 | } |
31 | 34 |
|
32 | | -int main() |
| 35 | +static std::shared_ptr<vix::sync::outbox::Outbox> make_outbox( |
| 36 | + const std::filesystem::path &dir, |
| 37 | + const char *owner) |
33 | 38 | { |
34 | | - using namespace vix::sync; |
35 | 39 | using namespace vix::sync::outbox; |
36 | | - using namespace vix::sync::engine; |
37 | 40 |
|
38 | | - // 1) Outbox store |
| 41 | + reset_test_dir(dir); |
39 | 42 | auto store = std::make_shared<FileOutboxStore>(FileOutboxStore::Config{ |
40 | | - .file_path = "./.vix_test/outbox.json", |
| 43 | + .file_path = dir / "outbox.json", |
41 | 44 | .pretty_json = true, |
42 | 45 | .fsync_on_write = false}); |
43 | 46 |
|
44 | | - auto outbox = std::make_shared<Outbox>( |
45 | | - Outbox::Config{ |
46 | | - .owner = "test-engine", |
47 | | - }, |
48 | | - store); |
| 47 | + return std::make_shared<Outbox>( |
| 48 | + Outbox::Config{.owner = owner}, |
| 49 | + std::move(store)); |
| 50 | +} |
| 51 | + |
| 52 | +static vix::sync::Operation ready_operation() |
| 53 | +{ |
| 54 | + vix::sync::Operation op; |
| 55 | + op.kind = "http.post"; |
| 56 | + op.target = "/api/messages"; |
| 57 | + op.payload = R"({"text":"hello offline"})"; |
| 58 | + return op; |
| 59 | +} |
| 60 | + |
| 61 | +static void test_online_send_and_completion() |
| 62 | +{ |
| 63 | + using namespace vix::sync; |
| 64 | + using namespace vix::sync::engine; |
49 | 65 |
|
50 | | - // 2) Network probe: always online for this test |
| 66 | + auto outbox = make_outbox("./.vix_test", "test-engine"); |
| 67 | + auto probe = std::make_shared<vix::net::NetworkProbe>( |
| 68 | + vix::net::NetworkProbe::Config{}, [] { return true; }); |
| 69 | + auto transport = std::make_shared<FakeHttpTransport>(); |
| 70 | + transport->setDefault({.ok = true}); |
| 71 | + |
| 72 | + SyncEngine engine( |
| 73 | + SyncEngine::Config{.worker_count = 1, .batch_limit = 10}, |
| 74 | + outbox, probe, transport); |
| 75 | + |
| 76 | + constexpr std::int64_t t0 = 1'000; |
| 77 | + const auto id = outbox->enqueue(ready_operation(), t0); |
| 78 | + assert(engine.tick(t0) >= 1); |
| 79 | + |
| 80 | + const auto saved = outbox->store()->get(id); |
| 81 | + assert(saved.has_value()); |
| 82 | + assert(saved->status == OperationStatus::Done); |
| 83 | +} |
| 84 | + |
| 85 | +static void test_offline_probe_blocks_ready_send() |
| 86 | +{ |
| 87 | + using namespace vix::sync; |
| 88 | + using namespace vix::sync::engine; |
| 89 | + |
| 90 | + constexpr std::int64_t t0 = 10'000; |
| 91 | + auto outbox = make_outbox("./.vix_test_offline_gate", "offline-gate"); |
| 92 | + int probe_calls = 0; |
51 | 93 | auto probe = std::make_shared<vix::net::NetworkProbe>( |
52 | 94 | vix::net::NetworkProbe::Config{}, |
53 | | - [] |
54 | | - { return true; }); |
| 95 | + [&probe_calls] |
| 96 | + { |
| 97 | + ++probe_calls; |
| 98 | + return false; |
| 99 | + }); |
| 100 | + auto transport = std::make_shared<FakeHttpTransport>(); |
| 101 | + const auto id = outbox->enqueue(ready_operation(), t0); |
| 102 | + |
| 103 | + SyncEngine engine(SyncEngine::Config{}, outbox, probe, transport); |
55 | 104 |
|
56 | | - // 3) Fake transport: success |
| 105 | + assert(engine.tick(t0) == 0); |
| 106 | + assert(probe_calls == 1); |
| 107 | + assert(transport->callCount() == 0); |
| 108 | + |
| 109 | + const auto saved = outbox->store()->get(id); |
| 110 | + assert(saved.has_value()); |
| 111 | + assert(saved->status == OperationStatus::Pending); |
| 112 | +} |
| 113 | + |
| 114 | +static void test_workers_share_rate_limited_probe_state() |
| 115 | +{ |
| 116 | + using namespace vix::sync; |
| 117 | + using namespace vix::sync::engine; |
| 118 | + |
| 119 | + constexpr std::int64_t t0 = 20'000; |
| 120 | + auto outbox = make_outbox("./.vix_test_shared_gate", "shared-gate"); |
| 121 | + int probe_calls = 0; |
| 122 | + vix::net::NetworkProbe::Config probe_config; |
| 123 | + probe_config.min_interval_ms = 100; |
| 124 | + auto probe = std::make_shared<vix::net::NetworkProbe>( |
| 125 | + probe_config, |
| 126 | + [&probe_calls] |
| 127 | + { |
| 128 | + ++probe_calls; |
| 129 | + return false; |
| 130 | + }); |
57 | 131 | auto transport = std::make_shared<FakeHttpTransport>(); |
58 | | - transport->setDefault({.ok = true}); |
| 132 | + outbox->enqueue(ready_operation(), t0); |
59 | 133 |
|
60 | | - // 4) Engine |
61 | 134 | SyncEngine engine( |
62 | | - SyncEngine::Config{ |
63 | | - .worker_count = 1, |
64 | | - .batch_limit = 10}, |
| 135 | + SyncEngine::Config{.worker_count = 2}, |
65 | 136 | outbox, probe, transport); |
66 | 137 |
|
67 | | - // 5) Enqueue operation |
68 | | - Operation op; |
69 | | - op.kind = "http.post"; |
70 | | - op.target = "/api/messages"; |
71 | | - op.payload = R"({"text":"hello offline"})"; |
| 138 | + assert(engine.tick(t0) == 0); |
| 139 | + assert(probe_calls == 1); |
| 140 | + assert(transport->callCount() == 0); |
| 141 | +} |
| 142 | + |
| 143 | +static void test_offline_to_online_recovery_after_rate_limit() |
| 144 | +{ |
| 145 | + using namespace vix::sync; |
| 146 | + using namespace vix::sync::engine; |
| 147 | + |
| 148 | + constexpr std::int64_t t0 = 30'000; |
| 149 | + auto outbox = make_outbox("./.vix_test_gate_recovery", "gate-recovery"); |
| 150 | + int probe_calls = 0; |
| 151 | + vix::net::NetworkProbe::Config probe_config; |
| 152 | + probe_config.min_interval_ms = 100; |
| 153 | + auto probe = std::make_shared<vix::net::NetworkProbe>( |
| 154 | + probe_config, |
| 155 | + [&probe_calls] |
| 156 | + { |
| 157 | + ++probe_calls; |
| 158 | + return probe_calls >= 2; |
| 159 | + }); |
| 160 | + auto transport = std::make_shared<FakeHttpTransport>(); |
| 161 | + const auto id = outbox->enqueue(ready_operation(), t0); |
| 162 | + |
| 163 | + SyncEngine engine(SyncEngine::Config{}, outbox, probe, transport); |
| 164 | + |
| 165 | + assert(engine.tick(t0) == 0); |
| 166 | + assert(probe_calls == 1); |
| 167 | + assert(transport->callCount() == 0); |
| 168 | + |
| 169 | + assert(engine.tick(t0 + 50) == 0); |
| 170 | + assert(probe_calls == 1); |
| 171 | + assert(transport->callCount() == 0); |
| 172 | + |
| 173 | + assert(engine.tick(t0 + 100) >= 1); |
| 174 | + assert(probe_calls == 2); |
| 175 | + assert(transport->callCount() == 1); |
| 176 | + |
| 177 | + const auto saved = outbox->store()->get(id); |
| 178 | + assert(saved.has_value()); |
| 179 | + assert(saved->status == OperationStatus::Done); |
| 180 | +} |
| 181 | + |
| 182 | +static void test_probe_exception_propagates_from_manual_tick() |
| 183 | +{ |
| 184 | + using namespace vix::sync; |
| 185 | + using namespace vix::sync::engine; |
| 186 | + |
| 187 | + auto outbox = make_outbox("./.vix_test_gate_exception", "gate-exception"); |
| 188 | + auto probe = std::make_shared<vix::net::NetworkProbe>( |
| 189 | + [] |
| 190 | + { |
| 191 | + throw std::runtime_error("probe failure"); |
| 192 | + return false; |
| 193 | + }); |
| 194 | + auto transport = std::make_shared<FakeHttpTransport>(); |
| 195 | + outbox->enqueue(ready_operation(), 40'000); |
72 | 196 |
|
73 | | - const auto t0 = now_ms(); |
74 | | - const auto id = outbox->enqueue(op, t0); |
| 197 | + SyncEngine engine(SyncEngine::Config{}, outbox, probe, transport); |
75 | 198 |
|
76 | | - // 6) Tick engine => should send and complete |
77 | | - const auto processed = engine.tick(now_ms()); |
78 | | - assert(processed >= 1); |
| 199 | + bool propagated = false; |
| 200 | + try |
| 201 | + { |
| 202 | + (void)engine.tick(40'000); |
| 203 | + } |
| 204 | + catch (const std::runtime_error &) |
| 205 | + { |
| 206 | + propagated = true; |
| 207 | + } |
79 | 208 |
|
80 | | - auto saved = store->get(id); |
| 209 | + assert(propagated); |
| 210 | + assert(transport->callCount() == 0); |
| 211 | +} |
| 212 | + |
| 213 | +static void test_null_probe_allows_send() |
| 214 | +{ |
| 215 | + using namespace vix::sync; |
| 216 | + using namespace vix::sync::engine; |
| 217 | + |
| 218 | + constexpr std::int64_t t0 = 50'000; |
| 219 | + auto outbox = make_outbox("./.vix_test_null_gate", "null-gate"); |
| 220 | + auto transport = std::make_shared<FakeHttpTransport>(); |
| 221 | + const auto id = outbox->enqueue(ready_operation(), t0); |
| 222 | + |
| 223 | + SyncEngine engine( |
| 224 | + SyncEngine::Config{}, |
| 225 | + outbox, |
| 226 | + std::shared_ptr<vix::net::NetworkProbe>{}, |
| 227 | + transport); |
| 228 | + |
| 229 | + assert(engine.tick(t0) >= 1); |
| 230 | + assert(transport->callCount() == 1); |
| 231 | + |
| 232 | + const auto saved = outbox->store()->get(id); |
81 | 233 | assert(saved.has_value()); |
82 | 234 | assert(saved->status == OperationStatus::Done); |
| 235 | +} |
| 236 | + |
| 237 | +int main() |
| 238 | +{ |
| 239 | + test_online_send_and_completion(); |
| 240 | + test_offline_probe_blocks_ready_send(); |
| 241 | + test_workers_share_rate_limited_probe_state(); |
| 242 | + test_offline_to_online_recovery_after_rate_limit(); |
| 243 | + test_probe_exception_propagates_from_manual_tick(); |
| 244 | + test_null_probe_allows_send(); |
83 | 245 |
|
84 | | - std::cout << "OK: operation sent and marked Done\n"; |
| 246 | + std::cout << "OK: sync send-gating regression tests passed\n"; |
85 | 247 | return 0; |
86 | 248 | } |
0 commit comments