-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcircuit_breaker_tests.erl
202 lines (178 loc) · 6.46 KB
/
circuit_breaker_tests.erl
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Copyright 2012 Klarna AB
%%%
%%% Licensed 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.
%%
%%% @doc Circuit breaker tests
%%% @copyright 2012 Klarna AB
%%% @end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%_* Module declaration ===============================================
-module(circuit_breaker_tests).
%%%_* Includes =========================================================
-include_lib("eunit/include/eunit.hrl").
-include("circuit_breaker.hrl").
%%%_* Defines ==========================================================
-define(SERVICE, service).
-define(APP, circuit_breaker).
%%%_* Tests ============================================================
circuit_breaker_test_() ->
{"Tests the circuit breaker functionality",
{ foreach
, fun start/0 % setup
, fun stop/1 % teardown
, [ fun undefined/1
, fun call_timeout_block/1
, fun timeout_block/1
, fun error_block/1
, fun manual_block/1
, fun manual_deblock/1
, fun manual_clear/1
, fun returns/1
, fun reset/1
, fun ignore_errors/1
, fun info/1
, fun info_to_text/1
]}}.
start() ->
case application:start(?APP) of
ok ->
ok = application:set_env(?APP, event_handler, circuit_breaker_event_test);
{error, {already_started, _}} ->
circuit_breaker:clear(?SERVICE),
already_started
end.
stop(already_started) -> ok;
stop(_) -> application:stop(?APP).
undefined(_Setup) ->
[ ?_assertEqual({error, undefined}, circuit_breaker:block(?SERVICE))
, ?_assertEqual({error, undefined}, circuit_breaker:deblock(?SERVICE))
, ?_assertEqual({error, undefined}, circuit_breaker:clear(?SERVICE))
, ?_assertEqual(false, circuit_breaker:disabled(?SERVICE))
, ?_assertEqual(false, circuit_breaker:blocked(?SERVICE))
].
call_timeout_block(_Setup) ->
call(),
{_, 5} = loop_call(fun() -> timer:sleep(200) end),
[ ?_assertEqual(false, circuit_breaker:blocked(?SERVICE))
, ?_assert(circuit_breaker:disabled(?SERVICE))
, ?_assertEqual({error, {circuit_breaker, ?CIRCUIT_BREAKER_CALL_TIMEOUT}},
call())
].
timeout_block(_Setup) ->
call(),
{_, 5} = loop_call(fun() -> {error, timeout} end),
[ ?_assertEqual(false, circuit_breaker:blocked(?SERVICE))
, ?_assert(circuit_breaker:disabled(?SERVICE))
, ?_assertEqual({error, {circuit_breaker, ?CIRCUIT_BREAKER_TIMEOUT}},
call())
].
error_block(_Setup) ->
call(),
{_, 5} = loop_call(fun() -> {error, crash} end),
[ ?_assertEqual(false, circuit_breaker:blocked(?SERVICE))
, ?_assert(circuit_breaker:disabled(?SERVICE))
, ?_assertEqual({error, {circuit_breaker, ?CIRCUIT_BREAKER_ERROR}},
call())
].
manual_block(_Setup) ->
call(),
circuit_breaker:block(?SERVICE),
[ ?_assert(circuit_breaker:blocked(?SERVICE))
, ?_assert(circuit_breaker:disabled(?SERVICE))
, ?_assertEqual({error, {circuit_breaker, ?CIRCUIT_BREAKER_BLOCKED}}, call())
].
manual_deblock(_Setup) ->
call(),
circuit_breaker:block(?SERVICE),
circuit_breaker:deblock(?SERVICE),
[ ?_assertEqual(false, circuit_breaker:blocked(?SERVICE))
, ?_assertEqual(false, circuit_breaker:disabled(?SERVICE))
, ?_assert(call())
].
manual_clear(_Setup) ->
call(),
{_, 5} = loop_call(fun() -> {error, crash} end),
circuit_breaker:clear(?SERVICE),
[ ?_assertEqual(false, circuit_breaker:blocked(?SERVICE))
, ?_assertEqual(false, circuit_breaker:disabled(?SERVICE))
, ?_assert(call())
].
returns(_Setup) ->
[ ?_assert(call())
, ?_assertEqual({error, timeout}, call(fun() -> timer:sleep(200) end))
, ?_assertEqual({error, timeout}, call(fun() -> {error, timeout} end))
, ?_assertEqual({error, some_error}, call(fun() -> {error, some_error} end))
, ?_assertException(exit, reason, call(fun() -> exit(reason) end))
, ?_assertException(error, reason, call(fun() -> error(reason) end))
, ?_assertException(throw, reason, call(fun() -> throw(reason) end))
].
reset(_Setup) ->
{_, 5} = loop_call(fun() -> {error, timeout} end),
[ ?_assert(retry(fun() ->
false =:= circuit_breaker:disabled(?SERVICE)
end, 1000, 10))
].
retry(F, T, N) when N > 0 ->
case F() of
Res when Res =:= false
; Res =:= error
; element(1, Res) =:= error ->
timer:sleep(T),
retry(F, T, N-1);
Res ->
Res
end;
retry(_F, _T, 0) -> false.
ignore_errors(_Setup) ->
call(),
ok = loop_call(fun() -> {error, some_error} end),
[ ?_assertEqual(false, circuit_breaker:disabled(?SERVICE))
].
info(_Setup) ->
call(),
[ ?_assertEqual(ok, circuit_breaker:info())
].
info_to_text(_Setup) ->
call(),
Result = iolist_to_binary(circuit_breaker:info_to_text()),
Expected =
<<"Service Status Error Timeout CallTime\n"
, "------------------------------- --------------- -------- -------- --------\n"
, "service OK 0 0 0 \n"
>>,
[ ?_assertEqual(Expected, Result)
].
%%%_* Helpers ==========================================================
call() -> call(fun() -> true end).
%% Use custom values to test that these are used
call(F) ->
circuit_breaker:call( ?SERVICE, F, 100, fun() -> true end, 2000
, [ {n_error, 5}
, {n_timeout, 5}
, {n_call_timeout, 5}
, {ignore_errors, [some_error]}
]
).
loop_call(F) -> loop_call(F, 0).
loop_call(_F, 20) -> ok;
loop_call(F, N) ->
case call(F) of
{error, {circuit_breaker, _}} = Error -> {Error, N};
_ -> loop_call(F, N + 1)
end.
%%%_* Emacs ============================================================
%%% Local Variables:
%%% allout-layout: t
%%% erlang-indent-level: 2
%%% End: