-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathdatabase-writer-pool.test.ts
More file actions
1138 lines (938 loc) · 37.2 KB
/
Copy pathdatabase-writer-pool.test.ts
File metadata and controls
1138 lines (938 loc) · 37.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Database from "better-sqlite3";
import { setDb, runMigrations } from "../src/indexer/db.js";
import {
queueWrite,
executeWriteTransaction,
executeBatchWrite,
flushWriteQueue,
getWriteQueueSize,
isWriteQueueProcessing,
createSqlOperation,
createReadWriteOperation,
isRpcTimeoutError,
computeRpcBackoffMs,
setWriterPoolRpcRetryConfig,
getWriterPoolRpcRetryConfig,
resetWriterPoolRpcRetryConfig,
type WriteOperation,
} from "../src/indexer/database-writer-pool.js";
describe("DatabaseWriterPool – Concurrent Write Operations", () => {
let testDb: Database.Database;
beforeAll(() => {
testDb = new Database(":memory:");
setDb(testDb);
});
afterAll(() => {
testDb.close();
});
beforeEach(async () => {
testDb.exec("DROP TABLE IF EXISTS test_data");
testDb.exec("DROP TABLE IF EXISTS events");
testDb.exec("DROP TABLE IF EXISTS indexer_state");
testDb.exec("DROP TABLE IF EXISTS monitored_contracts");
testDb.exec("DROP TABLE IF EXISTS schema_migrations");
// Create a test table
testDb.exec(`
CREATE TABLE test_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
value TEXT NOT NULL,
count INTEGER DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`);
runMigrations();
await flushWriteQueue();
});
// -------------------------------------------------------------------------
// Basic write operations
// -------------------------------------------------------------------------
describe("queueWrite – basic write operations", () => {
it("executes a simple INSERT operation", async () => {
const operation: WriteOperation<{ changes: number }> = {
name: "insert-test",
execute: (db) => {
const stmt = db.prepare("INSERT INTO test_data (value) VALUES (?)");
const result = stmt.run("test_value");
return { changes: result.changes };
},
};
const result = await queueWrite(operation);
expect(result.success).toBe(true);
expect(result.data?.changes).toBe(1);
expect(result.retries).toBe(0);
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(1);
expect((rows[0] as any).value).toBe("test_value");
});
it("executes a simple UPDATE operation", async () => {
// Seed data
testDb.prepare("INSERT INTO test_data (value) VALUES (?)").run("initial");
const operation: WriteOperation<{ changes: number }> = {
name: "update-test",
execute: (db) => {
const stmt = db.prepare("UPDATE test_data SET value = ? WHERE id = 1");
const result = stmt.run("updated");
return { changes: result.changes };
},
};
const result = await queueWrite(operation);
expect(result.success).toBe(true);
expect(result.data?.changes).toBe(1);
const row = testDb.prepare("SELECT * FROM test_data WHERE id = 1").get() as any;
expect(row.value).toBe("updated");
});
it("executes a simple DELETE operation", async () => {
testDb.prepare("INSERT INTO test_data (value) VALUES (?)").run("to_delete");
const operation: WriteOperation<{ changes: number }> = {
name: "delete-test",
execute: (db) => {
const stmt = db.prepare("DELETE FROM test_data WHERE id = 1");
const result = stmt.run();
return { changes: result.changes };
},
};
const result = await queueWrite(operation);
expect(result.success).toBe(true);
expect(result.data?.changes).toBe(1);
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(0);
});
});
// -------------------------------------------------------------------------
// Transaction atomicity
// -------------------------------------------------------------------------
describe("executeWriteTransaction – ACID guarantees", () => {
it("rolls back entire transaction on error", async () => {
testDb.prepare("INSERT INTO test_data (value) VALUES (?)").run("initial");
const operation: WriteOperation<void> = {
name: "failing-transaction",
execute: (db) => {
// Insert one row
db.prepare("INSERT INTO test_data (value) VALUES (?)").run("row1");
// Try to insert duplicate (will fail)
throw new Error("Simulated failure");
},
};
const result = await executeWriteTransaction(operation);
expect(result.success).toBe(false);
expect(result.error?.message).toContain("Simulated failure");
// Only the initial row should exist
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(1);
});
it("commits transaction on success", async () => {
const operation: WriteOperation<string> = {
name: "successful-transaction",
execute: (db) => {
db.prepare("INSERT INTO test_data (value) VALUES (?)").run("row1");
db.prepare("INSERT INTO test_data (value) VALUES (?)").run("row2");
return "success";
},
};
const result = await executeWriteTransaction(operation);
expect(result.success).toBe(true);
expect(result.data).toBe("success");
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(2);
});
it("isolation: concurrent writes are serialized", async () => {
const operation1 = async () => {
const op: WriteOperation<number> = {
name: "write-1",
execute: (db) => {
db.prepare("INSERT INTO test_data (value) VALUES (?)").run("write1");
return 1;
},
};
return queueWrite(op);
};
const operation2 = async () => {
const op: WriteOperation<number> = {
name: "write-2",
execute: (db) => {
db.prepare("INSERT INTO test_data (value) VALUES (?)").run("write2");
return 2;
},
};
return queueWrite(op);
};
const [result1, result2] = await Promise.all([operation1(), operation2()]);
expect(result1.success).toBe(true);
expect(result2.success).toBe(true);
const rows = testDb.prepare("SELECT * FROM test_data ORDER BY id").all() as any[];
expect(rows).toHaveLength(2);
expect(rows[0].value).toBe("write1");
expect(rows[1].value).toBe("write2");
});
});
// -------------------------------------------------------------------------
// Batch operations
// -------------------------------------------------------------------------
describe("executeBatchWrite – atomic batch operations", () => {
it("executes multiple operations as a single transaction", async () => {
const operations: WriteOperation<number>[] = [
{
name: "batch-insert-1",
execute: (db) => {
const result = db.prepare("INSERT INTO test_data (value) VALUES (?)").run("batch1");
return result.changes;
},
},
{
name: "batch-insert-2",
execute: (db) => {
const result = db.prepare("INSERT INTO test_data (value) VALUES (?)").run("batch2");
return result.changes;
},
},
{
name: "batch-insert-3",
execute: (db) => {
const result = db.prepare("INSERT INTO test_data (value) VALUES (?)").run("batch3");
return result.changes;
},
},
];
const result = await executeBatchWrite(operations, "insert-batch");
expect(result.success).toBe(true);
expect(result.data).toHaveLength(3);
expect(result.data?.every((r) => r === 1)).toBe(true);
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(3);
});
it("rolls back all operations if one fails", async () => {
const operations: WriteOperation<number>[] = [
{
name: "batch-insert-ok",
execute: (db) => {
const result = db.prepare("INSERT INTO test_data (value) VALUES (?)").run("ok");
return result.changes;
},
},
{
name: "batch-insert-fail",
execute: (db) => {
throw new Error("Batch operation failed");
},
},
];
const result = await executeBatchWrite(operations, "failing-batch");
expect(result.success).toBe(false);
// No rows should have been inserted
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(0);
});
});
// -------------------------------------------------------------------------
// Read-write consistency
// -------------------------------------------------------------------------
describe("createReadWriteOperation – consistent read-write", () => {
it("reads and writes within the same transaction", async () => {
testDb.prepare("INSERT INTO test_data (value, count) VALUES (?, ?)").run("counter", 5);
const operation = createReadWriteOperation("increment-counter", (db) => {
const row = db
.prepare("SELECT * FROM test_data WHERE value = 'counter'")
.get() as any;
const newCount = row.count + 1;
db.prepare("UPDATE test_data SET count = ? WHERE value = 'counter'").run(newCount);
return newCount;
});
const result = await executeWriteTransaction(operation);
expect(result.success).toBe(true);
expect(result.data).toBe(6);
const row = testDb
.prepare("SELECT * FROM test_data WHERE value = 'counter'")
.get() as any;
expect(row.count).toBe(6);
});
it("isolation: read-write is consistent across concurrent operations", async () => {
testDb.prepare("INSERT INTO test_data (value, count) VALUES (?, ?)").run("counter", 0);
const increment = () => {
const operation = createReadWriteOperation("increment", (db) => {
const row = db
.prepare("SELECT * FROM test_data WHERE value = 'counter'")
.get() as any;
db.prepare("UPDATE test_data SET count = ? WHERE value = 'counter'").run(row.count + 1);
return row.count + 1;
});
return executeWriteTransaction(operation);
};
const results = await Promise.all([
increment(),
increment(),
increment(),
increment(),
increment(),
]);
results.forEach((result) => {
expect(result.success).toBe(true);
});
const row = testDb
.prepare("SELECT * FROM test_data WHERE value = 'counter'")
.get() as any;
expect(row.count).toBe(5);
});
});
// -------------------------------------------------------------------------
// Queue management
// -------------------------------------------------------------------------
describe("Queue management functions", () => {
it("getWriteQueueSize returns correct queue length", async () => {
let queueSizeAtStart = 0;
const operation: WriteOperation<void> = {
name: "queue-size-check",
execute: (db) => {
// Capture queue size during execution
queueSizeAtStart = getWriteQueueSize();
},
};
const promise = queueWrite(operation);
const queueSizeAfterQueue = getWriteQueueSize();
await promise;
const queueSizeAfterExecution = getWriteQueueSize();
expect(queueSizeAfterExecution).toBe(0);
});
it("isWriteQueueProcessing returns correct state", async () => {
let wasProcessing = false;
const operation: WriteOperation<void> = {
name: "processing-check",
execute: () => {
wasProcessing = isWriteQueueProcessing();
},
};
const promise = queueWrite(operation);
await promise;
const isProcessingNow = isWriteQueueProcessing();
expect(isProcessingNow).toBe(false);
});
it("flushWriteQueue waits for all pending operations", async () => {
const executionOrder: string[] = [];
const operations = [1, 2, 3].map((num) => ({
name: `operation-${num}`,
execute: (db: any) => {
executionOrder.push(`op${num}`);
db.prepare("INSERT INTO test_data (value) VALUES (?)").run(`value${num}`);
},
}));
// Queue all operations
const promises = operations.map((op) => queueWrite(op));
// Flush and wait
await flushWriteQueue();
// All should have completed
expect(executionOrder).toHaveLength(3);
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(3);
});
});
// -------------------------------------------------------------------------
// Helper functions
// -------------------------------------------------------------------------
describe("createSqlOperation – SQL helper", () => {
it("creates and executes INSERT operation", async () => {
const operation = createSqlOperation(
"insert-via-helper",
"INSERT INTO test_data (value) VALUES (?)",
["test_value"]
);
const result = await queueWrite(operation);
expect(result.success).toBe(true);
expect(result.data?.changes).toBe(1);
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(1);
});
it("creates and executes UPDATE operation", async () => {
testDb.prepare("INSERT INTO test_data (value) VALUES (?)").run("initial");
const operation = createSqlOperation(
"update-via-helper",
"UPDATE test_data SET value = ? WHERE id = 1",
["updated"]
);
const result = await queueWrite(operation);
expect(result.success).toBe(true);
expect(result.data?.changes).toBe(1);
const row = testDb.prepare("SELECT * FROM test_data WHERE id = 1").get() as any;
expect(row.value).toBe("updated");
});
});
// -------------------------------------------------------------------------
// Error handling and retries
// -------------------------------------------------------------------------
describe("Error handling and retries", () => {
it("returns error information on failed operation", async () => {
const operation: WriteOperation<void> = {
name: "failing-operation",
execute: () => {
throw new Error("Intentional test failure");
},
};
const result = await queueWrite(operation);
expect(result.success).toBe(false);
expect(result.error?.message).toContain("Intentional test failure");
});
it("includes execution time metrics", async () => {
const operation: WriteOperation<void> = {
name: "metric-test",
execute: (db) => {
db.prepare("INSERT INTO test_data (value) VALUES (?)").run("test");
},
};
const result = await queueWrite(operation);
expect(result.success).toBe(true);
expect(result.executionTimeMs).toBeGreaterThanOrEqual(0);
expect(typeof result.executionTimeMs).toBe("number");
});
});
// -------------------------------------------------------------------------
// Stress testing
// -------------------------------------------------------------------------
describe("Stress tests", () => {
it("handles large number of sequential writes", async () => {
const operations = Array.from({ length: 100 }, (_, i) => ({
name: `write-${i}`,
execute: (db: any) => {
db.prepare("INSERT INTO test_data (value) VALUES (?)").run(`value-${i}`);
return i;
},
}));
const results = await Promise.all(operations.map((op) => queueWrite(op)));
expect(results.every((r) => r.success)).toBe(true);
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(100);
});
it("handles mixed read-write operations with consistency", async () => {
testDb.prepare("INSERT INTO test_data (value, count) VALUES (?, ?)").run("counter", 0);
const operations = Array.from({ length: 50 }, () =>
createReadWriteOperation("increment", (db) => {
const row = db
.prepare("SELECT * FROM test_data WHERE value = 'counter'")
.get() as any;
db.prepare("UPDATE test_data SET count = ? WHERE value = 'counter'").run(
row.count + 1
);
return row.count + 1;
})
);
const results = await Promise.all(operations.map((op) => executeWriteTransaction(op)));
expect(results.every((r) => r.success)).toBe(true);
const row = testDb
.prepare("SELECT * FROM test_data WHERE value = 'counter'")
.get() as any;
expect(row.count).toBe(50);
});
it("maintains consistency with batch operations under load", async () => {
const batches = Array.from({ length: 10 }, (_, batchNum) => {
const operations = Array.from({ length: 10 }, (_, opNum) => ({
execute: (db: any) => {
db.prepare("INSERT INTO test_data (value) VALUES (?)").run(
`batch-${batchNum}-op-${opNum}`
);
},
}));
return executeBatchWrite(operations, `batch-${batchNum}`);
});
const results = await Promise.all(batches);
expect(results.every((r) => r.success)).toBe(true);
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(100);
});
});
// -------------------------------------------------------------------------
// Integration: Complex scenarios
// -------------------------------------------------------------------------
describe("Integration scenarios", () => {
it("complete workflow: insert, update, read, delete", async () => {
// Step 1: Insert
const insertOp = createSqlOperation("insert", "INSERT INTO test_data (value) VALUES (?)", [
"workflow-test",
]);
const insertResult = await queueWrite(insertOp);
expect(insertResult.success).toBe(true);
// Step 2: Update
const updateOp = createSqlOperation("update", "UPDATE test_data SET count = ? WHERE id = 1", [
42,
]);
const updateResult = await queueWrite(updateOp);
expect(updateResult.success).toBe(true);
// Step 3: Read
const row = testDb.prepare("SELECT * FROM test_data WHERE id = 1").get();
expect((row as any).count).toBe(42);
// Step 4: Delete
const deleteOp = createSqlOperation("delete", "DELETE FROM test_data WHERE id = 1", []);
const deleteResult = await queueWrite(deleteOp);
expect(deleteResult.success).toBe(true);
const finalRows = testDb.prepare("SELECT * FROM test_data").all();
expect(finalRows).toHaveLength(0);
});
it("recovers from failure and processes subsequent operations", async () => {
const failOp: WriteOperation<void> = {
name: "failing",
execute: () => {
throw new Error("Intentional failure");
},
};
const successOp = createSqlOperation("insert", "INSERT INTO test_data (value) VALUES (?)", [
"recovery-test",
]);
const result1 = await queueWrite(failOp);
expect(result1.success).toBe(false);
const result2 = await queueWrite(successOp);
expect(result2.success).toBe(true);
const rows = testDb.prepare("SELECT * FROM test_data").all();
expect(rows).toHaveLength(1);
});
});
// -------------------------------------------------------------------------
// Simulated RPC events → database schema integration (#333)
// -------------------------------------------------------------------------
describe("Simulated RPC events — database schema integration", () => {
/**
* Helper: simulate an RPC event insertion into the events table,
* mirroring the pattern used by insertEventBatch in db.ts.
*/
function simulateRpcEvent(
contractId: string,
eventType: string,
ledgerSequence: number,
timestamp: number,
dataJson: string,
): WriteOperation<{ changes: number; lastInsertRowid: number | bigint }> {
return createSqlOperation(
`rpc-event-${contractId}-${ledgerSequence}`,
`INSERT OR IGNORE INTO events
(contract_id, event_type, ledger_sequence, timestamp, data_json)
VALUES (?, ?, ?, ?, ?)`,
[contractId, eventType, ledgerSequence, timestamp, dataJson],
);
}
it("simulated ContractInitialized event is written to events schema", async () => {
const op = simulateRpcEvent(
"CONTRACT-1",
"ContractInitialized",
100,
1700000000,
JSON.stringify({ client: "GABC", freelancer: "GDEF", amount: 500 }),
);
const result = await queueWrite(op);
expect(result.success).toBe(true);
expect(result.data?.changes).toBe(1);
const row = testDb
.prepare("SELECT * FROM events WHERE contract_id = ?")
.get("CONTRACT-1") as any;
expect(row).toBeDefined();
expect(row.event_type).toBe("ContractInitialized");
expect(row.ledger_sequence).toBe(100);
expect(row.timestamp).toBe(1700000000);
});
it("simulated MilestoneApproved event is written to events schema", async () => {
const op = simulateRpcEvent(
"CONTRACT-2",
"MilestoneApproved",
200,
1700001000,
JSON.stringify({ milestone_index: 0, approved_by: "GABC" }),
);
const result = await queueWrite(op);
expect(result.success).toBe(true);
const row = testDb
.prepare("SELECT event_type FROM events WHERE contract_id = ? AND event_type = ?")
.get("CONTRACT-2", "MilestoneApproved") as any;
expect(row).toBeDefined();
expect(row.event_type).toBe("MilestoneApproved");
});
it("multiple simulated RPC events for the same contract are all written", async () => {
const events = [
{ type: "ContractInitialized", ledger: 300 },
{ type: "FundsDeposited", ledger: 310 },
{ type: "MilestoneApproved", ledger: 320 },
{ type: "ContractCompleted", ledger: 330 },
];
const operations = events.map((ev) =>
simulateRpcEvent("CONTRACT-3", ev.type, ev.ledger, 1700002000 + ev.ledger, "{}"),
);
const results = await Promise.all(operations.map((op) => queueWrite(op)));
expect(results.every((r) => r.success)).toBe(true);
const count = (
testDb
.prepare("SELECT COUNT(*) as cnt FROM events WHERE contract_id = ?")
.get("CONTRACT-3") as { cnt: number }
).cnt;
expect(count).toBe(events.length);
});
it("duplicate RPC events (same contract+ledger+type) are not double-written", async () => {
const op1 = simulateRpcEvent("CONTRACT-4", "FundsDeposited", 400, 1700003000, "{}");
const op2 = simulateRpcEvent("CONTRACT-4", "FundsDeposited", 400, 1700003000, "{}");
const r1 = await queueWrite(op1);
const r2 = await queueWrite(op2);
expect(r1.success).toBe(true);
expect(r2.success).toBe(true);
// UNIQUE constraint means second insert is ignored.
const count = (
testDb
.prepare(
"SELECT COUNT(*) as cnt FROM events WHERE contract_id = ? AND event_type = ?",
)
.get("CONTRACT-4", "FundsDeposited") as { cnt: number }
).cnt;
expect(count).toBe(1);
});
it("concurrent simulated RPC events from multiple contracts are all persisted", async () => {
const contracts = ["CA", "CB", "CC", "CD", "CE"];
const operations = contracts.map((c, i) =>
simulateRpcEvent(c, "ContractInitialized", 100 + i, 1700004000 + i, "{}"),
);
const results = await Promise.all(operations.map((op) => queueWrite(op)));
expect(results.every((r) => r.success)).toBe(true);
const total = (
testDb.prepare("SELECT COUNT(*) as cnt FROM events").get() as { cnt: number }
).cnt;
expect(total).toBe(contracts.length);
});
it("simulated events survive a read-after-write consistency check", async () => {
const op = simulateRpcEvent(
"CONTRACT-6",
"ContractInitialized",
600,
1700005000,
JSON.stringify({ client: "GXYZ" }),
);
await queueWrite(op);
// Read back using a separate query to confirm write committed.
const row = testDb
.prepare("SELECT data_json FROM events WHERE contract_id = ?")
.get("CONTRACT-6") as { data_json: string };
expect(row).toBeDefined();
const parsed = JSON.parse(row.data_json);
expect(parsed.client).toBe("GXYZ");
});
it("batch write of simulated RPC events is atomic", async () => {
const events = [
{ type: "ContractInitialized", ledger: 700 },
{ type: "FundsDeposited", ledger: 701 },
{ type: "MilestoneApproved", ledger: 702 },
];
const operations = events.map((ev) =>
simulateRpcEvent("CONTRACT-7", ev.type, ev.ledger, 1700006000 + ev.ledger, "{}"),
);
const result = await executeBatchWrite(operations, "rpc-event-batch");
expect(result.success).toBe(true);
expect(result.data).toHaveLength(3);
const count = (
testDb
.prepare("SELECT COUNT(*) as cnt FROM events WHERE contract_id = ?")
.get("CONTRACT-7") as { cnt: number }
).cnt;
expect(count).toBe(3);
});
it("all required events schema columns are populated after write", async () => {
const op = simulateRpcEvent(
"CONTRACT-8",
"ContractInitialized",
800,
1700007000,
JSON.stringify({ test: true }),
);
await queueWrite(op);
const row = testDb
.prepare("SELECT * FROM events WHERE contract_id = ?")
.get("CONTRACT-8") as any;
expect(row.id).toBeDefined();
expect(row.contract_id).toBe("CONTRACT-8");
expect(row.event_type).toBe("ContractInitialized");
expect(row.ledger_sequence).toBe(800);
expect(row.timestamp).toBe(1700007000);
expect(row.data_json).toBe(JSON.stringify({ test: true }));
expect(row.created_at).toBeDefined();
});
it("write-through transaction provides ACID guarantees for RPC events", async () => {
const failingOp: WriteOperation<void> = {
name: "failing-rpc-event",
execute: (db) => {
// Insert one valid event
db.prepare(
`INSERT OR IGNORE INTO events
(contract_id, event_type, ledger_sequence, timestamp, data_json)
VALUES (?, ?, ?, ?, ?)`,
).run("C-OK", "ContractInitialized", 900, 1700008000, "{}");
// Then fail
throw new Error("Simulated RPC write failure");
},
};
const result = await executeWriteTransaction(failingOp);
expect(result.success).toBe(false);
// The entire transaction should have rolled back — no rows inserted.
const count = (
testDb.prepare("SELECT COUNT(*) as cnt FROM events WHERE contract_id = ?").get("C-OK") as { cnt: number }
).cnt;
expect(count).toBe(0);
});
});
// RPC connection timeout retry: helpers
// -------------------------------------------------------------------------
describe("computeRpcBackoffMs – exponential backoff calculation", () => {
const baseConfig = {
initialBackoffMs: 1000,
backoffMultiplier: 2,
maxBackoffMs: 30000,
};
it("returns initial backoff for attempt 0", () => {
expect(computeRpcBackoffMs(0, baseConfig)).toBe(1000);
});
it("increases exponentially with each subsequent attempt", () => {
expect(computeRpcBackoffMs(0, baseConfig)).toBe(1000);
expect(computeRpcBackoffMs(1, baseConfig)).toBe(2000);
expect(computeRpcBackoffMs(2, baseConfig)).toBe(4000);
expect(computeRpcBackoffMs(3, baseConfig)).toBe(8000);
expect(computeRpcBackoffMs(4, baseConfig)).toBe(16000);
const d0 = computeRpcBackoffMs(0, baseConfig);
const d1 = computeRpcBackoffMs(1, baseConfig);
const d2 = computeRpcBackoffMs(2, baseConfig);
expect(d1).toBeGreaterThan(d0);
expect(d2).toBeGreaterThan(d1);
});
it("caps at maxBackoffMs", () => {
expect(computeRpcBackoffMs(5, baseConfig)).toBe(30000);
expect(computeRpcBackoffMs(10, baseConfig)).toBe(30000);
expect(computeRpcBackoffMs(100, baseConfig)).toBe(30000);
});
it("respects custom multiplier", () => {
const cfg3x = { ...baseConfig, backoffMultiplier: 3 };
expect(computeRpcBackoffMs(0, cfg3x)).toBe(1000);
expect(computeRpcBackoffMs(1, cfg3x)).toBe(3000);
expect(computeRpcBackoffMs(2, cfg3x)).toBe(9000);
});
it("respects custom initial backoff and max", () => {
const cfgCustom = { initialBackoffMs: 500, backoffMultiplier: 2, maxBackoffMs: 4000 };
expect(computeRpcBackoffMs(0, cfgCustom)).toBe(500);
expect(computeRpcBackoffMs(1, cfgCustom)).toBe(1000);
expect(computeRpcBackoffMs(2, cfgCustom)).toBe(2000);
expect(computeRpcBackoffMs(3, cfgCustom)).toBe(4000);
expect(computeRpcBackoffMs(4, cfgCustom)).toBe(4000);
});
});
describe("isRpcTimeoutError – retryable error detection", () => {
it("matches all RPC connection timeout patterns", () => {
const patterns = [
"timeout",
"ECONNRESET",
"ECONNREFUSED",
"ETIMEDOUT",
"socket hang up",
"network error",
"status 429",
"status 503",
"status 502",
"request timeout",
"connect timeout",
];
for (const pattern of patterns) {
expect(isRpcTimeoutError(new Error(pattern))).toBe(true);
expect(isRpcTimeoutError(new Error("prefix " + pattern + " suffix"))).toBe(true);
expect(isRpcTimeoutError(new Error(pattern.toUpperCase()))).toBe(true);
}
});
it("does not match non-timeout errors", () => {
const nonRetryable = [
"UNIQUE constraint failed",
"SQLITE_CONSTRAINT",
"syntax error",
"invalid argument",
"no such table",
"permission denied",
"Intentional failure",
];
for (const msg of nonRetryable) {
expect(isRpcTimeoutError(new Error(msg))).toBe(false);
}
});
it("returns false for non-Error values", () => {
expect(isRpcTimeoutError("timeout")).toBe(false);
expect(isRpcTimeoutError(undefined)).toBe(false);
expect(isRpcTimeoutError(null)).toBe(false);
expect(isRpcTimeoutError({ message: "timeout" })).toBe(false);
});
});
describe("WriterPoolRpcRetryConfig – config management", () => {
afterEach(() => {
resetWriterPoolRpcRetryConfig();
});
it("exposes sensible defaults", () => {
const cfg = getWriterPoolRpcRetryConfig();
expect(cfg.maxRetries).toBe(5);
expect(cfg.initialBackoffMs).toBe(1000);
expect(cfg.backoffMultiplier).toBe(2);
expect(cfg.maxBackoffMs).toBe(30000);
});
it("applies partial overrides via setWriterPoolRpcRetryConfig", () => {
setWriterPoolRpcRetryConfig({ maxRetries: 3, initialBackoffMs: 500 });
const cfg = getWriterPoolRpcRetryConfig();
expect(cfg.maxRetries).toBe(3);
expect(cfg.initialBackoffMs).toBe(500);
expect(cfg.backoffMultiplier).toBe(2);
expect(cfg.maxBackoffMs).toBe(30000);
});
it("returns a defensive copy from getWriterPoolRpcRetryConfig", () => {
const cfg1 = getWriterPoolRpcRetryConfig();
cfg1.maxRetries = 999;
const cfg2 = getWriterPoolRpcRetryConfig();
expect(cfg2.maxRetries).toBe(5);
});
it("resetWriterPoolRpcRetryConfig restores defaults", () => {
setWriterPoolRpcRetryConfig({ maxRetries: 1, initialBackoffMs: 10, maxBackoffMs: 100 });
resetWriterPoolRpcRetryConfig();
const cfg = getWriterPoolRpcRetryConfig();
expect(cfg.maxRetries).toBe(5);
expect(cfg.initialBackoffMs).toBe(1000);
expect(cfg.maxBackoffMs).toBe(30000);
});
});
// -------------------------------------------------------------------------
// RPC connection timeout retry: integration with queueWrite
// -------------------------------------------------------------------------
describe("queueWrite – RPC connection timeout retry", () => {
beforeEach(() => {
setWriterPoolRpcRetryConfig({
maxRetries: 3,
initialBackoffMs: 1,
backoffMultiplier: 2,
maxBackoffMs: 10,
});
});
afterEach(() => {
resetWriterPoolRpcRetryConfig();
});
it("succeeds on first attempt without RPC retry", async () => {
let calls = 0;
const op: WriteOperation<number> = {
name: "first-attempt-ok",
execute: (db) => {
calls++;
const r = db.prepare("INSERT INTO test_data (value) VALUES (?)").run("ok");
return r.changes;
},
};
const result = await queueWrite(op);
expect(result.success).toBe(true);
expect(result.retries).toBe(0);
expect(result.rpcRetries).toBe(0);
expect(calls).toBe(1);
});
it("retries on repeated RPC timeouts and increases delay exponentially", async () => {
let calls = 0;