routemaster-drain 2.3.0 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +19 -0
  3. data/.env.test +2 -2
  4. data/.rubocop.yml +1156 -0
  5. data/.ruby-version +1 -1
  6. data/.travis.yml +8 -0
  7. data/Appraisals +3 -3
  8. data/CHANGELOG.md +31 -5
  9. data/Gemfile +7 -6
  10. data/Gemfile.lock +23 -17
  11. data/README.md +19 -0
  12. data/appraise +28 -0
  13. data/gemfiles/rails_3.gemfile +8 -8
  14. data/gemfiles/rails_3.gemfile.lock +64 -58
  15. data/gemfiles/rails_4.gemfile +8 -8
  16. data/gemfiles/rails_4.gemfile.lock +121 -92
  17. data/gemfiles/rails_5.gemfile +8 -8
  18. data/gemfiles/rails_5.gemfile.lock +78 -72
  19. data/lib/core_ext/forwardable.rb +14 -0
  20. data/lib/routemaster/api_client.rb +65 -36
  21. data/lib/routemaster/cache.rb +7 -1
  22. data/lib/routemaster/cache_key.rb +7 -0
  23. data/lib/routemaster/config.rb +12 -13
  24. data/lib/routemaster/dirty/map.rb +1 -1
  25. data/lib/routemaster/drain.rb +1 -1
  26. data/lib/routemaster/event_index.rb +21 -0
  27. data/lib/routemaster/jobs.rb +2 -0
  28. data/lib/routemaster/jobs/cache_and_sweep.rb +2 -1
  29. data/lib/routemaster/jobs/job.rb +2 -0
  30. data/lib/routemaster/middleware/cache.rb +2 -5
  31. data/lib/routemaster/middleware/parse.rb +2 -2
  32. data/lib/routemaster/middleware/response_caching.rb +54 -24
  33. data/lib/routemaster/null_logger.rb +16 -0
  34. data/lib/routemaster/redis_broker.rb +8 -7
  35. data/lib/routemaster/resources/rest_resource.rb +18 -7
  36. data/lib/routemaster/responses/future_response.rb +37 -17
  37. data/lib/routemaster/responses/hateoas_enumerable_response.rb +47 -0
  38. data/lib/routemaster/responses/hateoas_response.rb +9 -12
  39. data/routemaster-drain.gemspec +2 -2
  40. data/spec/routemaster/api_client_spec.rb +118 -44
  41. data/spec/routemaster/drain/caching_spec.rb +4 -3
  42. data/spec/routemaster/integration/api_client_spec.rb +266 -102
  43. data/spec/routemaster/integration/cache_spec.rb +52 -39
  44. data/spec/routemaster/middleware/cache_spec.rb +4 -6
  45. data/spec/routemaster/redis_broker_spec.rb +11 -11
  46. data/spec/routemaster/resources/rest_resource_spec.rb +4 -2
  47. data/spec/routemaster/responses/future_response_spec.rb +18 -0
  48. data/spec/routemaster/responses/hateoas_enumerable_response_spec.rb +78 -0
  49. data/spec/routemaster/responses/hateoas_response_spec.rb +52 -53
  50. data/spec/spec_helper.rb +2 -1
  51. data/spec/support/breakpoint_class.rb +14 -0
  52. data/spec/support/server.rb +52 -0
  53. data/spec/support/uses_redis.rb +2 -2
  54. metadata +26 -10
  55. data/test.rb +0 -17
@@ -1,73 +1,86 @@
1
1
  require 'spec_helper'
2
2
  require 'spec/support/uses_redis'
3
3
  require 'spec/support/uses_dotenv'
4
+ require 'spec/support/uses_webmock'
5
+ require 'spec/support/server'
4
6
  require 'routemaster/cache'
5
7
  require 'webrick'
6
8
 
7
- RSpec.describe 'Requests with caching' do
9
+ describe Routemaster::Cache do
8
10
  uses_dotenv
9
11
  uses_redis
12
+ uses_webmock
13
+
14
+ def time_us
15
+ Integer(Time.now.to_f * 1e6)
16
+ end
10
17
 
11
- let!(:log) { WEBrick::Log.new '/dev/null' }
12
18
  let(:service) do
