blob: 35794e6352f1eb8b76c1759c03821338cdd810cb [file] [log] [blame]
David Reiss5e530af2009-06-04 02:01:24 +00001%% 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
8t() ->
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
34is_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
45check_extras(0) -> ok;
46check_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
David Reissbb97fd92009-06-04 02:01:28 +000065make_thrift_client(Opts) ->
66 thrift_client:start(fun()->ok end, thriftTest_thrift, Opts).
67
David Reiss5e530af2009-06-04 02:01:24 +000068make_protocol_factory(Port) ->
69 {ok, TransportFactory} =
70 thrift_socket_transport:new_transport_factory(
71 "127.0.0.1", Port, []),
72 {ok, ProtocolFactory} =
73 thrift_binary_protocol:new_protocol_factory(
74 TransportFactory, []),
75 ProtocolFactory.
76
77
78test_start() ->
David Reissbb97fd92009-06-04 02:01:28 +000079 {ok, Client1} = make_thrift_client([{connect, false}]),
David Reiss5e530af2009-06-04 02:01:24 +000080 tester ! {client, unlinked, Client1},
David Reissbb97fd92009-06-04 02:01:28 +000081 {ok, Client2} = make_thrift_client([{connect, false}]),
David Reiss5e530af2009-06-04 02:01:24 +000082 io:format("PASS. Unlinked clients created.~n"),
83 try
84 gen_server:call(Client2, {connect, make_protocol_factory(2)}),
85 io:format("FAIL. Unlinked client connected.~n", [])
86 catch
87 Kind:Info ->
88 io:format("PASS. Caught unlinked error. ~p:~p~n", [Kind, Info])
89 end,
90 receive after 100 ->
91 io:format("PASS. Still alive after unlinked death.~n"),
92 %% Hang around a little longer so our parent can verify.
93 receive after 200 -> ok end
94 end,
95 %% Exit abnormally to not kill our unlinked extra client.
96 exit(die).
97
98test_linked() ->
David Reissbb97fd92009-06-04 02:01:28 +000099 {ok, Client1} = make_thrift_client([{connect, false}, {monitor, link}]),
David Reiss5e530af2009-06-04 02:01:24 +0000100 tester ! {client, linked, Client1},
David Reissbb97fd92009-06-04 02:01:28 +0000101 {ok, Client2} = make_thrift_client([{connect, false}, {monitor, link}]),
David Reiss5e530af2009-06-04 02:01:24 +0000102 io:format("PASS. Linked clients created.~n"),
103 try
104 gen_server:call(Client2, {connect, make_protocol_factory(2)}),
105 io:format("FAIL. Linked client connected.~n", [])
106 catch
107 Kind:Info ->
108 io:format("FAIL. Caught linked error. ~p:~p~n", [Kind, Info])
109 end,
110 receive after 100 ->
111 io:format("FAIL. Still alive after linked death.~n"),
112 % Hang around a little longer so our parent can verify.
113 receive after 200 -> ok end
114 end,
115 %% Exit abnormally to kill our linked extra client.
116 %% But we should never get here.
117 exit(die).