David Reiss | 5e530af | 2009-06-04 02:01:24 +0000 | [diff] [blame] | 1 | %% Tests the behavior of clients in the face of transport errors. |
| 2 | %% Makes sure start, start_linked, and start_tethered work as expected. |
| 3 | |
| 4 | -module(test_tether). |
| 5 | |
| 6 | -compile(export_all). |
| 7 | |
| 8 | t() -> |
| 9 | io:format("Starting.~n", []), |
| 10 | register(tester, self()), |
| 11 | |
| 12 | Pid1 = erlang:spawn(?MODULE, test_start, []), |
| 13 | receive after 200 -> ok end, % Wait for completion. |
| 14 | case is_up(Pid1) of |
| 15 | true -> |
| 16 | io:format("PASS. Unlinked owner still alive.~n"); |
| 17 | false -> |
| 18 | io:format("FAIL. Unlinked owner is dead.~n") |
| 19 | end, |
| 20 | |
| 21 | Pid2 = erlang:spawn(?MODULE, test_linked, []), |
| 22 | receive after 200 -> ok end, % Wait for completion. |
| 23 | case is_up(Pid2) of |
| 24 | true -> |
| 25 | io:format("FAIL. Linked owner still alive.~n"); |
| 26 | false -> |
| 27 | io:format("PASS. Linked owner is dead.~n") |
| 28 | end, |
| 29 | |
David Reiss | 1e1a697 | 2009-06-04 02:01:32 +0000 | [diff] [blame^] | 30 | Pid3 = erlang:spawn(?MODULE, test_tethered, []), |
| 31 | receive after 200 -> ok end, % Wait for completion. |
| 32 | case is_up(Pid3) of |
| 33 | true -> |
| 34 | io:format("PASS. Tethered owner still alive.~n"); |
| 35 | false -> |
| 36 | io:format("FAIL. Tethered owner is dead.~n") |
| 37 | end, |
| 38 | |
| 39 | check_extras(3), |
David Reiss | 5e530af | 2009-06-04 02:01:24 +0000 | [diff] [blame] | 40 | |
| 41 | erlang:halt(). |
| 42 | |
| 43 | is_up(Pid) -> |
| 44 | MonitorRef = erlang:monitor(process, Pid), |
| 45 | receive |
| 46 | {'DOWN', MonitorRef, process, Pid, _Info} -> |
| 47 | false |
| 48 | after |
| 49 | 50 -> |
| 50 | erlang:demonitor(MonitorRef), |
| 51 | true |
| 52 | end. |
| 53 | |
| 54 | check_extras(0) -> ok; |
| 55 | check_extras(N) -> |
| 56 | receive |
| 57 | {client, Type, Pid} -> |
| 58 | case {Type, is_up(Pid)} of |
| 59 | {unlinked, true} -> |
| 60 | io:format("PASS. Unlinked client still alive.~n"); |
| 61 | {unlinked, false} -> |
| 62 | io:format("FAIL. Unlinked client dead.~n"); |
| 63 | {linked, true} -> |
| 64 | io:format("FAIL. Linked client still alive.~n"); |
| 65 | {linked, false} -> |
David Reiss | 1e1a697 | 2009-06-04 02:01:32 +0000 | [diff] [blame^] | 66 | io:format("PASS. Linked client dead.~n"); |
| 67 | {tethered, true} -> |
| 68 | io:format("FAIL. Tethered client still alive.~n"); |
| 69 | {tethered, false} -> |
| 70 | io:format("PASS. Tethered client dead.~n") |
David Reiss | 5e530af | 2009-06-04 02:01:24 +0000 | [diff] [blame] | 71 | end, |
| 72 | check_extras(N-1) |
| 73 | after |
| 74 | 500 -> |
| 75 | io:format("FAIL. Expected ~p more clients.~n", [N]) |
| 76 | end. |
| 77 | |
David Reiss | bb97fd9 | 2009-06-04 02:01:28 +0000 | [diff] [blame] | 78 | make_thrift_client(Opts) -> |
| 79 | thrift_client:start(fun()->ok end, thriftTest_thrift, Opts). |
| 80 | |
David Reiss | 5e530af | 2009-06-04 02:01:24 +0000 | [diff] [blame] | 81 | make_protocol_factory(Port) -> |
| 82 | {ok, TransportFactory} = |
| 83 | thrift_socket_transport:new_transport_factory( |
| 84 | "127.0.0.1", Port, []), |
| 85 | {ok, ProtocolFactory} = |
| 86 | thrift_binary_protocol:new_protocol_factory( |
| 87 | TransportFactory, []), |
| 88 | ProtocolFactory. |
| 89 | |
| 90 | |
| 91 | test_start() -> |
David Reiss | bb97fd9 | 2009-06-04 02:01:28 +0000 | [diff] [blame] | 92 | {ok, Client1} = make_thrift_client([{connect, false}]), |
David Reiss | 5e530af | 2009-06-04 02:01:24 +0000 | [diff] [blame] | 93 | tester ! {client, unlinked, Client1}, |
David Reiss | bb97fd9 | 2009-06-04 02:01:28 +0000 | [diff] [blame] | 94 | {ok, Client2} = make_thrift_client([{connect, false}]), |
David Reiss | 5e530af | 2009-06-04 02:01:24 +0000 | [diff] [blame] | 95 | io:format("PASS. Unlinked clients created.~n"), |
| 96 | try |
| 97 | gen_server:call(Client2, {connect, make_protocol_factory(2)}), |
| 98 | io:format("FAIL. Unlinked client connected.~n", []) |
| 99 | catch |
| 100 | Kind:Info -> |
| 101 | io:format("PASS. Caught unlinked error. ~p:~p~n", [Kind, Info]) |
| 102 | end, |
| 103 | receive after 100 -> |
| 104 | io:format("PASS. Still alive after unlinked death.~n"), |
| 105 | %% Hang around a little longer so our parent can verify. |
| 106 | receive after 200 -> ok end |
| 107 | end, |
| 108 | %% Exit abnormally to not kill our unlinked extra client. |
| 109 | exit(die). |
| 110 | |
| 111 | test_linked() -> |
David Reiss | bb97fd9 | 2009-06-04 02:01:28 +0000 | [diff] [blame] | 112 | {ok, Client1} = make_thrift_client([{connect, false}, {monitor, link}]), |
David Reiss | 5e530af | 2009-06-04 02:01:24 +0000 | [diff] [blame] | 113 | tester ! {client, linked, Client1}, |
David Reiss | bb97fd9 | 2009-06-04 02:01:28 +0000 | [diff] [blame] | 114 | {ok, Client2} = make_thrift_client([{connect, false}, {monitor, link}]), |
David Reiss | 5e530af | 2009-06-04 02:01:24 +0000 | [diff] [blame] | 115 | io:format("PASS. Linked clients created.~n"), |
| 116 | try |
| 117 | gen_server:call(Client2, {connect, make_protocol_factory(2)}), |
| 118 | io:format("FAIL. Linked client connected.~n", []) |
| 119 | catch |
| 120 | Kind:Info -> |
| 121 | io:format("FAIL. Caught linked error. ~p:~p~n", [Kind, Info]) |
| 122 | end, |
| 123 | receive after 100 -> |
| 124 | io:format("FAIL. Still alive after linked death.~n"), |
| 125 | % Hang around a little longer so our parent can verify. |
| 126 | receive after 200 -> ok end |
| 127 | end, |
| 128 | %% Exit abnormally to kill our linked extra client. |
| 129 | %% But we should never get here. |
| 130 | exit(die). |
David Reiss | 1e1a697 | 2009-06-04 02:01:32 +0000 | [diff] [blame^] | 131 | |
| 132 | test_tethered() -> |
| 133 | {ok, Client1} = make_thrift_client([{connect, false}, {monitor, tether}]), |
| 134 | tester ! {client, tethered, Client1}, |
| 135 | {ok, Client2} = make_thrift_client([{connect, false}, {monitor, tether}]), |
| 136 | io:format("PASS. Tethered clients created.~n"), |
| 137 | try |
| 138 | gen_server:call(Client2, {connect, make_protocol_factory(2)}), |
| 139 | io:format("FAIL. Tethered client connected.~n", []) |
| 140 | catch |
| 141 | Kind:Info -> |
| 142 | io:format("PASS. Caught tethered error. ~p:~p~n", [Kind, Info]) |
| 143 | end, |
| 144 | receive after 100 -> |
| 145 | io:format("PASS. Still alive after tethered death.~n"), |
| 146 | % Hang around a little longer so our parent can verify. |
| 147 | receive after 200 -> ok end |
| 148 | end, |
| 149 | %% Exit abnormally to kill our tethered extra client. |
| 150 | exit(die). |