Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(testFloodAll).
- -export([runTest/0]).
- -export([floodPid/3, floodPidControl/2]).
- %% Circular network consisting of three nodes.
- %%
- circularNetwork3 () ->
- [
- {r1, [{r2, [r2]},{r3, [r3]},{r4, [r4]}, {r12, [r12]}, {r5, [r5]}]},
- {r2, [{r1, [r1, r12, r5]},{r3, [r3]},{r4, [r4]}]},
- {r12, [{r2, [r1,r2,r3,r4, r5]}]},
- {r3, [{r2, [r2]},{r1, [r1, r12, r5]},{r4, [r4]}]},
- {r4, [{r2, [r2]},{r3, [r3]},{r1, [r1, r12, r5]}]},
- {r5, [{r1, [r1,r12,r3,r4]}, {r2, [r2]}]}
- ].
- waitTillResult(Pid, SnapName, BaseSnapName, NextSnapNum) ->
- MyPid = self(),
- receive
- {abort, Pid, SnapName} -> [], floodPid(Pid, BaseSnapName, NextSnapNum);
- {snapshotComplete, Pid, SnapName} -> erlang:error("Unexpected flooded snapshot succeeded!"),halt();
- {snapshot, _Pid, MyPid, SnapName} -> waitTillResult(Pid, SnapName, BaseSnapName, NextSnapNum);
- {snapshotSaved, Pid, SnapName} -> waitTillResult(Pid, SnapName, BaseSnapName, NextSnapNum);
- {flooderShouldStop, ReplyPid} -> ReplyPid ! flooderHasStopped;
- {snapshot, _, MyPid, _} -> waitTillResult(Pid, SnapName, BaseSnapName, NextSnapNum);
- Other -> io:format("waitTillResult for ~p ~p got unknown ~p ~n", [Pid, SnapName, Other]), erlang:error("UNknown message in waitTilLResult"),halt()
- after 2000 ->
- erlang:error("***FAIL: timeout in waitTillResult!"),halt()
- end
- .
- % flood will keep sending controls to a pid
- % and will expect aborts for all
- floodPid(Pid, BaseSnapName, SnapNum) ->
- SnapName = BaseSnapName ++ integer_to_list(SnapNum),
- %io:format("floodpid~n"),
- %timer:sleep(10),
- Pid ! {snapshot, self(), self(), SnapName},
- waitTillResult(Pid, SnapName, BaseSnapName, SnapNum + 1)
- .
- % flood will keep sending controls to a pid
- % and will expect aborts for all
- floodPidControl(Pid, SeqNum) ->
- %io:format("floodpidcontrol sending seqnum ~p ~n", [SeqNum]),
- %timer:sleep(10),
- Pid ! {control, self(), self(), SeqNum, fun(_,_) -> [] end},
- MyPid = self(),
- receive
- {abort, Pid, SeqNum} -> [], floodPidControl(Pid, SeqNum+1);
- {committed, Pid, SeqNum} -> erlang:error("Unexpected flooded control succeeded!"), halt();
- {flooderShouldStop, ReplyPid} -> ReplyPid ! flooderHasStopped;
- Other -> io:format("floodPidControl got unknown ~p ~n", [Other]), erlang:error("UNknown message in floodPidControl"),halt()
- after 400 ->
- erlang:error("***FAIL: timeout in floodPidControl!"),halt()
- end
- .
- %% Build a circular network and then use a control request to change the
- %% direction of all edges; verify the original and reversed network
- %%
- runTest () ->
- io:format ("*** Starting router network...~n"),
- CGraph = circularNetwork3 (),
- R1Pid = control:graphToNetwork (CGraph),
- networkTest:verifyNetwork (R1Pid, CGraph),
- {R12Pid, _} = networkTest:probeNetwork(R1Pid, r12),
- {R2Pid, _} = networkTest:probeNetwork (R1Pid, r2),
- {R3Pid, _} = networkTest:probeNetwork (R1Pid, r3),
- {R4Pid, _} = networkTest:probeNetwork (R1Pid, r4),
- {R5Pid, _} = networkTest:probeNetwork(R1Pid, r5),
- MyPid = self(),
- % we pause r2 using a control
- R12Pid ! {control, self(), self(), 1, fun(RouterName, _T) ->
- if RouterName == r2 ->
- io:format("r2 is sleeping~n"),
- timer:sleep(2000),
- MyPid ! {flooderShouldStop, self()},
- % 2 flooders running, both should stop before we wake
- receive flooderHasStopped -> io:format("flooder1/5 has stopped!~n") end,
- receive flooderHasStopped -> io:format("flooder2/5 has stopped!~n") end,
- receive flooderHasStopped -> io:format("flooder3/5 has stopped!~n") end,
- receive flooderHasStopped -> io:format("flooder4/5 has stopped!~n") end,
- receive flooderHasStopped -> io:format("flooder5/5 has stopped!~n") end,
- timer:sleep(200),
- abort;
- RouterName == r12 -> [];
- true -> abort
- end end},
- timer:sleep(100),
- % and then we start our 2 flooders
- R1Flooder = spawn(testFloodAll, floodPidControl, [R1Pid, 2]),
- R3Flooder = spawn(testFloodAll, floodPid, [R3Pid, "snap3", 1]),
- R4Flooder = spawn(testFloodAll, floodPid, [R4Pid, "snap4", 1]),
- R5SFlooder = spawn(testFloodAll, floodPid, [R5Pid, "snap5", 1]),
- R5PFlooder = spawn(testFloodAll, floodPidControl, [R5Pid, 100000]),
- % should get our flooder should stop
- receive
- {flooderShouldStop, R2Pid} -> io:format("tell our flooder to stop!~n"),
- R3Flooder ! {flooderShouldStop, R2Pid},
- R4Flooder ! {flooderShouldStop, R2Pid},
- R5PFlooder ! {flooderShouldStop, R2Pid},
- R5SFlooder ! {flooderShouldStop, R2Pid},
- R1Flooder ! {flooderShouldStop, R2Pid};
- Other45 -> io:format("Unknown msg: ~p~n", [Other45]), erlang:error("**Fail unknown message"),halt()
- after 2500 ->
- erlang:error("Timeout waiting for r2 to wake and send flooderShouldStop"),halt()
- end,
- % and now the control will abort as we are aborting it
- receive
- {abort, R12Pid, 1} -> io:format("***Pass control aborted as expected~n");
- Othr345 -> io:format("Unknown : ~p~n", [Othr345]), erlang:error("Expected abort")
- after 4000 ->
- erlang:error("Timeout waiting for r2 to wake and send flooderShouldStop"),halt()
- end,
- pass.
Add Comment
Please, Sign In to add comment