fastly_nsq 0.5.0 → 0.5.1
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.
- checksums.yaml +4 -4
- data/Rakefile +1 -1
- data/lib/fastly_nsq/message_queue.rb +0 -3
- data/lib/fastly_nsq/message_queue/consumer.rb +8 -5
- data/lib/fastly_nsq/message_queue/producer.rb +1 -1
- data/lib/fastly_nsq/message_queue/strategy.rb +32 -10
- data/lib/fastly_nsq/version.rb +1 -1
- data/spec/lib/fastly_nsq/message_queue/consumer_spec.rb +26 -31
- data/spec/lib/fastly_nsq/message_queue/listener_spec.rb +0 -17
- data/spec/lib/fastly_nsq/message_queue/strategy_spec.rb +21 -15
- data/spec/lib/fastly_nsq/message_queue_spec.rb +15 -6
- data/spec/support/env_helpers.rb +4 -8
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b814efb023751a7379ea0190cbff20bd92eb082
|
4
|
+
data.tar.gz: b750d879dc3b688c4b3286294b373e02ae576db8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34d1cd71cd955642569c2228f70bf78f8e15b14c599f6527e30078cd8fbc4dc4e9db8d29928085fb24f5610dc903a791fb665889e6e833e5bd073359045f9ade
|
7
|
+
data.tar.gz: f226f49a4282418dcd558ad9207a11647e03b9d2ea791844e682d5303a9b40e8106ac8b4f9428e4195aac1911c1284a2e922cea8785d3cc26e8f11d0565ac1ac
|
data/Rakefile
CHANGED
@@ -7,9 +7,6 @@ require_relative 'message_queue/strategy'
|
|
7
7
|
require_relative 'ssl_context'
|
8
8
|
|
9
9
|
module MessageQueue
|
10
|
-
FALSY_VALUES = [false, 0, '0', 'false', 'FALSE', 'off', 'OFF', nil].freeze
|
11
|
-
TRUTHY_VALUES = [true, 1, '1', 'true', 'TRUE', 'on', 'ON'].freeze
|
12
|
-
|
13
10
|
def self.logger=(logger)
|
14
11
|
strategy.logger = logger
|
15
12
|
end
|
@@ -10,18 +10,21 @@ module MessageQueue
|
|
10
10
|
def_delegator :connection, :size
|
11
11
|
def_delegator :connection, :terminate
|
12
12
|
|
13
|
-
def initialize(topic:, channel:, ssl_context: nil)
|
14
|
-
@topic
|
15
|
-
@channel
|
13
|
+
def initialize(topic:, channel:, ssl_context: nil, connector: nil)
|
14
|
+
@topic = topic
|
15
|
+
@channel = channel
|
16
16
|
@ssl_context = SSLContext.new(ssl_context)
|
17
|
+
@connector = connector || DEFAULT_CONNECTOR
|
17
18
|
end
|
18
19
|
|
19
20
|
private
|
20
21
|
|
21
|
-
attr_reader :channel, :topic, :ssl_context
|
22
|
+
attr_reader :channel, :connector, :topic, :ssl_context
|
23
|
+
|
24
|
+
DEFAULT_CONNECTOR = ->(params) { MessageQueue.strategy::Consumer.new(params) }
|
22
25
|
|
23
26
|
def connection
|
24
|
-
|
27
|
+
@connection ||= connector.call(params)
|
25
28
|
end
|
26
29
|
|
27
30
|
def params
|
@@ -1,12 +1,34 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
1
|
+
module MessageQueue::Strategy
|
2
|
+
module_function
|
3
|
+
|
4
|
+
def for_queue
|
5
|
+
real_queue || fake_queue || error
|
6
|
+
end
|
7
|
+
|
8
|
+
private_class_method
|
9
|
+
|
10
|
+
ERR_MESSAGE = "You must set ENV['FAKE_QUEUE'] to either true or false".freeze
|
11
|
+
|
12
|
+
def error
|
13
|
+
raise InvalidParameterError, ERR_MESSAGE
|
14
|
+
end
|
15
|
+
|
16
|
+
FALSY_VALUES = [false, 0, '0', 'false', 'FALSE', 'off', 'OFF', nil].freeze
|
17
|
+
TRUTHY_VALUES = [true, 1, '1', 'true', 'TRUE', 'on', 'ON'].freeze
|
18
|
+
|
19
|
+
def fake_queue
|
20
|
+
FakeMessageQueue if should_use_fake_queue?
|
21
|
+
end
|
22
|
+
|
23
|
+
def should_use_real_queue?
|
24
|
+
FALSY_VALUES.include? ENV['FAKE_QUEUE']
|
25
|
+
end
|
26
|
+
|
27
|
+
def real_queue
|
28
|
+
Nsq if should_use_real_queue?
|
29
|
+
end
|
30
|
+
|
31
|
+
def should_use_fake_queue?
|
32
|
+
TRUTHY_VALUES.include? ENV['FAKE_QUEUE']
|
11
33
|
end
|
12
34
|
end
|
data/lib/fastly_nsq/version.rb
CHANGED
@@ -3,11 +3,8 @@ require 'spec_helper'
|
|
3
3
|
RSpec.describe MessageQueue::Consumer do
|
4
4
|
let(:channel) { 'star_killer_base' }
|
5
5
|
let(:topic) { 'death_star' }
|
6
|
-
let(:consumer) { MessageQueue::Consumer.new
|
7
|
-
|
8
|
-
def fake_consumer
|
9
|
-
double 'Consumer', connection: nil, terminate: nil, pop: :popped, size: 0
|
10
|
-
end
|
6
|
+
let(:consumer) { MessageQueue::Consumer.new topic: topic, channel: channel }
|
7
|
+
let(:backend) { double 'Consumer' }
|
11
8
|
|
12
9
|
describe 'when the ENV is set incorrectly' do
|
13
10
|
it 'raises with a helpful error' do
|
@@ -17,49 +14,47 @@ RSpec.describe MessageQueue::Consumer do
|
|
17
14
|
end
|
18
15
|
end
|
19
16
|
|
20
|
-
describe 'when
|
21
|
-
|
22
|
-
|
23
|
-
allow(Nsq::Consumer).to receive(:new).and_return(@fake_consumer)
|
17
|
+
describe 'when connector connects to a backend Consumer' do
|
18
|
+
let(:consumer) do
|
19
|
+
MessageQueue::Consumer.new topic: topic, channel: channel, connector: ->(_) { backend }
|
24
20
|
end
|
25
21
|
|
26
|
-
it 'forwards #pop
|
22
|
+
it 'forwards #pop' do
|
23
|
+
expect(backend).to receive(:pop)
|
27
24
|
consumer.pop
|
28
|
-
expect(@fake_consumer).to have_received(:pop)
|
29
25
|
end
|
30
26
|
|
31
|
-
it 'forwards #
|
27
|
+
it 'forwards #pop_without_blocking' do
|
28
|
+
expect(backend).to receive(:pop_without_blocking)
|
29
|
+
consumer.pop_without_blocking
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'forwards #size' do
|
33
|
+
expect(backend).to receive(:size)
|
32
34
|
consumer.size
|
33
|
-
expect(@fake_consumer).to have_received(:size)
|
34
35
|
end
|
35
36
|
|
36
|
-
it 'forwards #terminate
|
37
|
+
it 'forwards #terminate' do
|
38
|
+
expect(backend).to receive(:terminate)
|
37
39
|
consumer.terminate
|
38
|
-
|
39
|
-
expect(@fake_consumer).to have_received(:terminate)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
-
describe '
|
44
|
-
|
45
|
-
|
46
|
-
|
43
|
+
describe 'using the default connector' do
|
44
|
+
module TestStrategy
|
45
|
+
module Consumer
|
46
|
+
def self.new(*_); end
|
47
|
+
end
|
47
48
|
end
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
expect(@fake_consumer).to have_received(:pop)
|
50
|
+
before do
|
51
|
+
allow(MessageQueue).to receive(:strategy).and_return(TestStrategy)
|
52
52
|
end
|
53
53
|
|
54
|
-
it '
|
55
|
-
|
56
|
-
expect(
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'forwards #terminate to FakeMessageQueue::Consumer' do
|
54
|
+
it 'instantiates a consumer via Strategy' do
|
55
|
+
allow(backend).to receive(:terminate)
|
56
|
+
expect(TestStrategy::Consumer).to receive(:new).and_return(backend)
|
60
57
|
consumer.terminate
|
61
|
-
|
62
|
-
expect(@fake_consumer).to have_received(:terminate)
|
63
58
|
end
|
64
59
|
end
|
65
60
|
end
|
@@ -87,21 +87,4 @@ RSpec.describe MessageQueue::Listener do
|
|
87
87
|
end
|
88
88
|
end
|
89
89
|
end
|
90
|
-
|
91
|
-
describe '#go' do
|
92
|
-
describe 'when a SIGTERM is received' do
|
93
|
-
it 'closes the consumer connection' do
|
94
|
-
pid = fork do
|
95
|
-
MessageQueue::Listener.new(topic: topic, channel: channel).go
|
96
|
-
end
|
97
|
-
|
98
|
-
Process.kill('TERM', pid)
|
99
|
-
|
100
|
-
# Success for this test is to expect it to complete.
|
101
|
-
#
|
102
|
-
# Additional Note: We are NOT testing the SIGINT case because it
|
103
|
-
# orphans the test's Ruby process and is thus meaningless as a test.
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
90
|
end
|
@@ -1,29 +1,35 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
RSpec.describe Strategy do
|
4
|
-
describe '
|
5
|
-
|
6
|
-
|
7
|
-
|
3
|
+
RSpec.describe MessageQueue::Strategy do
|
4
|
+
describe 'when FAKE_QUEUE is falsy' do
|
5
|
+
it 'returns the strategy based on the ENV variable' do
|
6
|
+
[false, 0, '0', 'false', 'FALSE', 'off', 'OFF', nil].each do |no|
|
7
|
+
allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return(no)
|
8
8
|
|
9
|
-
|
9
|
+
strategy = MessageQueue::Strategy.for_queue
|
10
|
+
|
11
|
+
expect(strategy).to eq Nsq
|
10
12
|
end
|
11
13
|
end
|
14
|
+
end
|
12
15
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
+
describe 'when FAKE_QUEUE is truthy' do
|
17
|
+
it 'returns the strategy based on the ENV variable' do
|
18
|
+
[true, 1, '1', 'true', 'TRUE', 'on', 'ON'].each do |yes|
|
19
|
+
allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return(yes)
|
16
20
|
|
17
|
-
|
21
|
+
strategy = MessageQueue::Strategy.for_queue
|
22
|
+
|
23
|
+
expect(strategy).to eq FakeMessageQueue
|
18
24
|
end
|
19
25
|
end
|
26
|
+
end
|
20
27
|
|
21
|
-
|
22
|
-
|
23
|
-
|
28
|
+
describe 'when the ENV is set incorrectly' do
|
29
|
+
it 'raises with a helpful error' do
|
30
|
+
allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return('taco')
|
24
31
|
|
25
|
-
|
26
|
-
end
|
32
|
+
expect { MessageQueue::Strategy.for_queue }.to raise_error(InvalidParameterError)
|
27
33
|
end
|
28
34
|
end
|
29
35
|
end
|
@@ -1,14 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe MessageQueue do
|
4
|
+
module TestStrategy; end
|
5
|
+
|
6
|
+
it 'allows the logger to be set and retrieved' do
|
7
|
+
logger = Logger.new(STDOUT)
|
8
|
+
MessageQueue.logger = logger
|
9
|
+
|
10
|
+
expect(MessageQueue.logger).to eq logger
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'returns the current Strategy' do
|
14
|
+
allow(MessageQueue::Strategy).to receive(:for_queue).and_return(TestStrategy)
|
15
|
+
|
16
|
+
expect(MessageQueue.strategy).to eql(TestStrategy)
|
17
|
+
end
|
18
|
+
|
4
19
|
describe '.logger' do
|
5
20
|
describe 'when using the fake queue', fake_queue: true do
|
6
|
-
it 'allows the logger to be set and retrieved' do
|
7
|
-
logger = Logger.new(STDOUT)
|
8
|
-
MessageQueue.logger = logger
|
9
|
-
|
10
|
-
expect(MessageQueue.logger).to eq logger
|
11
|
-
end
|
12
21
|
end
|
13
22
|
|
14
23
|
describe 'when using the real queue, fake_queue: false' do
|
data/spec/support/env_helpers.rb
CHANGED
@@ -1,16 +1,12 @@
|
|
1
1
|
module EnvHelpers
|
2
2
|
def use_fake_connection
|
3
|
-
|
4
|
-
|
5
|
-
yield
|
6
|
-
end
|
3
|
+
allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return(true)
|
4
|
+
yield
|
7
5
|
end
|
8
6
|
|
9
7
|
def use_real_connection
|
10
|
-
|
11
|
-
|
12
|
-
yield
|
13
|
-
end
|
8
|
+
allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return(false)
|
9
|
+
yield
|
14
10
|
end
|
15
11
|
end
|
16
12
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fastly_nsq
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tommy O'Neil
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-05-
|
12
|
+
date: 2016-05-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: awesome_print
|