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 | |
| 30 | check_extras(2), |
| 31 | |
| 32 | erlang:halt(). |
| 33 | |
| 34 | is_up(Pid) -> |
| 35 | MonitorRef = erlang:monitor(process, Pid), |
| 36 | receive |
| 37 | {'DOWN', MonitorRef, process, Pid, _Info} -> |
| 38 | false |
| 39 | after |
| 40 | 50 -> |
| 41 | erlang:demonitor(MonitorRef), |
| 42 | true |
| 43 | end. |
| 44 | |
| 45 | check_extras(0) -> ok; |
| 46 | check_extras(N) -> |
| 47 | receive |
| 48 | {client, Type, Pid} -> |
| 49 | case {Type, is_up(Pid)} of |
| 50 | {unlinked, true} -> |
| 51 | io:format("PASS. Unlinked client still alive.~n"); |
| 52 | {unlinked, false} -> |
| 53 | io:format("FAIL. Unlinked client dead.~n"); |
| 54 | {linked, true} -> |
| 55 | io:format("FAIL. Linked client still alive.~n"); |
| 56 | {linked, false} -> |
| 57 | io:format("PASS. Linked client dead.~n") |
| 58 | end, |
| 59 | check_extras(N-1) |
| 60 | after |
| 61 | 500 -> |
| 62 | io:format("FAIL. Expected ~p more clients.~n", [N]) |
| 63 | end. |
| 64 | |
| 65 | make_protocol_factory(Port) -> |
| 66 | {ok, TransportFactory} = |
| 67 | thrift_socket_transport:new_transport_factory( |
| 68 | "127.0.0.1", Port, []), |
| 69 | {ok, ProtocolFactory} = |
| 70 | thrift_binary_protocol:new_protocol_factory( |
| 71 | TransportFactory, []), |
| 72 | ProtocolFactory. |
| 73 | |
| 74 | |
| 75 | test_start() -> |
| 76 | {ok, Client1} = gen_server:start(thrift_client, [thriftTest_thrift], []), |
| 77 | tester ! {client, unlinked, Client1}, |
| 78 | {ok, Client2} = gen_server:start(thrift_client, [thriftTest_thrift], []), |
| 79 | io:format("PASS. Unlinked clients created.~n"), |
| 80 | try |
| 81 | gen_server:call(Client2, {connect, make_protocol_factory(2)}), |
| 82 | io:format("FAIL. Unlinked client connected.~n", []) |
| 83 | catch |
| 84 | Kind:Info -> |
| 85 | io:format("PASS. Caught unlinked error. ~p:~p~n", [Kind, Info]) |
| 86 | end, |
| 87 | receive after 100 -> |
| 88 | io:format("PASS. Still alive after unlinked death.~n"), |
| 89 | %% Hang around a little longer so our parent can verify. |
| 90 | receive after 200 -> ok end |
| 91 | end, |
| 92 | %% Exit abnormally to not kill our unlinked extra client. |
| 93 | exit(die). |
| 94 | |
| 95 | test_linked() -> |
| 96 | {ok, Client1} = gen_server:start_link(thrift_client, [thriftTest_thrift], []), |
| 97 | tester ! {client, linked, Client1}, |
| 98 | {ok, Client2} = gen_server:start_link(thrift_client, [thriftTest_thrift], []), |
| 99 | io:format("PASS. Linked clients created.~n"), |
| 100 | try |
| 101 | gen_server:call(Client2, {connect, make_protocol_factory(2)}), |
| 102 | io:format("FAIL. Linked client connected.~n", []) |
| 103 | catch |
| 104 | Kind:Info -> |
| 105 | io:format("FAIL. Caught linked error. ~p:~p~n", [Kind, Info]) |
| 106 | end, |
| 107 | receive after 100 -> |
| 108 | io:format("FAIL. Still alive after linked death.~n"), |
| 109 | % Hang around a little longer so our parent can verify. |
| 110 | receive after 200 -> ok end |
| 111 | end, |
| 112 | %% Exit abnormally to kill our linked extra client. |
| 113 | %% But we should never get here. |
| 114 | exit(die). |