@@ -448,25 +448,21 @@ module V3
448448
449449 context 'when database disconnects during state rollback' do
450450 let ( :catalog_error ) { StandardError . new ( 'Catalog fetch failed' ) }
451+ let ( :mock_dataset ) { instance_double ( Sequel ::Postgres ::Dataset ) }
451452
452453 before do
453454 allow_any_instance_of ( VCAP ::CloudController ::V3 ::ServiceBrokerCatalogUpdater ) . to receive ( :refresh ) . and_raise ( catalog_error )
454-
455- mock_dataset = instance_double ( Sequel ::Postgres ::Dataset )
456455 allow ( mock_dataset ) . to receive ( :update ) . and_raise ( Sequel ::DatabaseDisconnectError . new ( 'connection lost' ) )
457-
458456 allow ( ServiceBroker ) . to receive ( :where ) . and_call_original
459- allow ( ServiceBroker ) . to receive ( :where ) . with ( id : broker . id ) . and_return ( mock_dataset )
457+ allow ( ServiceBroker ) . to receive ( :where ) .
458+ with ( id : update_broker_request . service_broker_id , state : ServiceBrokerStateEnum ::SYNCHRONIZING ) .
459+ and_return ( mock_dataset )
460460 end
461461
462- it 'swallows the database error and re-raises the original catalog error' do
462+ it 're-raises the original error instead of the rollback database error' do
463463 expect { job . perform } . to raise_error ( catalog_error )
464464 end
465465
466- it 'does not raise a database connection error' do
467- expect { job . perform } . not_to raise_error ( Sequel ::DatabaseDisconnectError )
468- end
469-
470466 it 'still cleans up the update request' do
471467 expect { job . perform } . to raise_error ( catalog_error )
472468 expect ( ServiceBrokerUpdateRequest . where ( id : update_broker_request . id ) . all ) . to be_empty
@@ -497,48 +493,45 @@ module V3
497493 end
498494
499495 it 'sets the broker to SYNCHRONIZATION_FAILED' do
500- job . recover_from_failure
501-
502- broker . reload
503- expect ( broker . state ) . to eq ( ServiceBrokerStateEnum ::SYNCHRONIZATION_FAILED )
496+ expect do
497+ job . recover_from_failure
498+ end . to change { broker . reload . state } .
499+ from ( ServiceBrokerStateEnum ::SYNCHRONIZING ) .
500+ to ( ServiceBrokerStateEnum ::SYNCHRONIZATION_FAILED )
504501 end
505502 end
506503
507- context 'when broker is not in SYNCHRONIZING state' do
508- before do
509- broker . update ( state : ServiceBrokerStateEnum ::AVAILABLE )
510- end
511-
512- it 'does not change the broker state' do
504+ shared_examples 'does not change the broker state' do |expected_state |
505+ it 'leaves the state unchanged' do
506+ broker . update ( state : expected_state )
513507 job . recover_from_failure
514508
515- broker . reload
516- expect ( broker . state ) . to eq ( ServiceBrokerStateEnum ::AVAILABLE )
509+ expect ( broker . reload . state ) . to eq ( expected_state )
517510 end
518511 end
519512
520- context 'when broker state has changed to something else' do
521- before do
522- broker . update ( state : ServiceBrokerStateEnum ::DELETE_IN_PROGRESS )
523- end
524-
525- it 'does not overwrite the newer state' do
526- job . recover_from_failure
527-
528- broker . reload
529- expect ( broker . state ) . to eq ( ServiceBrokerStateEnum ::DELETE_IN_PROGRESS )
530- end
513+ context 'when broker is in a different state' do
514+ include_examples 'does not change the broker state' , ServiceBrokerStateEnum ::AVAILABLE
515+ include_examples 'does not change the broker state' , ServiceBrokerStateEnum ::DELETE_IN_PROGRESS
531516 end
532517
533518 context 'when database error occurs during recovery' do
519+ let ( :dataset ) { instance_double ( Sequel ::Dataset ) }
520+ let ( :logger ) { instance_double ( Steno ::Logger , error : nil ) }
521+
534522 before do
535523 broker . update ( state : ServiceBrokerStateEnum ::SYNCHRONIZING )
536- allow ( ServiceBroker ) . to receive ( :where ) . and_raise ( Sequel ::DatabaseError . new ( 'connection lost' ) )
537- allow ( Steno ) . to receive ( :logger ) . and_return ( double ( error : nil ) )
524+ allow ( ServiceBroker ) . to receive ( :where ) .
525+ with ( guid : broker . guid , state : ServiceBrokerStateEnum ::SYNCHRONIZING ) .
526+ and_return ( dataset )
527+ allow ( dataset ) . to receive ( :update ) . and_raise ( Sequel ::DatabaseError . new ( RuntimeError . new ( 'connection lost' ) ) )
528+ allow ( Steno ) . to receive ( :logger ) . with ( 'cc.background' ) . and_return ( logger )
538529 end
539530
540531 it 'logs the error and does not raise' do
541532 expect { job . recover_from_failure } . not_to raise_error
533+ expect ( logger ) . to have_received ( :error ) . with ( /Failed to recover broker state/ )
534+ expect ( broker . reload . state ) . to eq ( ServiceBrokerStateEnum ::SYNCHRONIZING )
542535 end
543536 end
544537 end
0 commit comments