fastly_nsq 0.2.3 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 675e3b849867520a11d52017b73a07381287abee
4
- data.tar.gz: 6f6f61ed2785d5872e2ea944d88bafffdc1c7817
3
+ metadata.gz: 9f152c638769443e0a5a05d21222eeb4830d4873
4
+ data.tar.gz: d7bf933bf02602023f81c09480064745baf547cb
5
5
  SHA512:
6
- metadata.gz: 5c8e596008b721e62a78064d98e185dfeae6c2f176a995ffdf3835103d90b9bf1e537aa29a004c9acfdd9d3503961eecb998e9dcb6a7afaf9e6d46c3f6c3fc62
7
- data.tar.gz: f386146874a9fd8ed2bcfeaff3a17fac5568b28873a55de583dda3f33b0ab821bc4a56a36008b5ad971be4e4a4d69ac3f7cb23d1fb2bb326721372e3d71c06c1
6
+ metadata.gz: 987c9d1a7c2e654c8fcd540c6cd78f8d819a9b1723cabc74aee1a7ac27937d045c3c0ce2b80676dc171cb7b84e73d656d0b5b087618aff1dc67b4028f72847d2
7
+ data.tar.gz: e2e808a1f5daea8da64331e47b5097a1b33efd43376adbe7da14f906d3ac51f88d987b4c81a073f965a35f42b1b672fd37d15a90890dc05b3f78e83681a061d4
@@ -19,7 +19,7 @@ Checklist
19
19
  _Put an `x` in the boxes that apply.
20
20
  You can also fill these out after creating the PR._
21
21
 
22
- - [ ] I have linked to all relavant reference issues or work requests
22
+ - [ ] I have linked to all relevant reference issues or work requests
23
23
  - [ ] I have added tests that prove my fix is effective or that my feature works
24
24
  - [ ] I have added or updated necessary documentation (if appropriate)
25
25
  - [ ] Any dependent changes have been merged and published in downstream
data/README.md CHANGED
@@ -52,7 +52,6 @@ write messages onto the queue:
52
52
 
53
53
  ```ruby
54
54
  message_data = {
55
- "event_type" => "heartbeat",
56
55
  "data" => {
57
56
  "key" => "value"
58
57
  }
@@ -65,7 +64,6 @@ producer = MessageQueue::Producer.new(
65
64
 
66
65
  producer.write(message_data.to_json)
67
66
  ```
68
-
69
67
  The mock/real strategy used
70
68
  can be switched
71
69
  by adding an environment variable
