fastly_nsq 1.16.0 → 1.18.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/.env +2 -2
- data/.git-blame-ignore-revs +6 -0
- data/.ruby-version +1 -1
- data/.travis.yml +4 -3
- data/ChangeLog.md +31 -1
- data/Gemfile +8 -8
- data/README.md +84 -6
- data/Rakefile +10 -11
- data/fastly_nsq.gemspec +26 -26
- data/lib/fastly_nsq/cli.rb +43 -50
- data/lib/fastly_nsq/consumer.rb +27 -14
- data/lib/fastly_nsq/feeder.rb +5 -7
- data/lib/fastly_nsq/http/nsqd.rb +28 -28
- data/lib/fastly_nsq/http/nsqlookupd.rb +11 -11
- data/lib/fastly_nsq/http.rb +4 -4
- data/lib/fastly_nsq/launcher.rb +16 -16
- data/lib/fastly_nsq/listener.rb +16 -16
- data/lib/fastly_nsq/manager.rb +13 -12
- data/lib/fastly_nsq/message.rb +4 -4
- data/lib/fastly_nsq/messenger.rb +25 -15
- data/lib/fastly_nsq/new_relic.rb +8 -8
- data/lib/fastly_nsq/priority_queue.rb +2 -2
- data/lib/fastly_nsq/priority_thread_pool.rb +3 -3
- data/lib/fastly_nsq/producer.rb +23 -14
- data/lib/fastly_nsq/safe_thread.rb +1 -1
- data/lib/fastly_nsq/testing.rb +4 -3
- data/lib/fastly_nsq/tls_options.rb +6 -6
- data/lib/fastly_nsq/version.rb +1 -1
- data/lib/fastly_nsq.rb +64 -29
- data/spec/cli_spec.rb +2 -2
- data/spec/consumer_spec.rb +53 -12
- data/spec/fastly_nsq_spec.rb +108 -32
- data/spec/feeder_spec.rb +4 -4
- data/spec/http/nsqd_spec.rb +23 -23
- data/spec/http/nsqlookupd_spec.rb +19 -19
- data/spec/http_spec.rb +22 -22
- data/spec/integration_spec.rb +10 -10
- data/spec/launcher_spec.rb +21 -21
- data/spec/listener_spec.rb +50 -50
- data/spec/manager_spec.rb +27 -27
- data/spec/matchers/delegate.rb +4 -4
- data/spec/message_spec.rb +19 -19
- data/spec/messenger_spec.rb +71 -59
- data/spec/new_relic.rb +27 -27
- data/spec/priority_thread_pool_spec.rb +2 -2
- data/spec/producer_spec.rb +70 -31
- data/spec/spec_helper.rb +12 -12
- data/spec/support/http.rb +2 -2
- data/spec/support/webmock.rb +1 -1
- data/spec/testing_spec.rb +12 -12
- data/spec/tls_options_spec.rb +47 -47
- metadata +10 -11
- data/.rubocop.yml +0 -68
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class FastlyNsq::PriorityThreadPool < Concurrent::ThreadPoolExecutor
|
4
|
-
|
4
|
+
alias_method :max_threads, :max_length
|
5
5
|
|
6
6
|
def initialize(*)
|
7
7
|
super
|
@@ -15,7 +15,7 @@ class FastlyNsq::PriorityThreadPool < Concurrent::ThreadPoolExecutor
|
|
15
15
|
# @!visibility private
|
16
16
|
def ns_enqueue(*args, &task)
|
17
17
|
if !ns_limited_queue? || @queue.size < @max_queue
|
18
|
-
@queue.push([task, args[1
|
18
|
+
@queue.push([task, args[1..]], args[0])
|
19
19
|
true
|
20
20
|
else
|
21
21
|
false
|
@@ -27,6 +27,6 @@ class FastlyNsq::PriorityThreadPool < Concurrent::ThreadPoolExecutor
|
|
27
27
|
#
|
28
28
|
# @!visibility private
|
29
29
|
def ns_assign_worker(*args, &task)
|
30
|
-
super(args[1
|
30
|
+
super(args[1..], &task)
|
31
31
|
end
|
32
32
|
end
|
data/lib/fastly_nsq/producer.rb
CHANGED
@@ -24,6 +24,9 @@ class FastlyNsq::Producer
|
|
24
24
|
##
|
25
25
|
# Create a FastlyNsq::Producer
|
26
26
|
#
|
27
|
+
# Will connect to NSQDs in this priority: 1. direct from FastlyNsq.producer_nsqds 2. discovered via FastlyNsq.lookupd_http_addresses.
|
28
|
+
# If both `producer_nsqds` and `lookupd_http_addresses` are set only the FastlyNsq.producer_nsqds will be used.
|
29
|
+
#
|
27
30
|
# @param topic [String] NSQ topic on which to deliver the message
|
28
31
|
# @param tls_options [Hash] Hash of TSL options passed the connection.
|
29
32
|
# In most cases this should be nil unless you need to override the
|
@@ -31,10 +34,10 @@ class FastlyNsq::Producer
|
|
31
34
|
# @param logger [Logger] defaults to FastlyNsq.logger
|
32
35
|
# @param connect_timeout [Integer] NSQ connection timeout in seconds
|
33
36
|
def initialize(topic:, tls_options: nil, logger: FastlyNsq.logger, connect_timeout: DEFAULT_CONNECTION_TIMEOUT)
|
34
|
-
@topic
|
35
|
-
@tls_options
|
37
|
+
@topic = topic
|
38
|
+
@tls_options = FastlyNsq::TlsOptions.as_hash(tls_options)
|
36
39
|
@connect_timeout = connect_timeout
|
37
|
-
@logger
|
40
|
+
@logger = logger
|
38
41
|
|
39
42
|
connect
|
40
43
|
end
|
@@ -71,27 +74,33 @@ class FastlyNsq::Producer
|
|
71
74
|
# Create an Nsq::Producer and set as +@connection+ instance variable
|
72
75
|
# @return [Boolean]
|
73
76
|
def connect
|
77
|
+
producers = FastlyNsq.producer_nsqds
|
74
78
|
lookupd = FastlyNsq.lookupd_http_addresses
|
75
79
|
|
76
|
-
|
77
|
-
tls_options.merge(
|
78
|
-
nsqlookupd: lookupd,
|
79
|
-
topic: topic,
|
80
|
-
),
|
81
|
-
)
|
82
|
-
|
83
|
-
timeout_args = [connect_timeout, FastlyNsq::ConnectionFailed]
|
80
|
+
opts = tls_options.merge(topic: topic)
|
84
81
|
|
85
|
-
if
|
86
|
-
|
82
|
+
if !producers.empty?
|
83
|
+
opts[:nsqd] = producers
|
84
|
+
elsif !lookupd.empty?
|
85
|
+
opts[:nsqlookupd] = lookupd
|
86
|
+
else
|
87
|
+
raise FastlyNsq::ConnectionFailed, "One of FastlyNsq.producer_nsqds or FastlyNsq.lookupd_http_addresses must be present"
|
87
88
|
end
|
88
89
|
|
90
|
+
@connection ||= Nsq::Producer.new(opts)
|
91
|
+
|
92
|
+
timeout_args = [
|
93
|
+
connect_timeout,
|
94
|
+
FastlyNsq::ConnectionFailed,
|
95
|
+
"Failed connection to #{opts[:nsqd] || opts[:nsqlookupd]} within #{connect_timeout} seconds"
|
96
|
+
]
|
97
|
+
|
89
98
|
Timeout.timeout(*timeout_args) { Thread.pass until connection.connected? }
|
90
99
|
|
91
100
|
true
|
92
101
|
rescue FastlyNsq::ConnectionFailed
|
93
102
|
logger.error { "Producer for #{topic} failed to connect!" }
|
94
|
-
terminate
|
103
|
+
terminate if @connection
|
95
104
|
raise
|
96
105
|
end
|
97
106
|
|
data/lib/fastly_nsq/testing.rb
CHANGED
@@ -98,7 +98,7 @@ module FastlyNsq
|
|
98
98
|
# processor_klass.call(test_message)
|
99
99
|
# expect(Post.find(post_data['id']).not_to be nil
|
100
100
|
def message(data:, meta: nil)
|
101
|
-
test_message = FastlyNsq::TestMessage.new(JSON.dump(
|
101
|
+
test_message = FastlyNsq::TestMessage.new(JSON.dump("data" => data, "meta" => meta))
|
102
102
|
FastlyNsq::Message.new(test_message)
|
103
103
|
end
|
104
104
|
end
|
@@ -121,7 +121,7 @@ module FastlyNsq
|
|
121
121
|
|
122
122
|
def initialize(raw_body)
|
123
123
|
@raw_body = raw_body
|
124
|
-
@id
|
124
|
+
@id = Digest::SHA1.hexdigest(raw_body.to_s + Time.now.to_s)
|
125
125
|
@attempts = 0
|
126
126
|
end
|
127
127
|
|
@@ -250,7 +250,8 @@ module FastlyNsq
|
|
250
250
|
FastlyNsq::Listener.prepend(ListenerTesting)
|
251
251
|
|
252
252
|
class FakeConnection
|
253
|
-
def connected
|
253
|
+
def connected?
|
254
|
+
end
|
254
255
|
end
|
255
256
|
|
256
257
|
module ConsumerTesting
|
@@ -20,7 +20,7 @@ module FastlyNsq
|
|
20
20
|
else
|
21
21
|
{
|
22
22
|
tls_v1: true,
|
23
|
-
tls_options: @context
|
23
|
+
tls_options: @context
|
24
24
|
}
|
25
25
|
end
|
26
26
|
end
|
@@ -28,19 +28,19 @@ module FastlyNsq
|
|
28
28
|
private
|
29
29
|
|
30
30
|
def env_key
|
31
|
-
ENV.fetch(
|
31
|
+
ENV.fetch("NSQ_SSL_KEY", nil)
|
32
32
|
end
|
33
33
|
|
34
34
|
def env_certificate
|
35
|
-
ENV.fetch(
|
35
|
+
ENV.fetch("NSQ_SSL_CERTIFICATE", nil)
|
36
36
|
end
|
37
37
|
|
38
38
|
def env_ca_certificate
|
39
|
-
ENV.fetch(
|
39
|
+
ENV.fetch("NSQ_SSL_CA_CERTIFICATE", nil)
|
40
40
|
end
|
41
41
|
|
42
42
|
def verify_mode
|
43
|
-
ENV.fetch(
|
43
|
+
ENV.fetch("NSQ_SSL_VERIFY_MODE", nil)
|
44
44
|
end
|
45
45
|
|
46
46
|
def env_default_hash
|
@@ -48,7 +48,7 @@ module FastlyNsq
|
|
48
48
|
key: env_key,
|
49
49
|
certificate: env_certificate,
|
50
50
|
ca_certificate: env_ca_certificate,
|
51
|
-
verify_mode: verify_mode
|
51
|
+
verify_mode: verify_mode
|
52
52
|
}
|
53
53
|
end
|
54
54
|
|
data/lib/fastly_nsq/version.rb
CHANGED
data/lib/fastly_nsq.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
3
|
+
require "nsq"
|
4
|
+
require "concurrent"
|
5
|
+
require "fc"
|
6
|
+
require "set"
|
7
|
+
require "logger"
|
8
|
+
require "forwardable"
|
9
|
+
require "digest/md5"
|
10
10
|
|
11
11
|
module FastlyNsq
|
12
12
|
NotConnectedError = Class.new(StandardError)
|
@@ -58,7 +58,7 @@ module FastlyNsq
|
|
58
58
|
def logger
|
59
59
|
return @logger if @logger
|
60
60
|
|
61
|
-
self.logger = Logger.new(
|
61
|
+
self.logger = Logger.new($stderr)
|
62
62
|
end
|
63
63
|
|
64
64
|
##
|
@@ -112,21 +112,58 @@ module FastlyNsq
|
|
112
112
|
# @return [Integer]
|
113
113
|
# @see https://nsq.io/components/nsqd.html#command-line-options
|
114
114
|
def max_req_timeout
|
115
|
-
@max_req_timeout ||= ENV.fetch(
|
115
|
+
@max_req_timeout ||= ENV.fetch("MAX_REQ_TIMEOUT", 60 * 60 * 1_000).to_i
|
116
116
|
end
|
117
117
|
|
118
118
|
# Maximum number of threads for FastlyNsq::PriorityThreadPool
|
119
119
|
# Default setting is 5 and can be set via ENV['MAX_PROCESSING_POOL_THREADS']
|
120
120
|
# @return [Integer]
|
121
121
|
def max_processing_pool_threads
|
122
|
-
@max_processing_pool_threads ||= ENV.fetch(
|
122
|
+
@max_processing_pool_threads ||= ENV.fetch("MAX_PROCESSING_POOL_THREADS", 5).to_i
|
123
123
|
end
|
124
124
|
|
125
125
|
##
|
126
126
|
# Return an array of NSQ lookupd http addresses sourced from ENV['NSQLOOKUPD_HTTP_ADDRESS']
|
127
127
|
# @return [Array<String>] list of nsqlookupd http addresses
|
128
128
|
def lookupd_http_addresses
|
129
|
-
ENV.fetch(
|
129
|
+
@lookups ||= ENV.fetch("NSQLOOKUPD_HTTP_ADDRESS", "").split(/, ?|\s+/).map(&:strip)
|
130
|
+
end
|
131
|
+
|
132
|
+
##
|
133
|
+
# Set the lookupd_http_addresses
|
134
|
+
# @param lookups [Array] List of http lookupd addresses to use.
|
135
|
+
def lookupd_http_addresses=(lookups)
|
136
|
+
@lookups = lookups.nil? ? nil : Array(lookups)
|
137
|
+
end
|
138
|
+
|
139
|
+
##
|
140
|
+
# Return an array of NSQD TCP addresses for NSQ consumers. Defaults to the value of ENV['NSQD_CONSUMERS'].
|
141
|
+
# ENV['NSQD_CONSUMERS'] must be a comma or space seperated string of NSQD addresses
|
142
|
+
# @return [Array<String>] list of nsqd addresses
|
143
|
+
def consumer_nsqds
|
144
|
+
@consumer_nsqds ||= ENV.fetch("NSQD_CONSUMERS", "").split(/, ?|\s+/).map(&:strip)
|
145
|
+
end
|
146
|
+
|
147
|
+
##
|
148
|
+
# Set the consumer_nsqd addresses
|
149
|
+
# @param nsqd_addresses [Array] List of consumer nsqd addresses to use
|
150
|
+
def consumer_nsqds=(nsqd_addresses)
|
151
|
+
@consumer_nsqds = nsqd_addresses.nil? ? nil : Array(nsqd_addresses)
|
152
|
+
end
|
153
|
+
|
154
|
+
##
|
155
|
+
# Return an array of NSQD TCP addresses for NSQ producers. Defaults to the value of ENV['NSQD_PRODUCERS'].
|
156
|
+
# ENV['NSQD_PRODUCERS'] must be a comma or space seperated string of NSQD addresses
|
157
|
+
# @return [Array<String>] list of nsqd addresses
|
158
|
+
def producer_nsqds
|
159
|
+
@producer_nsqds ||= ENV.fetch("NSQD_PRODUCERS", "").split(/, ?|\s+/).map(&:strip)
|
160
|
+
end
|
161
|
+
|
162
|
+
##
|
163
|
+
# Set the producer_nsqd addresses
|
164
|
+
# @param nsqd_addresses [Array] List of producer nsqd addresses to use
|
165
|
+
def producer_nsqds=(nsqd_addresses)
|
166
|
+
@producer_nsqds = nsqd_addresses.nil? ? nil : Array(nsqd_addresses)
|
130
167
|
end
|
131
168
|
|
132
169
|
# Register a block to run at a point in the lifecycle.
|
@@ -151,11 +188,9 @@ module FastlyNsq
|
|
151
188
|
def fire_event(event)
|
152
189
|
blocks = FastlyNsq.events.fetch(event)
|
153
190
|
blocks.each do |block|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
logger.error "[#{event}] #{e.inspect}"
|
158
|
-
end
|
191
|
+
block.call
|
192
|
+
rescue => e
|
193
|
+
logger.error "[#{event}] #{e.inspect}"
|
159
194
|
end
|
160
195
|
end
|
161
196
|
|
@@ -168,16 +203,16 @@ module FastlyNsq
|
|
168
203
|
end
|
169
204
|
end
|
170
205
|
|
171
|
-
require
|
172
|
-
require
|
173
|
-
require
|
174
|
-
require
|
175
|
-
require
|
176
|
-
require
|
177
|
-
require
|
178
|
-
require
|
179
|
-
require
|
180
|
-
require
|
181
|
-
require
|
182
|
-
require
|
183
|
-
require
|
206
|
+
require "fastly_nsq/consumer"
|
207
|
+
require "fastly_nsq/feeder"
|
208
|
+
require "fastly_nsq/launcher"
|
209
|
+
require "fastly_nsq/listener"
|
210
|
+
require "fastly_nsq/manager"
|
211
|
+
require "fastly_nsq/message"
|
212
|
+
require "fastly_nsq/messenger"
|
213
|
+
require "fastly_nsq/new_relic"
|
214
|
+
require "fastly_nsq/priority_queue"
|
215
|
+
require "fastly_nsq/priority_thread_pool"
|
216
|
+
require "fastly_nsq/producer"
|
217
|
+
require "fastly_nsq/tls_options"
|
218
|
+
require "fastly_nsq/version"
|
data/spec/cli_spec.rb
CHANGED
data/spec/consumer_spec.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
RSpec.describe FastlyNsq::Consumer do
|
6
|
-
let!(:topic)
|
7
|
-
let!(:channel) {
|
8
|
-
let!(:queue)
|
6
|
+
let!(:topic) { "fnsq" }
|
7
|
+
let!(:channel) { "fnsq" }
|
8
|
+
let!(:queue) { nil }
|
9
9
|
|
10
10
|
subject { described_class.new(topic: topic, channel: channel, queue: queue) }
|
11
11
|
|
@@ -16,15 +16,15 @@ RSpec.describe FastlyNsq::Consumer do
|
|
16
16
|
|
17
17
|
it { should be_connected }
|
18
18
|
|
19
|
-
it
|
19
|
+
it "should terminate" do
|
20
20
|
expect { subject.terminate }.to change(subject, :connected?).to(false)
|
21
21
|
end
|
22
22
|
|
23
|
-
describe
|
23
|
+
describe "with a specified queue" do
|
24
24
|
let!(:queue) { Queue.new }
|
25
25
|
|
26
|
-
it
|
27
|
-
message =
|
26
|
+
it "passes #queue to Nsq::Consumer" do
|
27
|
+
message = "foo"
|
28
28
|
|
29
29
|
FastlyNsq::Messenger.deliver(message: message, topic: topic)
|
30
30
|
|
@@ -41,19 +41,60 @@ RSpec.describe FastlyNsq::Consumer do
|
|
41
41
|
it { should delegate(:pop).to(:connection) }
|
42
42
|
it { should delegate(:pop_without_blocking).to(:connection) }
|
43
43
|
|
44
|
-
describe
|
45
|
-
let(:message) {
|
44
|
+
describe "with a message" do
|
45
|
+
let(:message) { "foo" }
|
46
46
|
|
47
47
|
before { FastlyNsq::Messenger.deliver(message: message, topic: topic) }
|
48
48
|
|
49
|
-
it
|
49
|
+
it "should not be empty" do
|
50
50
|
expect { subject }.to eventually(be_empty).within(15)
|
51
51
|
end
|
52
52
|
|
53
|
-
describe
|
53
|
+
describe "that has finished" do
|
54
54
|
before { subject.pop.finish }
|
55
55
|
|
56
56
|
it { should be_empty }
|
57
57
|
end
|
58
58
|
end
|
59
|
+
|
60
|
+
describe "connection priority" do
|
61
|
+
after do
|
62
|
+
FastlyNsq.lookupd_http_addresses = nil
|
63
|
+
FastlyNsq.consumer_nsqds = nil
|
64
|
+
end
|
65
|
+
|
66
|
+
it "connects to consumer_nsqds if provided" do
|
67
|
+
allow(Nsq::Consumer).to receive(:new)
|
68
|
+
|
69
|
+
expect(FastlyNsq.lookupd_http_addresses).not_to be_empty
|
70
|
+
expect(FastlyNsq.consumer_nsqds).not_to be_empty
|
71
|
+
|
72
|
+
FastlyNsq::Consumer.new(topic: topic, channel: channel)
|
73
|
+
expect(Nsq::Consumer).to have_received(:new).with a_hash_including(nsqd: FastlyNsq.consumer_nsqds).and(excluding(:nsqlookupd))
|
74
|
+
end
|
75
|
+
|
76
|
+
it "connects to lookupd_http_addresses if consumer_nsqds is empty" do
|
77
|
+
FastlyNsq.consumer_nsqds = []
|
78
|
+
allow(Nsq::Consumer).to receive(:new)
|
79
|
+
|
80
|
+
expect(FastlyNsq.lookupd_http_addresses).not_to be_empty
|
81
|
+
expect(FastlyNsq.consumer_nsqds).to be_empty
|
82
|
+
|
83
|
+
FastlyNsq::Consumer.new(topic: topic, channel: channel)
|
84
|
+
expect(Nsq::Consumer).to have_received(:new).with a_hash_including(nsqlookupd: FastlyNsq.lookupd_http_addresses).and(excluding(:nsqd))
|
85
|
+
end
|
86
|
+
|
87
|
+
it "raises when neither consumer_nsqds or lookupd_http_addresses are available" do
|
88
|
+
FastlyNsq.consumer_nsqds = []
|
89
|
+
FastlyNsq.lookupd_http_addresses = []
|
90
|
+
allow(Nsq::Consumer).to receive(:new)
|
91
|
+
|
92
|
+
expect(FastlyNsq.lookupd_http_addresses).to be_empty
|
93
|
+
expect(FastlyNsq.consumer_nsqds).to be_empty
|
94
|
+
|
95
|
+
expect { FastlyNsq::Consumer.new(topic: topic, channel: channel) }
|
96
|
+
.to raise_error(FastlyNsq::ConnectionFailed, "One of FastlyNsq.consumer_nsqds or FastlyNsq.lookupd_http_addresses must be present")
|
97
|
+
expect(Nsq::Consumer).not_to have_received(:new)
|
98
|
+
end
|
99
|
+
end
|
59
100
|
end
|
data/spec/fastly_nsq_spec.rb
CHANGED
@@ -1,89 +1,93 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
RSpec.describe FastlyNsq do
|
6
|
-
describe
|
6
|
+
describe "#configure" do
|
7
7
|
specify { expect { |b| described_class.configure(&b) }.to yield_with_args(described_class) }
|
8
8
|
end
|
9
9
|
|
10
|
-
describe
|
10
|
+
describe "#listen" do
|
11
11
|
let!(:default_channel) { subject.channel }
|
12
|
-
let!(:topic) {
|
12
|
+
let!(:topic) { "fnsq" }
|
13
13
|
|
14
|
-
before { subject.channel =
|
14
|
+
before { subject.channel = "fnsq" }
|
15
15
|
after { subject.channel = default_channel }
|
16
16
|
|
17
|
-
it
|
17
|
+
it "creates a listener" do
|
18
18
|
expect { subject.listen topic, ->(*) {} }.to change { subject.manager.topics }.to([topic])
|
19
19
|
end
|
20
20
|
|
21
|
-
it
|
21
|
+
it "creates a listener with a specific priority" do
|
22
22
|
listener = subject.listen topic, ->(*) {}, priority: 10
|
23
23
|
expect(listener.priority).to eq(10)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
-
describe
|
27
|
+
describe "#channel=" do
|
28
28
|
let!(:default_channel) { subject.channel }
|
29
29
|
after { subject.channel = default_channel }
|
30
30
|
|
31
|
-
it
|
31
|
+
it "allows the channel to be set and retrieved" do
|
32
32
|
expect(subject.channel).to be_nil
|
33
|
-
subject.channel =
|
34
|
-
expect(subject.channel).to eq(
|
33
|
+
subject.channel = "foo"
|
34
|
+
expect(subject.channel).to eq("foo")
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
describe
|
38
|
+
describe "#logger" do
|
39
39
|
let!(:default_logger) { subject.logger }
|
40
|
-
after
|
40
|
+
after do
|
41
|
+
subject.logger = default_logger
|
42
|
+
$stderr = STDERR
|
43
|
+
end
|
41
44
|
|
42
|
-
it
|
45
|
+
it "returns the set logger" do
|
43
46
|
logger = Logger.new(nil)
|
44
47
|
subject.logger = logger
|
45
48
|
|
46
49
|
expect(subject.logger).to eq logger
|
47
50
|
end
|
48
51
|
|
49
|
-
it
|
52
|
+
it "sets the default logger if none is set" do
|
53
|
+
$stderr = File.new("/dev/null", "w")
|
50
54
|
subject.instance_variable_set(:@logger, nil)
|
51
55
|
expect(subject.instance_variable_get(:@logger)).to be nil
|
52
56
|
logger = subject.logger
|
53
57
|
|
54
58
|
expect(logger).to be_instance_of(Logger)
|
55
|
-
expect(logger.instance_variable_get(:@logdev).dev).to eq(
|
59
|
+
expect(logger.instance_variable_get(:@logdev).dev).to eq($stderr)
|
56
60
|
expect(logger).to eq(Nsq.logger)
|
57
61
|
end
|
58
62
|
end
|
59
63
|
|
60
|
-
describe
|
64
|
+
describe "#logger=" do
|
61
65
|
let!(:default_logger) { subject.logger }
|
62
66
|
after { subject.logger = default_logger }
|
63
67
|
|
64
|
-
it
|
65
|
-
logger = Logger.new(
|
68
|
+
it "allows the logger to be set and retrieved" do
|
69
|
+
logger = Logger.new("/dev/null")
|
66
70
|
subject.logger = logger
|
67
71
|
|
68
72
|
expect(subject.logger).to eq logger
|
69
73
|
end
|
70
74
|
|
71
|
-
it
|
72
|
-
logger = Logger.new(
|
75
|
+
it "sets Nsq.logger" do
|
76
|
+
logger = Logger.new("/dev/null")
|
73
77
|
subject.logger = logger
|
74
78
|
|
75
79
|
expect(Nsq.logger).to eq logger
|
76
80
|
end
|
77
81
|
end
|
78
82
|
|
79
|
-
describe
|
80
|
-
it
|
83
|
+
describe "#manager" do
|
84
|
+
it "represents the active default manager" do
|
81
85
|
expect(subject.manager).not_to be_stopped
|
82
86
|
end
|
83
87
|
end
|
84
88
|
|
85
|
-
describe
|
86
|
-
it
|
89
|
+
describe "#manager=" do
|
90
|
+
it "transfers to specified manager" do
|
87
91
|
old_manager = subject.manager
|
88
92
|
new_manager = FastlyNsq::Manager.new
|
89
93
|
|
@@ -93,17 +97,89 @@ RSpec.describe FastlyNsq do
|
|
93
97
|
end
|
94
98
|
end
|
95
99
|
|
96
|
-
describe
|
97
|
-
|
98
|
-
|
100
|
+
describe "#lookupd_http_addresses" do
|
101
|
+
after { subject.instance_variable_set(:@lookups, nil) }
|
102
|
+
|
103
|
+
it "retreives NSQLOOKUPD_HTTP_ADDRESS by default" do
|
104
|
+
expect(subject.lookupd_http_addresses).to eq(ENV["NSQLOOKUPD_HTTP_ADDRESS"].split(","))
|
105
|
+
end
|
106
|
+
|
107
|
+
it "returns the value of the instance variable" do
|
108
|
+
subject.instance_variable_set(:@lookups, ["lolcathost"])
|
109
|
+
|
110
|
+
expect(subject.lookupd_http_addresses).to eq(["lolcathost"])
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "#lookupd_http_addresses=" do
|
115
|
+
let!(:default_loookups) { subject.lookupd_http_addresses }
|
116
|
+
after { subject.lookupd_http_addresses = default_loookups }
|
117
|
+
|
118
|
+
it "allows the lookups to be set and retrieved" do
|
119
|
+
lookups = ["lolcathost:1234"]
|
120
|
+
subject.lookupd_http_addresses = lookups
|
121
|
+
|
122
|
+
expect(subject.lookupd_http_addresses).to eq lookups
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "#producer_nsqds" do
|
127
|
+
after { subject.instance_variable_set(:@producer_nsqds, nil) }
|
128
|
+
|
129
|
+
it "retreives NSQD_PRODUCERS by default" do
|
130
|
+
expect(subject.producer_nsqds).to eq(ENV["NSQD_PRODUCERS"].split(","))
|
131
|
+
end
|
132
|
+
|
133
|
+
it "returns the value of the instance variable" do
|
134
|
+
subject.instance_variable_set(:@producer_nsqds, ["producer:1234"])
|
135
|
+
|
136
|
+
expect(subject.producer_nsqds).to eq(["producer:1234"])
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "#producer_nsqds=" do
|
141
|
+
let!(:default_producers) { subject.producer_nsqds }
|
142
|
+
after { subject.producer_nsqds = default_producers }
|
143
|
+
|
144
|
+
it "allows the producer_nsqds to be set and retrieved" do
|
145
|
+
producers = ["producer:1234"]
|
146
|
+
subject.producer_nsqds = producers
|
147
|
+
|
148
|
+
expect(subject.producer_nsqds).to eq producers
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
describe "#consumer_nsqds" do
|
153
|
+
after { subject.instance_variable_set(:@consumer_nsqds, nil) }
|
154
|
+
|
155
|
+
it "retreives NSQD_CONSUMERS by default" do
|
156
|
+
expect(subject.consumer_nsqds).to eq(ENV["NSQD_CONSUMERS"].split(","))
|
157
|
+
end
|
158
|
+
|
159
|
+
it "returns the value of the instance variable" do
|
160
|
+
subject.instance_variable_set(:@consumer_nsqds, ["consumer:1234"])
|
161
|
+
|
162
|
+
expect(subject.consumer_nsqds).to eq(["consumer:1234"])
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe "#consumer_nsqds=" do
|
167
|
+
let!(:default_consumers) { subject.consumer_nsqds }
|
168
|
+
after { subject.consumer_nsqds = default_consumers }
|
169
|
+
|
170
|
+
it "allows the consumer_nsqds to be set and retrieved" do
|
171
|
+
consumers = ["consumer:1234"]
|
172
|
+
subject.consumer_nsqds = consumers
|
173
|
+
|
174
|
+
expect(subject.consumer_nsqds).to eq consumers
|
99
175
|
end
|
100
176
|
end
|
101
177
|
|
102
|
-
describe
|
178
|
+
describe "#on" do
|
103
179
|
before { FastlyNsq.events.each { |(_, v)| v.clear } }
|
104
|
-
after
|
180
|
+
after { FastlyNsq.events.each { |(_, v)| v.clear } }
|
105
181
|
|
106
|
-
it
|
182
|
+
it "registers callbacks for events" do
|
107
183
|
%i[startup shutdown heartbeat].each do |event|
|
108
184
|
block = -> {}
|
109
185
|
FastlyNsq.on(event, &block)
|
@@ -111,7 +187,7 @@ RSpec.describe FastlyNsq do
|
|
111
187
|
end
|
112
188
|
end
|
113
189
|
|
114
|
-
it
|
190
|
+
it "limits callback registration to valid events" do
|
115
191
|
expect { FastlyNsq.on(:foo, &-> {}) }.to raise_error(ArgumentError, /Invalid event name/)
|
116
192
|
end
|
117
193
|
end
|
data/spec/feeder_spec.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
RSpec.describe FastlyNsq::Feeder do
|
6
|
-
describe
|
7
|
-
it
|
6
|
+
describe "#push" do
|
7
|
+
it "sends message to processor with the specified priority" do
|
8
8
|
messages = []
|
9
9
|
processor = ->(m) { messages << m }
|
10
10
|
priority = 5
|
11
|
-
message =
|
11
|
+
message = "foo"
|
12
12
|
|
13
13
|
feeder = described_class.new(processor, priority)
|
14
14
|
|