- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
dirty_boostrap_test() ->
SourceTab = ets:new(source, [public, named_table]),
ReplicaTab = ets:new(replica, [public, named_table]),
%% Insert some initial data:
ets:insert(source, {1, 1}),
ets:insert(source, {2, 2}),
ets:insert(source, {3, 3}),
try
register(testcase, self()),
Replica = spawn_link(fun replica/0),
register(replica, Replica),
%% "importer" process emulates mnesia_tm:
spawn_link(fun importer/0),
%% "bootstrapper" process emulates bootstrapper server:
spawn_link(fun bootstrapper/0),
receive
done ->
SrcData = lists:sort(ets:tab2list(source)),
RcvData = lists:sort(ets:tab2list(replica)),
?assertEqual(SrcData, RcvData)
end
after
ets:delete(SourceTab),
ets:delete(ReplicaTab)
end.
importer() ->
Ops = [ {write, 3, 3}
, {write, 4, 4}
, {write, 4, 5}
, {delete, 2}
],
lists:map(fun(OP) ->
import_op(source, OP),
%% Imitate mnesia event (note: here we send it
%% directly to the replica process bypassing
%% the agent):
replica ! {tlog, OP}
end,
Ops),
replica ! last_trans.
replica() ->
receive
{bootstrap, K, V} ->
ets:insert(replica, {K, V}),
replica();
bootstrap_done ->
replay()
end.
replay() ->
receive
{tlog, Op} ->
import_op(replica, Op),
replay();
last_trans ->
testcase ! done
end.
import_op(Tab, {write, K, V}) ->
ets:insert(Tab, {K, V});
import_op(Tab, {delete, K}) ->
ets:delete(Tab, K).
bootstrapper() ->
{Keys, _} = lists:unzip(ets:tab2list(source)),
[replica ! {bootstrap, K, V} || K <- Keys, {_, V} <- ets:lookup(source, K)],
replica ! bootstrap_done.