-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathprotected_resource_test.rb
164 lines (139 loc) · 3.38 KB
/
protected_resource_test.rb
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
require 'test_helper'
class TestProtectedResource < Minitest::Test
include CircuitBreakerHelper
include ResourceHelper
include BackgroundHelper
def setup
Semian.destroy(:testing)
rescue
nil
end
def teardown
destroy_resources
super
end
def test_get_name_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)
refute_nil Semian.resources[:testing].name
end
def test_get_name_without_bulkhead_or_circuit_breaker
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
circuit_breaker: false,
)
assert_nil Semian.resources[:testing].name
end
def test_get_semid_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)
assert_equal(0, Semian.resources[:testing].semid)
end
def test_get_tickets_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)
assert_equal(0, Semian.resources[:testing].tickets)
end
def test_get_count_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)
assert_equal(0, Semian.resources[:testing].count)
end
def test_get_registered_workers_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)
assert_equal(0, Semian.resources[:testing].registered_workers)
end
def test_acquire_without_bulkhead
Semian.register(
:testing,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
bulkhead: false,
)
10.times do
background do
block_called = false
@resource = Semian[:testing]
@resource.acquire { block_called = true }
assert_equal true, block_called
assert_instance_of Semian::CircuitBreaker, @resource.circuit_breaker
assert_nil @resource.bulkhead
end
end
yield_to_background
end
def test_acquire_bulkhead_without_circuit_breaker
Semian.register(
:testing,
tickets: 2,
circuit_breaker: false,
)
acquired = false
@resource = Semian[:testing]
@resource.acquire do
acquired = true
assert_equal 1, @resource.count
assert_equal 2, @resource.tickets
end
assert acquired
assert_nil @resource.circuit_breaker
end
def test_acquire_bulkhead_with_circuit_breaker
Semian.register(
:testing,
tickets: 2,
exceptions: [SomeError],
error_threshold: 2,
error_timeout: 5,
success_threshold: 1,
)
acquired = false
@resource = Semian[:testing]
@resource.acquire do
acquired = true
assert_equal 1, @resource.count
assert_equal 2, @resource.tickets
half_open_cicuit!(@resource)
assert_circuit_closed(@resource)
end
assert acquired
end
end