コード例 #1
0
    def test_switch_leader_async(self):
        key, topic, partition = random_string(5), self.topic, 0

        # Test the base class Producer -- send_messages to a specific partition
        producer = Producer(self.client, async=True)

        # Send 10 random messages
        self._send_random_messages(producer, topic, partition, 10)

        # kill leader for partition
        broker = self._kill_leader(topic, partition)

        logging.debug("attempting to send 'success' message after leader killed")

        # in async mode, this should return immediately
        producer.send_messages(topic, partition, 'success')

        # send to new leader
        self._send_random_messages(producer, topic, partition, 10)

        # wait until producer queue is empty
        while not producer.queue.empty():
            time.sleep(0.1)
        producer.stop()

        # count number of messages
        count = self._count_messages('test_switch_leader_async group', topic,
                                     partitions=(partition,))

        # Should be equal to 10 before + 1 recovery + 10 after
        self.assertEquals(count, 21)
コード例 #2
0
    def test_switch_leader(self):
        key, topic, partition = random_string(5), self.topic, 0

        # Test the base class Producer -- send_messages to a specific partition
        producer = Producer(self.client, async=False)

        # Send 10 random messages
        self._send_random_messages(producer, topic, partition, 10)

        # kill leader for partition
        broker = self._kill_leader(topic, partition)

        # expect failure, but dont wait more than 60 secs to recover
        recovered = False
        started = time.time()
        timeout = 60
        while not recovered and (time.time() - started) < timeout:
            try:
                logging.debug("attempting to send 'success' message after leader killed")
                producer.send_messages(topic, partition, 'success')
                logging.debug("success!")
                recovered = True
            except FailedPayloadsError, ConnectionError:
                logging.debug("caught exception sending message -- will retry")
                continue
コード例 #3
0
    def test_producer_message_types(self):

        producer = Producer(MagicMock())
        topic = "test-topic"
        partition = 0

        bad_data_types = (u'你怎么样?', 12, ['a','list'], ('a','tuple'), {'a': 'dict'})
        for m in bad_data_types:
            with self.assertRaises(TypeError):
                logging.debug("attempting to send message of type %s", type(m))
                producer.send_messages(topic, partition, m)

        good_data_types = ('a string!',)
        for m in good_data_types:
            # This should not raise an exception
            producer.send_messages(topic, partition, m)