]> git.proxmox.com Git - ceph.git/blame - ceph/src/jaegertracing/thrift/lib/erl/test/test_thrift_socket_transport.erl
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / erl / test / test_thrift_socket_transport.erl
CommitLineData
f67539c2
TL
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
20-module(test_thrift_socket_transport).
21-include_lib("eunit/include/eunit.hrl").
22
23
24new(Socket) -> thrift_socket_transport:new(Socket).
25new(Socket, Opts) -> thrift_socket_transport:new(Socket, Opts).
26
27new_test_() ->
28 [
29 {"new socket", ?_assertMatch(
30 {ok, {_, thrift_socket_transport, {t_socket, a_fake_socket, 60000, []}}},
31 new(a_fake_socket)
32 )},
33 {"new socket with no options", ?_assertMatch(
34 {ok, {_, thrift_socket_transport, {t_socket, a_fake_socket, 60000, []}}},
35 new(a_fake_socket, [])
36 )},
37 {"new socket with integer timeout", ?_assertMatch(
38 {ok, {_, thrift_socket_transport, {t_socket, a_fake_socket, 5000, []}}},
39 new(a_fake_socket, [{recv_timeout, 5000}])
40 )},
41 {"new socket with infinity timeout", ?_assertMatch(
42 {ok, {_, thrift_socket_transport, {t_socket, a_fake_socket, infinity, []}}},
43 new(a_fake_socket, [{recv_timeout, infinity}])
44 )}
45 ].
46
47
48read(Socket, Bytes) -> thrift_socket_transport:read(Socket, Bytes).
49
50read_test_() ->
51 {setup,
52 fun() ->
53 meck:new(gen_tcp, [unstick, passthrough]),
54 meck:expect(gen_tcp, recv, fun(Bin, 0, _) -> {ok, Bin} end)
55 end,
56 fun(_) -> meck:unload(gen_tcp) end,
57 [
58 {"read zero bytes from empty socket", ?_assertMatch(
59 {_, {ok, <<>>}},
60 read({t_socket, <<>>, 60000, []}, 0)
61 )},
62 {"read 1 byte from empty socket", ?_assertMatch(
63 {_, {ok, <<>>}},
64 read({t_socket, <<>>, 60000, []}, 1)
65 )},
66 {"read zero bytes from nonempty socket", ?_assertMatch(
67 {{t_socket, _, _, _}, {ok, <<>>}},
68 read({t_socket, <<"hallo world">>, 60000, []}, 0)
69 )},
70 {"read 1 byte from nonempty socket", ?_assertMatch(
71 {{t_socket, _, _, <<"allo world">>}, {ok, <<"h">>}},
72 read({t_socket, <<"hallo world">>, 60000, []}, 1)
73 )},
74 {"read a zillion bytes from nonempty socket", ?_assertMatch(
75 {{t_socket, _, _, <<>>}, {ok, <<"hallo world">>}},
76 read({t_socket, <<"hallo world">>, 60000, []}, 65536)
77 )},
78 {"read 1 byte from previously buffered socket", ?_assertMatch(
79 {{t_socket, _, _, <<"allo">>}, {ok, <<"h">>}},
80 read({t_socket, <<" world">>, 60000, <<"hallo">>}, 1)
81 )},
82 {"read 6 byte from previously buffered socket", ?_assertMatch(
83 {{t_socket, _, _, <<"world">>}, {ok, <<"hallo ">>}},
84 read({t_socket, <<" world">>, 60000, <<"hallo">>}, 6)
85 )},
86 {"read a zillion bytes from previously buffered socket", ?_assertMatch(
87 {{t_socket, _, _, <<>>}, {ok, <<"hallo world">>}},
88 read({t_socket, <<" world">>, 60000, <<"hallo">>}, 65536)
89 )}
90 ]
91 }.
92
93
94read_exact(Socket, Bytes) -> thrift_socket_transport:read_exact(Socket, Bytes).
95
96read_exact_test_() ->
97 {setup,
98 fun() ->
99 meck:new(gen_tcp, [unstick, passthrough]),
100 meck:expect(gen_tcp, recv, fun(Bin, N, _) ->
101 case N of
102 0 -> {ok, Bin};
103 1 -> {ok, <<"h">>};
104 N when N > 2 -> {error, timeout}
105 end
106 end),
107 meck:expect(gen_tcp, close, fun(_) -> ok end)
108 end,
109 fun(_) -> meck:unload(gen_tcp) end,
110 [
111 {"read_exact zero bytes from empty socket", ?_assertMatch(
112 {_, {ok, <<>>}},
113 read_exact({t_socket, <<>>, 60000, []}, 0)
114 )},
115 {"read_exact zero bytes from nonempty socket", ?_assertMatch(
116 {{t_socket, _, _, _}, {ok, <<>>}},
117 read_exact({t_socket, <<"hallo world">>, 60000, []}, 0)
118 )},
119 {"read_exact 1 byte from nonempty socket", ?_assertMatch(
120 {{t_socket, _, _, []}, {ok, <<"h">>}},
121 read_exact({t_socket, <<"hallo world">>, 60000, []}, 1)
122 )},
123 {"read_exact a zillion bytes from nonempty socket", ?_assertMatch(
124 {{t_socket, _, _, []}, {error, timeout}},
125 read_exact({t_socket, <<"hallo world">>, 60000, []}, 65536)
126 )},
127 {"read_exact 1 byte from previously buffered socket", ?_assertMatch(
128 {{t_socket, _, _, <<"allo">>}, {ok, <<"h">>}},
129 read_exact({t_socket, <<" world">>, 60000, <<"hallo">>}, 1)
130 )},
131 {"read_exact 6 byte from previously buffered socket", ?_assertMatch(
132 {{t_socket, _, _, []}, {ok, <<"more h">>}},
133 read_exact({t_socket, <<"hallo">>, 60000, <<"more ">>}, 6)
134 )},
135 {"read_exact a zillion bytes from previously buffered socket", ?_assertMatch(
136 {{t_socket, _, _, <<"hallo">>}, {error, timeout}},
137 read_exact({t_socket, <<" world">>, 60000, <<"hallo">>}, 65536)
138 )}
139 ]
140 }.
141
142
143write(Socket, Data) -> thrift_socket_transport:write(Socket, Data).
144
145write_test_() ->
146 {setup,
147 fun() ->
148 meck:new(gen_tcp, [unstick, passthrough]),
149 meck:expect(gen_tcp, send, fun(_, _) -> ok end)
150 end,
151 fun(_) -> meck:unload(gen_tcp) end,
152 [
153 {"write empty list to socket", ?_assertMatch(
154 {{t_socket, a_fake_socket, 60000, []}, ok},
155 write({t_socket, a_fake_socket, 60000, []}, [])
156 )},
157 {"write empty binary to socket", ?_assertMatch(
158 {{t_socket, a_fake_socket, 60000, []}, ok},
159 write({t_socket, a_fake_socket, 60000, []}, <<>>)
160 )},
161 {"write a list to socket", ?_assertMatch(
162 {{t_socket, a_fake_socket, 60000, []}, ok},
163 write({t_socket, a_fake_socket, 60000, []}, "hallo world")
164 )},
165 {"write a binary to socket", ?_assertMatch(
166 {{t_socket, a_fake_socket, 60000, []}, ok},
167 write({t_socket, a_fake_socket, 60000, []}, <<"hallo world">>)
168 )}
169 ]
170 }.
171
172
173flush(Transport) -> thrift_socket_transport:flush(Transport).
174
175flush_test_() ->
176 [
177 {"flush socket", ?_assertMatch(
178 {{t_socket, a_fake_socket, 60000, []}, ok},
179 flush({t_socket, a_fake_socket, 60000, []})
180 )}
181 ].
182
183
184close(Transport) -> thrift_socket_transport:close(Transport).
185
186close_test_() ->
187 {setup,
188 fun() ->
189 meck:new(gen_tcp, [unstick, passthrough]),
190 meck:expect(gen_tcp, close, fun(_) -> ok end)
191 end,
192 fun(_) -> meck:unload(gen_tcp) end,
193 [
194 {"close membuffer", ?_assertMatch(
195 {{t_socket, a_fake_socket, 60000, []}, ok},
196 close({t_socket, a_fake_socket, 60000, []})
197 )}
198 ]
199 }.