def test_mixed_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._should_fail_on_bind = False
        good_connection.connected = True

        failed_then_retried_connection = MockGearmanConnection()
        failed_then_retried_connection._should_fail_on_bind = True
        failed_then_retried_connection.connected = True

        failed_connection = MockGearmanConnection()
        failed_connection._should_fail_on_bind = True
        failed_connection.connected = False

        # 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._get_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_connection_rotation_for_requests(self):
        # Spin up a bunch of imaginary gearman connections
        failed_connection = MockGearmanConnection()
        failed_connection._should_fail_on_bind = True
        failed_connection.connected = False

        failed_then_retried_connection = MockGearmanConnection()
        failed_then_retried_connection._should_fail_on_connect = True
        failed_then_retried_connection.connected = True

        good_connection = MockGearmanConnection()
        good_connection._should_fail_on_bind = False
        failed_then_retried_connection.connected = True

        self.connection_manager.connection_list = [failed_connection, failed_then_retried_connection, good_connection]

        # Register all our connections
        current_request = self.generate_job_request()

        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_conn = self.connection_manager._choose_request_connection(current_request)
        self.assertEqual(chosen_conn, good_connection)

        # No state changed so we should still go there
        chosen_conn = self.connection_manager._choose_request_connection(current_request)
        self.assertEqual(chosen_conn, good_connection)

        # Pretend like our good connection died so we'll need to choose somethign else
        good_connection._should_fail_on_bind = True
        good_connection.connected = False

        failed_then_retried_connection._should_fail_on_connect = False
        failed_then_retried_connection.connected = True

        # Make sure we rotate good_connection and failed_connection out
        chosen_conn = self.connection_manager._choose_request_connection(current_request)
        self.assertEqual(chosen_conn, failed_then_retried_connection)
    def test_work_with_no_live_connections(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._should_fail_on_bind = True
        dead_connection.connected = False
        self.connection_manager.connection_list = [dead_connection]

        self.assertRaises(ServerUnavailable, self.connection_manager.work)
    def test_no_connections_for_rotation_for_requests(self):
        self.connection_manager.connection_list = []
        self.connection_manager.command_handlers = {}

        current_request = self.generate_job_request()

        # No connections == death
        self.assertRaises(ServerUnavailable, self.connection_manager._choose_request_connection, current_request)

        # Spin up a bunch of imaginary gearman connections
        failed_connection = MockGearmanConnection()
        failed_connection._should_fail_on_bind = True
        failed_connection.connected = False
        self.connection_manager.connection_list.append(failed_connection)

        # All failed connections == death
        self.assertRaises(ServerUnavailable, self.connection_manager._choose_request_connection, current_request)