Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use provided connection in WorkerGroup #322

Merged
merged 2 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions lib/sneakers/workergroup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ def run
worker_classes = worker_classes.call
end

# if we don't provide a connection to a worker,
# If we don't provide a connection to a worker,
# the queue used in the worker will create a new one
# so if we want to have a shared bunny connection for the workers
# we must create it here
bunny_connection = create_connection_or_nil

@workers = worker_classes.map{|w| w.new(nil, pool, {connection: bunny_connection}) }
@workers = worker_classes.map do |worker_class|
worker_class.new(nil, pool, { connection: config[:connection] })
end

# if more than one worker this should be per worker
# accumulate clients and consumers as well
@workers.each do |worker|
Expand All @@ -47,7 +47,6 @@ def run
Sneakers.logger.debug("Heartbeat: running threads [#{Thread.list.count}]")
# report aggregated stats?
end

end

def stop
Expand All @@ -57,14 +56,5 @@ def stop
end
@stop_flag.set!
end

def create_bunny_connection
Bunny.new(Sneakers::CONFIG[:amqp], :vhost => Sneakers::CONFIG[:vhost], :heartbeat => Sneakers::CONFIG[:heartbeat], :logger => Sneakers::logger)
end

def create_connection_or_nil
config[:share_bunny_connection] ? create_bunny_connection : nil
end
private :create_bunny_connection, :create_connection_or_nil
end
end
11 changes: 8 additions & 3 deletions spec/sneakers/runner_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'logger'
require 'spec_helper'
require 'sneakers'
require 'sneakers/runner'

describe Sneakers::Runner do
let(:logger) { Logger.new('logtest.log') }
Expand Down Expand Up @@ -29,10 +30,10 @@
let(:logger) { Logger.new("logtest.log") }
let(:runner_config) { Sneakers::Runner.new([]).instance_variable_get("@runnerconfig") }



describe "with a connection" do
before { Sneakers.configure(log: logger, connection: Object.new) }
let(:connection) { Object.new }

before { Sneakers.configure(log: logger, connection: connection) }

describe "#reload_config!" do
it "does not throw exception" do
Expand All @@ -46,6 +47,10 @@
it "must have :logger key as an instance of Logger" do
runner_config.reload_config![:logger].is_a?(Logger).must_equal true
end

it "must have :connection" do
runner_config.reload_config![:connection].is_a?(Object).must_equal true
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/sneakers/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,22 @@ def with_test_queuefactory(ctx, ack=true, msg=nil, nowork=false)
DummyWorker.new.id.must_match(/^worker-/)
end
end

describe 'when connection provided' do
before do
@connection = Bunny.new(host: 'any-host.local')
Sneakers.configure(
exchange: 'some-exch',
exchange_options: { type: :direct },
connection: @connection,
)
end

it "should build a queue with given connection" do
@dummy_q = DummyWorker.new.queue
@dummy_q.opts[:connection].must_equal(@connection)
end
end
end


Expand Down
83 changes: 83 additions & 0 deletions spec/sneakers/workergroup_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
require 'logger'
require 'spec_helper'
require 'sneakers'
require 'sneakers/runner'

class DummyFlag
def wait_for_set(*)
true
end
end

class DummyEngine
include Sneakers::WorkerGroup

attr_reader :config

def initialize(config)
@config = config
@stop_flag = DummyFlag.new
end
end

class DefaultsWorker
include Sneakers::Worker
from_queue 'defaults'

def work(msg); end
end

class StubbedWorker
attr_reader :opts

def initialize(_, _, opts)
@opts = opts
end

def run
true
end
end

describe Sneakers::WorkerGroup do
let(:logger) { Logger.new('logtest.log') }
let(:connection) { Bunny.new(host: 'any-host.local') }
let(:runner) { Sneakers::Runner.new([DefaultsWorker]) }
let(:runner_config) { runner.instance_variable_get('@runnerconfig') }
let(:config) { runner_config.reload_config! }
let(:engine) { DummyEngine.new(config) }

describe '#run' do
describe 'with connecion provided' do
before do
Sneakers.clear!
Sneakers.configure(connection: connection, log: logger)
end

it 'creates workers with connection: connection' do
DefaultsWorker.stub(:new, ->(*args) { StubbedWorker.new(*args) }) do
engine.run

workers = engine.instance_variable_get('@workers')
workers.first.opts[:connection].must_equal(connection)
end
end
end

describe 'without connecion provided' do
before do
Sneakers.clear!
Sneakers.configure(log: logger)
end

it 'creates workers with connection: nil' do
DefaultsWorker.stub(:new, ->(*args) { StubbedWorker.new(*args) }) do
engine.run

workers = engine.instance_variable_get('@workers')
assert_nil(workers.first.opts[:connection])
end
end
end
end
end