routemaster-drain 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ describe Routemaster::Drain::Caching do
12
12
 
13
13
  let(:app) { described_class.new }
14
14
  let(:listener) { double 'listener' }
15
-
15
+
16
16
  before { app.subscribe(listener, prefix: true) }
17
17
 
18
18
  let(:path) { '/' }
@@ -40,7 +40,7 @@ describe Routemaster::Drain::Caching do
40
40
  end
41
41
 
42
42
  it 'schedules caching jobs' do
43
- expect(Resque).to receive(:enqueue_to).exactly(3).times
43
+ expect_any_instance_of(Routemaster::Jobs::Client).to receive(:enqueue).exactly(3).times
44
44
  perform
45
45
  end
46
46
  end
@@ -1,17 +1,20 @@
1
1
  require 'spec_helper'
2
2
  require 'spec/support/uses_dotenv'
3
+ require 'spec/support/uses_redis'
3
4
  require 'spec/support/uses_webmock'
4
5
  require 'routemaster/fetcher'
5
6
  require 'json'
6
7
 
7
8
  describe Routemaster::Fetcher do
8
9
  uses_dotenv
10
+ uses_redis
9
11
  uses_webmock
10
12
 
11
13
  describe '.get' do
12
14
  let(:url) { 'https://example.com/widgets/132' }
13
15
  let(:headers) {{}}
14
- subject { described_class.get(url, headers: headers) }
16
+ let(:fetcher) { described_class.new }
17
+ subject { fetcher.get(url, headers: headers) }
15
18
 
16
19
  before do
