Skip to content

Commit

Permalink
More changes to new interface format
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianfranco committed Jul 2, 2011
1 parent 1931517 commit dc45fff
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 47 deletions.
82 changes: 43 additions & 39 deletions src/frame_axiom.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
-export([diff/2, -export([diff/2,
diff/3]). diff/3]).


snapshot(SnapShot) when SnapShot == node;
SnapShot == named_process ->
snapshot(ets:new(snapshot,[private]),SnapShot);
snapshot(SnapShots) when is_list(SnapShots) -> snapshot(SnapShots) when is_list(SnapShots) ->
lists:foldl(fun(SnapShot,Ets) -> snapshot(Ets,SnapShot) lists:foldl(fun(SnapShot,Ets) -> snapshot(Ets,SnapShot)
end,ets:new(snapshot,[private]),SnapShots). end,ets:new(snapshot,[private]),SnapShots).
Expand Down Expand Up @@ -58,22 +55,17 @@ snapshot(Ets,{port,all}) ->
snapshot(Ets,{port,Options}) when is_list(Options) -> snapshot(Ets,{port,Options}) when is_list(Options) ->
lists:foldl(fun(Option,EtsAcc) -> snapshot(EtsAcc,port,Option) lists:foldl(fun(Option,EtsAcc) -> snapshot(EtsAcc,port,Option)
end,Ets,Options); end,Ets,Options);
snapshot(Ets,{dir,{all,Path}}) ->
snapshot(Ets,{dir,[{X,Path}||X<-all(dir)]});
snapshot(Ets,{dir,Options}) when is_list(Options) -> snapshot(Ets,{dir,Options}) when is_list(Options) ->
lists:foldl(fun(Option,EtsAcc) -> snapshot(EtsAcc,dir,Option) lists:foldl(fun(Option,EtsAcc) -> snapshot(EtsAcc,dir,Option)
end,Ets,Options); end,Ets,Options);

snapshot(Ets,{node,all}) ->
snapshot(Ets,ets) -> snapshot(Ets,{node,all(node)});
Existing = ets:all(), snapshot(Ets,{node,Options}) when is_list(Options) ->
ets:insert(Ets,{ets,Existing}), lists:foldl(fun(Option,EtsAcc) -> snapshot(EtsAcc,node,Option)
Ets; end,Ets,Options).
snapshot(Ets,{dir,Path}) ->
Structure = collect(false,Path),
ets:insert(Ets,{{dir,Path},Structure}),
Ets;
snapshot(Ets,node) ->
Current = nodes(),
ets:insert(Ets,{node,Current}),
Ets.


snapshot(Ets,process,creation) -> snapshot(Ets,process,creation) ->
Pids = erlang:processes(), Pids = erlang:processes(),
Expand Down Expand Up @@ -146,7 +138,16 @@ snapshot(Ets,dir,{deletion,Path}) ->
snapshot(Ets,dir,{content_changes,Path}) -> snapshot(Ets,dir,{content_changes,Path}) ->
Current = collect(true,Path), Current = collect(true,Path),
ets:insert(Ets,{{content_changes,Path},Current}), ets:insert(Ets,{{content_changes,Path},Current}),
Ets;
snapshot(Ets,node,connection) ->
Current = nodes(),
ets:insert(Ets,{{node,connection},Current}),
Ets;
snapshot(Ets,node,disconnection) ->
Current = nodes(),
ets:insert(Ets,{{node,disconnection},Current}),
Ets. Ets.



diff(Ets,[X]) -> diff(Ets,[X]) ->
diff(Ets,X); diff(Ets,X);
Expand Down Expand Up @@ -174,25 +175,16 @@ diff(Ets,{port,all}) ->
diff(Ets,{port,Options}) when is_list(Options) -> diff(Ets,{port,Options}) when is_list(Options) ->
lists:foldl(fun(Option,Res) -> Res++diff(Ets,port,Option) lists:foldl(fun(Option,Res) -> Res++diff(Ets,port,Option)
end,[],Options); end,[],Options);
diff(Ets,{dir,{all,Path}}) ->
diff(Ets,{dir,[{X,Path}||X<-all(dir)]});
diff(Ets,{dir,Options}) when is_list(Options) -> diff(Ets,{dir,Options}) when is_list(Options) ->
lists:foldl(fun(Option,Res) -> Res++diff(Ets,dir,Option) lists:foldl(fun(Option,Res) -> Res++diff(Ets,dir,Option)
end,[],Options); end,[],Options);

