mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 10:08:40 -05:00
(POOLER-73) Add spec tests for check_snapshot_queue
Add spec tests for check_snapshot_queue Previously the check_snapshot_queue method would execute the loop indefinitely as it did not have a terminating condition. This made it impossible to test. This commit modifies the check_snapshot_queue method so that it can take a maxloop and delay parameter so that it can be tested.
This commit is contained in:
parent
4dd0c96a78
commit
6f127d32bc
2 changed files with 74 additions and 2 deletions
|
|
@ -443,15 +443,21 @@ module Vmpooler
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_snapshot_queue
|
def check_snapshot_queue(maxloop = 0, loop_delay = 5)
|
||||||
$logger.log('d', "[*] [snapshot_manager] starting worker thread")
|
$logger.log('d', "[*] [snapshot_manager] starting worker thread")
|
||||||
|
|
||||||
$vsphere['snapshot_manager'] ||= Vmpooler::VsphereHelper.new $config, $metrics
|
$vsphere['snapshot_manager'] ||= Vmpooler::VsphereHelper.new $config, $metrics
|
||||||
|
|
||||||
$threads['snapshot_manager'] = Thread.new do
|
$threads['snapshot_manager'] = Thread.new do
|
||||||
|
loop_count = 1
|
||||||
loop do
|
loop do
|
||||||
_check_snapshot_queue $vsphere['snapshot_manager']
|
_check_snapshot_queue $vsphere['snapshot_manager']
|
||||||
sleep(5)
|
sleep(loop_delay)
|
||||||
|
|
||||||
|
unless maxloop.zero?
|
||||||
|
break if loop_count >= maxloop
|
||||||
|
loop_count = loop_count + 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1021,6 +1021,72 @@ EOT
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#check_snapshot_queue' do
|
||||||
|
let(:threads) {[]}
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
expect(Thread).to receive(:new).and_yield
|
||||||
|
allow(subject).to receive(:_check_snapshot_queue)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should log the disk manager is starting' do
|
||||||
|
expect(logger).to receive(:log).with('d', "[*] [snapshot_manager] starting worker thread")
|
||||||
|
|
||||||
|
expect($threads.count).to be(0)
|
||||||
|
subject.check_snapshot_queue(1,0)
|
||||||
|
expect($threads.count).to be(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should add the manager to the global thread list' do
|
||||||
|
# Note - Ruby core types are not necessarily thread safe
|
||||||
|
expect($threads.count).to be(0)
|
||||||
|
subject.check_snapshot_queue(1,0)
|
||||||
|
expect($threads.count).to be(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should call _check_snapshot_queue' do
|
||||||
|
expect(subject).to receive(:_check_snapshot_queue).with(Vmpooler::VsphereHelper)
|
||||||
|
|
||||||
|
subject.check_snapshot_queue(1,0)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'delays between loops' do
|
||||||
|
let(:maxloop) { 2 }
|
||||||
|
let(:loop_delay) { 1 }
|
||||||
|
# Note a maxloop of zero can not be tested as it never terminates
|
||||||
|
|
||||||
|
it 'defaults to 5 second loop delay' do
|
||||||
|
expect(subject).to receive(:sleep).with(5).exactly(maxloop).times
|
||||||
|
subject.check_snapshot_queue(maxloop)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'when a non-default loop delay is specified' do
|
||||||
|
start_time = Time.now
|
||||||
|
subject.check_snapshot_queue(maxloop,loop_delay)
|
||||||
|
finish_time = Time.now
|
||||||
|
|
||||||
|
# Use a generous delta to take into account various CPU load etc.
|
||||||
|
expect(finish_time - start_time).to be_within(0.75).of(maxloop * loop_delay)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'loops specified number of times (5)' do
|
||||||
|
let(:maxloop) { 5 }
|
||||||
|
# Note a maxloop of zero can not be tested as it never terminates
|
||||||
|
|
||||||
|
after(:each) do
|
||||||
|
# Reset the global variable - Note this is a code smell
|
||||||
|
$threads = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should call _check_snapshot_queue 5 times' do
|
||||||
|
expect(subject).to receive(:_check_snapshot_queue).with(Vmpooler::VsphereHelper).exactly(maxloop).times
|
||||||
|
|
||||||
|
subject.check_snapshot_queue(maxloop,0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#migration_limit' do
|
describe '#migration_limit' do
|
||||||
# This is a little confusing. Is this supposed to return a boolean
|
# This is a little confusing. Is this supposed to return a boolean
|
||||||
# or integer type?
|
# or integer type?
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue