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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b927006ea75538d36b77a9988c99f159327e72c
4
- data.tar.gz: 0ba8e537719bcb36721c65292741d779f9d4da4c
3
+ metadata.gz: 6b814efb023751a7379ea0190cbff20bd92eb082
4
+ data.tar.gz: b750d879dc3b688c4b3286294b373e02ae576db8
5
5
  SHA512:
6
- metadata.gz: acaa3eb91200476a12e5f4099ef6515803293007f8643d49cd9651d5f786a6c9e2f36f39ff8086cff38a339a2767346ffada9131bb584db7d6155343d08788ed
7
- data.tar.gz: 667ebee86054600dbd16e1f0450f51ce0186d759210c4c4c5e3fbac1f1d84e994124c4496cbcfce2ce6281da1398a5e3e0b8c22e45fdf3b1700511081be008bf
6
+ metadata.gz: 34d1cd71cd955642569c2228f70bf78f8e15b14c599f6527e30078cd8fbc4dc4e9db8d29928085fb24f5610dc903a791fb665889e6e833e5bd073359045f9ade
7
+ data.tar.gz: f226f49a4282418dcd558ad9207a11647e03b9d2ea791844e682d5303a9b40e8106ac8b4f9428e4195aac1911c1284a2e922cea8785d3cc26e8f11d0565ac1ac
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ end
11
11
  require 'rake'
12
12
 
13
13
  require 'rubygems/tasks'
14
- Gem::Tasks.new
14
+ Gem::Tasks.new scm: {status: false, tag: false}
15
15
 
16
16
  require 'rdoc/task'
17
17
  RDoc::Task.new
@@ -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 = topic
15
- @channel = 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
- Strategy.for_queue::Consumer.new(params)
27
+ @connection ||= connector.call(params)
25
28
  end
26
29
 
27
30
  def params
@@ -20,7 +20,7 @@ module MessageQueue
20
20
  attr_reader :topic, :ssl_context
21
21
 
22
22
  def producer
23
- Strategy.for_queue::Producer
23
+ MessageQueue.strategy::Producer
24
24
  end
25
25
 
26
26
  def params
@@ -1,12 +1,34 @@
1
- class Strategy
2
- def self.for_queue
3
- if MessageQueue::FALSY_VALUES.include?(ENV['FAKE_QUEUE'])
4
- Nsq
5
- elsif MessageQueue::TRUTHY_VALUES.include?(ENV['FAKE_QUEUE'])
6
- FakeMessageQueue
7
- else
8
- message = "You must set ENV['FAKE_QUEUE'] to either true or false"
9
- raise InvalidParameterError, message
10
- end
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
@@ -1,3 +1,3 @@
1
1
  module FastlyNsq
2
- VERSION = '0.5.0'.freeze
2
+ VERSION = '0.5.1'.freeze
3
3
  end
@@ -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(topic: topic, channel: channel) }
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 using the real queue', fake_queue: false do
21
- before(:example) do
22
- @fake_consumer = fake_consumer
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 to Nsq::Consumer' do
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 #size to Nsq::Consumer' do
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 to Nsq::Consumer' do
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 'when using the fake queue', fake_queue: true do
44
- before(:example) do
45
- @fake_consumer = fake_consumer
46
- allow(FakeMessageQueue::Consumer).to receive(:new).and_return(@fake_consumer)
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
- it 'forwards #pop to FakeMessageQueue::Consumer' do
50
- consumer.pop
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 'forwards #size to FakeMessageQueue::Consumer' do
55
- consumer.size
56
- expect(@fake_consumer).to have_received(:size)
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 '.for_queue' do
5
- describe 'when using the fake queue', fake_queue: true do
6
- it 'returns the strategy based on the ENV variable' do
7
- strategy = Strategy.for_queue
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
- expect(strategy).to eq FakeMessageQueue
9
+ strategy = MessageQueue::Strategy.for_queue
10
+
11
+ expect(strategy).to eq Nsq
10
12
  end
11
13
  end
14
+ end
12
15
 
13
- describe 'when using the real queue', fake_queue: false do
14
- it 'returns the strategy based on the ENV variable' do
15
- strategy = Strategy.for_queue
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
- expect(strategy).to eq Nsq
21
+ strategy = MessageQueue::Strategy.for_queue
22
+
23
+ expect(strategy).to eq FakeMessageQueue
18
24
  end
19
25
  end
26
+ end
20
27
 
21
- describe 'when the ENV is set incorrectly' do
22
- it 'raises with a helpful error' do
23
- allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return('taco')
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
- expect { Strategy.for_queue }.to raise_error(InvalidParameterError)
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
@@ -1,16 +1,12 @@
1
1
  module EnvHelpers
2
2
  def use_fake_connection
3
- MessageQueue::TRUTHY_VALUES.each do |yes|
4
- allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return(yes)
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
- MessageQueue::FALSY_VALUES.each do |no|
11
- allow(ENV).to receive(:[]).with('FAKE_QUEUE').and_return(no)
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.0
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-10 00:00:00.000000000 Z
12
+ date: 2016-05-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_print