17
20
  @req = stub_request(:get, /example\.com/).to_return(
@@ -41,7 +44,8 @@ describe Routemaster::Fetcher do
41
44
  it 'uses auth' do
42
45
  subject
43
46
  assert_requested(:get, /example/) do |req|
44
- expect(req.uri.userinfo).to eq('username:s3cr3t')
47
+ credentials = Base64.strict_encode64('username:s3cr3t')
48
+ expect(req.headers['Authorization']).to eq("Basic #{credentials}")
45
49
  end
46
50
  end
47
51
 
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+ require 'spec/support/uses_redis'
3
+ require 'spec/support/uses_dotenv'
4
+ require 'routemaster/cache'
5
+ require 'webrick'
6
+
7
+ RSpec.describe 'Requests with caching' do
8
+ uses_dotenv
9
+ uses_redis
10
+
11
+ let!(:log) { WEBrick::Log.new '/dev/null' }
12
+ let(:service) do
13
+ WEBrick::HTTPServer.new(Port: 8000, DocumentRoot: Dir.pwd, Logger: log).tap do |server|
14
+ server.mount_proc '/test' do |req, res|
15
+ res.body = { field: 'test' }.to_json
16
+ end
17
+ end
18
+ end
19
+
20
+ before do
21
+ @pid = fork do
22
+ trap 'INT' do service.shutdown end
23
+ service.start
24
+ end
25
+ sleep(0.5) # leave sometime for the previous webrick to teardown
26
+ end
27
+
28
+ after do
29
+ Process.kill('KILL', @pid)
30
+ Process.wait(@pid)
31
+ end
32
+
33
+ subject { Routemaster::Cache.new }
34
+
35
+ describe 'GET request' do
36
+ let(:cache_keys) { ["cache:#{url}", "v:,l:"] }
37
+ let(:url) { 'http://localhost:8000/test' }
38
+
39
+ context 'when there is no previous cached response' do
40
+ it 'makes an http call' do
41
+ response = subject.get(url)
42
+ expect(response.headers['server']).to be
43
+ end
44
+
45
+ it 'sets the new response onto the cache' do
46
+ expect { subject.get(url) }
47
+ .to change { Routemaster::Config.cache_redis.hget(*cache_keys)}
48
+ .from(nil)
49
+ .to({ field: 'test'}.to_json)
50
+ end
51
+ end
52
+
53
+ context 'when there is a previous cached response' do
54
+ before do
55
+ subject.get(url)
56
+ end
57
+
58
+ it 'fetches the cached response' do
59
+ expect(subject.get(url).body).to eq({ field: 'test' }.to_json)
60
+ end
61
+
62
+ it 'does not make an http call' do
63
+ response = subject.get(url)
64
+ expect(response.headers['server']).to be_nil
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,21 @@
1
+ RSpec.shared_examples 'a job backend' do
2
+ class TestJob
3
+ def perform(x, y); end
4
+ end
5
+
6
+ let(:job) { TestJob.new }
7
+ let(:x) { 123 }
8
+ let(:y) { 'hello' }
9
+
10
+ before do
11
+ allow(TestJob).to receive(:new).and_return(job)
12
+ allow(job).to receive(:perform).and_call_original
13
+ described_class.new.enqueue('test_queue', TestJob, x, y)
14
+ end
15
+
16
+ describe '#enqueue' do
17
+ it 'should execute jobs with a #perform method with the passed arguments' do
18
+ expect(job).to have_received(:perform).with(x, y)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ require 'routemaster/jobs/backends/resque'
2
+ require 'spec/routemaster/jobs/backends/backend_examples'
3
+
4
+ RSpec.describe Routemaster::Jobs::Backends::Resque do
5
+ around do |example|
6
+ original_inline = Resque.inline
7
+ begin
8
+ Resque.inline = true
9
+ example.run
10
+ ensure
11
+ Resque.inline = original_inline
12
+ end
13
+ end
14
+
15
+ it_behaves_like 'a job backend'
16
+ end
@@ -0,0 +1,11 @@
1
+ require 'sidekiq/testing'
2
+ require 'routemaster/jobs/backends/sidekiq'
3
+ require 'spec/routemaster/jobs/backends/backend_examples'
4
+
5
+ RSpec.describe Routemaster::Jobs::Backends::Sidekiq do
6
+ around do |example|
7
+ Sidekiq::Testing.inline! { example.run }
8
+ end
9
+
10
+ it_behaves_like 'a job backend'
11
+ end
@@ -0,0 +1,46 @@
1
+ require 'routemaster/jobs/client'
2
+ require 'routemaster/jobs/cache_and_sweep'
3
+
4
+ RSpec.describe Routemaster::Jobs::Client do
5
+ let(:client) { Routemaster::Jobs::Client.new(adapter) }
6
+
7
+ let(:perform) do
8
+ client.enqueue('routemaster', Routemaster::Jobs::CacheAndSweep, 'https://example.com/1')
9
+ end
10
+
11
+ describe '#enqueue' do
12
+ before do
13
+ allow(Routemaster::Config).to receive(:queue_adapter).and_return(backend)
14
+ allow(client).to receive(:enqueue).and_call_original
15
+ end
16
+
17
+ context 'when the backend is Resque' do
18
+ let(:backend) { :resque }
19
+ let(:adapter) { double('Resque', enqueue_to: nil) }
20
+
21
+ it 'queues a Resque fetch job' do
22
+ expect(adapter).to receive(:enqueue_to).with(
23
+ 'routemaster',
24
+ Routemaster::Jobs::Backends::Resque::JobWrapper,
25
+ { 'class' => 'Routemaster::Jobs::CacheAndSweep', 'args' => ['https://example.com/1'] })
26
+ perform
27
+ end
28
+ end
29
+
30
+ context 'when the backend is Sidekiq' do
31
+ let(:backend) { :sidekiq }
32
+ let(:adapter) { double('Sidekiq', push: nil) }
33
+
34
+ it 'queues a Sidekiq fetch job' do
35
+ expect(adapter).to receive(:push).with(
36
+ 'queue' => 'routemaster',
37
+ 'class' => Routemaster::Jobs::Backends::Sidekiq::JobWrapper,
38
+ 'args' => [{ 'class' => 'Routemaster::Jobs::CacheAndSweep', 'args' => ['https://example.com/1'] }])
39
+ perform
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+
46
+
@@ -2,30 +2,29 @@ require 'spec_helper'
2
2
  require 'spec/support/rack_test'
3
3
  require 'routemaster/middleware/cache'
4
4
 
5
- describe Routemaster::Middleware::Cache do
6
-
5
+ RSpec.describe Routemaster::Middleware::Cache do
7
6
  # busts the cache for each dirty url
8
7
 
9
8
  let(:terminator) { ErrorRackApp.new }
10
9
  let(:app) { described_class.new(terminator, **options) }
11
- let(:resque) { double 'resque', enqueue_to: nil }
12
- let(:cache) { double 'cache', bust: nil }
13
- let(:options) {{ cache: cache, resque: resque }}
14
-
10
+ let(:client) { Routemaster::Jobs::Client.new }
11
+ let(:cache) { instance_double(Routemaster::Cache, bust: nil) }
12
+ let(:options) {{ cache: cache, client: client }}
13
+
15
14
  let(:perform) do
16
15
  post '/whatever', '', 'routemaster.dirty' => payload
17
16
  end
18
17
 
19
18
  describe '#call' do
20
19
  let(:payload) { ['https://example.com/1'] }
21
-
20
+
22
21
  it 'busts the cache' do
23
22
  expect(cache).to receive(:bust).with(payload.first)
24
23
  perform
25
24
  end
26
25
 
27
26
  it 'queues a fetch job' do
28
- expect(resque).to receive(:enqueue_to).with('routemaster', anything, 'https://example.com/1')
27
+ expect(client).to receive(:enqueue).with('routemaster', Routemaster::Jobs::CacheAndSweep, 'https://example.com/1')
29
28
  perform
30
29
  end
31
30
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,8 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ require 'byebug'
5
+
1
6
  # This file was generated by the `rspec --init` command. Conventionally, all
2
7
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
8
  # Require this file using `require "spec_helper"` to ensure that it is only
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: routemaster-drain
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.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: 2015-09-15 00:00:00.000000000 Z
11
+ date: 2016-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -42,44 +42,44 @@ dependencies:
42
42
  name: net-http-persistent
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "<"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '3'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "<"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rack
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 1.6.2
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 1.6.2
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: wisper
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: 1.4.0
75
+ version: 1.6.1
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: 1.4.0
82
+ version: 1.6.1
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: hashie
85
85
  requirement: !ruby/object:Gem::Requirement
@@ -108,20 +108,6 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- - !ruby/object:Gem::Dependency
112
- name: resque
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - ">="
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- type: :runtime
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - ">="
123
- - !ruby/object:Gem::Version
124
- version: '0'
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: thread
127
113
  requirement: !ruby/object:Gem::Requirement
@@ -167,10 +153,15 @@ files:
167
153
  - lib/routemaster/drain/mapping.rb
168
154
  - lib/routemaster/drain/terminator.rb
169
155
  - lib/routemaster/fetcher.rb
156
+ - lib/routemaster/jobs/backends/resque.rb
157
+ - lib/routemaster/jobs/backends/sidekiq.rb
170
158
  - lib/routemaster/jobs/cache.rb
171
159
  - lib/routemaster/jobs/cache_and_sweep.rb
160
+ - lib/routemaster/jobs/client.rb
161
+ - lib/routemaster/jobs/job.rb
172
162
  - lib/routemaster/middleware/authenticate.rb
173
163
  - lib/routemaster/middleware/cache.rb
164
+ - lib/routemaster/middleware/caching.rb
174
165
  - lib/routemaster/middleware/dirty.rb
175
166
  - lib/routemaster/middleware/filter.rb
176
167
  - lib/routemaster/middleware/parse.rb
@@ -186,6 +177,11 @@ files:
186
177
  - spec/routemaster/drain/mapping_spec.rb
187
178
  - spec/routemaster/drain/terminator_spec.rb
188
179
  - spec/routemaster/fetcher_spec.rb
180
+ - spec/routemaster/integration/cache_spec.rb
181
+ - spec/routemaster/jobs/backends/backend_examples.rb
182
+ - spec/routemaster/jobs/backends/resque_spec.rb
183
+ - spec/routemaster/jobs/backends/sidekiq_spec.rb
184
+ - spec/routemaster/jobs/client_spec.rb
189
185
  - spec/routemaster/middleware/authenticate_spec.rb
190
186
  - spec/routemaster/middleware/cache_spec.rb
191
187
  - spec/routemaster/middleware/dirty_spec.rb
@@ -220,7 +216,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
216
  version: '0'
221
217
  requirements: []
222
218
  rubyforge_project:
223
- rubygems_version: 2.2.2
219
+ rubygems_version: 2.5.1
224
220
  signing_key:
225
221
  specification_version: 4
226
222
  summary: Event receiver for the Routemaster bus
@@ -234,6 +230,11 @@ test_files:
234
230
  - spec/routemaster/drain/mapping_spec.rb
235
231
  - spec/routemaster/drain/terminator_spec.rb
236
232
  - spec/routemaster/fetcher_spec.rb
233
+ - spec/routemaster/integration/cache_spec.rb
234
+ - spec/routemaster/jobs/backends/backend_examples.rb
235
+ - spec/routemaster/jobs/backends/resque_spec.rb
236
+ - spec/routemaster/jobs/backends/sidekiq_spec.rb
237
+ - spec/routemaster/jobs/client_spec.rb
237
238
  - spec/routemaster/middleware/authenticate_spec.rb
238
239
  - spec/routemaster/middleware/cache_spec.rb
239
240
  - spec/routemaster/middleware/dirty_spec.rb