Skip to content

Commit

Permalink
add specs of private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nurse committed Nov 18, 2015
1 parent 69a4c2a commit 121de35
Showing 1 changed file with 105 additions and 56 deletions.
161 changes: 105 additions & 56 deletions spec/multiprocess/child_process_monitor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
require 'spec_helper'

describe PerfectQueue::Multiprocess::ChildProcessMonitor do
let (:rpipe){ double('rpipe') }
let (:last_heartbeat){ 42 }
let (:last_kill_time){ 42 }
let (:processor_id){ double('processor_id') }
let (:log){ double('log').as_null_object }
let (:cpm) {
cpm = Multiprocess::ChildProcessMonitor.new(log, processor_id, rpipe)
cpm.instance_variable_set(:@last_heartbeat, last_heartbeat)
cpm
}
let (:now){ 72 }
describe '.new' do
it 'returns a PerfectQueue::Multiprocess::ChildProcessMonitor' do
log = double('log')
processor_id = double('processor_id')
rpipe = double('rpipe')
processor = Multiprocess::ChildProcessMonitor.new(log, processor_id, rpipe)
expect(processor).to be_an_instance_of(Multiprocess::ChildProcessMonitor)
end
end

describe '#check_heartbeat' do
let (:rpipe){ double('rpipe') }
let (:last_heartbeat){ 42 }
let (:cpm) {
log = double('log')
processor_id = double('processor_id')
cpm = Multiprocess::ChildProcessMonitor.new(log, processor_id, rpipe)
cpm.instance_variable_set(:@last_heartbeat, last_heartbeat)
cpm
}
let (:now){ 72 }
before do
allow(object_double('Time').as_stubbed_const).to \
receive_message_chain(:now, :to_i).and_return(now)
Expand Down Expand Up @@ -52,13 +50,6 @@
end

describe '#start_killing' do
let (:cpm) do
log = double('log')
processor_id = double('processor_id')
rpipe = double('rpipe')
Multiprocess::ChildProcessMonitor.new(log, processor_id, rpipe)
end
let (:now){ 72 }
before do
allow(object_double('Time').as_stubbed_const).to \
receive_message_chain(:now, :to_i).and_return(now)
Expand Down Expand Up @@ -106,12 +97,6 @@
end

describe '#killing_status' do
let (:cpm) do
log = double('log')
processor_id = double('processor_id')
rpipe = double('rpipe')
Multiprocess::ChildProcessMonitor.new(log, processor_id, rpipe)
end
context '@kill_start_time: nil' do
before { cpm.instance_variable_set(:@kill_start_time, nil) }
it 'returns nil' do
Expand All @@ -136,12 +121,6 @@
end

describe '#try_join' do
let (:processor_id){ double('processor_id') }
let (:cpm) do
log = double('log').as_null_object
rpipe = double('rpipe')
Multiprocess::ChildProcessMonitor.new(log, processor_id, rpipe)
end
context 'not killed yet' do
it 'returns nil' do
expect(cpm).not_to receive(:kill_children)
Expand Down Expand Up @@ -174,8 +153,6 @@
end
end
context 'waitpid returns nil' do
let (:last_kill_time){ 42 }
let (:now){ 72 }
before do
cProcess.and_return(nil)
allow(object_double('Time').as_stubbed_const).to \
Expand All @@ -198,39 +175,22 @@

describe '#cleanup' do
context 'rpipe is open' do
let (:rpipe){ double('rpipe', closed?: false) }
let (:cpm) do
log = double('log')
processor_id = double('processor_id')
Multiprocess::ChildProcessMonitor.new(log, processor_id, rpipe)
end
it 'closes rpipe' do
allow(rpipe).to receive(:closed?).and_return(false)
expect(rpipe).to receive(:close).exactly(:once)
cpm.cleanup
end
end
context 'rpipe is closed' do
let (:rpipe){ double('rpipe', closed?: true) }
let (:cpm) do
log = double('log')
processor_id = double('processor_id')
rpipe = double('rpipe', closed?: true)
Multiprocess::ChildProcessMonitor.new(log, processor_id, rpipe)
end
it 'doesn\'t close rpipe' do
allow(rpipe).to receive(:closed?).and_return(true)
expect(rpipe).not_to receive(:close)
cpm.cleanup
end
end
end

