Guest User

Untitled

a guest
Jun 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. -module(testFloodAll).
  2. -export([runTest/0]).
  3. -export([floodPid/3, floodPidControl/2]).
  4.  
  5. %% Circular network consisting of three nodes.
  6. %%
  7. circularNetwork3 () ->
  8. [
  9. {r1, [{r2, [r2]},{r3, [r3]},{r4, [r4]}, {r12, [r12]}, {r5, [r5]}]},
  10. {r2, [{r1, [r1, r12, r5]},{r3, [r3]},{r4, [r4]}]},
  11. {r12, [{r2, [r1,r2,r3,r4, r5]}]},
  12. {r3, [{r2, [r2]},{r1, [r1, r12, r5]},{r4, [r4]}]},
  13. {r4, [{r2, [r2]},{r3, [r3]},{r1, [r1, r12, r5]}]},
  14. {r5, [{r1, [r1,r12,r3,r4]}, {r2, [r2]}]}
  15. ].
  16.  
  17.  
  18. waitTillResult(Pid, SnapName, BaseSnapName, NextSnapNum) ->
  19. MyPid = self(),
  20. receive
  21. {abort, Pid, SnapName} -> [], floodPid(Pid, BaseSnapName, NextSnapNum);
  22. {snapshotComplete, Pid, SnapName} -> erlang:error("Unexpected flooded snapshot succeeded!"),halt();
  23. {snapshot, _Pid, MyPid, SnapName} -> waitTillResult(Pid, SnapName, BaseSnapName, NextSnapNum);
  24. {snapshotSaved, Pid, SnapName} -> waitTillResult(Pid, SnapName, BaseSnapName, NextSnapNum);
  25. {flooderShouldStop, ReplyPid} -> ReplyPid ! flooderHasStopped;
  26. {snapshot, _, MyPid, _} -> waitTillResult(Pid, SnapName, BaseSnapName, NextSnapNum);
  27. Other -> io:format("waitTillResult for ~p ~p got unknown ~p ~n", [Pid, SnapName, Other]), erlang:error("UNknown message in waitTilLResult"),halt()
  28. after 2000 ->
  29. erlang:error("***FAIL: timeout in waitTillResult!"),halt()
  30. end
  31. .
  32.  
  33. % flood will keep sending controls to a pid
  34. % and will expect aborts for all
  35. floodPid(Pid, BaseSnapName, SnapNum) ->
  36. SnapName = BaseSnapName ++ integer_to_list(SnapNum),
  37. %io:format("floodpid~n"),
  38. %timer:sleep(10),
  39. Pid ! {snapshot, self(), self(), SnapName},
  40. waitTillResult(Pid, SnapName, BaseSnapName, SnapNum + 1)
  41. .
  42.  
  43. % flood will keep sending controls to a pid
  44. % and will expect aborts for all
  45. floodPidControl(Pid, SeqNum) ->
  46. %io:format("floodpidcontrol sending seqnum ~p ~n", [SeqNum]),
  47. %timer:sleep(10),
  48. Pid ! {control, self(), self(), SeqNum, fun(_,_) -> [] end},
  49. MyPid = self(),
  50. receive
  51. {abort, Pid, SeqNum} -> [], floodPidControl(Pid, SeqNum+1);
  52. {committed, Pid, SeqNum} -> erlang:error("Unexpected flooded control succeeded!"), halt();
  53. {flooderShouldStop, ReplyPid} -> ReplyPid ! flooderHasStopped;
  54. Other -> io:format("floodPidControl got unknown ~p ~n", [Other]), erlang:error("UNknown message in floodPidControl"),halt()
  55. after 400 ->
  56. erlang:error("***FAIL: timeout in floodPidControl!"),halt()
  57. end
  58. .
  59.  
  60.  
  61.  
  62. %% Build a circular network and then use a control request to change the
  63. %% direction of all edges; verify the original and reversed network
  64. %%
  65. runTest () ->
  66. io:format ("*** Starting router network...~n"),
  67. CGraph = circularNetwork3 (),
  68. R1Pid = control:graphToNetwork (CGraph),
  69. networkTest:verifyNetwork (R1Pid, CGraph),
  70.  
  71. {R12Pid, _} = networkTest:probeNetwork(R1Pid, r12),
  72. {R2Pid, _} = networkTest:probeNetwork (R1Pid, r2),
  73. {R3Pid, _} = networkTest:probeNetwork (R1Pid, r3),
  74. {R4Pid, _} = networkTest:probeNetwork (R1Pid, r4),
  75. {R5Pid, _} = networkTest:probeNetwork(R1Pid, r5),
  76.  
  77. MyPid = self(),
  78.  
  79. % we pause r2 using a control
  80. R12Pid ! {control, self(), self(), 1, fun(RouterName, _T) ->
  81. if RouterName == r2 ->
  82. io:format("r2 is sleeping~n"),
  83. timer:sleep(2000),
  84. MyPid ! {flooderShouldStop, self()},
  85. % 2 flooders running, both should stop before we wake
  86. receive flooderHasStopped -> io:format("flooder1/5 has stopped!~n") end,
  87. receive flooderHasStopped -> io:format("flooder2/5 has stopped!~n") end,
  88. receive flooderHasStopped -> io:format("flooder3/5 has stopped!~n") end,
  89. receive flooderHasStopped -> io:format("flooder4/5 has stopped!~n") end,
  90. receive flooderHasStopped -> io:format("flooder5/5 has stopped!~n") end,
  91. timer:sleep(200),
  92. abort;
  93. RouterName == r12 -> [];
  94. true -> abort
  95. end end},
  96.  
  97. timer:sleep(100),
  98.  
  99. % and then we start our 2 flooders
  100. R1Flooder = spawn(testFloodAll, floodPidControl, [R1Pid, 2]),
  101. R3Flooder = spawn(testFloodAll, floodPid, [R3Pid, "snap3", 1]),
  102. R4Flooder = spawn(testFloodAll, floodPid, [R4Pid, "snap4", 1]),
  103. R5SFlooder = spawn(testFloodAll, floodPid, [R5Pid, "snap5", 1]),
  104. R5PFlooder = spawn(testFloodAll, floodPidControl, [R5Pid, 100000]),
  105.  
  106.  
  107. % should get our flooder should stop
  108. receive
  109. {flooderShouldStop, R2Pid} -> io:format("tell our flooder to stop!~n"),
  110. R3Flooder ! {flooderShouldStop, R2Pid},
  111. R4Flooder ! {flooderShouldStop, R2Pid},
  112. R5PFlooder ! {flooderShouldStop, R2Pid},
  113. R5SFlooder ! {flooderShouldStop, R2Pid},
  114. R1Flooder ! {flooderShouldStop, R2Pid};
  115. Other45 -> io:format("Unknown msg: ~p~n", [Other45]), erlang:error("**Fail unknown message"),halt()
  116. after 2500 ->
  117. erlang:error("Timeout waiting for r2 to wake and send flooderShouldStop"),halt()
  118. end,
  119.  
  120. % and now the control will abort as we are aborting it
  121. receive
  122. {abort, R12Pid, 1} -> io:format("***Pass control aborted as expected~n");
  123. Othr345 -> io:format("Unknown : ~p~n", [Othr345]), erlang:error("Expected abort")
  124. after 4000 ->
  125. erlang:error("Timeout waiting for r2 to wake and send flooderShouldStop"),halt()
  126. end,
  127.  
  128. pass.
Add Comment
Please, Sign In to add comment