fastly_nsq 0.6.0 → 0.7.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.
- checksums.yaml +4 -4
- data/.github/PULL_REQUEST_TEMPLATE.md +6 -3
- data/.travis.yml +1 -1
- data/README.md +23 -73
- data/env_configuration_for_local_gem_tests.yml +0 -1
- data/examples/.sample.env +0 -2
- data/examples/Rakefile +20 -67
- data/lib/fastly_nsq/{message_queue/consumer.rb → consumer.rb} +8 -8
- data/lib/fastly_nsq/fake_backend.rb +100 -0
- data/lib/fastly_nsq/listener.rb +59 -0
- data/lib/fastly_nsq/message.rb +18 -0
- data/lib/fastly_nsq/{message_queue/producer.rb → producer.rb} +8 -8
- data/lib/fastly_nsq/rake_task.rb +39 -43
- data/lib/fastly_nsq/ssl_context.rb +35 -33
- data/lib/fastly_nsq/strategy.rb +38 -0
- data/lib/fastly_nsq/version.rb +1 -1
- data/lib/fastly_nsq.rb +21 -1
- data/spec/lib/fastly_nsq/consumer_spec.rb +68 -0
- data/spec/lib/fastly_nsq/{fake_message_queue_spec.rb → fake_backend_spec.rb} +23 -23
- data/spec/lib/fastly_nsq/listener_spec.rb +116 -0
- data/spec/lib/fastly_nsq/message_spec.rb +24 -0
- data/spec/lib/fastly_nsq/producer_spec.rb +56 -0
- data/spec/lib/fastly_nsq/rake_task_spec.rb +87 -74
- data/spec/lib/fastly_nsq/ssl_context_spec.rb +6 -6
- data/spec/lib/fastly_nsq/{message_queue/strategy_spec.rb → strategy_spec.rb} +6 -5
- data/spec/lib/fastly_nsq_spec.rb +18 -0
- data/spec/spec_helper.rb +1 -6
- metadata +15 -16
- data/lib/fastly_nsq/fake_message_queue.rb +0 -98
- data/lib/fastly_nsq/message_queue/listener.rb +0 -49
- data/lib/fastly_nsq/message_queue/strategy.rb +0 -34
- data/lib/fastly_nsq/message_queue.rb +0 -21
- data/lib/fastly_nsq/sample_message_processor.rb +0 -50
- data/spec/lib/fastly_nsq/message_queue/consumer_spec.rb +0 -60
- data/spec/lib/fastly_nsq/message_queue/listener_spec.rb +0 -90
- data/spec/lib/fastly_nsq/message_queue/producer_spec.rb +0 -49
- data/spec/lib/fastly_nsq/message_queue_spec.rb +0 -32
- data/spec/lib/fastly_nsq/sample_message_processor_spec.rb +0 -38
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe MessageQueue::Consumer do
|
4
|
-
let(:channel) { 'star_killer_base' }
|
5
|
-
let(:topic) { 'death_star' }
|
6
|
-
let(:consumer) { MessageQueue::Consumer.new topic: topic, channel: channel }
|
7
|
-
let(:backend) { double 'Consumer' }
|
8
|
-
|
9
|
-
describe 'when the ENV is set incorrectly' do
|
10
|
-
it 'raises with a helpful error' do
|
11
|
-
allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return('taco')
|
12
|
-
|
13
|
-
expect { consumer.terminate }.to raise_error(InvalidParameterError)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe 'when connector connects to a backend Consumer' do
|
18
|
-
let(:consumer) do
|
19
|
-
MessageQueue::Consumer.new topic: topic, channel: channel, connector: ->(_) { backend }
|
20
|
-
end
|
21
|
-
|
22
|
-
it 'forwards #pop' do
|
23
|
-
expect(backend).to receive(:pop)
|
24
|
-
consumer.pop
|
25
|
-
end
|
26
|
-
|
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)
|
34
|
-
consumer.size
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'forwards #terminate' do
|
38
|
-
expect(backend).to receive(:terminate)
|
39
|
-
consumer.terminate
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe 'using the default connector' do
|
44
|
-
module TestStrategy
|
45
|
-
module Consumer
|
46
|
-
def self.new(*_); end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
before do
|
51
|
-
allow(MessageQueue).to receive(:strategy).and_return(TestStrategy)
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'instantiates a consumer via Strategy' do
|
55
|
-
allow(backend).to receive(:terminate)
|
56
|
-
expect(TestStrategy::Consumer).to receive(:new).and_return(backend)
|
57
|
-
consumer.terminate
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe MessageQueue::Listener do
|
4
|
-
let(:topic) { 'testing_topic' }
|
5
|
-
let(:channel) { 'testing_channel' }
|
6
|
-
let(:consumer) { FakeMessageQueue::Consumer.new topic: topic, channel: channel }
|
7
|
-
|
8
|
-
module TestMessageProcessor
|
9
|
-
@@messages_processed = []
|
10
|
-
Message = Struct.new(:body, :topic) do
|
11
|
-
def finish
|
12
|
-
@did_finish = true
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def self.call(body, topic)
|
17
|
-
@@messages_processed.push Message.new(body, topic)
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.messages_processed
|
21
|
-
@@messages_processed
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.clear
|
25
|
-
@@messages_processed = []
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
let(:listener) do
|
30
|
-
MessageQueue::Listener.new topic: topic,
|
31
|
-
channel: channel,
|
32
|
-
processor: TestMessageProcessor,
|
33
|
-
consumer: consumer
|
34
|
-
end
|
35
|
-
|
36
|
-
let(:message) { TestMessageProcessor::Message.new 'this is message body', topic }
|
37
|
-
let(:messages_processed) { TestMessageProcessor.messages_processed }
|
38
|
-
let(:expected_message) { TestMessageProcessor::Message.new('this is message body', topic) }
|
39
|
-
let(:expected_messages) { [expected_message] }
|
40
|
-
|
41
|
-
describe 'instantiating without a consumer' do
|
42
|
-
it 'instantiates a consumer, passing the topic and channel' do
|
43
|
-
allow(MessageQueue::Consumer).to receive(:new)
|
44
|
-
|
45
|
-
MessageQueue::Listener.new topic: topic,
|
46
|
-
channel: channel,
|
47
|
-
processor: TestMessageProcessor,
|
48
|
-
consumer: nil
|
49
|
-
|
50
|
-
expect(MessageQueue::Consumer).to have_received(:new).
|
51
|
-
with(topic: topic, channel: channel)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
describe 'when processing next message' do
|
56
|
-
before(:each) { TestMessageProcessor.clear }
|
57
|
-
|
58
|
-
it 'processes the next message' do
|
59
|
-
allow(consumer).to receive(:pop).and_return(message)
|
60
|
-
listener.process_next_message
|
61
|
-
|
62
|
-
expect(messages_processed).to eql(expected_messages)
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'finishes the message' do
|
66
|
-
allow(consumer).to receive(:pop).and_return(message)
|
67
|
-
allow(message).to receive(:finish)
|
68
|
-
|
69
|
-
listener.process_next_message
|
70
|
-
|
71
|
-
expect(message).to have_received(:finish).once
|
72
|
-
end
|
73
|
-
|
74
|
-
context 'when using the fake queue and it is empty', fake_queue: true do
|
75
|
-
before do
|
76
|
-
FakeMessageQueue.delay = 0.1
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'blocks on the process for longer than the check cycle' do
|
80
|
-
delay = FakeMessageQueue.delay + 0.1
|
81
|
-
|
82
|
-
expect do
|
83
|
-
Timeout.timeout(delay) do
|
84
|
-
listener.process_next_message
|
85
|
-
end
|
86
|
-
end.to raise_error(Timeout::Error)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe MessageQueue::Producer do
|
4
|
-
let(:topic) { 'death_star' }
|
5
|
-
let(:producer) { MessageQueue::Producer.new(topic: topic) }
|
6
|
-
let(:backend) { double 'Producer' }
|
7
|
-
|
8
|
-
describe 'when the ENV is set incorrectly' do
|
9
|
-
it 'raises with a helpful error' do
|
10
|
-
allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return('taco')
|
11
|
-
|
12
|
-
expect { producer.terminate }.to raise_error(InvalidParameterError)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
describe 'when connector connects to a backend Producer' do
|
17
|
-
let(:producer) do
|
18
|
-
MessageQueue::Producer.new topic: topic, connector: ->(_) { backend }
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'forwards #write' do
|
22
|
-
expect(backend).to receive(:write).with("it's a message")
|
23
|
-
producer.write "it's a message"
|
24
|
-
end
|
25
|
-
|
26
|
-
it 'forwards #terminate' do
|
27
|
-
expect(backend).to receive(:terminate)
|
28
|
-
producer.terminate
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
describe 'using the default connector' do
|
33
|
-
module TestStrategy
|
34
|
-
module Producer
|
35
|
-
def self.new(*_); end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
before do
|
40
|
-
allow(MessageQueue).to receive(:strategy).and_return(TestStrategy)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'instantiates a producer via Strategy' do
|
44
|
-
allow(backend).to receive(:terminate)
|
45
|
-
expect(TestStrategy::Producer).to receive(:new).and_return(backend)
|
46
|
-
producer.terminate
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
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
|
-
|
19
|
-
describe '.logger' do
|
20
|
-
describe 'when using the fake queue', fake_queue: true do
|
21
|
-
end
|
22
|
-
|
23
|
-
describe 'when using the real queue, fake_queue: false' do
|
24
|
-
it 'allows the logger to be set and retrieved' do
|
25
|
-
logger = Logger.new(STDOUT)
|
26
|
-
MessageQueue.logger = logger
|
27
|
-
|
28
|
-
expect(MessageQueue.logger).to eq logger
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe SampleMessageProcessor do
|
4
|
-
describe '.topics' do
|
5
|
-
it 'specifies the array of topics to listen to' do
|
6
|
-
topics = SampleMessageProcessor.topics
|
7
|
-
|
8
|
-
expect(topics).to be_an Array
|
9
|
-
expect(topics.first).to be_a String
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
describe '#go' do
|
14
|
-
it 'enqueues the appropriate message processor' do
|
15
|
-
data = { 'key' => 'value' }
|
16
|
-
message_body = { 'data' => data }.to_json
|
17
|
-
allow(HeartbeatWorker).to receive(:perform_async)
|
18
|
-
topic = 'heartbeat'
|
19
|
-
|
20
|
-
SampleMessageProcessor.new(message_body: message_body, topic: topic).go
|
21
|
-
|
22
|
-
expect(HeartbeatWorker).to have_received(:perform_async).with(data)
|
23
|
-
end
|
24
|
-
|
25
|
-
describe 'when the message topic is not known' do
|
26
|
-
it 'uses the null object processor' do
|
27
|
-
data = { 'sample_key' => 'sample value' }
|
28
|
-
message_body = { 'data' => data }.to_json
|
29
|
-
allow(UnknownMessageWorker).to receive(:perform_async)
|
30
|
-
topic = 'unknown_topic'
|
31
|
-
|
32
|
-
SampleMessageProcessor.new(message_body: message_body, topic: topic).go
|
33
|
-
|
34
|
-
expect(UnknownMessageWorker).to have_received(:perform_async).with(data)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|