describe '#send_signal' do
let (:processor_id){ double('processor_id') }
let (:cpm) do
log = double('log')
rpipe = double('rpipe')
Multiprocess::ChildProcessMonitor.new(log, processor_id, rpipe)
end
let (:sig){ double('sig') }
let (:cProcess) do
allow(Process).to receive(:kill).with(sig, processor_id)
Expand All @@ -241,11 +201,100 @@
end
it { cpm.send_signal(sig) }
end
context 'kill raies SRCH' do
context 'kill raises ESRCH' do
before{ cProcess.and_raise(Errno::ESRCH) }
it { cpm.send_signal(sig) }
end
context 'kill raises EPERM' do
before{ cProcess.and_raise(Errno::EPERM) }
it { cpm.send_signal(sig) }
end
end

describe '#kill_children' do
context '@kill_start_time: nil' do
# don't happen
end
context '@kill_start_time: <time>' do
before do
cProcess.and_raise(Errno::ESRCH)
cpm.instance_variable_set(:@kill_start_time, 42)
end
it { cpm.send_signal(sig) }
context '@kill_immediate: true' do
before do
cpm.instance_variable_set(:@kill_immediate, true)
expect(cpm).to receive(:get_ppid_pids_map).with(no_args).and_return({1=>processor_id}).exactly(:once)
expect(cpm).to receive(:collect_child_pids).with({1=>processor_id}, [processor_id], processor_id) \
.and_return([processor_id]).exactly(:once)
expect(cpm).to receive(:kill_process).with(processor_id, true)
end
it 'calls kill_process immediately' do
cpm.__send__(:kill_children, now, double)
end
end
context '@kill_immediate: false' do
before do
cpm.instance_variable_set(:@kill_immediate, false)
end
it 'calls kill_process immediately' do
expect(cpm).to receive(:get_ppid_pids_map).with(no_args).and_return({1=>processor_id}).exactly(:once)
expect(cpm).to receive(:collect_child_pids).with({1=>processor_id}, [processor_id], processor_id) \
.and_return([processor_id]).exactly(:once)
expect(cpm).to receive(:kill_process).with(processor_id, true)
cpm.__send__(:kill_children, now, 29)
end
it 'calls kill_process' do
expect(cpm).not_to receive(:get_ppid_pids_map)
expect(cpm).not_to receive(:collect_child_pids)
expect(cpm).to receive(:kill_process).with(processor_id, false)
cpm.__send__(:kill_children, now, 30)
end
end
end
end

describe '#get_ppid_pids_map' do
before do
expect(cpm).to receive(:`).with('ps axo pid,ppid') \
.and_return <<eom
PID PPID
1 0
2 1
3 1
4 2
5 3
eom
end
it 'returns a tree of hash' do
expect(cpm.__send__(:get_ppid_pids_map)).to eq({0=>[1], 1=>[2, 3], 2=>[4], 3=>[5]})
end
end

describe '#collect_child_pids' do
it 'returns a flat array of given children' do
ppid_pids = {0=>[1], 1=>[2, 3], 2=>[4], 3=>[5]}
parent_pid = 1
results = cpm.__send__(:collect_child_pids, ppid_pids, [parent_pid], parent_pid)
expect(results).to eq([1, 2, 4, 3, 5])
end
end

describe '#kill_process' do
let (:pid){ double('pid') }
it 'kill(:KILL, pid) for immediate:true' do
expect(Process).to receive(:kill).with(:KILL, pid).and_return(pid).exactly(:once)
expect(cpm.__send__(:kill_process, pid, true)).to eq(pid)
end
it 'kill(:TERM, pid) for immediate:false' do
expect(Process).to receive(:kill).with(:TERM, pid).and_return(pid).exactly(:once)
expect(cpm.__send__(:kill_process, pid, false)).to eq(pid)
end
it 'rescues ESRCH' do
expect(Process).to receive(:kill).with(:KILL, pid).and_raise(Errno::ESRCH).exactly(:once)
expect(cpm.__send__(:kill_process, pid, true)).to be_nil
end
it 'rescues EPERM' do
expect(Process).to receive(:kill).with(:KILL, pid).and_raise(Errno::EPERM).exactly(:once)
expect(cpm.__send__(:kill_process, pid, true)).to be_nil
end
end
end

0 comments on commit 121de35

Please sign in to comment.