-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommand_spec.rb
More file actions
214 lines (171 loc) · 4.41 KB
/
command_spec.rb
File metadata and controls
214 lines (171 loc) · 4.41 KB
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
214
require 'spec_helper'
describe Hystrix::Command do
class CommandHelloWorld < Hystrix::Command
attr_accessor :string, :wait, :fail
def initialize(string, wait = 0, fail = false)
self.string = string
self.fail = fail
self.wait = wait
super
end
def run
sleep wait
if fail
raise 'error'
else
return self.string
end
end
def fallback(error)
return 'it failed'
end
end
context 'with a circuit, ' do
before do
@cmd = CommandHelloWorld.new 'circuit string'
@circuit_mock = double('Hystrix::Circuit')
@cmd.wrapped_object.stub(:circuit).and_return(@circuit_mock)
end
context 'when the circuit is closed, ' do
before do
@circuit_mock.stub(:is_closed?).and_return(true)
end
it 'allows commands to succeed' do
@cmd.execute.should == 'circuit string'
end
end
context 'when the circuit is open, ' do
before do
@circuit_mock.stub(:is_closed?).and_return(false)
end
it 'triggers fallback responses' do
@cmd.execute.should == 'it failed'
end
it 'does not attempt to run the command' do
@cmd.wrapped_object.should_not_receive(:run)
end
end
end
context 'notifies callbacks,' do
before do
end
after do
Hystrix::Configuration.reset
end
it 'on success' do
test_name = nil
test_duration = nil
Hystrix.configure do
on_success do |params|
test_name = params[:command_name]
test_duration = params[:duration]
end
end
CommandHelloWorld.new('keith').execute
test_name.should == 'CommandHelloWorld'
test_duration.should > 0
end
it 'on fallback' do
test_name = nil
test_duration = nil
test_error = nil
Hystrix.configure do
on_fallback do |params|
test_name = params[:command_name]
test_duration = params[:duration]
test_error = params[:error]
end
end
CommandHelloWorld.new('keith', 0, true).execute
test_name.should == 'CommandHelloWorld'
test_duration.should > 0
test_error.message.should == 'error'
end
it 'on failure' do
test_name = nil
test_duration = nil
test_error = nil
class NoFallbackCommand < Hystrix::Command
def run
raise 'fail'
end
end
Hystrix.configure do
on_failure do |params|
test_name = params[:command_name]
test_duration = params[:duration]
test_error = params[:error]
end
end
expect {
NoFallbackCommand.new.execute
}.to raise_error
test_name.should == "NoFallbackCommand"
test_duration.should > 0
test_error.message.should == 'fail'
end
end
it 'allows commands to define their pool size' do
class SizedPoolCommand < Hystrix::Command
pool_size 3
end
cmd = SizedPoolCommand.new
cmd.executor_pool.size.should == 3
end
context '.execute' do
it 'supports sychronous execution' do
CommandHelloWorld.new('keith').execute.should == 'keith'
end
it 'returns fallback value on error' do
CommandHelloWorld.new('keith', 0, true).execute.should == 'it failed'
end
it 'sends exception to fallback method on error' do
c = CommandHelloWorld.new('keith', 0, true)
c.wrapped_object.should_receive(:fallback) do |error|
error.message.should == 'error'
end
c.execute
end
end
context '.queue' do
it 'supports asynchronous execution' do
CommandHelloWorld.new('keith').queue.value.should == 'keith'
end
it 'returns fallback value on error' do
CommandHelloWorld.new('keith', 0, true).queue.value.should == 'it failed'
end
end
it 'can execute only once' do
c = CommandHelloWorld.new('keith')
c.execute.should == 'keith'
expect { c.execute }.to raise_error
expect { c.queue.value }.to raise_error
end
it 'raises the original exception if no fallback is defined' do
class CommandWithNoFallback < Hystrix::Command
def run
abort 'the error'
end
end
expect { CommandWithNoFallback.new.execute }.to raise_error('the error')
end
it 'executes the fallback if it unable to grab an executor to run the command' do
pool = Hystrix::CommandExecutorPool.new('my pool', 1)
c1 = CommandHelloWorld.new('foo', 1)
c1.executor_pool = pool
c2 = CommandHelloWorld.new('bar')
c2.executor_pool = pool
future = c1.queue
c2.execute.should == 'it failed'
future.value.should == 'foo'
end
it 'throws an error if a command class does not run the base initialize method' do
class Cmd < Hystrix::Command
def initialize; end
def run; end
end
expect {
Cmd.new.execute
}.to raise_error
end
end