From 4334e2d7343195cf1953d7815d46a398dcb2ba91 Mon Sep 17 00:00:00 2001 From: Russell Brown Date: Thu, 6 Sep 2018 14:08:09 +0100 Subject: [PATCH] Add unit test for pclr snapshot closing This was tested by the eqc, but we merged the fix without the test. This eunit test fixes that. Coverage! --- src/leveled_penciller.erl | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/leveled_penciller.erl b/src/leveled_penciller.erl index aa63e69..c7e8b10 100644 --- a/src/leveled_penciller.erl +++ b/src/leveled_penciller.erl @@ -2156,4 +2156,57 @@ coverage_cheat_test() -> {noreply, _State0} = handle_info(timeout, #state{}), {ok, _State1} = code_change(null, #state{}, null). +handle_down_test() -> + RootPath = "../test/ledger", + clean_testdir(RootPath), + {ok, PCLr} = pcl_start(#penciller_options{root_path=RootPath, + max_inmemory_tablesize=1000, + compression_method=native}), + FakeBookie = spawn(fun loop/0), + + Mon = erlang:monitor(process, FakeBookie), + + FakeBookie ! {snap, PCLr, self()}, + + {ok, PclSnap, null} = + receive + {FakeBookie, {ok, Snap, null}} -> + {ok, Snap, null} + end, + + FakeBookie ! stop, + + receive + {'DOWN', Mon, process, FakeBookie, normal} -> + %% Now we know that pclr should have received this too! + %% (better than timer:sleep/1) + ok + end, + + ?assertEqual(undefined, erlang:process_info(PclSnap)), + + pcl_close(PCLr), + clean_testdir(RootPath). + +%% the fake bookie. Some calls to leveled_bookie (like the two below) +%% do not go via the gen_server (but it looks like they expect to be +%% called by the gen_server, internally!) they use "self()" to +%% populate the bookie's pid in the pclr. This process wrapping the +%% calls ensures that the TEST controls the bookie's Pid. The +%% FakeBookie. +loop() -> + receive + {snap, PCLr, TestPid} -> + Res = leveled_bookie:snapshot_store(leveled_bookie:empty_ledgercache(), + PCLr, + null, + ledger, + undefined, + false), + TestPid ! {self(), Res}, + loop(); + stop -> + ok + end. + -endif.