def test_establish_worker_connections(self): self.connection_manager.connection_list = [] self.connection_manager.command_handlers = {} # Spin up a bunch of imaginary gearman connections good_connection = MockGearmanConnection() good_connection.connect() good_connection._fail_on_bind = False failed_then_retried_connection = MockGearmanConnection() failed_then_retried_connection._fail_on_bind = False failed_connection = MockGearmanConnection() failed_connection._fail_on_bind = True # Register all our connections self.connection_manager.connection_list = [ good_connection, failed_then_retried_connection, failed_connection ] # The only alive connections should be the ones that ultimately be connection.connected alive_connections = self.connection_manager.establish_worker_connections( ) self.assertTrue(good_connection in alive_connections) self.assertTrue(failed_then_retried_connection in alive_connections) self.assertFalse(failed_connection in alive_connections)
def test_establish_request_connection_complex(self): # Spin up a bunch of imaginary gearman connections failed_connection = MockGearmanConnection() failed_connection._fail_on_bind = True failed_then_retried_connection = MockGearmanConnection() failed_then_retried_connection._fail_on_bind = True good_connection = MockGearmanConnection() good_connection.connect() # Register all our connections self.connection_manager.connection_list = [ failed_connection, failed_then_retried_connection, good_connection ] # When we first create our request, our client shouldn't know anything about it current_request = self.generate_job_request(submitted=False, accepted=False) self.failIf(current_request in self.connection_manager. request_to_rotating_connection_queue) # Make sure that when we start up, we get our good connection chosen_connection = self.connection_manager.establish_request_connection( current_request) self.assertEqual(chosen_connection, good_connection) self.assertFalse(failed_connection.connected) self.assertFalse(failed_then_retried_connection.connected) self.assertTrue(good_connection.connected) # No state changed so we should still go to the correct connection chosen_connection = self.connection_manager.establish_request_connection( current_request) self.assertEqual(chosen_connection, good_connection) # Pretend like our good connection died so we'll need to choose somethign else good_connection._reset_connection() good_connection._fail_on_bind = True failed_then_retried_connection._fail_on_bind = False failed_then_retried_connection.connect() # Make sure we rotate good_connection and failed_connection out chosen_connection = self.connection_manager.establish_request_connection( current_request) self.assertEqual(chosen_connection, failed_then_retried_connection) self.assertFalse(failed_connection.connected) self.assertTrue(failed_then_retried_connection.connected) self.assertFalse(good_connection.connected)
def test_establish_request_connection_complex(self): # Spin up a bunch of imaginary gearman connections failed_connection = MockGearmanConnection() failed_connection._fail_on_bind = True failed_then_retried_connection = MockGearmanConnection() failed_then_retried_connection._fail_on_bind = True good_connection = MockGearmanConnection() good_connection.connect() # Register all our connections self.connection_manager.connection_list = [failed_connection, failed_then_retried_connection, good_connection] # When we first create our request, our client shouldn't know anything about it current_request = self.generate_job_request(submitted=False, accepted=False) self.failIf(current_request in self.connection_manager.request_to_rotating_connection_queue) # Make sure that when we start up, we get our good connection chosen_connection = self.connection_manager.establish_request_connection(current_request) self.assertEqual(chosen_connection, good_connection) self.assertFalse(failed_connection.connected) self.assertFalse(failed_then_retried_connection.connected) self.assertTrue(good_connection.connected) # No state changed so we should still go to the correct connection chosen_connection = self.connection_manager.establish_request_connection(current_request) self.assertEqual(chosen_connection, good_connection) # Pretend like our good connection died so we'll need to choose somethign else good_connection._reset_connection() good_connection._fail_on_bind = True failed_then_retried_connection._fail_on_bind = False failed_then_retried_connection.connect() # Make sure we rotate good_connection and failed_connection out chosen_connection = self.connection_manager.establish_request_connection(current_request) self.assertEqual(chosen_connection, failed_then_retried_connection) self.assertFalse(failed_connection.connected) self.assertTrue(failed_then_retried_connection.connected) self.assertFalse(good_connection.connected)
def test_establish_worker_connections(self): self.connection_manager.connection_list = [] self.connection_manager.command_handlers = {} # Spin up a bunch of imaginary gearman connections good_connection = MockGearmanConnection() good_connection.connect() good_connection._fail_on_bind = False failed_then_retried_connection = MockGearmanConnection() failed_then_retried_connection._fail_on_bind = False failed_connection = MockGearmanConnection() failed_connection._fail_on_bind = True # Register all our connections self.connection_manager.connection_list = [good_connection, failed_then_retried_connection, failed_connection] # The only alive connections should be the ones that ultimately be connection.connected alive_connections = self.connection_manager.establish_worker_connections() self.assertTrue(good_connection in alive_connections) self.assertTrue(failed_then_retried_connection in alive_connections) self.assertFalse(failed_connection in alive_connections)
def test_establish_worker_connections_dead(self): self.connection_manager.connection_list = [] self.connection_manager.command_handlers = {} # We have no connections so there will never be any work to do self.assertRaises(ServerUnavailable, self.connection_manager.work) # We were started with a dead connection, make sure we bail again dead_connection = MockGearmanConnection() dead_connection._fail_on_bind = True dead_connection.connected = False self.connection_manager.connection_list = [dead_connection] self.assertRaises(ServerUnavailable, self.connection_manager.work)
def test_establish_request_connection_dead(self): self.connection_manager.connection_list = [] self.connection_manager.command_handlers = {} current_request = self.generate_job_request(submitted=False, accepted=False) # No connections == death self.assertRaises(ServerUnavailable, self.connection_manager.establish_request_connection, current_request) # Spin up a bunch of imaginary gearman connections failed_connection = MockGearmanConnection() failed_connection._fail_on_bind = True self.connection_manager.connection_list.append(failed_connection) # All failed connections == death self.assertRaises(ServerUnavailable, self.connection_manager.establish_request_connection, current_request)