Skip to content

Commit 0308734

Browse files
committed
Enhance test
1 parent c6e8ca8 commit 0308734

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

spec/unit/jobs/v3/services/update_broker_job_spec.rb

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -447,26 +447,36 @@ module V3
447447
end
448448

449449
context 'when database disconnects during state rollback' do
450+
let(:catalog_error) { StandardError.new('Catalog fetch failed') }
451+
450452
before do
451-
setup_broker_with_invalid_catalog
453+
# Simulate catalog refresh failure
454+
allow_any_instance_of(VCAP::CloudController::V3::ServiceBrokerCatalogUpdater).to receive(:refresh).and_raise(catalog_error)
452455

453-
# Mock the where clause to raise database error only when called from rescue block
454-
# We detect this by checking if it's being called with just an id condition
455-
call_count = 0
456-
allow(ServiceBroker).to receive(:where).and_wrap_original do |original_method, *args|
457-
call_count += 1
458-
# The rescue block calls ServiceBroker.where(id: ...).update(state: ...)
459-
# This is the only place that calls .where with just {:id => ...}
460-
raise Sequel::DatabaseDisconnectError.new('connection lost') if args.first.is_a?(Hash) && args.first.keys == [:id] && call_count > 1
456+
# Create a mock dataset that raises database error on update
457+
mock_dataset = instance_double(Sequel::Postgres::Dataset)
458+
allow(mock_dataset).to receive(:update).and_raise(Sequel::DatabaseDisconnectError.new('connection lost'))
461459

462-
original_method.call(*args)
463-
end
460+
# Mock ServiceBroker.where to return the mock dataset for the state rollback call
461+
allow(ServiceBroker).to receive(:where).and_call_original
462+
allow(ServiceBroker).to receive(:where).with(id: broker.id).and_return(mock_dataset)
464463
end
465464

466-
it 'handles the database error gracefully and re-raises the original error' do
467-
expect { job.perform }.to raise_error(::CloudController::Errors::ApiError) do |error|
468-
expect(error.message).to include('Service broker catalog is invalid')
469-
end
465+
it 'swallows the database error and re-raises the original catalog error' do
466+
# With the fix: original error is raised, database error is swallowed
467+
# Without the fix: database error would be raised instead
468+
expect { job.perform }.to raise_error(catalog_error)
469+
end
470+
471+
it 'does not raise a database connection error' do
472+
# This verifies the fix: database errors during rollback should be caught
473+
expect { job.perform }.not_to raise_error(Sequel::DatabaseDisconnectError)
474+
end
475+
476+
it 'still cleans up the update request' do
477+
# Ensure the ensure block still executes
478+
expect { job.perform }.to raise_error(catalog_error)
479+
expect(ServiceBrokerUpdateRequest.where(id: update_broker_request.id).all).to be_empty
470480
end
471481
end
472482

0 commit comments

Comments
 (0)