fastly_nsq 1.16.0 → 1.18.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/spec/http/nsqd_spec.rb
CHANGED
@@ -1,77 +1,77 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "spec_helper"
|
4
|
+
require "fastly_nsq/http/nsqd"
|
5
5
|
|
6
6
|
RSpec.describe FastlyNsq::Http::Nsqd, :webmock do
|
7
|
-
let(:base_uri) {
|
7
|
+
let(:base_uri) { "http://example.com" }
|
8
8
|
|
9
|
-
it
|
9
|
+
it "makes simple get requests" do
|
10
10
|
%w[ping info config/nsqlookupd_tcp_addresses].each do |api|
|
11
11
|
url = "#{base_uri}/#{api}"
|
12
12
|
stub_request(:get, url)
|
13
|
-
FastlyNsq::Http::Nsqd.send(api.tr(
|
13
|
+
FastlyNsq::Http::Nsqd.send(api.tr("/", "_").to_sym, base_uri: base_uri)
|
14
14
|
|
15
15
|
expect(a_request(:get, url)).to have_been_requested
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
describe
|
20
|
-
it
|
19
|
+
describe "stats" do
|
20
|
+
it "can fetch stats" do
|
21
21
|
url = "#{base_uri}/stats?topic=lol&channel=foo&format=json"
|
22
22
|
stub_request(:get, url)
|
23
|
-
data = {
|
23
|
+
data = {topic: "lol", channel: "foo", format: "json"}
|
24
24
|
|
25
|
-
FastlyNsq::Http::Nsqd.stats(topic:
|
25
|
+
FastlyNsq::Http::Nsqd.stats(topic: "lol", channel: "foo", base_uri: base_uri)
|
26
26
|
|
27
27
|
expect(a_request(:get, url).with(query: data)).to have_been_requested
|
28
28
|
end
|
29
29
|
|
30
|
-
it
|
30
|
+
it "raises InvaildFormatError if provided format is not in list" do
|
31
31
|
expect do
|
32
|
-
FastlyNsq::Http::Nsqd.stats(format:
|
32
|
+
FastlyNsq::Http::Nsqd.stats(format: "foo")
|
33
33
|
end.to raise_error(FastlyNsq::Http::Nsqd::InvalidFormatError)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
-
it
|
37
|
+
it "can publish messages" do
|
38
38
|
url = "#{base_uri}/pub?topic=lol&defer=999"
|
39
39
|
stub_request(:post, url)
|
40
|
-
data = {
|
40
|
+
data = {topic: "lol", defer: 999}
|
41
41
|
|
42
|
-
FastlyNsq::Http::Nsqd.pub(topic:
|
42
|
+
FastlyNsq::Http::Nsqd.pub(topic: "lol", defer: 999, message: "SOMETHING", base_uri: base_uri)
|
43
43
|
|
44
|
-
expect(a_request(:post, url).with(query: data, body:
|
44
|
+
expect(a_request(:post, url).with(query: data, body: "SOMETHING")).to have_been_requested
|
45
45
|
end
|
46
46
|
|
47
|
-
it
|
47
|
+
it "can publish multiple messages" do
|
48
48
|
url = "#{base_uri}/mpub?topic=lol&binary=false"
|
49
49
|
stub_request(:post, url)
|
50
|
-
data = {
|
50
|
+
data = {topic: "lol"}
|
51
51
|
body = "ONE MESSAGE\nTWO MESSAGE\nRED MESSAGE\nBLUE MESSAGE"
|
52
52
|
|
53
|
-
FastlyNsq::Http::Nsqd.mpub(topic:
|
53
|
+
FastlyNsq::Http::Nsqd.mpub(topic: "lol", message: body, base_uri: base_uri)
|
54
54
|
|
55
55
|
expect(a_request(:post, url).with(query: data, body: body)).to have_been_requested
|
56
56
|
end
|
57
57
|
|
58
|
-
it
|
58
|
+
it "can create, delete, empty, pause and unpause topics and channels" do
|
59
59
|
verbs = %w[create delete empty pause unpause]
|
60
60
|
|
61
61
|
verbs.each do |verb|
|
62
62
|
url = "#{base_uri}/topic/#{verb}?topic=lol"
|
63
63
|
stub_request(:post, url)
|
64
|
-
data = {
|
64
|
+
data = {topic: "lol"}
|
65
65
|
|
66
|
-
FastlyNsq::Http::Nsqd.send("topic_#{verb}".to_sym, topic:
|
66
|
+
FastlyNsq::Http::Nsqd.send("topic_#{verb}".to_sym, topic: "lol", base_uri: base_uri)
|
67
67
|
|
68
68
|
expect(a_request(:post, url).with(query: data)).to have_been_requested
|
69
69
|
|
70
70
|
url = "#{base_uri}/channel/#{verb}?topic=lol&channel=foo"
|
71
71
|
stub_request(:post, url)
|
72
|
-
data = {
|
72
|
+
data = {topic: "lol", channel: "foo"}
|
73
73
|
|
74
|
-
FastlyNsq::Http::Nsqd.send("channel_#{verb}".to_sym, topic:
|
74
|
+
FastlyNsq::Http::Nsqd.send("channel_#{verb}".to_sym, topic: "lol", channel: "foo", base_uri: base_uri)
|
75
75
|
|
76
76
|
expect(a_request(:post, url).with(query: data)).to have_been_requested
|
77
77
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "spec_helper"
|
4
|
+
require "fastly_nsq/http/nsqlookupd"
|
5
5
|
|
6
6
|
RSpec.describe FastlyNsq::Http::Nsqlookupd, :webmock do
|
7
|
-
let(:base_uri) {
|
7
|
+
let(:base_uri) { "http://example.com" }
|
8
8
|
|
9
|
-
it
|
9
|
+
it "makes simple get requests" do
|
10
10
|
%w[topics nodes ping info].each do |api|
|
11
11
|
url = "#{base_uri}/#{api}"
|
12
12
|
stub_request(:get, url)
|
@@ -16,52 +16,52 @@ RSpec.describe FastlyNsq::Http::Nsqlookupd, :webmock do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
it
|
19
|
+
it "can lookup producers for a topic" do
|
20
20
|
url = "#{base_uri}/lookup?topic=lol"
|
21
21
|
stub_request(:get, url)
|
22
|
-
data = {
|
22
|
+
data = {topic: "lol"}
|
23
23
|
|
24
|
-
FastlyNsq::Http::Nsqlookupd.lookup(topic:
|
24
|
+
FastlyNsq::Http::Nsqlookupd.lookup(topic: "lol", base_uri: base_uri)
|
25
25
|
|
26
26
|
expect(a_request(:get, url).with(query: data)).to have_been_requested
|
27
27
|
end
|
28
28
|
|
29
|
-
it
|
29
|
+
it "can lookup channels for a topic" do
|
30
30
|
url = "#{base_uri}/channels?topic=lol"
|
31
31
|
stub_request(:get, url)
|
32
|
-
data = {
|
32
|
+
data = {topic: "lol"}
|
33
33
|
|
34
|
-
FastlyNsq::Http::Nsqlookupd.channels(topic:
|
34
|
+
FastlyNsq::Http::Nsqlookupd.channels(topic: "lol", base_uri: base_uri)
|
35
35
|
|
36
36
|
expect(a_request(:get, url).with(query: data)).to have_been_requested
|
37
37
|
end
|
38
38
|
|
39
|
-
it
|
39
|
+
it "can delete a topic" do
|
40
40
|
url = "#{base_uri}/delete_topic?topic=lol"
|
41
41
|
stub_request(:get, url)
|
42
|
-
data = {
|
42
|
+
data = {topic: "lol"}
|
43
43
|
|
44
|
-
FastlyNsq::Http::Nsqlookupd.delete_topic(topic:
|
44
|
+
FastlyNsq::Http::Nsqlookupd.delete_topic(topic: "lol", base_uri: base_uri)
|
45
45
|
|
46
46
|
expect(a_request(:get, url).with(query: data)).to have_been_requested
|
47
47
|
end
|
48
48
|
|
49
|
-
it
|
49
|
+
it "can delete a channel" do
|
50
50
|
url = "#{base_uri}/delete_channel?topic=lol&channel=foo"
|
51
51
|
stub_request(:get, url)
|
52
|
-
data = {
|
52
|
+
data = {topic: "lol", channel: "foo"}
|
53
53
|
|
54
|
-
FastlyNsq::Http::Nsqlookupd.delete_channel(topic:
|
54
|
+
FastlyNsq::Http::Nsqlookupd.delete_channel(topic: "lol", channel: "foo", base_uri: base_uri)
|
55
55
|
|
56
56
|
expect(a_request(:get, url).with(query: data)).to have_been_requested
|
57
57
|
end
|
58
58
|
|
59
|
-
it
|
59
|
+
it "can tombstone a producer" do
|
60
60
|
url = "#{base_uri}/tombstone_topic_producer?topic=lol&node=localhost:8989"
|
61
61
|
stub_request(:get, url)
|
62
|
-
data = {
|
62
|
+
data = {topic: "lol", node: "localhost:8989"}
|
63
63
|
|
64
|
-
FastlyNsq::Http::Nsqlookupd.tombstone_topic_producer(topic:
|
64
|
+
FastlyNsq::Http::Nsqlookupd.tombstone_topic_producer(topic: "lol", node: "localhost:8989", base_uri: base_uri)
|
65
65
|
|
66
66
|
expect(a_request(:get, url).with(query: data)).to have_been_requested
|
67
67
|
end
|
data/spec/http_spec.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require "spec_helper"
|
4
|
+
require "fastly_nsq/http"
|
5
5
|
|
6
6
|
RSpec.describe FastlyNsq::Http, :webmock do
|
7
|
-
let(:base_url) {
|
8
|
-
describe
|
9
|
-
it
|
7
|
+
let(:base_url) { "http://example.com" }
|
8
|
+
describe "get" do
|
9
|
+
it "can make simple requests" do
|
10
10
|
url = "#{base_url}/boop"
|
11
11
|
stub_request(:get, url)
|
12
12
|
|
@@ -16,9 +16,9 @@ RSpec.describe FastlyNsq::Http, :webmock do
|
|
16
16
|
expect(a_request(:get, url)).to have_been_requested.twice
|
17
17
|
end
|
18
18
|
|
19
|
-
it
|
19
|
+
it "can make requests with params" do
|
20
20
|
url = "#{base_url}/boop?sloop=noop"
|
21
|
-
data = {
|
21
|
+
data = {sloop: "noop"}
|
22
22
|
stub_request(:get, url)
|
23
23
|
|
24
24
|
FastlyNsq::Http.new(uri: url).get(data)
|
@@ -27,11 +27,11 @@ RSpec.describe FastlyNsq::Http, :webmock do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
describe
|
31
|
-
it
|
30
|
+
describe "post" do
|
31
|
+
it "can make simple post requests" do
|
32
32
|
url = "#{base_url}/boop?sloop=noop"
|
33
33
|
stub_request(:post, url)
|
34
|
-
data = {
|
34
|
+
data = {sloop: "noop"}
|
35
35
|
|
36
36
|
FastlyNsq::Http.new(uri: URI.parse(url)).post(data)
|
37
37
|
FastlyNsq::Http.new(uri: url).post(data)
|
@@ -39,11 +39,11 @@ RSpec.describe FastlyNsq::Http, :webmock do
|
|
39
39
|
expect(a_request(:post, url)).to have_been_requested.twice
|
40
40
|
end
|
41
41
|
|
42
|
-
it
|
42
|
+
it "can make post requests with bodies" do
|
43
43
|
url = "#{base_url}/boop?sloop=noop"
|
44
44
|
stub_request(:post, url)
|
45
|
-
data = {
|
46
|
-
body =
|
45
|
+
data = {sloop: "noop"}
|
46
|
+
body = "SOME MESSAGE"
|
47
47
|
|
48
48
|
FastlyNsq::Http.new(uri: url).post(data, body)
|
49
49
|
|
@@ -51,18 +51,18 @@ RSpec.describe FastlyNsq::Http, :webmock do
|
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
describe
|
55
|
-
it
|
56
|
-
ssl_url =
|
54
|
+
describe "SSL" do
|
55
|
+
it "can be asked to use SSL" do
|
56
|
+
ssl_url = "https://example.com:80/boop"
|
57
57
|
stub_request(:get, ssl_url)
|
58
58
|
|
59
|
-
cert_file =
|
60
|
-
key_file =
|
59
|
+
cert_file = "/tmp/thing.cert"
|
60
|
+
key_file = "/tmp/thing.key"
|
61
61
|
|
62
|
-
allow(File).to receive(:read).with(cert_file).and_return(
|
63
|
-
allow(File).to receive(:read).with(key_file).and_return(
|
64
|
-
allow(OpenSSL::X509::Certificate).to receive(:new).with(
|
65
|
-
allow(OpenSSL::PKey::RSA).to receive(:new).with(
|
62
|
+
allow(File).to receive(:read).with(cert_file).and_return("something")
|
63
|
+
allow(File).to receive(:read).with(key_file).and_return("something")
|
64
|
+
allow(OpenSSL::X509::Certificate).to receive(:new).with("something").and_return(true)
|
65
|
+
allow(OpenSSL::PKey::RSA).to receive(:new).with("something").and_return(true)
|
66
66
|
|
67
67
|
url = "#{base_url}/boop"
|
68
68
|
http = FastlyNsq::Http.new(uri: url, cert_filename: cert_file, key_filename: key_file)
|
data/spec/integration_spec.rb
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
|
-
RSpec.describe
|
6
|
-
let!(:topic) {
|
7
|
-
let!(:channel) {
|
8
|
-
let!(:message) { {
|
5
|
+
RSpec.describe "integration" do
|
6
|
+
let!(:topic) { "fnsq-topic" }
|
7
|
+
let!(:channel) { "fnsq-channel" }
|
8
|
+
let!(:message) { {"foo" => "bar"} }
|
9
9
|
|
10
10
|
before { reset_topic(topic, channel: channel) }
|
11
11
|
|
12
|
-
it
|
12
|
+
it "processes jobs" do
|
13
13
|
received = nil
|
14
14
|
producer = FastlyNsq::Producer.new(topic: topic)
|
15
15
|
FastlyNsq::Listener.new(topic: topic, channel: channel, processor: ->(m) { received = m })
|
@@ -18,8 +18,8 @@ RSpec.describe 'integration' do
|
|
18
18
|
expect { received&.body }.to eventually(eq(message)).within(2)
|
19
19
|
end
|
20
20
|
|
21
|
-
describe
|
22
|
-
it
|
21
|
+
describe "inline", :inline do
|
22
|
+
it "processes job" do
|
23
23
|
received = nil
|
24
24
|
producer = FastlyNsq::Producer.new(topic: topic)
|
25
25
|
FastlyNsq::Listener.new(topic: topic, channel: channel, processor: ->(m) { received = m })
|
@@ -29,8 +29,8 @@ RSpec.describe 'integration' do
|
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
32
|
-
describe
|
33
|
-
it
|
32
|
+
describe "fake", :fake do
|
33
|
+
it "stores jobs" do
|
34
34
|
received = nil
|
35
35
|
encoded_message = JSON.dump(message)
|
36
36
|
producer = FastlyNsq::Producer.new(topic: topic)
|
data/spec/launcher_spec.rb
CHANGED
@@ -1,40 +1,40 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
RSpec.describe FastlyNsq::Launcher do
|
6
|
-
let!(:channel)
|
7
|
-
let!(:options)
|
8
|
-
let!(:topic)
|
6
|
+
let!(:channel) { "fnsq" }
|
7
|
+
let!(:options) { {max_threads: 3, timeout: 9} }
|
8
|
+
let!(:topic) { "fnsq" }
|
9
9
|
|
10
|
-
let(:launcher) { FastlyNsq::Launcher.new
|
11
|
-
let(:listener)
|
10
|
+
let(:launcher) { FastlyNsq::Launcher.new(**options) }
|
11
|
+
let(:listener) { FastlyNsq::Listener.new(topic: topic, channel: channel, processor: ->(*) {}) }
|
12
12
|
let(:manager) { launcher.manager }
|
13
13
|
|
14
14
|
before { reset_topic(topic, channel: channel) }
|
15
15
|
before { expect { listener }.to eventually(be_connected).within(5) }
|
16
|
-
after
|
16
|
+
after { listener.terminate if listener.connected? }
|
17
17
|
|
18
|
-
it
|
18
|
+
it "creates a manager with correct options" do
|
19
19
|
launcher
|
20
20
|
|
21
21
|
expect(FastlyNsq.manager.pool.max_threads).to eq(3)
|
22
22
|
end
|
23
23
|
|
24
|
-
describe
|
25
|
-
let!(:logger)
|
24
|
+
describe "#beat" do
|
25
|
+
let!(:logger) { Logger.new(nil).tap { |l| l.level = Logger::DEBUG } }
|
26
26
|
let!(:launcher) { FastlyNsq::Launcher.new pulse: 0.01, logger: logger }
|
27
27
|
|
28
|
-
it
|
28
|
+
it "creates a heartbeat thread" do
|
29
29
|
expect(logger).not_to receive(:error)
|
30
|
-
expect { launcher.beat }.to eventually_not(eq(
|
30
|
+
expect { launcher.beat }.to eventually_not(eq("dead")).pause_for(1)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
describe
|
34
|
+
describe "#stop_listeners" do
|
35
35
|
before { launcher }
|
36
36
|
|
37
|
-
it
|
37
|
+
it "stops listeners and sets done" do
|
38
38
|
expect(launcher).not_to be_stopping
|
39
39
|
expect(manager).to receive(:stop_listeners)
|
40
40
|
expect(manager).not_to receive(:terminate)
|
@@ -45,20 +45,20 @@ RSpec.describe FastlyNsq::Launcher do
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
describe
|
48
|
+
describe "#stop" do
|
49
49
|
before { launcher }
|
50
50
|
|
51
|
-
it
|
51
|
+
it "stops the manager within a deadline" do
|
52
52
|
expect(manager).to receive(:terminate).with(options[:timeout])
|
53
53
|
launcher.stop
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
describe
|
57
|
+
describe "callbacks" do
|
58
58
|
before { FastlyNsq.events.each { |(_, v)| v.clear } }
|
59
|
-
after
|
59
|
+
after { FastlyNsq.events.each { |(_, v)| v.clear } }
|
60
60
|
|
61
|
-
it
|
61
|
+
it "fires :startup event on initialization" do
|
62
62
|
obj = spy
|
63
63
|
block = -> { obj.start }
|
64
64
|
FastlyNsq.on(:startup, &block)
|
@@ -67,7 +67,7 @@ RSpec.describe FastlyNsq::Launcher do
|
|
67
67
|
expect(obj).to have_received(:start)
|
68
68
|
end
|
69
69
|
|
70
|
-
it
|
70
|
+
it "fires :shutdown event on #stop" do
|
71
71
|
launcher
|
72
72
|
|
73
73
|
obj = spy
|
@@ -78,7 +78,7 @@ RSpec.describe FastlyNsq::Launcher do
|
|
78
78
|
expect(obj).to have_received(:stop)
|
79
79
|
end
|
80
80
|
|
81
|
-
it
|
81
|
+
it "fires :heartbeat event on #heartbeat" do
|
82
82
|
obj = spy
|
83
83
|
block = -> { obj.beat }
|
84
84
|
FastlyNsq.on(:heartbeat, &block)
|
data/spec/listener_spec.rb
CHANGED
@@ -1,26 +1,26 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
3
|
+
require "spec_helper"
|
4
4
|
|
5
5
|
RSpec.describe FastlyNsq::Listener do
|
6
|
-
let!(:topic) {
|
7
|
-
let!(:channel) {
|
6
|
+
let!(:topic) { "fnsq" }
|
7
|
+
let!(:channel) { "fnsq" }
|
8
8
|
let!(:messages) { [] }
|
9
9
|
let(:processor) { ->(m) { messages << m.body } }
|
10
10
|
|
11
11
|
before { reset_topic(topic, channel: channel) }
|
12
12
|
before { expect { subject }.to eventually(be_connected).within(5) }
|
13
|
-
after
|
13
|
+
after { subject.terminate if subject.connected? }
|
14
14
|
|
15
15
|
subject { described_class.new(topic: topic, channel: channel, processor: processor) }
|
16
16
|
|
17
|
-
describe
|
18
|
-
describe
|
17
|
+
describe "#initialize" do
|
18
|
+
describe "with FastlyNsq.max_attempts set" do
|
19
19
|
let!(:default_max_attempts) { FastlyNsq.max_attempts }
|
20
20
|
before { FastlyNsq.max_attempts = 19 }
|
21
21
|
after { FastlyNsq.max_attempts = default_max_attempts }
|
22
22
|
|
23
|
-
it
|
23
|
+
it "defaults to FastlyNsq.max_attempts" do
|
24
24
|
listener = described_class.new(topic: topic, processor: processor, channel: channel)
|
25
25
|
expect(listener.max_attempts).to eq(FastlyNsq.max_attempts)
|
26
26
|
|
@@ -31,91 +31,91 @@ RSpec.describe FastlyNsq::Listener do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
describe
|
34
|
+
describe "with FastlyNsq.channel set" do
|
35
35
|
let!(:default_channel) { FastlyNsq.channel }
|
36
|
-
before { FastlyNsq.channel =
|
36
|
+
before { FastlyNsq.channel = "fnsq" }
|
37
37
|
after { FastlyNsq.channel = default_channel }
|
38
38
|
|
39
|
-
it
|
39
|
+
it "defaults to FastlyNsq.channel" do
|
40
40
|
listener = described_class.new(topic: topic, processor: processor)
|
41
41
|
expect(listener.channel).to eq(FastlyNsq.channel)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
|
-
describe
|
45
|
+
describe "with FastlyNsq.preprocessor set" do
|
46
46
|
let!(:default_preprocessor) { FastlyNsq.preprocessor }
|
47
|
-
before { FastlyNsq.preprocessor =
|
47
|
+
before { FastlyNsq.preprocessor = "fnsq" }
|
48
48
|
after { FastlyNsq.preprocessor = default_preprocessor }
|
49
49
|
|
50
|
-
it
|
50
|
+
it "defaults to FastlyNsq.preprocessor" do
|
51
51
|
listener = described_class.new(topic: topic, processor: processor, channel: channel)
|
52
52
|
expect(listener.preprocessor).to eq(FastlyNsq.preprocessor)
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
describe
|
56
|
+
describe "with FastlyNsq.logger set" do
|
57
57
|
let!(:default_logger) { FastlyNsq.logger }
|
58
58
|
before { FastlyNsq.logger = Logger.new(nil) }
|
59
59
|
after { FastlyNsq.logger = default_logger }
|
60
60
|
|
61
|
-
it
|
61
|
+
it "defaults to FastlyNsq.logger" do
|
62
62
|
listener = described_class.new(topic: topic, processor: processor, channel: channel)
|
63
63
|
expect(listener.logger).to eq(FastlyNsq.logger)
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
67
|
-
it
|
67
|
+
it "warns when creating a listener for the same topic" do
|
68
68
|
expect(FastlyNsq.manager.logger).to receive(:warn).and_yield.and_return(match("#{topic} was added more than once"))
|
69
69
|
|
70
70
|
described_class.new(topic: topic, channel: channel, processor: processor)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
-
describe
|
74
|
+
describe "#priority" do
|
75
75
|
specify { expect(subject.priority).to eq(described_class::DEFAULT_PRIORITY) }
|
76
76
|
end
|
77
77
|
|
78
|
-
describe
|
78
|
+
describe "#consumer" do
|
79
79
|
specify { expect(subject.consumer).to be_a(FastlyNsq::Consumer) }
|
80
80
|
end
|
81
81
|
|
82
|
-
describe
|
82
|
+
describe "connect_timeout" do
|
83
83
|
specify { expect(subject.consumer.connect_timeout).to eq(described_class::DEFAULT_CONNECTION_TIMEOUT) }
|
84
84
|
end
|
85
85
|
|
86
|
-
it
|
87
|
-
expect { described_class.new(topic: topic, channel: channel, processor:
|
88
|
-
to raise_error(ArgumentError, match(
|
86
|
+
it "requires processor to respond_to #call" do
|
87
|
+
expect { described_class.new(topic: topic, channel: channel, processor: "foo") }
|
88
|
+
.to raise_error(ArgumentError, match("#call"))
|
89
89
|
end
|
90
90
|
|
91
|
-
it
|
92
|
-
expect { described_class.new(topic: topic, channel: channel, processor: ->(*) {}, priority:
|
93
|
-
to raise_error(ArgumentError, match(
|
91
|
+
it "requires priority to be a Fixnum" do
|
92
|
+
expect { described_class.new(topic: topic, channel: channel, processor: ->(*) {}, priority: "foo") }
|
93
|
+
.to raise_error(ArgumentError, match("Integer"))
|
94
94
|
end
|
95
95
|
|
96
|
-
describe
|
97
|
-
it
|
98
|
-
body = {
|
99
|
-
message = spy(
|
96
|
+
describe "#call" do
|
97
|
+
it "processes a message" do
|
98
|
+
body = {"foo" => "bar"}
|
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
|
|
103
|
-
describe
|
103
|
+
describe "when the processor returns true" do
|
104
104
|
let(:processor) { ->(_) { true } }
|
105
105
|
|
106
|
-
it
|
107
|
-
message = spy(
|
106
|
+
it "finishes the message" do
|
107
|
+
message = spy("message", body: "{}", attempts: 1, id: 1)
|
108
108
|
subject.call(message)
|
109
109
|
|
110
110
|
expect(message).to have_received(:finish)
|
111
111
|
end
|
112
112
|
end
|
113
113
|
|
114
|
-
describe
|
114
|
+
describe "when the processor returns false" do
|
115
115
|
let(:processor) { ->(_) { false } }
|
116
116
|
|
117
|
-
it
|
118
|
-
message = spy(
|
117
|
+
it "finishes the message" do
|
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)
|
@@ -125,18 +125,18 @@ RSpec.describe FastlyNsq::Listener do
|
|
125
125
|
|
126
126
|
it { should be_connected }
|
127
127
|
|
128
|
-
it
|
128
|
+
it "should terminate" do
|
129
129
|
expect { subject.terminate }.to change(subject, :connected?).to(false)
|
130
130
|
end
|
131
131
|
|
132
|
-
describe
|
133
|
-
let!(:message) { JSON.dump(
|
132
|
+
describe "faking", :fake do
|
133
|
+
let!(:message) { JSON.dump("foo" => "bar") }
|
134
134
|
|
135
135
|
before { subject }
|
136
136
|
|
137
137
|
it { should be_connected }
|
138
138
|
|
139
|
-
it
|
139
|
+
it "should terminate" do
|
140
140
|
expect { subject.terminate }.to change(subject, :connected?).to(false)
|
141
141
|
end
|
142
142
|
|
@@ -149,49 +149,49 @@ RSpec.describe FastlyNsq::Listener do
|
|
149
149
|
expect(test_message.raw_body).to eq(message)
|
150
150
|
end
|
151
151
|
|
152
|
-
describe
|
152
|
+
describe "when the processor returns true" do
|
153
153
|
let(:processor) { ->(_) { true } }
|
154
154
|
|
155
|
-
it
|
155
|
+
it "drains queued messages" do
|
156
156
|
FastlyNsq::Producer.new(topic: topic).write(message)
|
157
157
|
expect { subject.drain }.to change { subject.messages.size }.by(-1)
|
158
158
|
end
|
159
159
|
end
|
160
160
|
|
161
|
-
describe
|
161
|
+
describe "when the processor returns false" do
|
162
162
|
let(:processor) { ->(_) { false } }
|
163
163
|
|
164
|
-
it
|
164
|
+
it "does not remove messages" do
|
165
165
|
FastlyNsq::Producer.new(topic: topic).write(message)
|
166
166
|
expect { subject.drain }.not_to change { subject.messages.size }
|
167
167
|
end
|
168
168
|
end
|
169
169
|
end
|
170
170
|
|
171
|
-
describe
|
172
|
-
let!(:message) { JSON.dump(
|
171
|
+
describe "inline", :inline do
|
172
|
+
let!(:message) { JSON.dump("foo" => "bar") }
|
173
173
|
let!(:processor) { ->(m) { messages << m.raw_body } }
|
174
174
|
|
175
175
|
before { subject }
|
176
176
|
|
177
177
|
it { should be_connected }
|
178
178
|
|
179
|
-
it
|
179
|
+
it "should terminate" do
|
180
180
|
expect { subject.terminate }.to change(subject, :connected?).to(false)
|
181
181
|
end
|
182
182
|
|
183
|
-
describe
|
184
|
-
it
|
183
|
+
describe "when the processor returns true" do
|
184
|
+
it "processes and removes messages" do
|
185
185
|
expect { FastlyNsq::Producer.new(topic: topic).write(message) }.to change { messages.size }.by(1)
|
186
186
|
expect(messages).to contain_exactly(message)
|
187
187
|
expect(subject.messages).to be_empty
|
188
188
|
end
|
189
189
|
end
|
190
190
|
|
191
|
-
describe
|
191
|
+
describe "when the processor returns false" do
|
192
192
|
let(:processor) { ->(_) { false } }
|
193
193
|
|
194
|
-
it
|
194
|
+
it "does not remove messages" do
|
195
195
|
FastlyNsq::Producer.new(topic: topic).write(message)
|
196
196
|
expect { subject.drain }.not_to change { subject.messages.size }
|
197
197
|
end
|