diff(Ets,{node,all}) ->
diff(Ets,ets) -> diff(Ets,{node,all(node)});
Current = ets:all(), diff(Ets,{node,Options}) when is_list(Options) ->
[{ets,Recorded}] = ets:lookup(Ets,ets), lists:foldl(fun(Option,Res) -> Res++diff(Ets,node,Option)
{Created,Deleted} = split(created,deleted,Current,Recorded), end,[],Options).
Created++Deleted;
diff(Ets,{dir,Path}) ->
Current = collect(false,Path),
[{{dir,Path},Recorded}] = ets:lookup(Ets,{dir,Path}),
{Created,Deleted} = split(created,deleted,Current,Recorded),
Created++Deleted;
diff(Ets,node) ->
Current = nodes(),
[{node,Recorded}] = ets:lookup(Ets,node),
{Connected,Disconnected} = split(connected,disconnected,Current,Recorded),
Connected++Disconnected.


diff(Ets,process,creation) -> diff(Ets,process,creation) ->
CurrentPids = erlang:processes(), CurrentPids = erlang:processes(),
Expand Down Expand Up @@ -303,7 +295,18 @@ diff(Ets,dir,{content_changes,Path}) ->
Current = collect(true,Path), Current = collect(true,Path),
Key = {content_changes,Path}, Key = {content_changes,Path},
[{Key,Recorded}] = ets:lookup(Ets,Key), [{Key,Recorded}] = ets:lookup(Ets,Key),
contents_changed(Recorded,Current). contents_changed(Recorded,Current);
diff(Ets,node,connection) ->
Current = nodes(),
Key = {node,connection},
[{Key,Recorded}] = ets:lookup(Ets,Key),
[{connected,N}||N<-Current,not lists:member(N,Recorded)];
diff(Ets,node,disconnection) ->
Current = nodes(),
Key = {node,disconnection},
[{Key,Recorded}] = ets:lookup(Ets,Key),
[{disconnected,N}||N<-Recorded,not lists:member(N,Current)].





%% Helpers section %% Helpers section
Expand All @@ -316,7 +319,13 @@ all(application) ->
all(ets) -> all(ets) ->
[creation,deletion]; [creation,deletion];
all(port) -> all(port) ->
[opened,closed]. [opened,closed];
all(node) ->
[connection,disconnection];
all(dir) ->
[creation,deletion,content_changes].








Expand All @@ -341,11 +350,6 @@ collect(ExactP,Path) ->
end end
end. end.



split(KeyA,KeyB,As,Bs) ->
{[{KeyA,A}||A<-As,not lists:member(A,Bs)],
[{KeyB,B}||B<-Bs,not lists:member(B,As)]}.

