summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/thrift/lib/erl/test/test_thrift_file_transport.erl
blob: 3e5c1d1e46c96502afc0d471b624cae1b690ed48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
%%
%% Licensed to the Apache Software Foundation (ASF) under one
%% or more contributor license agreements. See the NOTICE file
%% distributed with this work for additional information
%% regarding copyright ownership. The ASF licenses this file
%% to you under the Apache License, Version 2.0 (the
%% "License"); you may not use this file except in compliance
%% with the License. You may obtain a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%

-module(test_thrift_file_transport).
-include_lib("eunit/include/eunit.hrl").


new(File) -> thrift_file_transport:new(File).
new(File, Opts) -> thrift_file_transport:new(File, Opts).

new_test_() ->
  [
    {"new file", ?_assertMatch(
      {ok, {_, thrift_file_transport, {t_file, a_fake_file, true, write}}},
      new(a_fake_file)
    )},
    {"new file in read mode", ?_assertMatch(
      {ok, {_, thrift_file_transport, {t_file, a_fake_file, true, read}}},
      new(a_fake_file, [{mode, read}])
    )},
    {"new file in write mode", ?_assertMatch(
      {ok, {_, thrift_file_transport, {t_file, a_fake_file, true, write}}},
      new(a_fake_file, [{mode, write}])
    )},
    {"new file in should_close true mode", ?_assertMatch(
      {ok, {_, thrift_file_transport, {t_file, a_fake_file, true, write}}},
      new(a_fake_file, [{should_close, true}])
    )},
    {"new file in should_close false mode", ?_assertMatch(
      {ok, {_, thrift_file_transport, {t_file, a_fake_file, false, write}}},
      new(a_fake_file, [{should_close, false}])
    )}
  ].


read(File, Bytes) -> thrift_file_transport:read(File, Bytes).

read_test_() ->
  {setup,
    fun() ->
      meck:new(file, [unstick, passthrough]),
      meck:expect(file, read, fun(Bin, N) ->
        {Result, _} = split_binary(Bin, min(iolist_size(Bin), N)),
        {ok, Result}
      end)
    end,
    fun(_) -> meck:unload(file) end,
    [
      {"read zero bytes from empty file", ?_assertMatch(
        {_, {ok, <<>>}},
        read({t_file, <<>>, true, read}, 0)
      )},
      {"read 1 byte from empty file", ?_assertMatch(
        {_, {ok, <<>>}},
        read({t_file, <<>>, true, read}, 1)
      )},
      {"read zero bytes from nonempty file", ?_assertMatch(
        {_, {ok, <<>>}},
        read({t_file, <<"hallo world">>, true, read}, 0)
      )},
      {"read 1 byte from nonempty file", ?_assertMatch(
        {_, {ok, <<"h">>}},
        read({t_file, <<"hallo world">>, true, read}, 1)
      )},
      {"read a zillion bytes from nonempty file", ?_assertMatch(
        {_, {ok, <<"hallo world">>}},
        read({t_file, <<"hallo world">>, true, read}, 65536)
      )},
      {"read 0 byte from file in write mode", ?_assertMatch(
        {_, {error, write_mode}},
        read({t_file, <<>>, true, write}, 0)
      )},
      {"read 1 byte from file in write mode", ?_assertMatch(
        {_, {error, write_mode}},
        read({t_file, <<>>, true, write}, 1)
      )}
    ]
  }.


read_exact(File, Bytes) -> thrift_file_transport:read_exact(File, Bytes).

read_exact_test_() ->
  {setup,
    fun() ->
      meck:new(file, [unstick, passthrough]),
      meck:expect(file, read, fun(Bin, N) ->
        {Result, _} = split_binary(Bin, min(iolist_size(Bin), N)),
        {ok, Result}
      end)
    end,
    fun(_) -> meck:unload(file) end,
    [
      {"read exactly zero bytes from empty file", ?_assertMatch(
        {_, {ok, <<>>}},
        read_exact({t_file, <<>>, true, read}, 0)
      )},
      {"read exactly 1 byte from empty file", ?_assertMatch(
        {_, {error, eof}},
        read_exact({t_file, <<>>, true, read}, 1)
      )},
      {"read exactly zero bytes from nonempty file", ?_assertMatch(
        {_, {ok, <<>>}},
        read_exact({t_file, <<"hallo world">>, true, read}, 0)
      )},
      {"read exactly 1 byte from nonempty file", ?_assertMatch(
        {_, {ok, <<"h">>}},
        read_exact({t_file, <<"hallo world">>, true, read}, 1)
      )},
      {"read exactly a zillion bytes from nonempty file", ?_assertMatch(
        {_, {error, eof}},
        read_exact({t_file, <<"hallo world">>, true, read}, 65536)
      )},
      {"read exactly 0 byte from file in write mode", ?_assertMatch(
        {_, {error, write_mode}},
        read_exact({t_file, <<>>, true, write}, 0)
      )},
      {"read exactly 1 byte from file in write mode", ?_assertMatch(
        {_, {error, write_mode}},
        read_exact({t_file, <<>>, true, write}, 1)
      )}
    ]
  }.


write(File, Data) -> thrift_file_transport:write(File, Data).

write_test_() ->
  {setup,
    fun() ->
      meck:new(file, [unstick, passthrough]),
      meck:expect(file, write, fun(_, _) -> ok end)
    end,
    fun(_) -> meck:unload(file) end,
    [
      {"write empty list to file", ?_assertMatch(
        {{t_file, a_fake_file, true, write}, ok},
        write({t_file, a_fake_file, true, write}, [])
      )},
      {"write empty binary to file", ?_assertMatch(
        {{t_file, a_fake_file, true, write}, ok},
        write({t_file, a_fake_file, true, write}, <<>>)
      )},
      {"write a list to file", ?_assertMatch(
        {{t_file, a_fake_file, true, write}, ok},
        write({t_file, a_fake_file, true, write}, "hallo world")
      )},
      {"write a binary to file", ?_assertMatch(
        {{t_file, a_fake_file, true, write}, ok},
        write({t_file, a_fake_file, true, write}, <<"hallo world">>)
      )},
      {"write a binary to file in read mode", ?_assertMatch(
        {_, {error, read_mode}},
        write({t_file, a_fake_file, true, read}, <<"hallo world">>)
      )},
      {"write a list to file in read mode", ?_assertMatch(
        {_, {error, read_mode}},
        write({t_file, a_fake_file, true, read}, "hallo world")
      )}
    ]
  }.


flush(Transport) -> thrift_file_transport:flush(Transport).

flush_test_() ->
  {setup,
    fun() ->
      meck:new(file, [unstick, passthrough]),
      meck:expect(file, sync, fun(_File) -> ok end)
    end,
    fun(_) -> meck:unload(file) end,
    [
      {"flush file", ?_assertMatch(
        {{t_file, a_fake_file, true, write}, ok},
        flush({t_file, a_fake_file, true, write})
      )}
    ]
  }.


close(Transport) -> thrift_file_transport:close(Transport).

close_test_() ->
  {setup,
    fun() ->
      meck:new(file, [unstick, passthrough]),
      meck:expect(file, close, fun(_) -> ok end)
    end,
    fun(_) -> meck:unload(file) end,
    [
      {"close file", ?_assertMatch(
        {{t_file, a_fake_file, true, write}, ok},
        close({t_file, a_fake_file, true, write})
      )}
    ]
  }.