13
- WEBrick::HTTPServer.new(Port: 8000, DocumentRoot: Dir.pwd, Logger: log).tap do |server|
19
+ TestServer.new(8000) do |server|
14
20
  server.mount_proc '/test' do |req, res|
15
- res.body = { field: 'test' }.to_json
21
+ res['content-type'] = 'application/json'
22
+ res['x-timestamp'] = time_us.to_s
23
+ res.body = { value: time_us }.to_json
16
24
  end
17
25
  end
18
26
  end
19
27
 
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
28
+ before { service.start }
29
+ after { service.stop }
27
30
 
28
- after do
29
- Process.kill('KILL', @pid)
30
- Process.wait(@pid)
31
- end
32
-
33
- subject { Routemaster::Cache.new }
31
+ before { WebMock.disable_net_connect!(allow_localhost: true) }
34
32
 
35
- describe 'GET request' do
36
- let(:body_cache_keys) { ["cache:#{url}", "v:,l:,body"] }
37
- let(:headers_cache_keys) { ["cache:#{url}", "v:,l:,headers"] }
33
+ shared_examples 'a cached GET' do
38
34
  let(:url) { 'http://localhost:8000/test' }
39
35
 
36
+ let(:response) { perform.call }
37
+
40
38
  context 'when there is no previous cached response' do
41
- it 'makes an http call' do
42
- response = subject.get(url)
43
- expect(response.headers['server']).to be
39
+ it 'makes a HTTP request' do
40
+ perform.call
41
+ expect(WebMock).to have_requested(:get, url)
44
42
  end
45
43
 
46
- it 'sets the new response onto the cache' do
47
- expect { subject.get(url) }
48
- .to change { Routemaster::Config.cache_redis.hget(*body_cache_keys)}
49
- .from(nil)
50
- .to({ field: 'test'}.to_json)
44
+ it 'returns fresh headers' do
45
+ time_before = time_us()
46
+ expect(response.headers['x-timestamp'].to_i).to be > time_before
51
47
  end
52
48
 
53
- it 'sets the response headers onto the cache' do
54
- expect { subject.get(url) }
55
- .to change { Routemaster::Config.cache_redis.hget(*headers_cache_keys)}
56
- .from(nil)
49
+ it 'returns a fresh body' do
50
+ time_before = time_us()
51
+ expect(response.body.value).to be > time_before
57
52
  end
58
53
  end
59
54
 
60
- context 'when there is a previous cached response' do
61
- before { subject.get(url) }
55
+ context 'when there is a cached response' do
56
+ before { perform.call }
62
57
 
63
- it 'fetches the cached response' do
64
- expect(subject.get(url).body).to eq({ field: 'test' }.to_json)
58
+ it 'returns cached headers' do
59
+ time_before = time_us()
60
+ expect(response.headers['x-timestamp'].to_i).to be < time_before
65
61
  end
66
62
 
