summaryrefslogtreecommitdiff
path: root/test/test_integration.py
diff options
context:
space:
mode:
authorJim Lim <jim@quixey.com>2013-09-27 14:02:10 -0700
committerJim Lim <jim@quixey.com>2013-10-04 14:49:44 -0700
commita6c99b287b1cf9c39068be74d72150808588dd43 (patch)
tree0c036a3d0691c6c7f69166bdc373fbe4e79b2705 /test/test_integration.py
parentcfd9f86e60429d1f7af8bcac5849808354b8719e (diff)
downloadkafka-python-a6c99b287b1cf9c39068be74d72150808588dd43.tar.gz
make changes to be more fault tolerant: clean up connections, brokers, failed_messages
- add integration tests for sync producer - add integration tests for async producer w. leadership election - use log.exception
Diffstat (limited to 'test/test_integration.py')
-rw-r--r--test/test_integration.py107
1 files changed, 107 insertions, 0 deletions
diff --git a/test/test_integration.py b/test/test_integration.py
index d8ead59..a10dae2 100644
--- a/test/test_integration.py
+++ b/test/test_integration.py
@@ -770,6 +770,113 @@ class TestConsumer(unittest.TestCase):
self.assertEquals(all_messages[i], message.message)
self.assertEquals(i, 19)
+class TestFailover(unittest.TestCase):
+
+ @classmethod
+ def setUpClass(cls):
+
+ zk_chroot = random_string(10)
+ replicas = 2
+ partitions = 2
+
+ # mini zookeeper, 2 kafka brokers
+ cls.zk = ZookeeperFixture.instance()
+ kk_args = [cls.zk.host, cls.zk.port, zk_chroot, replicas, partitions]
+ cls.brokers = [KafkaFixture.instance(i, *kk_args) for i in range(replicas)]
+ cls.client = KafkaClient(cls.brokers[0].host, cls.brokers[0].port)
+
+ @classmethod
+ def tearDownClass(cls):
+ cls.client.close()
+ for broker in cls.brokers:
+ broker.close()
+ cls.zk.close()
+
+ def test_switch_leader(self):
+
+ key, topic, partition = random_string(5), 'test_switch_leader', 0
+ producer = SimpleProducer(self.client, topic)
+
+ for i in range(1, 4):
+
+ # XXX unfortunately, the conns dict needs to be warmed for this to work
+ # XXX unfortunately, for warming to work, we need at least as many partitions as brokers
+ self._send_random_messages(producer, 10)
+
+ # kil leader for partition 0
+ broker = self._kill_leader(topic, partition)
+
+ # expect failure, reload meta data
+ with self.assertRaises(FailedPayloadsException):
+ producer.send_messages('part 1')
+ producer.send_messages('part 2')
+ time.sleep(1)
+
+ # send to new leader
+ self._send_random_messages(producer, 10)
+
+ broker.open()
+ time.sleep(3)
+
+ # count number of messages
+ count = self._count_messages('test_switch_leader group %s' % i, topic)
+ self.assertIn(count, range(20 * i, 22 * i + 1))
+
+ producer.stop()
+
+ def test_switch_leader_async(self):
+
+ key, topic, partition = random_string(5), 'test_switch_leader_async', 0
+ producer = SimpleProducer(self.client, topic, async=True)
+
+ for i in range(1, 4):
+
+ self._send_random_messages(producer, 10)
+
+ # kil leader for partition 0
+ broker = self._kill_leader(topic, partition)
+
+ # expect failure, reload meta data
+ producer.send_messages('part 1')
+ producer.send_messages('part 2')
+ time.sleep(1)
+
+ # send to new leader
+ self._send_random_messages(producer, 10)
+
+ broker.open()
+ time.sleep(3)
+
+ # count number of messages
+ count = self._count_messages('test_switch_leader_async group %s' % i, topic)
+ self.assertIn(count, range(20 * i, 22 * i + 1))
+
+ producer.stop()
+
+ def _send_random_messages(self, producer, n):
+ for j in range(n):
+ resp = producer.send_messages(random_string(10))
+ if len(resp) > 0:
+ self.assertEquals(resp[0].error, 0)
+ time.sleep(1) # give it some time
+
+ def _kill_leader(self, topic, partition):
+ leader = self.client.topics_to_brokers[TopicAndPartition(topic, partition)]
+ broker = self.brokers[leader.nodeId]
+ broker.close()
+ time.sleep(1) # give it some time
+ return broker
+
+ def _count_messages(self, group, topic):
+ client = KafkaClient(self.brokers[0].host, self.brokers[0].port)
+ consumer = SimpleConsumer(client, group, topic, auto_commit=False)
+ all_messages = []
+ for message in consumer:
+ all_messages.append(message)
+ consumer.stop()
+ client.close()
+ return len(all_messages)
+
def random_string(l):
s = "".join(random.choice(string.letters) for i in xrange(l))