@@ -94,7 +92,7 @@ consumer = MessageQueue::Consumer.new(
94
92
 
95
93
  consumer.size #=> 1
96
94
  message = consumer.pop
97
- message.body #=> "{ 'event_type': 'heartbeat','data': { 'key': 'value' } }"
95
+ message.body #=> "{ 'data': { 'key': 'value' } }"
98
96
  message.finish
99
97
  consumer.size #=> 0
100
98
  consumer.terminate
@@ -119,7 +117,7 @@ MessageQueue::Listener.new(topic: topic, channel: channel).process_next_message
119
117
  This will pop the next message
120
118
  off of the queue
121
119
  and send the JSON text body
122
- to `MessageProcessor.new(message_body).go`.
120
+ to `MessageProcessor.new(message_body: message_body, topic: topic).go`.
123
121
 
124
122
  To initiate a blocking loop to process messages continuously:
125
123
 
@@ -141,15 +139,23 @@ there is a new message on the queue,
141
139
  To help facilitate running the `MessageQueue::Listener` in a blocking fashion
142
140
  outside your application, a simple `RakeTask` is provided.
143
141
 
144
- This can be added into your `Rakefile` in one of two ways:
142
+ NOTE: The rake task expects a
143
+ `MessageProcessor.topics` method,
144
+ which must return an array of strings
145
+ defining the topics to which
146
+ we would like to listen and process messages.
147
+
148
+ The task will listen
149
+ to all specified topics,
150
+ each in a separate thread.
151
+
152
+ This task can be added into your `Rakefile` in one of two ways:
145
153
 
146
154
  Using a block:
147
155
  ```ruby
148
- require 'fastly_nsq'
149
156
  require 'fastly_nsq/rake_task'
150
157
 
151
158
  MessageQueue::RakeTask.new(:listen_task) do |task|
152
- task.topic = 'some_topic'
153
159
  task.channel = 'some_channel'
154
160
  end
155
161
 
@@ -159,13 +165,12 @@ end
159
165
 
160
166
  or using passed in values:
161
167
  ```ruby
162
- require 'fastly_nsq'
163
168
  require 'fastly_nsq/rake_task'
164
169
 
165
- MessageQueue::RakeTask.new(:listen_task, [:topic, :channel])
170
+ MessageQueue::RakeTask.new(:listen_task, [:channel])
166
171
 
167
172
  # usage:
168
- `rake listen_task['my_topic','my_channel']`
173
+ `rake listen_task['my_channel']`
169
174
  ```
170
175
 
171
176
  Both methods can be used at the same time with the passed in values taking
@@ -1,41 +1,88 @@
1
- require 'fastly_nsq'
2
1
  require 'fastly_nsq/rake_task'
3
2
 
4
- ##
5
- # Both topic and channel will need to be passed into the rake task when
6
- # it is called.
7
- #
8
- # ex: `rake listen_task[my_topic,my_channel]`
9
- # topic will be 'my_topic' and channel will be 'my_channel'
10
- #
11
- MessageQueue::RakeTask.new(:listen_task, [:topic, :channel])
3
+ #-------------------------------------------------------------------------------
4
+ # You are required to to set the topics in MessageProcessor.topics as an array:
12
5
 
13
- ##
14
- # In Rails, you can include the environment like so:
15
- #
16
- MessageQueue::RakeTask.new(:listen_task, [:topic, :channel] => :environment)
6
+ class MessageProcessor
7
+ def self.topics
8
+ ['customer_created', 'customer_deleted']
9
+ end
17
10
 
18
- ##
19
- # Topic and channel can also be preset in the task by passing a block and
20
- # assigning value like so. This is useful if you want a single purpose
21
- # listener task.
22
- #
23
- # ex: `rake listen_task`
24
- # topic will be 'some_topic' and channel will be 'some_channel'
11
+ # ...
12
+ end
13
+
14
+ # A channel will need to be passed in
15
+ # to the Rake task
16
+ # when it is called.
25
17
  #
18
+ # The task looks like:
19
+
20
+ desc 'Listen to the messaging queue with the given channel.'
21
+
22
+ MessageQueue::RakeTask.new(:listen_task, [:channel])
23
+
24
+ # Call `rake listen_task[my_channel]`.
25
+ # The topics will be ['customer_created', 'customer_deleted']
26
+ # and channel will be 'my_channel'.
27
+
28
+ #-------------------------------------------------------------------------------
29
+ # In Rails, you can include the application environment. The task looks like:
30
+
31
+ desc 'Listen to the messaging queue with the given channel.'
32
+
33
+ MessageQueue::RakeTask.new(:listen_task, [:channel] => :environment)
34
+
35
+ # Call `rake listen_task[your_channel]`.
36
+ # The topics will be ['customer_created', 'customer_deleted']
37
+ # and channel will be 'your_channel'.
38
+
39
+ #-------------------------------------------------------------------------------
40
+ # The channel can also be preset in the task
41
+ # by passing a block and assigning the value. The task looks like:
42
+
43
+ desc 'Listen to the messaging queue with the given channel.'
44
+
26
45
  MessageQueue::RakeTask.new(:listen_task) do |task|
27
- task.topic = 'some_topic'
28
46
  task.channel = 'some_channel'
29
47
  end
30
48
 
31
- ##
32
- # Both forms can be combine to provide defaults of a sort with the ability
33
- # to override at time of rake call
34
- #
35
- # ex: `rake listen_task[altered_topic]`
36
- # topic will be 'altered_topic' and channel will be 'some_channel'
37
- #
38
- MessageQueue::RakeTask.new(:listen_task, [:topic, :channel]) do |task|
39
- task.topic = 'some_topic'
40
- task.channel = 'some_channel'
49
+ # Call `rake listen_task`.
50
+ # The topics will be ['customer_created', 'customer_deleted']
51
+ # and channel will be 'some_channel'.
52
+
53
+ #-------------------------------------------------------------------------------
54
+ # Do the same thing and include the environment:
55
+
56
+ desc 'Listen to the messaging queue with the given channel.'
57
+
58
+ MessageQueue::RakeTask.new(:listen_task, [] => :environment) do |task|
59
+ task.channel = 'her_channel'
41
60
  end
61
+
62
+ # Call `rake listen_task`.
63
+ # The topics will be ['customer_created', 'customer_deleted']
64
+ # and channel will be 'her_channel'.
65
+
66
+ #-------------------------------------------------------------------------------
67
+ # Both forms can be combined
68
+ # to provide defaults
69
+ # with the ability to override at time of Rake call:
70
+
71
+ MessageQueue::RakeTask.new(:listen_task, [:channel]) do |task|
72
+ task.channel = 'default_channel'
73
+ end
74
+
75
+ # Call `rake listen_task[overridden_channel]`.
76
+ # The topics will be ['customer_created', 'customer_deleted']
77
+ # and channel will be 'overridden_channel'.
78
+
79
+ #-------------------------------------------------------------------------------
80
+ # The same, but including the environment:
81
+
82
+ MessageQueue::RakeTask.new(:listen_task, [:channel] => :environment) do |task|
83
+ task.channel = 'default_channel'
84
+ end
85
+
86
+ # Call `rake listen_task[overridden_channel]`.
87
+ # The topics will be ['customer_created', 'customer_deleted']
88
+ # and channel will be 'overridden_channel'.
@@ -26,14 +26,14 @@ module MessageQueue
26
26
 
27
27
  private
28
28
 
29
+ attr_reader :channel, :topic
30
+
29
31
  def process_one_message
30
32
  message = consumer.pop
31
- MessageProcessor.new(message.body).go
33
+ MessageProcessor.new(message_body: message.body, topic: topic).go
32
34
  message.finish
33
35
  end
34
36
 
35
- attr_reader :channel, :topic
36
-
37
37
  def consumer
38
38
  @consumer ||= MessageQueue::Consumer.new(consumer_params).connection
39
39
  end
@@ -3,38 +3,77 @@ require 'rake/tasklib'
3
3
 
4
4
  module MessageQueue
5
5
  class RakeTask < Rake::TaskLib
6
- attr_accessor :name, :topic, :channel
6
+ attr_accessor :name, :channel
7
7
 
8
8
  def initialize(*args, &task_block)
9
9
  @name = args.shift || :begin_listening
10
10
 
11
- desc 'Listen to NSQ on topic using channel' unless ::Rake.application.last_comment
11
+ unless ::Rake.application.last_comment
12
+ desc 'Listen to NSQ on topic using channel'
13
+ end
12
14
 
13
15
  task(name, *args) do |_, task_args|
14
16
  RakeFileUtils.send(:verbose, verbose) do
15
- yield(*[self, task_args].slice(0, task_block.arity)) if block_given?
16
- @topic = task_args[:topic] if task_args[:topic]
17
- @channel = task_args[:channel] if task_args[:channel]
18
- run_task
17
+ if block_given?
18
+ yield(*[self, task_args].slice(0, task_block.arity))
19
+ end
20
+
21
+ if task_args[:channel]
22
+ @channel = task_args[:channel]
23
+ end
24
+
25
+ guard_missing_channel
26
+ run_tasks
19
27
  end
20
28
  end
21
29
  end
22
30
 
23
31
  private
24
32
 
25
- def run_task
26
- raise ArgumentError, "topic and channel are required. Recieved topic: #{topic} channel: #{channel}" unless topic && channel
33
+ def run_tasks
34
+ topics.each do |topic|
35
+ Thread.new do
36
+ wrap_helpful_output(topic) do
37
+ MessageQueue::Listener.new(topic: topic, channel: channel).go
38
+ end
39
+ end
40
+ end
41
+
42
+ non_main_threads.map(&:join)
43
+ end
27
44
 
28
- output "Listening to the queue on topic:'#{topic}' and channel ':#{channel}'"
45
+ def non_main_threads
46
+ (Thread.list - [Thread.main])
47
+ end
29
48
 
30
- MessageQueue::Listener.new(topic: topic, channel: channel).go
49
+ def guard_missing_channel
50
+ unless channel
51
+ raise ArgumentError, "channel is required. Received channel: #{channel}"
52
+ end
53
+ end
54
+
55
+ def wrap_helpful_output(topic)
56
+ output "Listening to queue, topic:'#{topic}' and channel: '#{channel}'"
57
+ yield
58
+ output "... done listening on topic:'#{topic}' and channel: '#{channel}'."
59
+ end
60
+
61
+ def topics
62
+ MessageProcessor.topics
63
+ rescue NoMethodError => exception
64
+ if exception.message =~ /undefined method `topics'/
65
+ raise ArgumentError, 'MessageProcessor.topics is not defined.'
66
+ else
67
+ raise exception
68
+ end
69
+ end
31
70
 
32
- output "... done listening to queue on topic:'#{topic}' and channel ':#{channel}'"
71
+ def output(string)
72
+ logger.info(string)
33
73
  end
34
74
 
35
- # wrapping output for stubbing in tests to avoid clobbering output...
36
- def output(str)
37
- puts str
75
+ def logger
76
+ MessageQueue.logger = Logger.new(STDOUT)
38
77
  end
39
78
  end
40
79
  end
@@ -11,12 +11,17 @@ class UnknownMessageWorker
11
11
  end
12
12
 
13
13
  class SampleMessageProcessor
14
- EVENT_TYPE_TO_WORKER_MAP = {
14
+ TOPIC_TO_WORKER_MAP = {
15
15
  'heartbeat' => HeartbeatWorker,
16
16
  }.freeze
17
17
 
18
- def initialize(message_body)
18
+ def self.topics
19
+ TOPIC_TO_WORKER_MAP.keys
20
+ end
21
+
22
+ def initialize(message_body:, topic:)
19
23
  @message_body = message_body
24
+ @topic = topic
20
25
  end
21
26
 
22
27
  def go
@@ -25,18 +30,14 @@ class SampleMessageProcessor
25
30
 
26
31
  private
27
32
 
28
- attr_reader :message_body
33
+ attr_reader :message_body, :topic
29
34
 
30
35
  def process_message_body
31
36
  message_processor.perform_async(message_data)
32
37
  end
33
38
 
34
39
  def message_processor
35
- EVENT_TYPE_TO_WORKER_MAP.fetch(event_type, UnknownMessageWorker)
36
- end
37
-
38
- def event_type
39
- parsed_message_body['event_type']
40
+ TOPIC_TO_WORKER_MAP.fetch(topic, UnknownMessageWorker)
40
41
  end
41
42
 
42
43
  def message_data
@@ -1,3 +1,3 @@
1
1
  module FastlyNsq
2
- VERSION = '0.2.3'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -32,7 +32,8 @@ RSpec.describe MessageQueue::Listener do
32
32
  MessageQueue::Listener.new(topic: topic, channel: channel).
33
33
  process_next_message
34
34
 
35
- expect(MessageProcessor).to have_received(:new).with(message_body)
35
+ expect(MessageProcessor).to have_received(:new).
36
+ with(topic: topic, message_body: message_body)
36
37
  expect(process_message).to have_received(:go)
37
38
  end
38
39
 
@@ -2,25 +2,21 @@ require 'spec_helper'
2
2
 
3
3
  RSpec.describe MessageQueue do
4
4
  describe '.logger' do
5
- describe 'when using the fake queue' do
5
+ describe 'when using the fake queue', fake_queue: true do
6
6
  it 'allows the logger to be set and retrieved' do
7
- use_fake_connection do
8
- logger = Logger.new(STDOUT)
9
- MessageQueue.logger = logger
7
+ logger = Logger.new(STDOUT)
8
+ MessageQueue.logger = logger
10
9
 
11
- expect(MessageQueue.logger).to eq logger
12
- end
10
+ expect(MessageQueue.logger).to eq logger
13
11
  end
14
12
  end
15
13
 
16
- describe 'when using the real queue' do
14
+ describe 'when using the real queue, fake_queue: false' do
17
15
  it 'allows the logger to be set and retrieved' do
18
- use_real_connection do
19
- logger = Logger.new(STDOUT)
20
- MessageQueue.logger = logger
16
+ logger = Logger.new(STDOUT)
17
+ MessageQueue.logger = logger
21
18
 
22
- expect(MessageQueue.logger).to eq logger
23
- end
19
+ expect(MessageQueue.logger).to eq logger
24
20
  end
25
21
  end
26
22
  end
@@ -3,71 +3,126 @@ require 'fastly_nsq/rake_task'
3
3
 
4
4
  RSpec.describe MessageQueue::RakeTask do
5
5
  before(:each) do
6
- Rake::Task['begin_listening'].clear if Rake::Task.task_defined?('begin_listening')
6
+ Rake::Task.clear
7
+ allow_any_instance_of(MessageQueue::RakeTask).to receive(:output) { nil }
7
8
  end
8
9
 
9
10
  describe 'when defining tasks' do
10
- it 'creates a begin_listening task' do
11
- MessageQueue::RakeTask.new
11
+ context 'when no task name is provided' do
12
+ it 'creates a task with the default name' do
13
+ default_task_name = 'begin_listening'
12
14
 
13
- allow_any_instance_of(MessageQueue::RakeTask).to receive(:output) { nil }
15
+ MessageQueue::RakeTask.new
16
+ defined_tasks = Rake::Task.tasks
17
+ first_task_name = defined_tasks.first.name
14
18
 
15
- is_defined = Rake::Task.task_defined?(:begin_listening)
16
- expect(is_defined).to be_truthy
19
+ expect(first_task_name).to eq default_task_name
20
+ end
17
21
  end
18
22
 
19
- it 'creates a named task' do
20
- MessageQueue::RakeTask.new(:test_name)
23
+ context 'when a task name is passed in' do
24
+ it 'creates a task with the provided name' do
25
+ task_name = 'test_name'
21
26
 
22
- allow_any_instance_of(MessageQueue::RakeTask).to receive(:output) { nil }
27
+ MessageQueue::RakeTask.new(task_name.to_sym)
28
+ defined_tasks = Rake::Task.tasks
29
+ first_task_name = defined_tasks.first.name
23
30
 
24
- is_defined = Rake::Task.task_defined?(:test_name)
25
- expect(is_defined).to be_truthy
31
+ expect(first_task_name).to eq task_name
32
+ end
26
33
  end
27
34
  end
28
35
 
29
36
  describe 'when running tasks' do
30
- it 'runs with inline options defined' do
31
- options = { topic: 'dwarf', channel: 'star' }
32
- task = MessageQueue::RakeTask.new(:begin_listening, [:topic, :channel])
37
+ context 'when multiple topics are defined' do
38
+ it 'creates a listener for each' do
39
+ channel = 'clown_generating_service'
40
+ topics = ['customer_created', 'customer_now_awesome']
41
+ allow(SampleMessageProcessor).to receive(:topics).and_return(topics)
42
+ message_queue_listener = double('listener', go: nil)
43
+ allow(MessageQueue::Listener).to receive(:new).
44
+ and_return(message_queue_listener)
45
+
46
+ MessageQueue::RakeTask.new(:begin_listening, [:channel])
47
+ Rake::Task['begin_listening'].execute(channel: channel)
48
+
49
+ topics.each do |topic|
50
+ expect(MessageQueue::Listener).to have_received(:new).
51
+ with(topic: topic, channel: channel)
52
+ end
53
+ end
54
+ end
55
+
56
+ it 'listens to the command-line-provided channel' do
57
+ channel = 'salesforce'
58
+ topics = ['customer_created']
59
+ allow(SampleMessageProcessor).to receive(:topics).and_return(topics)
33
60
 
34
- message_queue_listener = double('go', go: nil)
61
+ message_queue_listener = double('listener', go: nil)
35
62
  expect(MessageQueue::Listener).to receive(:new).
36
- with(options).
63
+ with(topic: topics.first, channel: channel).
37
64
  and_return(message_queue_listener)
38
65
 
39
- allow_any_instance_of(MessageQueue::RakeTask).to receive(:output) { nil }
40
- Rake::Task['begin_listening'].execute(topic: 'dwarf', channel: 'star')
66
+ MessageQueue::RakeTask.new(:begin_listening, [:channel])
67
+ Rake::Task['begin_listening'].execute(channel: channel)
41
68
  end
42
69
 
43
- it 'runs with specified options if a block is given' do
70
+ it 'runs with specified channel if a block is given' do
71
+ channel = 'send_new_customers_a_sticker_service'
72
+ topics = ['customer_created']
73
+ allow(SampleMessageProcessor).to receive(:topics).and_return(topics)
74
+
75
+ message_queue_listener = double('listener', go: nil)
76
+ expect(MessageQueue::Listener).to receive(:new).
77
+ with(topic: topics.first, channel: channel).
78
+ and_return(message_queue_listener)
79
+
44
80
  MessageQueue::RakeTask.new do |task|
45
- task.topic = 'dwarf'
46
- task.channel = 'star'
81
+ task.channel = channel
47
82
  end
83
+ Rake::Task['begin_listening'].execute(channel: channel)
84
+ end
85
+
86
+ it 'prefers inline channel definition over block assignments' do
87
+ default_channel = 'throw_a_huge_pizza_party_service'
88
+ new_channel = 'send_balloons_to_customer_service'
89
+ topics = ['customer_created']
90
+ allow(SampleMessageProcessor).to receive(:topics).and_return(topics)
48
91
 
49
- message_queue_listener = double('go', go: nil)
92
+ message_queue_listener = double('listener', go: nil)
50
93
  expect(MessageQueue::Listener).to receive(:new).
51
- with(topic: 'dwarf', channel: 'star').
94
+ with(topic: topics.first, channel: new_channel).
52
95
  and_return(message_queue_listener)
53
96
 
54
- allow_any_instance_of(MessageQueue::RakeTask).to receive(:output) { nil }
55
- Rake::Task['begin_listening'].execute(topic: 'dwarf', channel: 'star')
97
+ MessageQueue::RakeTask.new(:begin_listening, [:channel]) do |task|
98
+ task.channel = default_channel
99
+ end
100
+ Rake::Task['begin_listening'].execute(channel: new_channel)
56
101
  end
57
102
 
58
- it 'uses inline definitions over block assignments' do
59
- MessageQueue::RakeTask.new(:begin_listening, [:topic, :channel]) do |task|
60
- task.topic = 'loud'
61
- task.channel = 'noise'
103
+ context 'when no channel is provided' do
104
+ it 'raises an error' do
105
+ topics = ['customer_created']
106
+ allow(SampleMessageProcessor).to receive(:topics).and_return(topics)
107
+
108
+ expect {
109
+ MessageQueue::RakeTask.new(:begin_listening, [:channel])
110
+ Rake::Task['begin_listening'].execute
111
+ }.to raise_error(ArgumentError, /channel is required/)
62
112
  end
113
+ end
63
114
 
64
- message_queue_listener = double('go', go: nil)
65
- expect(MessageQueue::Listener).to receive(:new).
66
- with(topic: 'dwarf', channel: 'star').
67
- and_return(message_queue_listener)
115
+ context 'when MessageProcessor.topics is not defined' do
116
+ it 'raises an error' do
117
+ channel = 'best_server_number_1'
118
+ allow(SampleMessageProcessor).to receive(:topics).
119
+ and_raise(NoMethodError, "undefined method `topics'")
68
120
 
69
- allow_any_instance_of(MessageQueue::RakeTask).to receive(:output) { nil }
70
- Rake::Task['begin_listening'].execute(topic: 'dwarf', channel: 'star')
121
+ expect {
122
+ MessageQueue::RakeTask.new(:begin_listening, [:channel])
123
+ Rake::Task['begin_listening'].execute(channel: channel)
124
+ }.to raise_error(ArgumentError, /MessageProcessor.topics is not defined/)
125
+ end
71
126
  end
72
127
  end
73
128
  end
@@ -1,42 +1,35 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe SampleMessageProcessor do
4
- describe '#start' do
5
- it 'enqueues the appropriate message processor' do
6
- data = { 'key' => 'value' }
7
- message_body = { 'event_type' => 'heartbeat', 'data' => data }.to_json
8
- allow(HeartbeatWorker).to receive(:perform_async)
4
+ describe '.topics' do
5
+ it 'specifies the array of topics to listen to' do
6
+ topics = SampleMessageProcessor.topics
9
7
 
10
- SampleMessageProcessor.new(message_body).go
11
-
12
- expect(HeartbeatWorker).to have_received(:perform_async).with(data)
8
+ expect(topics).to be_an Array
9
+ expect(topics.first).to be_a String
13
10
  end
11
+ end
14
12
 
15
- describe 'when the message event_type is not known' do
16
- it 'uses the null object processor' do
17
- data = { 'sample_key' => 'sample value' }
18
- message_body = {
19
- 'event_type' => 'unregistered_message_type',
20
- 'data' => data,
21
- }.to_json
22
- allow(UnknownMessageWorker).to receive(:perform_async)
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'
23
19
 
24
- SampleMessageProcessor.new(message_body).go
20
+ SampleMessageProcessor.new(message_body: message_body, topic: topic).go
25
21
 
26
- expect(UnknownMessageWorker).to have_received(:perform_async).with(data)
27
- end
22
+ expect(HeartbeatWorker).to have_received(:perform_async).with(data)
28
23
  end
29
24
 
30
- describe 'when the message lacks an event_type' do
25
+ describe 'when the message topic is not known' do
31
26
  it 'uses the null object processor' do
32
27
  data = { 'sample_key' => 'sample value' }
33
- message_body = {
34
- 'not_the_event_type_key' => 'unregistered_message_type',
35
- 'data' => data,
36
- }.to_json
28
+ message_body = { 'data' => data }.to_json
37
29
  allow(UnknownMessageWorker).to receive(:perform_async)
30
+ topic = 'unknown_topic'
38
31
 
39
- SampleMessageProcessor.new(message_body).go
32
+ SampleMessageProcessor.new(message_body: message_body, topic: topic).go
40
33
 
41
34
  expect(UnknownMessageWorker).to have_received(:perform_async).with(data)
42
35
  end
@@ -5,8 +5,6 @@ require 'pry-byebug'
5
5
  require_relative '../lib/fastly_nsq/sample_message_processor'
6
6
  require_relative 'support/env_helpers'
7
7
 
8
- MessageProcessor = SampleMessageProcessor
9
-
10
8
  RSpec.configure do |config|
11
9
  config.expect_with :rspec do |expectations|
12
10
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
@@ -21,7 +19,7 @@ RSpec.configure do |config|
21
19
  config.example_status_persistence_file_path = 'spec/examples.txt'
22
20
  config.filter_run :focus
23
21
  config.order = :random
24
- config.profile_examples = false
22
+ config.profile_examples = 1
25
23
  config.run_all_when_everything_filtered = true
26
24
  Kernel.srand config.seed
27
25
 
@@ -29,6 +27,10 @@ RSpec.configure do |config|
29
27
  config.default_formatter = 'doc'
30
28
  end
31
29
 
30
+ config.before(:suite) do
31
+ MessageProcessor = SampleMessageProcessor
32
+ end
33
+
32
34
  config.before(:each) do
33
35
  load_sample_environment_variables
34
36
  FakeMessageQueue.reset!
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.2.3
4
+ version: 0.3.0
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-03-11 00:00:00.000000000 Z
12
+ date: 2016-03-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: awesome_print