67
- it 'does not make an http call' do
68
- response = subject.get(url)
69
- expect(response.env.request).to be_empty
63
+ it 'returns a cached body' do
64
+ time_before = time_us()
65
+ expect(response.body.value).to be < time_before
66
+ end
67
+
68
+ it 'makes no HTTP requests' do
69
+ WebMock.reset!
70
+ perform.call
71
+ expect(WebMock).not_to have_requested(:any, //)
70
72
  end
71
73
  end
72
74
  end
75
+
76
+
77
+ describe '#get' do
78
+ let(:perform) { -> { subject.get(url) } }
79
+ it_behaves_like 'a cached GET'
80
+ end
81
+
82
+ describe '#fget' do
83
+ let(:perform) { -> { subject.fget(url).value } }
84
+ it_behaves_like 'a cached GET'
85
+ end
73
86
  end
@@ -1,6 +1,7 @@
1
1
  require 'spec_helper'
2
2
  require 'spec/support/rack_test'
3
3
  require 'routemaster/middleware/cache'
4
+ require 'routemaster/event_index'
4
5
 
5
6
  RSpec.describe Routemaster::Middleware::Cache do
6
7
  # busts the cache for each dirty url
@@ -8,7 +9,7 @@ RSpec.describe Routemaster::Middleware::Cache do
8
9
  let(:terminator) { ErrorRackApp.new }
9
10
  let(:app) { described_class.new(terminator, **options) }
10
11
  let(:client) { Routemaster::Jobs::Client.new }
11
- let(:cache) { instance_double(Routemaster::Cache, bust: nil) }
12
+ let(:cache) { instance_double(Routemaster::Cache, bust: nil, invalidate: nil) }
12
13
  let(:options) {{ cache: cache, client: client }}
13
14
 
14
15
  let(:perform) do
@@ -18,8 +19,8 @@ RSpec.describe Routemaster::Middleware::Cache do
18
19
  describe '#call' do
19
20
  let(:payload) { ['https://example.com/1'] }
20
21
 
21
- it 'busts the cache' do
22
- expect(cache).to receive(:bust).with(payload.first)
22
+ it 'increments the event_index' do
23
+ expect(cache).to receive(:invalidate)
23
24
  perform
24
25
  end
25
26
 
@@ -29,6 +30,3 @@ RSpec.describe Routemaster::Middleware::Cache do
29
30
  end
30
31
  end
31
32
  end
32
-
33
-
34
-
@@ -6,50 +6,50 @@ describe Routemaster::RedisBroker do
6
6
  subject { Class.new(Routemaster::RedisBroker).instance }
7
7
 
8
8
  describe "#get" do
9
- let(:url) { 'redis://localhost/12' }
9
+ let(:urls) { ['redis://localhost/12'] }
10
10
 
11
11
  context "setting up a redis namespace" do
12
- let(:redis) { instance_double(Redis) }
12
+ let(:redis) { instance_double(Redis, id: 1) }
13
13
  let(:redis_namespace) { instance_double(Redis::Namespace) }
14
14
 
15
15
  before do
16
- allow(Redis).to receive(:new) { redis }
16
+ allow(Redis::Distributed).to receive(:new) { redis }
17
17
  allow(Redis::Namespace).to receive(:new) { redis_namespace }
18
18
  end
19
19
 
20
20
  it 'returns a namespaced redis connection' do
21
- expect(subject.get(url)).to eq(redis_namespace)
21
+ expect(subject.get(:name, urls: urls)).to eq(redis_namespace)
22
22
  end
23
23
 
24
24
  it 'uses the url to initialise redis' do
25
- expect(Redis).to receive(:new).with(url: url)
26
- subject.get(url)
25
+ expect(Redis::Distributed).to receive(:new).with(urls)
26
+ subject.get(:name, urls: urls)
27
27
  end
28
28
 
29
29
  it 'namespaces with rm by default' do
30
30
  expect(Redis::Namespace).to receive(:new).with('rm', redis: redis)
31
- subject.get(url)
31
+ subject.get(:name, urls: urls)
32
32
  end
33
33
 
34
34
  it 'can use a namespace based on the url' do
35
35
  expect(Redis::Namespace).to receive(:new).with('other', redis: redis)
36
- subject.get('redis://localhost/12/other')
36
+ subject.get(:name, urls: ['redis://localhost/12/other'])
37
37
  end
38
38
  end
39
39
 
40
40
  context "when we are in the same process" do
41
41
  it 'is a single connection for each url' do
42
- expect(subject.get(url)).to eql(subject.get(url))
42
+ expect(subject.get(:name, urls: urls)).to eql(subject.get(:name, urls: urls))
43
43
  end
44
44
  end
45
45
 
46
46
  context "when we have forked" do
47
- let!(:connection) { subject.get(url) }
47
+ let!(:connection) { subject.get(:name, urls: urls) }
48
48
 
49
49
  it 'is a new connection for the newly forked process' do
50
50
  # this is tied to implementation, but tests a 'fork' well enough
51
51
  allow(Process).to receive(:pid) { -1 }
52
- expect(subject.get(url)).to_not eql(connection)
52
+ expect(subject.get(:name, urls: urls)).to_not eql(connection)
53
53
  end
54
54
  end
55
55
  end
@@ -10,6 +10,8 @@ module Routemaster
10
10
 
11
11
  subject { described_class.new(url, client: client) }
12
12
 
13
+ before { allow(client).to receive(:with_response).and_yield }
14
+
13
15
  describe '#create' do
14
16
  it 'posts to the given url' do
15
17
  expect(client).to receive(:post).with(url, body: params)
@@ -19,14 +21,14 @@ module Routemaster
19
21
 
20
22
  describe '#show' do
21
23
  it 'gets to the given url' do
22
- expect(client).to receive(:get).with(url)
24
+ expect(client).to receive(:get).with(url, options: { enable_caching: true })
23
25
  subject.show(1)
24
26
  end
25
27
  end
26
28
 
27
29
  describe '#index' do
28
30
  it 'gets to the given url' do
29
- expect(client).to receive(:get).with(url)
31
+ expect(client).to receive(:get).with(url, params: {}, options: { enable_caching: false })
30
32
  subject.index
31
33
  end
32
34
  end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ require 'hashie/mash'
3
+ require 'routemaster/responses/future_response'
4
+
5
+ describe Routemaster::Responses::FutureResponse do
6
+ %i[status headers body].each do |method|
7
+ it "passes through '#{method}'" do
8
+ future = described_class.new { Hashie::Mash.new(method => 'foobar') }
9
+ expect(future.public_send(method)).to eq('foobar')
10
+ end
11
+ end
12
+
13
+ it 're-raises exceptions' do
14
+ future = described_class.new { raise 'foobar' }
15
+
16
+ expect { future.status }.to raise_error(RuntimeError, 'foobar')
17
+ end
18
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+ require 'spec/support/uses_webmock'
3
+ require 'spec/support/uses_redis'
4
+ require 'routemaster/responses/hateoas_enumerable_response'
5
+
6
+ # need to load this here to resolve circular dependency
7
+ require 'routemaster/resources/rest_resource'
8
+
9
+ describe Routemaster::Responses::HateoasEnumerableResponse do
10
+ uses_webmock
11
+ uses_redis
12
+
13
+ let(:resource_tpl) { Addressable::Template.new('https://example.com/shebangs/{id}') }
14
+ let(:index_tpl) { Addressable::Template.new('https://example.com/shebangs{?page,per_page}') }
15
+ let(:index_url) { index_tpl.expand(page: nil, per_page:nil).to_s }
16
+ let(:pages) { 5 }
17
+ let(:per_page) { 3 }
18
+
19
+ before do
20
+ @resource_stub = stub_request(:get, resource_tpl).to_return do |req|
21
+ {
22
+ status: 200,
23
+ headers: { 'Content-Type' => 'application/json' },
24
+ body: {
25
+ id: req.uri.path.split('/').last.to_i,
26
+ _links: {
27
+ self: { href: req.uri.to_s },
28
+ }
29
+ }.to_json
30
+ }
31
+ end
32
+
33
+ @index_stub = stub_request(:get, index_tpl).to_return do |req|
34
+ req.uri.query_values ||= { 'page' => '1' }
35
+ page = req.uri.query_values['page'].to_i
36
+ start = (page-1) * per_page + 1
37
+ stop = page * per_page
38
+
39
+ data = Hashie::Mash.new(
40
+ _links: {
41
+ self: { href: index_tpl.expand(page: page, per_page: per_page).to_s },
42
+ shebangs: (start..stop).map { |idx|
43
+ { href: resource_tpl.expand(id: idx).to_s }
44
+ }
45
+ },
46
+ page: page,
47
+ per_page: per_page,
48
+ total: pages * per_page,
49
+ )
50
+
51
+ data._links.next!.href = index_tpl.expand(page: page+1, per_page: per_page).to_s if page < pages
52
+ data._links.prev!.href = index_tpl.expand(page: page-1, per_page: per_page).to_s if page > 1
53
+
54
+ { status: 200, headers: { 'Content-Type' => 'application/json' }, body: data.to_json }
55
+ end
56
+ end
57
+
58
+ let(:client) { Routemaster::APIClient.new }
59
+ subject { described_class.new(client.get(index_url)) }
60
+
61
+ # so we don't pollute future specs with pending requests:
62
+ after { Routemaster::Responses::FutureResponse::Pool.reset }
63
+
64
+ describe '#each' do
65
+ it 'is enumerable' do
66
+ expect(subject.count).to eq(15)
67
+ end
68
+
69
+ it 'lists all paginated resources' do
70
+ expect(subject.map(&:body).map(&:id)).to eq (1..15).to_a
71
+ end
72
+
73
+ it 'does not fetch eagerly' do
74
+ subject.first
75
+ expect(@index_stub).to have_been_requested.once
76
+ end
77
+ end
78
+ end
@@ -1,59 +1,58 @@
1
1
  require 'spec_helper'
2
2
  require 'routemaster/responses/hateoas_response'
3
3
 
4
- module Routemaster
5
- module Responses
6
- RSpec.describe HateoasResponse do
7
- let(:response) { double('Response', status: status, body: body, headers: headers) }
8
- let(:status) { 200 }
9
- let(:body) { {}.to_json }
10
- let(:headers) { {} }
11
-
12
- subject { described_class.new(response) }
13
-
14
- context 'link traversal' do
15
- let(:body) do
16
- {
17
- '_links' => {
18
- 'self' => { 'href' => 'self_url' },
19
- 'resource_a' => { 'href' => 'resource_a_url' },
20
- 'resource_b' => { 'href' => 'resource_b_url' }
21
- }
22
- }
23
- end
24
-
25
- it 'creates a method for every key in _links attribute' do
26
- expect(subject.resource_a.url).to eq('resource_a_url')
27
- expect(subject.resource_b.url).to eq('resource_b_url')
28
- end
29
-
30
- it 'creates a _self method if there is a link with name self' do
31
- expect(subject._self.url).to eq('self_url')
32
- end
33
-
34
- it 'raise an exception when requested link does not exist' do
35
- expect { subject.some_unsupported_link }.to raise_error(NoMethodError)
36
- end
37
-
38
- describe '#body_without_links' do
39
- before do
40
- body.merge!('foo' => 'bar')
41
- end
42
-
43
- it 'returns the body without the _links key' do
44
- expect(subject.body_without_links).to eq({ 'foo' => 'bar' })
45
- end
46
- end
47
-
48
- describe '#has?' do
49
- it 'returns true for an existing link' do
50
- expect(subject.has?(:resource_a)).to be_truthy
51
- end
52
-
53
- it 'returns false for a non existing link' do
54
- expect(subject.has?(:other_resource)).to be_falsy
55
- end
56
- end
4
+ # need to laod this here to resolve circular dependency
5
+ require 'routemaster/resources/rest_resource'
6
+
7
+ describe Routemaster::Responses::HateoasResponse do
8
+ let(:response) { double('Response', status: status, body: body, headers: headers) }
9
+ let(:status) { 200 }
10
+ let(:body) { {}.to_json }
11
+ let(:headers) { {} }
12
+
13
+ subject { described_class.new(response) }
14
+
15
+ context 'link traversal' do
16
+ let(:body) do
17
+ {
18
+ '_links' => {
19
+ 'self' => { 'href' => 'self_url' },
20
+ 'resource_a' => { 'href' => 'resource_a_url' },
21
+ 'resource_b' => { 'href' => 'resource_b_url' }
22
+ }
23
+ }
24
+ end
25
+
26
+ it 'creates a method for every key in _links attribute' do
27
+ expect(subject.resource_a.url).to eq('resource_a_url')
28
+ expect(subject.resource_b.url).to eq('resource_b_url')
29
+ end
30
+
31
+ it 'creates a _self method if there is a link with name self' do
32
+ expect(subject._self.url).to eq('self_url')
33
+ end
34
+
35
+ it 'raise an exception when requested link does not exist' do
36
+ expect { subject.some_unsupported_link }.to raise_error(NoMethodError)
37
+ end
38
+
39
+ describe '#body_without_links' do
40
+ before do
41
+ body.merge!('foo' => 'bar')
42
+ end
43
+
44
+ it 'returns the body without the _links key' do
45
+ expect(subject.body_without_links).to eq({ 'foo' => 'bar' })
46
+ end
47
+ end
48
+
49
+ describe '#has?' do
50
+ it 'returns true for an existing link' do
51
+ expect(subject.has?(:resource_a)).to be_truthy
52
+ end
53
+
54
+ it 'returns false for a non existing link' do
55
+ expect(subject.has?(:other_resource)).to be_falsy
57
56
  end
58
57
  end
59
58
  end