summaryrefslogtreecommitdiff
path: root/ocaml/scripts/test_lmdb_eventlog.ml
diff options
context:
space:
mode:
Diffstat (limited to 'ocaml/scripts/test_lmdb_eventlog.ml')
-rw-r--r--ocaml/scripts/test_lmdb_eventlog.ml114
1 files changed, 114 insertions, 0 deletions
diff --git a/ocaml/scripts/test_lmdb_eventlog.ml b/ocaml/scripts/test_lmdb_eventlog.ml
new file mode 100644
index 0000000..e7a4971
--- /dev/null
+++ b/ocaml/scripts/test_lmdb_eventlog.ml
@@ -0,0 +1,114 @@
+open Nock_lib
+
+let rec find_project_root dir =
+ let pills_dir = Filename.concat dir "pills" in
+ if Sys.file_exists pills_dir && Sys.is_directory pills_dir then dir
+ else
+ let parent = Filename.dirname dir in
+ if String.equal parent dir then failwith "unable to locate project root containing pills/"
+ else find_project_root parent
+
+let project_root =
+ match Sys.getenv_opt "NEOVERE_ROOT" with
+ | Some root -> root
+ | None ->
+ let exe_dir = Filename.dirname Sys.executable_name in
+ find_project_root exe_dir
+
+let () =
+ Printf.printf "\n╔═══════════════════════════════════════════════════════╗\n";
+ Printf.printf "║ LMDB Event Log Test ║\n";
+ Printf.printf "╚═══════════════════════════════════════════════════════╝\n\n";
+
+ let test_pier = Filename.concat project_root "test-pier-lmdb" in
+
+ (* Clean up old test pier if it exists *)
+ if Sys.file_exists test_pier then begin
+ Printf.printf "Removing old test pier...\n";
+ ignore (Sys.command (Printf.sprintf "rm -rf %s" test_pier))
+ end;
+
+ (* Create test pier directory *)
+ Unix.mkdir test_pier 0o755;
+
+ Printf.printf "Creating LMDB event log...\n";
+ let log = Eventlog_lmdb.create test_pier in
+
+ Printf.printf "Creating test events...\n\n";
+
+ (* Create some test events *)
+ let test_events = [
+ Noun.atom (Z.of_int 42);
+ Noun.cell (Noun.atom (Z.of_int 1)) (Noun.atom (Z.of_int 2));
+ Noun.cell (Noun.atom (Z.of_int 3)) (Noun.cell (Noun.atom (Z.of_int 4)) (Noun.atom Z.zero));
+ Noun.atom (Z.of_int 0xdeadbeef);
+ Noun.cell (Noun.atom (Z.of_bits "hello")) (Noun.atom Z.zero);
+ ] in
+
+ (* Write events *)
+ Printf.printf "Writing %d events to LMDB...\n" (List.length test_events);
+ List.iteri (fun i event ->
+ let event_num = Eventlog_lmdb.append log event in
+ Printf.printf " Event %d: wrote as event number %Ld\n" (i+1) event_num
+ ) test_events;
+
+ (* Sync to disk *)
+ Printf.printf "\nSyncing to disk...\n";
+ Eventlog_lmdb.sync log;
+
+ (* Check gulf *)
+ Printf.printf "\nChecking event range (gulf)...\n";
+ begin match Eventlog_lmdb.gulf log with
+ | None -> Printf.printf " No events in log\n"
+ | Some (first, last) ->
+ Printf.printf " First event: %Ld, Last event: %Ld\n" first last
+ end;
+
+ (* Read events back *)
+ Printf.printf "\nReading events back...\n";
+ for i = 1 to List.length test_events do
+ let event_num = Int64.of_int i in
+ let event = Eventlog_lmdb.read_event log event_num in
+ Printf.printf " Event %Ld: %s\n" event_num
+ (if Noun.is_cell event then "cell" else "atom")
+ done;
+
+ (* Test replay *)
+ Printf.printf "\nTesting replay...\n";
+ let replay_count = ref 0 in
+ Eventlog_lmdb.replay log (fun event_num _noun ->
+ incr replay_count;
+ Printf.printf " Replayed event %Ld\n" event_num
+ );
+ Printf.printf "Replayed %d events total\n" !replay_count;
+
+ (* Close the log *)
+ Printf.printf "\nClosing event log...\n";
+ Eventlog_lmdb.close log;
+
+ (* Reopen and verify *)
+ Printf.printf "Reopening event log...\n";
+ let log2 = Eventlog_lmdb.create test_pier in
+ let last = Eventlog_lmdb.last_event log2 in
+ Printf.printf " Last event after reopen: %Ld\n" last;
+
+ if last = Int64.of_int (List.length test_events) then begin
+ Printf.printf "\n✓ LMDB event log test PASSED!\n";
+ Printf.printf " - Wrote %d events\n" (List.length test_events);
+ Printf.printf " - Read all events back successfully\n";
+ Printf.printf " - Replay worked correctly\n";
+ Printf.printf " - Persistence verified after reopen\n"
+ end else begin
+ Printf.printf "\n✗ LMDB event log test FAILED!\n";
+ Printf.printf " Expected last event: %d, Got: %Ld\n"
+ (List.length test_events) last;
+ exit 1
+ end;
+
+ Eventlog_lmdb.close log2;
+
+ Printf.printf "\nTest pier created at: %s\n" test_pier;
+ Printf.printf "LMDB files:\n";
+ ignore (Sys.command (Printf.sprintf "ls -lh %s/.urb/log/" test_pier));
+
+ Printf.printf "\n"