fastly_nsq 1.17.1 → 1.18.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6973f2a953a18f9f5b63d6e7225c94264f56485b4632a409c820b5b61f91b050
4
- data.tar.gz: 1a8f39f1085191666a6ebccff53a8c6f691fad4770bea6a48affc8f7998a878b
3
+ metadata.gz: 7c499298a1a690208afc03693e6bdae6730408f7e61544033f5b6fdbab778364
4
+ data.tar.gz: 3c346c1bcb3a7d44508144beb14b91edad7f8bcb2cb07e35cd8a301a67069e57
5
5
  SHA512:
6
- metadata.gz: da7997c5cdc6b8cb7d5feeacb0feab24c062076c92b8a3b23ddb49416d5a1fe57d0c241a68b20b5aed15c3a2c6cad89015557268dd2827fa49a7c63236de6300
7
- data.tar.gz: 007a732d613f15d099e28f301db5537d35cfe7675d45f46946526eb275b5196496520a1b2f0aaf3c011414b0b55eafa19218f5f71cf6a9fb42ea59678fc11b1b
6
+ metadata.gz: b350e9fd2f0d2c993aa3d3a84b24935137faa7cbf65967f0342434c108e7b13213b3dd45b753c2e73de3e487729f2038a8a07045ef1987c1d3e5d55d78797ee3
7
+ data.tar.gz: a2f4494d86c8d5d37db0ed59e149edebfecee56888971cc2e1cfecff5d88fbcbd05e65a72cfe5d1d1e6d8758f88296a20b877d43a8ebed06d663519070e6f7fd
data/.env CHANGED
@@ -1,4 +1,4 @@
1
- NSQD_TCP_ADDRESS=127.0.0.1:4150
2
1
  NSQD_HTTP_ADDRESS=127.0.0.1:4151
3
- NSQLOOKUPD_TCP_ADDRESS=127.0.0.1:4160
4
2
  NSQLOOKUPD_HTTP_ADDRESS=127.0.0.1:4161
3
+ NSQD_CONSUMERS=127.0.0.1:4150
4
+ NSQD_PRODUCERS=127.0.0.1:4150
data/ChangeLog.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Change Log
2
2
 
