David Reiss | f32d0fb | 2010-08-30 22:05:00 +0000 | [diff] [blame] | 1 | %% |
| 2 | %% Licensed to the Apache Software Foundation (ASF) under one |
| 3 | %% or more contributor license agreements. See the NOTICE file |
| 4 | %% distributed with this work for additional information |
| 5 | %% regarding copyright ownership. The ASF licenses this file |
| 6 | %% to you under the Apache License, Version 2.0 (the |
| 7 | %% "License"); you may not use this file except in compliance |
| 8 | %% with the License. You may obtain a copy of the License at |
| 9 | %% |
| 10 | %% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | %% |
| 12 | %% Unless required by applicable law or agreed to in writing, |
| 13 | %% software distributed under the License is distributed on an |
| 14 | %% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | %% KIND, either express or implied. See the License for the |
| 16 | %% specific language governing permissions and limitations |
| 17 | %% under the License. |
| 18 | %% |
| 19 | %% Tests the behavior of clients in the face of transport errors. |
| 20 | %% Makes sure start, start_linked, and start_tethered work as expected. |
| 21 | |
| 22 | -module(test_tether). |
| 23 | |
| 24 | -compile(export_all). |
| 25 | |
| 26 | |
| 27 | t() -> |
| 28 | io:format("Beginning transport error test.~n"), |
| 29 | Pid1 = erlang:spawn(?MODULE, t_sub, [2]), |
| 30 | wait_for(Pid1), |
| 31 | io:format("Beginning protocol error test.~n"), |
| 32 | Pid2 = erlang:spawn(?MODULE, t_sub, [22]), |
| 33 | wait_for(Pid2), |
| 34 | ok. |
| 35 | |
| 36 | t_sub(Port) -> |
| 37 | io:format("Starting.~n", []), |
| 38 | register(tester, self()), |
| 39 | |
| 40 | Pid1 = erlang:spawn(?MODULE, test_start, [Port]), |
| 41 | receive after 200 -> ok end, % Wait for completion. |
| 42 | case is_up(Pid1) of |
| 43 | true -> |
| 44 | io:format("PASS. Unlinked owner still alive.~n"); |
| 45 | false -> |
| 46 | io:format("FAIL. Unlinked owner is dead.~n") |
| 47 | end, |
| 48 | |
| 49 | Pid2 = erlang:spawn(?MODULE, test_linked, [Port]), |
| 50 | receive after 200 -> ok end, % Wait for completion. |
| 51 | case is_up(Pid2) of |
| 52 | true -> |
| 53 | io:format("FAIL. Linked owner still alive.~n"); |
| 54 | false -> |
| 55 | io:format("PASS. Linked owner is dead.~n") |
| 56 | end, |
| 57 | |
| 58 | Pid3 = erlang:spawn(?MODULE, test_tethered, [Port]), |
| 59 | receive after 200 -> ok end, % Wait for completion. |
| 60 | case is_up(Pid3) of |
| 61 | true -> |
| 62 | io:format("PASS. Tethered owner still alive.~n"); |
| 63 | false -> |
| 64 | io:format("FAIL. Tethered owner is dead.~n") |
| 65 | end, |
| 66 | |
| 67 | check_extras(3). |
| 68 | |
| 69 | is_up(Pid) -> |
| 70 | MonitorRef = erlang:monitor(process, Pid), |
| 71 | receive |
| 72 | {'DOWN', MonitorRef, process, Pid, _Info} -> |
| 73 | false |
| 74 | after |
| 75 | 50 -> |
| 76 | erlang:demonitor(MonitorRef), |
| 77 | true |
| 78 | end. |
| 79 | |
| 80 | wait_for(Pid) -> |
| 81 | MonitorRef = erlang:monitor(process, Pid), |
| 82 | receive |
| 83 | {'DOWN', MonitorRef, process, Pid, _Info} -> |
| 84 | ok |
| 85 | end. |
| 86 | |
| 87 | check_extras(0) -> ok; |
| 88 | check_extras(N) -> |
| 89 | receive |
| 90 | {client, Type, Pid} -> |
| 91 | case {Type, is_up(Pid)} of |
| 92 | {unlinked, true} -> |
| 93 | io:format("PASS. Unlinked client still alive.~n"); |
| 94 | {unlinked, false} -> |
| 95 | io:format("FAIL. Unlinked client dead.~n"); |
| 96 | {linked, true} -> |
| 97 | io:format("FAIL. Linked client still alive.~n"); |
| 98 | {linked, false} -> |
| 99 | io:format("PASS. Linked client dead.~n"); |
| 100 | {tethered, true} -> |
| 101 | io:format("FAIL. Tethered client still alive.~n"); |
| 102 | {tethered, false} -> |
| 103 | io:format("PASS. Tethered client dead.~n") |
| 104 | end, |
| 105 | check_extras(N-1) |
| 106 | after |
| 107 | 500 -> |
| 108 | io:format("FAIL. Expected ~p more clients.~n", [N]) |
| 109 | end. |
| 110 | |
| 111 | make_thrift_client(Opts) -> |
| 112 | thrift_client:start(fun()->ok end, thriftTest_thrift, Opts). |
| 113 | |
| 114 | make_protocol_factory(Port) -> |
| 115 | {ok, TransportFactory} = |
| 116 | thrift_socket_transport:new_transport_factory( |
| 117 | "127.0.0.1", Port, []), |
| 118 | {ok, ProtocolFactory} = |
| 119 | thrift_binary_protocol:new_protocol_factory( |
| 120 | TransportFactory, []), |
| 121 | ProtocolFactory. |
| 122 | |
| 123 | |
| 124 | test_start(Port) -> |
| 125 | {ok, Client1} = make_thrift_client([{connect, false}]), |
| 126 | tester ! {client, unlinked, Client1}, |
| 127 | {ok, Client2} = make_thrift_client([{connect, false}]), |
| 128 | io:format("PASS. Unlinked clients created.~n"), |
| 129 | try |
| 130 | gen_server:call(Client2, {connect, make_protocol_factory(Port)}), |
| 131 | thrift_client:call(Client2, testVoid, []), |
| 132 | io:format("FAIL. Unlinked client connected and called.~n", []) |
| 133 | catch |
| 134 | Kind:Info -> |
| 135 | io:format("PASS. Caught unlinked error. ~p:~p~n", [Kind, Info]) |
| 136 | end, |
| 137 | receive after 100 -> |
| 138 | io:format("PASS. Still alive after unlinked death.~n"), |
| 139 | %% Hang around a little longer so our parent can verify. |
| 140 | receive after 200 -> ok end |
| 141 | end, |
| 142 | %% Exit abnormally to not kill our unlinked extra client. |
| 143 | exit(die). |
| 144 | |
| 145 | test_linked(Port) -> |
| 146 | {ok, Client1} = make_thrift_client([{connect, false}, {monitor, link}]), |
| 147 | tester ! {client, linked, Client1}, |
| 148 | {ok, Client2} = make_thrift_client([{connect, false}, {monitor, link}]), |
| 149 | io:format("PASS. Linked clients created.~n"), |
| 150 | try |
| 151 | gen_server:call(Client2, {connect, make_protocol_factory(Port)}), |
| 152 | thrift_client:call(Client2, testVoid, []), |
| 153 | io:format("FAIL. Linked client connected and called.~n", []) |
| 154 | catch |
| 155 | Kind:Info -> |
| 156 | io:format("FAIL. Caught linked error. ~p:~p~n", [Kind, Info]) |
| 157 | end, |
| 158 | receive after 100 -> |
| 159 | io:format("FAIL. Still alive after linked death.~n"), |
| 160 | % Hang around a little longer so our parent can verify. |
| 161 | receive after 200 -> ok end |
| 162 | end, |
| 163 | %% Exit abnormally to kill our linked extra client. |
| 164 | %% But we should never get here. |
| 165 | exit(die). |
| 166 | |
| 167 | test_tethered(Port) -> |
| 168 | {ok, Client1} = make_thrift_client([{connect, false}, {monitor, tether}]), |
| 169 | tester ! {client, tethered, Client1}, |
| 170 | {ok, Client2} = make_thrift_client([{connect, false}, {monitor, tether}]), |
| 171 | io:format("PASS. Tethered clients created.~n"), |
| 172 | try |
| 173 | gen_server:call(Client2, {connect, make_protocol_factory(Port)}), |
| 174 | thrift_client:call(Client2, testVoid, []), |
| 175 | io:format("FAIL. Tethered client connected and called.~n", []) |
| 176 | catch |
| 177 | Kind:Info -> |
| 178 | io:format("PASS. Caught tethered error. ~p:~p~n", [Kind, Info]) |
| 179 | end, |
| 180 | receive after 100 -> |
| 181 | io:format("PASS. Still alive after tethered death.~n"), |
| 182 | % Hang around a little longer so our parent can verify. |
| 183 | receive after 200 -> ok end |
| 184 | end, |
| 185 | %% Exit abnormally to kill our tethered extra client. |
| 186 | exit(die). |