### Question:
HighAvailabilityModeSwitcher implements HighAvailabilityMemberListener, BindingListener, Lifecycle { public void forceElections() { if ( canAskForElections.compareAndSet( true, false ) ) { clusterMemberAvailability.memberIsUnavailable( HighAvailabilityModeSwitcher.SLAVE ); election.performRoleElections(); } } HighAvailabilityModeSwitcher( SwitchToSlave switchToSlave,
                                         SwitchToMaster switchToMaster,
                                         Election election,
                                         ClusterMemberAvailability clusterMemberAvailability,
                                         ClusterClient clusterClient,
                                         Supplier<StoreId> storeIdSupplier,
                                         InstanceId instanceId,
                                         ComponentSwitcher componentSwitcher,
                                         DataSourceManager neoStoreDataSourceSupplier,
                                         LogService logService ); static InstanceId getServerId( URI haUri ); @Override void listeningAt( URI myUri ); @Override synchronized void init(); @Override synchronized void start(); @Override synchronized void stop(); @Override synchronized void shutdown(); @Override void masterIsElected( HighAvailabilityMemberChangeEvent event ); @Override void masterIsAvailable( HighAvailabilityMemberChangeEvent event ); @Override void slaveIsAvailable( HighAvailabilityMemberChangeEvent event ); @Override void instanceStops( HighAvailabilityMemberChangeEvent event ); @Override void instanceDetached( HighAvailabilityMemberChangeEvent event ); void forceElections(); static final String MASTER; static final String SLAVE; static final String UNKNOWN; static final String INADDR_ANY; }### Answer:
@Test public void shouldPerformForcedElections() { ClusterMemberAvailability memberAvailability = mock( ClusterMemberAvailability.class ); Election election = mock( Election.class ); HighAvailabilityModeSwitcher modeSwitcher = new HighAvailabilityModeSwitcher( mock( SwitchToSlaveCopyThenBranch.class ), mock( SwitchToMaster.class ), election, memberAvailability, mock( ClusterClient.class ), storeSupplierMock(), mock( InstanceId.class ), new ComponentSwitcherContainer(), neoStoreDataSourceSupplierMock(), NullLogService.getInstance() ); modeSwitcher.forceElections(); InOrder inOrder = inOrder( memberAvailability, election ); inOrder.verify( memberAvailability ).memberIsUnavailable( HighAvailabilityModeSwitcher.SLAVE ); inOrder.verify( election ).performRoleElections(); inOrder.verifyNoMoreInteractions(); }
@Test public void shouldPerformForcedElectionsOnlyOnce() { ClusterMemberAvailability memberAvailability = mock( ClusterMemberAvailability.class ); Election election = mock( Election.class ); HighAvailabilityModeSwitcher modeSwitcher = new HighAvailabilityModeSwitcher( mock( SwitchToSlaveCopyThenBranch.class ), mock( SwitchToMaster.class ), election, memberAvailability, mock( ClusterClient.class ), storeSupplierMock(), mock( InstanceId.class ), new ComponentSwitcherContainer(), neoStoreDataSourceSupplierMock(), NullLogService.getInstance() ); modeSwitcher.forceElections(); modeSwitcher.forceElections(); modeSwitcher.forceElections(); InOrder inOrder = inOrder( memberAvailability, election ); inOrder.verify( memberAvailability ).memberIsUnavailable( HighAvailabilityModeSwitcher.SLAVE ); inOrder.verify( election ).performRoleElections(); inOrder.verifyNoMoreInteractions(); }