3
+ ## [v1.18.0](https://github.com/fastly/fastly_nsq/tree/v1.18.0) (2022-03-04)
4
+ [Full Changelog](https://github.com/fastly/fastly_nsq/compare/v1.17.1...v1.18.0)
5
+
6
+ **Merged pull requests:**
7
+
8
+ - Configurable NSQDs for Consumers and Producers [\#105](https://github.com/fastly/fastly_nsq/pull/105)
9
+
3
10
  ## [v1.17.1](https://github.com/fastly/fastly_nsq/tree/v1.17.1) (2022-02-23)
4
11
  [Full Changelog](https://github.com/fastly/fastly_nsq/compare/v1.17.0...v1.17.1)
5
12
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # fastly_nsq [![Build Status](https://travis-ci.org/fastly/fastly_nsq.svg?branch=main)](https://travis-ci.org/fastly/fastly_nsq)
1
+ # fastly_nsq [![Build Status](https://travis-ci.com/fastly/fastly_nsq.svg?branch=main)](https://travis-ci.com/fastly/fastly_nsq)
2
2
 
3
3
  NSQ adapter and testing objects
4
4
  for using the NSQ messaging system
@@ -12,9 +12,9 @@ We also include fakes
12
12
  to make testing easier.
13
13
 
14
14
  This library is dependent
15
- on the [`nsq-ruby`] gem.
15
+ on the [`nsq-ruby-fastly`] gem.
16
16
 
17
- [`nsq-ruby`]: https://github.com/wistia/nsq-ruby
17
+ [`nsq-ruby-fastly`]: https://github.com/fastly/nsq-ruby
18
18
 
19
19
  Please use [GitHub Issues] to report bugs.
20
20
 
@@ -34,6 +34,65 @@ and `bundle install`.
34
34
 
35
35
  ## Usage
36
36
 
37
+ ### Connections
38
+
39
+ NSQD cconnections can be discovered via nsqlookupd's or
40
+ specified explicity for consumers and producers.
41
+
42
+ #### Using nsqlookup:
43
+
44
+ Set the ENV variable to a comma sepearated string of lookups:
45
+
46
+ ```
47
+ ENV['NSQLOOKUPD_HTTP_ADDRESS'] = "lookup01:1234,lookup01:1234"
48
+ ```
49
+
50
+ Or configure them directly:
51
+
52
+ ```
53
+ FastlyNsq.configure do |config|
54
+ config.lookupd_http_addresses = ["lookup01:1234", "lookup02:1234"]
55
+ end
56
+ ```
57
+
58
+ #### Using nsqd directly:
59
+
60
+ NSQD connections can be specified for consumers and producers. Being able to set
61
+ different sets for consumers and producers facilitates removing and adding new instances
62
+ without downtime.
63
+
64
+ Set the following ENV variables to a comma sepearted string of nsqds:
65
+
66
+ ```
67
+ ENV['NSQD_CONSUMERS']="nsqd01:4150,nsd02:4150"
68
+ ENV['NSQD_PRODUCERS']="nsqd01:4150,nsd02:4150"
69
+ ```
70
+
71
+ Or configure them directly:
72
+
73
+ ```
74
+ FastlyNsq.configure do |config|
75
+ config.consumer_nsqds = ["nsqd01:4150", "nsqd02:4150"]
76
+ config.producer_nsqds = ["nsqd01:4150", "nsqd02:4150"]
77
+ end
78
+ ```
79
+
80
+ ### Connection Priority
81
+
82
+ When `FastlyNsq.consumer_nsqds` or `FastlyNsq.producer_nsqds` are set they
83
+ will be used instead of `FastlyNsq.lookupd_http_addresses`.
84
+
85
+ ### TLS
86
+
87
+ Set the following ENV variables to enable TLS support:
88
+
89
+ ```
90
+ NSQ_SSL_KEY
91
+ NSQ_SSL_CERTIFICATE
92
+ NSQ_SSL_CA_CERTIFICATE
93
+ NSQ_SSL_VERIFY_MODE (optional)
94
+ ```
95
+
37
96
  ### `FastlyNsq::Producer`
38
97
 
39
98
  This is a class
@@ -50,7 +109,6 @@ message_data = {
50
109
  }
51
110
 
52
111
  producer = FastlyNsq::Producer.new(
53
- nsqd: ENV.fetch('NSQD_TCP_ADDRESS'),
54
112
  topic: topic,
55
113
  )
56
114
 
@@ -281,6 +339,22 @@ expect(some_result)
281
339
 
282
340
  ## Configuration
283
341
 
342
+ See the [documentation](https://www.rubydoc.info/gems/fastly_nsq/FastlyNsq) for additional settings
343
+
344
+ Example:
345
+
346
+ ```
347
+ FastlyNsq.configure do |config|
348
+ config.channel = "z"
349
+ config.producer_nsqds = ["nsqd01:4150", "nsqd02:4150"]
350
+ config.lookupd_http_addresses = ["lookupd01:4161", "lookupd02:4161"]
351
+ config.logger = Logger.new(STDOUT)
352
+ config.max_attempts = 10
353
+ config.max_req_timeout = 10_000
354
+ config.max_processing_pool_threads = 42
355
+ end
356
+ ```
357
+
284
358
  ### Environment Variables
285
359
 
286
360
  The URLs for the various
@@ -293,10 +367,10 @@ stock NSQ on OS X,
293
367
  installed via Homebrew:
294
368
 
295
369
  ```shell
296
- NSQD_TCP_ADDRESS='127.0.0.1:4150'
297
370
  NSQD_HTTP_ADDRESS='127.0.0.1:4151'
298
- NSQLOOKUPD_TCP_ADDRESS='127.0.0.1:4160'
299
371
  NSQLOOKUPD_HTTP_ADDRESS='127.0.0.1:4161, 10.1.1.101:4161'
372
+ NSQD_CONSUMERS='127.0.0.1:4150'
373
+ NSQD_PRODUCERS='127.0.0.1:4150'
300
374
  ```
301
375
 
302
376
  See the [`.sample.env`](examples/.sample.env) file
@@ -61,6 +61,9 @@ class FastlyNsq::Consumer
61
61
  ##
62
62
  # Create a FastlyNsq::Consumer
63
63
  #
64
+ # Will connect to NSQDs in this priority: 1. direct from FastlyNsq.consumer_nsqds 2. discovered via FastlyNsq.lookupd_http_addresses.
65
+ # If both `consumer_nsqds` and `lookupd_http_addresses` are set only the FastlyNsq.consumer_nsqds will be used.
66
+ #
64
67
  # @param topic [String] NSQ topic from which to consume
65
68
  # @param channel [String] NSQ channel from which to consume
66
69
  # @param queue [#pop, #size] Queue object, most likely an instance of {FastlyNsq::Feeder}
@@ -99,15 +102,25 @@ class FastlyNsq::Consumer
99
102
  attr_reader :tls_options
100
103
 
101
104
  def connect(queue, **options)
102
- Nsq::Consumer.new(
103
- {
104
- nsqlookupd: FastlyNsq.lookupd_http_addresses,
105
- topic: topic,
106
- channel: channel,
107
- queue: queue,
108
- max_attempts: max_attempts,
109
- **options
110
- }.merge(tls_options)
111
- )
105
+ consumers = FastlyNsq.consumer_nsqds
106
+ lookupd = FastlyNsq.lookupd_http_addresses
107
+
108
+ opts = {
109
+ topic: topic,
110
+ channel: channel,
111
+ queue: queue,
112
+ max_attempts: max_attempts,
113
+ **options
114
+ }.merge(tls_options)
115
+
116
+ if !consumers.empty?
117
+ opts[:nsqd] = consumers
118
+ elsif !lookupd.empty?
119
+ opts[:nsqlookupd] = lookupd
120
+ else
121
+ raise FastlyNsq::ConnectionFailed, "One of FastlyNsq.consumer_nsqds or FastlyNsq.lookupd_http_addresses must be present"
122
+ end
123
+
124
+ Nsq::Consumer.new(opts)
112
125
  end
113
126
  end
@@ -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
@@ -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
- @connection ||= Nsq::Producer.new(
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 RUBY_VERSION > "2.4.0"
86
- timeout_args << "Failed connection to #{lookupd} within #{connect_timeout} seconds"
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FastlyNsq
4
- VERSION = "1.17.1"
4
+ VERSION = "1.18.0"
5
5
  end
data/lib/fastly_nsq.rb CHANGED
@@ -126,7 +126,44 @@ module FastlyNsq
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("NSQLOOKUPD_HTTP_ADDRESS").split(",").map(&:strip)
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.
@@ -56,4 +56,45 @@ RSpec.describe FastlyNsq::Consumer do
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
@@ -37,7 +37,10 @@ RSpec.describe FastlyNsq do
37
37
 
38
38
  describe "#logger" do
39
39
  let!(:default_logger) { subject.logger }
40
- after { subject.logger = default_logger }
40
+ after do
41
+ subject.logger = default_logger
42
+ $stderr = STDERR
43
+ end
41
44
 
42
45
  it "returns the set logger" do
43
46
  logger = Logger.new(nil)
@@ -47,6 +50,7 @@ RSpec.describe FastlyNsq do
47
50
  end
48
51
 
49
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
@@ -62,14 +66,14 @@ RSpec.describe FastlyNsq do
62
66
  after { subject.logger = default_logger }
63
67
 
64
68
  it "allows the logger to be set and retrieved" do
65
- logger = Logger.new($stdout)
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
75
  it "sets Nsq.logger" do
72
- logger = Logger.new($stdout)
76
+ logger = Logger.new("/dev/null")
73
77
  subject.logger = logger
74
78
 
75
79
  expect(Nsq.logger).to eq logger
@@ -94,9 +98,81 @@ RSpec.describe FastlyNsq do
94
98
  end
95
99
 
96
100
  describe "#lookupd_http_addresses" do
97
- it "retreives NSQLOOKUPD_HTTP_ADDRESS" do
101
+ after { subject.instance_variable_set(:@lookups, nil) }
102
+
103
+ it "retreives NSQLOOKUPD_HTTP_ADDRESS by default" do
98
104
  expect(subject.lookupd_http_addresses).to eq(ENV["NSQLOOKUPD_HTTP_ADDRESS"].split(","))
99
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
175
+ end
100
176
  end
101
177
 
102
178
  describe "#on" do
@@ -96,7 +96,7 @@ RSpec.describe FastlyNsq::Listener do
96
96
  describe "#call" do
97
97
  it "processes a message" do
98
98
  body = {"foo" => "bar"}
99
- message = spy("message", body: JSON.dump(body))
99
+ message = spy("message", body: JSON.dump(body), attempts: 1, id: 1)
100
100
  expect { subject.call(message) }.to change { messages }.to([body])
101
101
  end
102
102
 
@@ -104,7 +104,7 @@ RSpec.describe FastlyNsq::Listener do
104
104
  let(:processor) { ->(_) { true } }
105
105
 
106
106
  it "finishes the message" do
107
- message = spy("message", body: "{}")
107
+ message = spy("message", body: "{}", attempts: 1, id: 1)
108
108
  subject.call(message)
109
109
 
110
110
  expect(message).to have_received(:finish)
@@ -115,7 +115,7 @@ RSpec.describe FastlyNsq::Listener do
115
115
  let(:processor) { ->(_) { false } }
116
116
 
117
117
  it "finishes the message" do
118
- message = spy("message", body: "{}")
118
+ message = spy("message", body: "{}", attempts: 1, id: 1)
119
119
  subject.call(message)
120
120
 
121
121
  expect(message).not_to have_received(:finish)
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "spec_helper"
4
+ RSpec::Matchers.define_negated_matcher :excluding, :include
4
5
 
5
6
  RSpec.describe FastlyNsq::Producer do
6
7
  let!(:topic) { "fnsq" }
@@ -42,12 +43,50 @@ RSpec.describe FastlyNsq::Producer do
42
43
  logger = spy("logger")
43
44
  expect(logger).to receive(:error).and_return("Producer for #{topic} failed to connect!")
44
45
 
45
- if RUBY_VERSION > "2.4.0"
46
- expect { FastlyNsq::Producer.new(topic: topic, logger: logger, connect_timeout: 0.2) }
47
- .to raise_error(FastlyNsq::ConnectionFailed, match(FastlyNsq.lookupd_http_addresses.inspect))
48
- else
49
- expect { FastlyNsq::Producer.new(topic: topic, logger: logger, connect_timeout: 0.2) }
50
- .to raise_error(FastlyNsq::ConnectionFailed)
46
+ expect { FastlyNsq::Producer.new(topic: topic, logger: logger, connect_timeout: 0.2) }
47
+ .to raise_error(FastlyNsq::ConnectionFailed, match(FastlyNsq.lookupd_http_addresses.inspect))
48
+ end
49
+
50
+ describe "connection priority" do
51
+ after do
52
+ FastlyNsq.lookupd_http_addresses = nil
53
+ FastlyNsq.producer_nsqds = nil
54
+ end
55
+
56
+ it "connects to producer_nsqds if provided" do
57
+ connection = double "FastlyNsq::Producer", connected?: true
58
+ allow(Nsq::Producer).to receive(:new).and_return(connection)
59
+
60
+ expect(FastlyNsq.lookupd_http_addresses).not_to be_empty
61
+ expect(FastlyNsq.producer_nsqds).not_to be_empty
62
+
63
+ FastlyNsq::Producer.new(topic: topic)
64
+ expect(Nsq::Producer).to have_received(:new).with a_hash_including(nsqd: FastlyNsq.producer_nsqds).and(excluding(:nsqlookupd))
65
+ end
66
+
67
+ it "connects to lookupd_http_addresses if producer_nsqds is empty" do
68
+ FastlyNsq.producer_nsqds = []
69
+ connection = double "FastlyNsq::Producer", connected?: true
70
+ allow(Nsq::Producer).to receive(:new).and_return(connection)
71
+
72
+ expect(FastlyNsq.lookupd_http_addresses).not_to be_empty
73
+ expect(FastlyNsq.producer_nsqds).to be_empty
74
+
75
+ FastlyNsq::Producer.new(topic: topic)
76
+ expect(Nsq::Producer).to have_received(:new).with a_hash_including(nsqlookupd: FastlyNsq.lookupd_http_addresses).and(excluding(:nsqd))
77
+ end
78
+
79
+ it "raises when neither producer_nsqds or lookupd_http_addresses are available" do
80
+ FastlyNsq.producer_nsqds = []
81
+ FastlyNsq.lookupd_http_addresses = []
82
+ allow(Nsq::Producer).to receive(:new)
83
+
84
+ expect(FastlyNsq.lookupd_http_addresses).to be_empty
85
+ expect(FastlyNsq.producer_nsqds).to be_empty
86
+
87
+ expect { FastlyNsq::Producer.new(topic: topic) }
88
+ .to raise_error(FastlyNsq::ConnectionFailed, "One of FastlyNsq.producer_nsqds or FastlyNsq.lookupd_http_addresses must be present")
89
+ expect(Nsq::Producer).not_to have_received(:new)
51
90
  end
52
91
  end
53
92
 
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: 1.17.1
4
+ version: 1.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tommy O'Neil
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2022-02-23 00:00:00.000000000 Z
16
+ date: 2022-03-04 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: awesome_print