]>
Commit | Line | Data |
---|---|---|
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_framed_transport). | |
21 | -include_lib("eunit/include/eunit.hrl"). | |
22 | ||
23 | ||
24 | new(Transport) -> thrift_framed_transport:new(Transport). | |
25 | ||
26 | new_test_() -> | |
27 | [ | |
28 | {"new framed membuffer", ?_assertMatch( | |
29 | {ok, {t_transport, thrift_framed_transport, {t_framed, | |
30 | {t_transport, thrift_membuffer_transport, {t_membuffer, []}}, | |
31 | [], | |
32 | [] | |
33 | }}}, | |
34 | new({t_transport, thrift_membuffer_transport, {t_membuffer, []}}) | |
35 | )} | |
36 | ]. | |
37 | ||
38 | ||
39 | read(Frame, Bytes) -> thrift_framed_transport:read(Frame, Bytes). | |
40 | ||
41 | read_test_() -> | |
42 | [ | |
43 | {"read zero bytes from an empty framed membuffer", ?_assertMatch( | |
44 | { | |
45 | {t_framed, | |
46 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
47 | [], | |
48 | [] | |
49 | }, | |
50 | {ok, <<>>} | |
51 | }, | |
52 | read( | |
53 | {t_framed, | |
54 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
55 | [], | |
56 | [] | |
57 | }, | |
58 | 0 | |
59 | ) | |
60 | )}, | |
61 | {"read 1 byte from an empty framed membuffer", ?_assertMatch( | |
62 | {_, {error, eof}}, | |
63 | read( | |
64 | {t_framed, | |
65 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
66 | [], | |
67 | [] | |
68 | }, | |
69 | 1 | |
70 | ) | |
71 | )}, | |
72 | {"read zero bytes from nonempty framed membuffer", ?_assertMatch( | |
73 | { | |
74 | {t_framed, | |
75 | {t_transport, thrift_membuffer_transport, {t_membuffer, | |
76 | <<0, 0, 0, 11, "hallo world">> | |
77 | }}, | |
78 | [], | |
79 | [] | |
80 | }, | |
81 | {ok, <<>>} | |
82 | }, | |
83 | read( | |
84 | {t_framed, | |
85 | {t_transport, thrift_membuffer_transport, {t_membuffer, | |
86 | <<0, 0, 0, 11, "hallo world">> | |
87 | }}, | |
88 | [], | |
89 | [] | |
90 | }, | |
91 | 0 | |
92 | ) | |
93 | )}, | |
94 | {"read 1 byte from nonempty framed membuffer", ?_assertMatch( | |
95 | { | |
96 | {t_framed, | |
97 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
98 | <<"allo world">>, | |
99 | [] | |
100 | }, | |
101 | {ok, <<"h">>} | |
102 | }, | |
103 | read( | |
104 | {t_framed, | |
105 | {t_transport, thrift_membuffer_transport, {t_membuffer, | |
106 | <<0, 0, 0, 11, "hallo world">> | |
107 | }}, | |
108 | [], | |
109 | [] | |
110 | }, | |
111 | 1 | |
112 | ) | |
113 | )}, | |
114 | {"read 1 byte from nonempty buffer", ?_assertMatch( | |
115 | { | |
116 | {t_framed, | |
117 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
118 | <<"allo world">>, | |
119 | [] | |
120 | }, | |
121 | {ok, <<"h">>} | |
122 | }, | |
123 | read( | |
124 | {t_framed, | |
125 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
126 | <<"hallo world">>, | |
127 | [] | |
128 | }, | |
129 | 1 | |
130 | ) | |
131 | )}, | |
132 | {"read a zillion bytes from nonempty framed membuffer", ?_assertMatch( | |
133 | { | |
134 | {t_framed, | |
135 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
136 | <<>>, | |
137 | [] | |
138 | }, | |
139 | {ok, <<"hallo world">>} | |
140 | }, | |
141 | read( | |
142 | {t_framed, | |
143 | {t_transport, thrift_membuffer_transport, {t_membuffer, | |
144 | <<0, 0, 0, 11, "hallo world">> | |
145 | }}, | |
146 | [], | |
147 | [] | |
148 | }, | |
149 | 65536 | |
150 | ) | |
151 | )} | |
152 | ]. | |
153 | ||
154 | ||
155 | read_exact(Frame, Bytes) -> thrift_framed_transport:read_exact(Frame, Bytes). | |
156 | ||
157 | read_exact_test_() -> | |
158 | [ | |
159 | {"read exactly zero bytes from an empty framed membuffer", ?_assertMatch( | |
160 | { | |
161 | {t_framed, | |
162 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
163 | <<>>, | |
164 | [] | |
165 | }, | |
166 | {ok, <<>>} | |
167 | }, | |
168 | read_exact( | |
169 | {t_framed, | |
170 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
171 | [], | |
172 | [] | |
173 | }, | |
174 | 0 | |
175 | ) | |
176 | )}, | |
177 | {"read exactly 1 byte from an empty framed membuffer", ?_assertMatch( | |
178 | {_, {error, eof}}, | |
179 | read_exact( | |
180 | {t_framed, | |
181 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
182 | [], | |
183 | [] | |
184 | }, | |
185 | 1 | |
186 | ) | |
187 | )}, | |
188 | {"read exactly zero bytes from nonempty framed membuffer", ?_assertMatch( | |
189 | { | |
190 | {t_framed, | |
191 | {t_transport, thrift_membuffer_transport, {t_membuffer, | |
192 | <<0, 0, 0, 11, "hallo world">> | |
193 | }}, | |
194 | <<>>, | |
195 | [] | |
196 | }, | |
197 | {ok, <<>>} | |
198 | }, | |
199 | read_exact( | |
200 | {t_framed, | |
201 | {t_transport, thrift_membuffer_transport, {t_membuffer, | |
202 | <<0, 0, 0, 11, "hallo world">> | |
203 | }}, | |
204 | [], | |
205 | [] | |
206 | }, | |
207 | 0 | |
208 | ) | |
209 | )}, | |
210 | {"read exactly 1 byte from nonempty framed membuffer", ?_assertMatch( | |
211 | { | |
212 | {t_framed, | |
213 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
214 | <<"allo world">>, | |
215 | [] | |
216 | }, | |
217 | {ok, <<"h">>} | |
218 | }, | |
219 | read_exact( | |
220 | {t_framed, | |
221 | {t_transport, thrift_membuffer_transport, {t_membuffer, | |
222 | <<0, 0, 0, 11, "hallo world">> | |
223 | }}, | |
224 | [], | |
225 | [] | |
226 | }, | |
227 | 1 | |
228 | ) | |
229 | )}, | |
230 | {"read exactly 1 byte from nonempty buffer", ?_assertMatch( | |
231 | { | |
232 | {t_framed, | |
233 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
234 | <<"allo world">>, | |
235 | [] | |
236 | }, | |
237 | {ok, <<"h">>} | |
238 | }, | |
239 | read_exact( | |
240 | {t_framed, | |
241 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
242 | <<"hallo world">>, | |
243 | [] | |
244 | }, | |
245 | 1 | |
246 | ) | |
247 | )}, | |
248 | {"read exactly a zillion bytes from nonempty framed membuffer", ?_assertMatch( | |
249 | { | |
250 | {t_framed, | |
251 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
252 | [[],<<"hallo world">>], | |
253 | [] | |
254 | }, | |
255 | {error, eof} | |
256 | }, | |
257 | read_exact( | |
258 | {t_framed, | |
259 | {t_transport, thrift_membuffer_transport, {t_membuffer, | |
260 | <<0, 0, 0, 11, "hallo world">> | |
261 | }}, | |
262 | [], | |
263 | [] | |
264 | }, | |
265 | 65536 | |
266 | ) | |
267 | )} | |
268 | ]. | |
269 | ||
270 | ||
271 | write(Framed, Data) -> thrift_framed_transport:write(Framed, Data). | |
272 | ||
273 | write_test_() -> | |
274 | [ | |
275 | {"write empty list to empty framed membuffer", ?_assertMatch( | |
276 | { | |
277 | {t_framed, | |
278 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
279 | [], | |
280 | [[], []] | |
281 | }, | |
282 | ok | |
283 | }, | |
284 | write( | |
285 | {t_framed, | |
286 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
287 | [], | |
288 | [] | |
289 | }, | |
290 | [] | |
291 | ) | |
292 | )}, | |
293 | {"write empty list to nonempty framed membuffer", ?_assertMatch( | |
294 | { | |
295 | {t_framed, | |
296 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
297 | [], | |
298 | [["hallo world"], []] | |
299 | }, | |
300 | ok | |
301 | }, | |
302 | write( | |
303 | {t_framed, | |
304 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
305 | [], | |
306 | ["hallo world"] | |
307 | }, | |
308 | [] | |
309 | ) | |
310 | )}, | |
311 | {"write empty binary to empty framed membuffer", ?_assertMatch( | |
312 | { | |
313 | {t_framed, | |
314 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
315 | [], | |
316 | [[], <<>>] | |
317 | }, | |
318 | ok | |
319 | }, | |
320 | write( | |
321 | {t_framed, | |
322 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
323 | [], | |
324 | [] | |
325 | }, | |
326 | <<>> | |
327 | ) | |
328 | )}, | |
329 | {"write empty binary to nonempty framed membuffer", ?_assertMatch( | |
330 | { | |
331 | {t_framed, | |
332 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
333 | [], | |
334 | [["hallo world"], <<>>] | |
335 | }, | |
336 | ok | |
337 | }, | |
338 | write( | |
339 | {t_framed, | |
340 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
341 | [], | |
342 | ["hallo world"] | |
343 | }, | |
344 | <<>> | |
345 | ) | |
346 | )} | |
347 | ]. | |
348 | ||
349 | ||
350 | flush(Transport) -> thrift_framed_transport:flush(Transport). | |
351 | ||
352 | flush_test_() -> | |
353 | [ | |
354 | {"flush empty framed membuffer", ?_assertMatch( | |
355 | {{t_framed, | |
356 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
357 | [], | |
358 | [] | |
359 | }, | |
360 | ok | |
361 | }, | |
362 | flush({t_framed, | |
363 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
364 | [], | |
365 | [] | |
366 | }) | |
367 | )}, | |
368 | {"flush nonempty framed membuffer", ?_assertMatch( | |
369 | {{t_framed, | |
370 | {t_transport, thrift_membuffer_transport, {t_membuffer, | |
371 | [<<>>, [<<0, 0, 0, 11>>, <<"hallo world">>]] | |
372 | }}, | |
373 | [], | |
374 | [] | |
375 | }, | |
376 | ok | |
377 | }, | |
378 | flush({t_framed, | |
379 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
380 | [], | |
381 | <<"hallo world">> | |
382 | }) | |
383 | )} | |
384 | ]. | |
385 | ||
386 | ||
387 | close(Transport) -> thrift_framed_transport:close(Transport). | |
388 | ||
389 | close_test_() -> | |
390 | {"close framed membuffer", ?_assertMatch( | |
391 | {{t_framed, | |
392 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
393 | [], | |
394 | [] | |
395 | }, | |
396 | ok | |
397 | }, | |
398 | close({t_framed, | |
399 | {t_transport, thrift_membuffer_transport, {t_membuffer, <<>>}}, | |
400 | [], | |
401 | [] | |
402 | }) | |
403 | )}. | |
404 |