named_processes() -> named_processes() ->
Procs = [{P,erlang:process_info(P,registered_name)}||P<-erlang:processes()], Procs = [{P,erlang:process_info(P,registered_name)}||P<-erlang:processes()],
lists:foldl(fun({_,{registered_name,N}},Acc) -> Acc++[N]; lists:foldl(fun({_,{registered_name,N}},Acc) -> Acc++[N];
Expand Down
34 changes: 26 additions & 8 deletions test/frame_axiom_tests.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ named_process_replaced_diff_test() ->
synchronoulsy_kill_process(Pid2). synchronoulsy_kill_process(Pid2).


all_no_change_diff_test() -> all_no_change_diff_test() ->
timer:sleep(300), %% Let the Eunit processes settle in peace
Options = all, Options = all,
Ref = frame_axiom:snapshot([{process,Options}]), Ref = frame_axiom:snapshot([{process,Options}]),
?assertEqual([],frame_axiom:diff(Ref,[{process,Options}])). ?assertEqual([],frame_axiom:diff(Ref,[{process,Options}])).
Expand Down Expand Up @@ -302,22 +303,41 @@ file_no_change_all_diff_test() ->
node_connected_diff_test() -> node_connected_diff_test() ->
ensure_empd(), ensure_empd(),
net_kernel:start([box_box,shortnames]), net_kernel:start([box_box,shortnames]),
Ref = frame_axiom:snapshot(node), Options = [connection],
Ref = frame_axiom:snapshot([{node,Options}]),
{ok,Node} = slave:start(list_to_atom(inet_db:gethostname()),'slave'), {ok,Node} = slave:start(list_to_atom(inet_db:gethostname()),'slave'),
?assertEqual([{connected,Node}],frame_axiom:diff(Ref,node)), ?assertEqual([{connected,Node}],frame_axiom:diff(Ref,[{node,Options}])),
slave:stop(Node), slave:stop(Node),
net_kernel:stop(). net_kernel:stop().


node_disconnected_diff_test() -> node_disconnected_diff_test() ->
ensure_empd(), ensure_empd(),
net_kernel:start([box_box,shortnames]), net_kernel:start([box_box,shortnames]),
Options = [disconnection],
{ok,Node} = slave:start(list_to_atom(inet_db:gethostname()),'slave'), {ok,Node} = slave:start(list_to_atom(inet_db:gethostname()),'slave'),
Ref = frame_axiom:snapshot(node), Ref = frame_axiom:snapshot([{node,Options}]),
slave:stop(Node), slave:stop(Node),
?assertEqual([{disconnected,Node}],frame_axiom:diff(Ref,node)), ?assertEqual([{disconnected,Node}],frame_axiom:diff(Ref,[{node,Options}])),
net_kernel:stop(). net_kernel:stop().


node_no_diff_all_test() ->
Options = all,
Ref = frame_axiom:snapshot([{node,Options}]),
?assertEqual([],frame_axiom:diff(Ref,[{node,Options}])).


node_all_diff_test() ->
ensure_empd(),
net_kernel:start([box_box,shortnames]),
Options = all,
{ok,Node1} = slave:start(list_to_atom(inet_db:gethostname()),'slave_one'),
Ref = frame_axiom:snapshot([{node,Options}]),
slave:stop(Node1),
{ok,Node2} = slave:start(list_to_atom(inet_db:gethostname()),'slave_two'),
?assertEqual([{connected,Node2},
{disconnected,Node1}],frame_axiom:diff(Ref,[{node,Options}])),
slave:stop(Node2),
net_kernel:stop().

%% multiple type snapshot %% multiple type snapshot
%% --------------------------------------------------------- %% ---------------------------------------------------------
multiple_type_creation_test() -> multiple_type_creation_test() ->
Expand Down Expand Up @@ -404,16 +424,14 @@ second_snapshot_call_test() ->
{application,A_Options}])), {application,A_Options}])),
application:stop(snmp). application:stop(snmp).



successive_snapshots_of_same_resets_test() -> successive_snapshots_of_same_resets_test() ->
E_Options = all, E_Options = all,
Ref = frame_axiom:snapshot([{ets,E_Options}]), Ref = frame_axiom:snapshot([{ets,E_Options}]),
Ets = ets:new(a,[]), Ets = ets:new(a,[]),
frame_axiom:snapshot(Ref,ets), frame_axiom:snapshot(Ref,[{ets,E_Options}]),
?assertEqual([],frame_axiom:diff(Ref,ets)), ?assertEqual([],frame_axiom:diff(Ref,[{ets,E_Options}])),
ets:delete(Ets). ets:delete(Ets).



%% helpers %% helpers
%% ----------------------------------------------------------------------------- %% -----------------------------------------------------------------------------
synchronoulsy_start_named(Name) -> synchronoulsy_start_named(Name) ->
Expand Down

0 comments on commit dc45fff

Please sign in to comment.