Skip to content

Commit

Permalink
fixed specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Ceraunograph committed Jul 13, 2016
1 parent 7cbb389 commit 53e925c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 40 deletions.
7 changes: 2 additions & 5 deletions lib/chore/strategies/worker/helpers/worker_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,8 @@ def ready_workers(sockets = [], &block)
private

def create_sockets(&block)
num_new_sockets = Chore.config.num_workers - @pid_to_worker.size
new_sockets = []

num_new_sockets.times do
new_sockets << add_worker_socket
new_sockets = Array.new(Chore.config.num_workers - @pid_to_worker.size) do
add_worker_socket
end

yield new_sockets if block_given?
Expand Down
2 changes: 1 addition & 1 deletion spec/chore/strategies/worker/helpers/ipc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DummyClass

context '#child_connection' do
it 'should accept a connection and return the connection socket' do
expect(socket).to receive(:accept).and_return(connection)
expect(socket).to receive(:accept_nonblock).and_return(connection)
expect(@dummy_instance.child_connection(socket)).to eq connection
end
end
Expand Down
10 changes: 5 additions & 5 deletions spec/chore/strategies/worker/helpers/worker_info_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'spec_helper'

describe Chore::Strategy::WorkerInfo do
let(:pid) { 1 }
let(:socket) { double('socket') }
let(:worker_info) { Chore::Strategy::WorkerInfo.new(pid) }

context '#initialize' do
it 'should initialize the WorkerInfo with a pid' do
wi = Chore::Strategy::WorkerInfo.new(pid)
expect(wi.pid).to equal(pid)
expect(wi.socket).to equal(nil)
it 'should initialize the WorkerInfo with a socket' do
wi = Chore::Strategy::WorkerInfo.new(socket)
expect(wi.socket).to equal(socket)
expect(wi.pid).to equal(nil)
end
end
end
60 changes: 31 additions & 29 deletions spec/chore/strategies/worker/helpers/worker_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@

context '#create_and_attach_workers' do
before(:each) do
allow(worker_manager).to receive(:create_workers).and_yield(2)
allow(worker_manager).to receive(:create_sockets).and_yield([socket_1, socket_2])
allow(worker_manager).to receive(:attach_workers).and_return(true)
end

it 'should call to create replacement workers' do
expect(worker_manager).to receive(:create_workers)
expect(worker_manager).to receive(:create_sockets)
worker_manager.create_and_attach_workers
end

it 'should create a map between new workers to new sockets' do
expect(worker_manager).to receive(:attach_workers).with(2)
expect(worker_manager).to receive(:attach_workers).with([socket_1, socket_2])
worker_manager.create_and_attach_workers
end
end
Expand Down Expand Up @@ -87,10 +87,8 @@
worker_manager.instance_variable_set(:@pid_to_worker, pid_sock_hash)
end

it 'should forward the signal received to each of the child processes' do
pid_sock_hash.each do |pid, sock|
expect(Process).to receive(:kill).with(signal, pid)
end
it 'should forward the signal received to the process group' do
expect(Process).to receive(:kill).with(signal, 0)
worker_manager.stop_workers(signal)
end

Expand Down Expand Up @@ -130,24 +128,37 @@
allow(Chore).to receive(:config).and_return(config)
end

it 'should fork it running process till we have the right optimized number of workers and return the number of workers it created' do
it 'should fork the running process the number of times specified return the number of workers it created' do
allow(config).to receive(:num_workers).and_return(1)
expect(worker_manager).to receive(:fork).once
expect(worker_manager).to receive(:run_worker_instance).once
worker_manager.send(:create_workers)
worker_manager.send(:create_workers, 1)
end

it 'should raise an exception if an inconsistent number of workers are created' do
allow(config).to receive(:num_workers).and_return(0)
allow(worker_manager).to receive(:inconsistent_worker_number).and_return(true)
expect(worker_manager).to receive(:inconsistent_worker_number)
expect { worker_manager.send(:create_workers) }.to raise_error(RuntimeError)
expect { worker_manager.send(:create_workers, 1) }.to raise_error(RuntimeError)
end
end

it 'should call the block passed to it with the number of workers it created' do
allow(config).to receive(:num_workers).and_return(0)
allow(worker_manager).to receive(:inconsistent_worker_number).and_return(false)
expect { |b| worker_manager.send(:create_workers, &b) }.to yield_control.once
context "#create_sockets" do
before(:each) do
allow(Chore).to receive(:config).and_return(config)
end

it 'should return an array of sockets equal to the number of missing workers' do
allow(worker_manager).to receive(:add_worker_socket).and_return(socket_1, socket_2)
allow(config).to receive(:num_workers).and_return(2)
res = worker_manager.send(:create_sockets)
expect(res).to eq([socket_1, socket_2])
end

it 'should call the block passed to it with the number of sockets it created' do
allow(worker_manager).to receive(:add_worker_socket).and_return(socket_1, socket_2)
allow(config).to receive(:num_workers).and_return(2)
expect { |b| worker_manager.send(:create_sockets, &b) }.to yield_control.once
end
end

Expand Down Expand Up @@ -197,46 +208,37 @@

it 'should add as many sockets as the number passed to it as a param' do
expect(worker_manager).to receive(:read_msg).twice
worker_manager.send(:attach_workers, 2)
worker_manager.send(:attach_workers, [socket_1, socket_2])
end

it 'should select on each socket to make sure its readable' do
expect(worker_manager).to receive(:select_sockets).twice
worker_manager.send(:attach_workers, 2)
worker_manager.send(:attach_workers, [socket_1, socket_2])
end

it 'should get the PID from each socket it creates and map that to the worker that it is connected to' do
worker_manager.send(:attach_workers, 2)
worker_manager.send(:attach_workers, [socket_1, socket_2])
expect(worker_manager.instance_variable_get(:@socket_to_worker)).to eq(socket_to_worker)
end

it 'should kill any unattached workers' do
expect(worker_manager).to receive(:kill_unattached_workers)
worker_manager.send(:attach_workers, 2)
worker_manager.send(:attach_workers, [socket_1, socket_2])
end

it 'should close sockets that failed to get a connection by timing out' do
allow(worker_manager).to receive(:select_sockets).and_return([[socket_1], [], []], [nil, nil, nil])
expect(socket_1).not_to receive(:close)
expect(socket_2).to receive(:close)
worker_manager.send(:attach_workers, 2)
worker_manager.send(:attach_workers, [socket_1, socket_2])
end

it 'should close sockets that failed to get a connection by econnreset' do
allow(worker_manager).to receive(:select_sockets).with(socket_1, nil, 2).and_return([[socket_1], [], []])
allow(worker_manager).to receive(:select_sockets).with(socket_2, nil, 2).and_raise(Errno::ECONNRESET)
expect(socket_1).not_to receive(:close)
expect(socket_2).to receive(:close)
worker_manager.send(:attach_workers, 2)
end
end

context "#create_worker_sockets" do
it 'should return an array of sockets equal to the number passed to it' do
allow(worker_manager).to receive(:add_worker_socket).and_return(socket_1, socket_2, socket_2, socket_1)
num = 3
res = worker_manager.send(:create_worker_sockets, num)
expect(res.size).to eq(num)
worker_manager.send(:attach_workers, [socket_1, socket_2])
end
end

Expand Down

0 comments on commit 53e925c

Please sign in to comment.