routemaster-client 2.1.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/spec/client_spec.rb CHANGED
@@ -1,51 +1,37 @@
1
1
  require 'spec_helper'
2
+ require 'spec/support/configuration_helper'
2
3
  require 'routemaster/client'
4
+ require 'routemaster/client/backends/sidekiq'
3
5
  require 'routemaster/topic'
4
6
  require 'webmock/rspec'
5
7
  require 'sidekiq/testing'
6
8
 
7
9
  describe Routemaster::Client do
10
+
11
+ reset_config_between_tests!
12
+
8
13
  let(:options) {{
9
14
  url: 'https://bus.example.com',
10
15
  uuid: 'john_doe',
11
- verify_ssl: false,
16
+ verify_ssl: false,
12
17
  }}
13
18
  let(:pulse_response) { 204 }
14
19
 
15
- subject { described_class.new(options) }
20
+ subject do
21
+ Routemaster::Client.configure do |config|
22
+ options.each do |key, val|
23
+ config.send(:"#{key}=", val)
24
+ end
25
+ end
26
+ end
16
27
 
17
28
  before do
18
- @stub_pulse = stub_request(:get, %r{^https://bus.example.com/pulse$}).
29
+ stub_request(:get, %r{^https://bus.example.com/pulse$}).
19
30
  with(basic_auth: [options[:uuid], 'x']).
20
31
  to_return(status: pulse_response)
21
32
  end
22
33
 
23
- describe '#initialize' do
24
- it 'passes with valid arguments' do
25
- expect { subject }.not_to raise_error
26
- end
27
-
28
- it 'fails with a non-SSL URL' do
29
- options[:url].sub!(/https/, 'http')
30
- expect { subject }.to raise_error(ArgumentError)
31
- end
32
-
33
- it 'fails with a bad URL' do
34
- options[:url].replace('foobar')
35
- expect { subject }.to raise_error(ArgumentError)
36
- end
37
-
38
- it 'fails with a bad client id' do
39
- options[:uuid].replace('123 $%')
40
- expect { subject }.to raise_error(ArgumentError)
41
- end
42
-
43
- it 'fails with an invalid worker_type' do
44
- Jeff = double
45
- options[:backend_type] = Jeff
46
- expect { subject }.to raise_error(ArgumentError)
47
- end
48
-
34
+ describe "configure" do
49
35
  context 'when connection fails' do
50
36
  before do
51
37
  stub_request(:any, %r{^https://bus.example.com}).
@@ -70,10 +56,16 @@ describe Routemaster::Client do
70
56
  expect { subject }.to raise_error(RuntimeError)
71
57
  end
72
58
  end
59
+ end
60
+
61
+ shared_examples 'an unconfigured async event sender' do
62
+ let(:callback) { 'https://app.example.com/widgets/123' }
63
+ let(:topic) { 'widgets' }
64
+ let(:perform) { subject.send(method, topic, callback) }
65
+ let(:http_status) { nil }
73
66
 
74
- it 'fails if the timeout value is not an integer' do
75
- options[:timeout] = 'timeout'
76
- expect { subject }.to raise_error(ArgumentError)
67
+ it 'raises an error' do
68
+ expect { perform }.to raise_error(Routemaster::Client::MissingAsyncBackendError)
77
69
  end
78
70
  end
79
71
 
@@ -107,17 +99,21 @@ describe Routemaster::Client do
107
99
 
108
100
  it 'fails with a bad callback URL' do
109
101
  callback.replace 'http.foo.bar'
110
- expect { perform }.to raise_error(ArgumentError)
102
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
111
103
  end
112
104
 
113
105
  it 'fails with a non-SSL URL' do
114
106
  callback.replace 'http://example.com'
115
- expect { perform }.to raise_error(ArgumentError)
107
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
116
108
  end
117
109
 
118
110
  it 'fails with a bad topic name' do
119
111
  topic.replace 'foo123$bar'
120
- expect { perform }.to raise_error(ArgumentError, 'bad topic name: must only include letters and underscores')
112
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
113
+ end
114
+
115
+ it 'returns true' do
116
+ expect(perform).to eq true
121
117
  end
122
118
  end
123
119
 
@@ -160,7 +156,7 @@ describe Routemaster::Client do
160
156
  let(:timestamp) { 'foo' }
161
157
 
162
158
  it 'fails' do
163
- expect { perform }.to raise_error(ArgumentError)
159
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
164
160
  end
165
161
  end
166
162
 
@@ -168,7 +164,7 @@ describe Routemaster::Client do
168
164
  let(:timestamp) { 123.45 }
169
165
 
170
166
  it 'fails' do
171
- expect { perform }.to raise_error(ArgumentError)
167
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
172
168
  end
173
169
  end
174
170
  end
@@ -180,53 +176,98 @@ describe Routemaster::Client do
180
176
  it_behaves_like 'an event sender'
181
177
  end
182
178
 
179
+ describe '#created_async' do
180
+ let(:method) { 'created_async' }
181
+ it_behaves_like 'an unconfigured async event sender'
182
+ end
183
+
183
184
  describe '#updated' do
184
185
  let(:event) { 'updated' }
185
186
  it_behaves_like 'an event sender'
186
187
  end
187
188
 
189
+ describe '#updated_async' do
190
+ let(:method) { 'updated_async' }
191
+ it_behaves_like 'an unconfigured async event sender'
192
+ end
193
+
188
194
  describe '#deleted' do
189
195
  let(:event) { 'deleted' }
190
196
  it_behaves_like 'an event sender'
191
197
  end
192
198
 
199
+ describe '#deleted_async' do
200
+ let(:method) { 'deleted_async' }
201
+ it_behaves_like 'an unconfigured async event sender'
202
+ end
203
+
193
204
  describe '#noop' do
194
205
  let(:event) { 'noop' }
195
206
  it_behaves_like 'an event sender'
196
207
  end
197
- end
198
-
199
- context "With the sidekiq back end" do
200
- before do
201
- options[:backend_type] = Routemaster::Client::Backends::Sidekiq
202
- end
203
208
 
204
- around do |example|
205
- Sidekiq::Testing.inline! do
206
- example.run
207
- end
209
+ describe '#noop_async' do
210
+ let(:method) { 'noop_async' }
211
+ it_behaves_like 'an unconfigured async event sender'
208
212
  end
213
+ end
209
214
 
210
- describe '#created' do
211
- let(:event) { 'created' }
215
+ context "With the sidekiq back end" do
216
+ reset_sidekiq_config_between_tests!
217
+
218
+ before do
219
+ options[:async_backend] = Routemaster::Client::Backends::Sidekiq.configure do |config|
220
+ config.queue = :realtime
221
+ config.retry = true
222
+ end
223
+ end
224
+
225
+ around do |example|
226
+ Sidekiq::Testing.inline! do
227
+ example.run
228
+ end
229
+ end
230
+
231
+ describe '#created' do
232
+ let(:event) { 'created' }
233
+ it_behaves_like 'an event sender'
234
+ end
235
+
236
+ describe '#created_async' do
237
+ let(:event) { 'created_async' }
212
238
  it_behaves_like 'an event sender'
213
239
  end
214
240
 
215
- describe '#updated' do
216
- let(:event) { 'updated' }
217
- it_behaves_like 'an event sender'
218
- end
241
+ describe '#updated' do
242
+ let(:event) { 'updated' }
243
+ it_behaves_like 'an event sender'
244
+ end
219
245
 
220
- describe '#deleted' do
221
- let(:event) { 'deleted' }
222
- it_behaves_like 'an event sender'
223
- end
246
+ describe '#updated_async' do
247
+ let(:event) { 'updated_async' }
248
+ it_behaves_like 'an event sender'
249
+ end
224
250
 
225
- describe '#noop' do
226
- let(:event) { 'noop' }
227
- it_behaves_like 'an event sender'
228
- end
229
- end
251
+ describe '#deleted' do
252
+ let(:event) { 'deleted' }
253
+ it_behaves_like 'an event sender'
254
+ end
255
+
256
+ describe '#deleted_async' do
257
+ let(:event) { 'deleted_async' }
258
+ it_behaves_like 'an event sender'
259
+ end
260
+
261
+ describe '#noop' do
262
+ let(:event) { 'noop' }
263
+ it_behaves_like 'an event sender'
264
+ end
265
+
266
+ describe '#noop_async' do
267
+ let(:event) { 'noop_async' }
268
+ it_behaves_like 'an event sender'
269
+ end
270
+ end
230
271
 
231
272
  describe '#subscribe' do
232
273
  let(:perform) { subject.subscribe(subscribe_options) }
@@ -253,27 +294,27 @@ describe Routemaster::Client do
253
294
 
254
295
  it 'fails with a bad callback' do
255
296
  subscribe_options[:callback] = 'http://example.com'
256
- expect { perform }.to raise_error(ArgumentError)
297
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
257
298
  end
258
299
 
259
300
  it 'fails with a bad timeout' do
260
301
  subscribe_options[:timeout] = -5
261
- expect { perform }.to raise_error(ArgumentError)
302
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
262
303
  end
263
304
 
264
305
  it 'fails with a bad max number of events' do
265
306
  subscribe_options[:max] = 1_000_000
266
- expect { perform }.to raise_error(ArgumentError)
307
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
267
308
  end
268
309
 
269
310
  it 'fails with a bad topic list' do
270
311
  subscribe_options[:topics] = ['widgets', 'foo123$%bar']
271
- expect { perform }.to raise_error(ArgumentError)
312
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
272
313
  end
273
314
 
274
315
  it 'fails on HTTP error' do
275
316
  @stub.to_return(status: 500)
276
- expect { perform }.to raise_error(RuntimeError)
317
+ expect { perform }.to raise_error(RuntimeError, 'subscribe rejected')
277
318
  end
278
319
 
279
320
  it 'accepts a uuid' do
@@ -300,7 +341,7 @@ describe Routemaster::Client do
300
341
 
301
342
  it 'fails with a bad topic' do
302
343
  args.replace ['foo123%bar']
303
- expect { perform }.to raise_error(ArgumentError)
344
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
304
345
  end
305
346
 
306
347
  it 'fails on HTTP error' do
@@ -347,7 +388,7 @@ describe Routemaster::Client do
347
388
 
348
389
  it 'fails with a bad topic' do
349
390
  args.replace ['foo123%bar']
350
- expect { perform }.to raise_error(ArgumentError)
391
+ expect { perform }.to raise_error(Routemaster::Client::InvalidArgumentError)
351
392
  end
352
393
 
353
394
  it 'fails on HTTP error' do
@@ -370,23 +411,40 @@ describe Routemaster::Client do
370
411
  ]
371
412
  end
372
413
 
373
- before do
374
- @stub = stub_request(:get, 'https://bus.example.com/topics').
375
- with(basic_auth: [options[:uuid], 'x']).
376
- with { |r|
414
+ context 'the connection to the bus is successful' do
415
+ before do
416
+ @stub = stub_request(:get, 'https://bus.example.com/topics').
417
+ with(basic_auth: [options[:uuid], 'x']).
418
+ with { |r|
377
419
  r.headers['Content-Type'] == 'application/json'
378
420
  }.to_return {
379
421
  { status: 200, body: expected_result.to_json }
380
422
  }
423
+ end
424
+
425
+ it 'expects a collection of topics' do
426
+ expect(perform.map(&:attributes)).to eql(expected_result)
427
+ end
381
428
  end
382
429
 
383
- it 'expects a collection of topics' do
384
- expect(perform.map(&:attributes)).to eql(expected_result)
430
+ context 'the connection to the bus errors' do
431
+ before do
432
+ @stub = stub_request(:get, 'https://bus.example.com/topics').
433
+ with(basic_auth: [options[:uuid], 'x']).
434
+ with { |r|
435
+ r.headers['Content-Type'] == 'application/json'
436
+ }.to_return(status: 500)
437
+ end
438
+
439
+ it 'expects a collection of topics' do
440
+ expect { perform }.to raise_error(RuntimeError)
441
+ end
385
442
  end
386
443
  end
387
444
 
388
445
  describe '#monitor_subscriptions' do
389
446
  it 'passes'
390
447
  end
448
+
391
449
  end
392
450
 
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,12 @@
1
1
  require 'simplecov'
2
+ require 'webmock'
2
3
  SimpleCov.start
3
4
 
5
+ SimpleCov.start do
6
+ add_filter 'routemaster/client/configuration'
7
+ add_filter 'routemaster/client/backends/sidekiq/configuration'
8
+ end
9
+
4
10
  # This file was generated by the `rspec --init` command. Conventionally, all
5
11
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
12
  # Require this file using `require "spec_helper"` to ensure that it is only
@@ -0,0 +1,17 @@
1
+ module ConfigurationHelper
2
+ def reset_config_between_tests!
3
+ after do
4
+ Routemaster::Client.send(:remove_const, :Configuration)
5
+ load 'routemaster/client/configuration.rb'
6
+ end
7
+ end
8
+
9
+ def reset_sidekiq_config_between_tests!
10
+ after do
11
+ Routemaster::Client::Backends::Sidekiq.send(:remove_const, :Configuration)
12
+ load 'routemaster/client/backends/sidekiq/configuration.rb'
13
+ end
14
+ end
15
+ end
16
+
17
+ RSpec.configure { |c| c.extend ConfigurationHelper }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: routemaster-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Letessier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-12 00:00:00.000000000 Z
11
+ date: 2017-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus
@@ -86,17 +86,24 @@ files:
86
86
  - Rakefile
87
87
  - routemaster-client.gemspec
88
88
  - routemaster/client.rb
89
- - routemaster/client/backends.rb
89
+ - routemaster/client/assertion_helpers.rb
90
+ - routemaster/client/backends/missing_asynchronous.rb
90
91
  - routemaster/client/backends/sidekiq.rb
92
+ - routemaster/client/backends/sidekiq/configuration.rb
91
93
  - routemaster/client/backends/sidekiq/worker.rb
92
94
  - routemaster/client/backends/synchronous.rb
95
+ - routemaster/client/configuration.rb
93
96
  - routemaster/client/connection.rb
97
+ - routemaster/client/errors.rb
94
98
  - routemaster/client/version.rb
95
99
  - routemaster/receiver.rb
96
100
  - routemaster/topic.rb
101
+ - spec/client/backends/sidekiq/configuration_spec.rb
102
+ - spec/client/configuration_spec.rb
97
103
  - spec/client_spec.rb
98
104
  - spec/receiver_spec.rb
99
105
  - spec/spec_helper.rb
106
+ - spec/support/configuration_helper.rb
100
107
  - spec/topic_spec.rb
101
108
  homepage: http://github.com/deliveroo/routemaster-client
102
109
  licenses:
@@ -123,7 +130,10 @@ signing_key:
123
130
  specification_version: 4
124
131
  summary: Client API for the Routemaster event bus
125
132
  test_files:
133
+ - spec/client/backends/sidekiq/configuration_spec.rb
134
+ - spec/client/configuration_spec.rb
126
135
  - spec/client_spec.rb
127
136
  - spec/receiver_spec.rb
128
137
  - spec/spec_helper.rb
138
+ - spec/support/configuration_helper.rb
129
139
  - spec/topic_spec.rb
@@ -1,10 +0,0 @@
1
- module Routemaster
2
- class Client
3
- module Backends
4
- NAMES = ["Routemaster::Client::Backends::Synchronous", "Routemaster::Client::Backends::Sidekiq"]
5
-
6
- autoload 'Synchronous', 'routemaster/client/backends/synchronous'
7
- autoload 'Sidekiq', 'routemaster/client/backends/sidekiq'
8
- end
9
- end
10
- end