blob: 3088cbdc1591e084816f4c33e72041aa26a43b1b [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
David Reiss1e1a6972009-06-04 02:01:32 +000030 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 Reiss5e530af2009-06-04 02:01:24 +000040
41 erlang:halt().
42
43is_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
54check_extras(0) -> ok;
55check_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 Reiss1e1a6972009-06-04 02:01:32 +000066 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 Reiss5e530af2009-06-04 02:01:24 +000071 end,
72 check_extras(N-1)
73 after
74 500 ->
75 io:format("FAIL. Expected ~p more clients.~n", [N])
76 end.
77
David Reissbb97fd92009-06-04 02:01:28 +000078make_thrift_client(Opts) ->
79 thrift_client:start(fun()->ok end, thriftTest_thrift, Opts).
80
David Reiss5e530af2009-06-04 02:01:24 +000081make_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
91test_start() ->
David Reissbb97fd92009-06-04 02:01:28 +000092 {ok, Client1} = make_thrift_client([{connect, false}]),
David Reiss5e530af2009-06-04 02:01:24 +000093 tester ! {client, unlinked, Client1},
David Reissbb97fd92009-06-04 02:01:28 +000094 {ok, Client2} = make_thrift_client([{connect, false}]),
David Reiss5e530af2009-06-04 02:01:24 +000095 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
111test_linked() ->
David Reissbb97fd92009-06-04 02:01:28 +0000112 {ok, Client1} = make_thrift_client([{connect, false}, {monitor, link}]),
David Reiss5e530af2009-06-04 02:01:24 +0000113 tester ! {client, linked, Client1},
David Reissbb97fd92009-06-04 02:01:28 +0000114 {ok, Client2} = make_thrift_client([{connect, false}, {monitor, link}]),
David Reiss5e530af2009-06-04 02:01:24 +0000115 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 Reiss1e1a6972009-06-04 02:01:32 +0000131
132test_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).