forked from dsully/pykafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_kafka.py
167 lines (127 loc) · 5.88 KB
/
test_kafka.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import logging
import time
import unittest
from kafka import (
Kafka,
LATEST_OFFSET, EARLIEST_OFFSET, Lengths,
ConnectionFailure,
OffsetOutOfRange,
InvalidOffset,
)
try:
from tornado.testing import AsyncTestCase, LogTrapTestCase
from kafka.nonblocking import KafkaTornado
has_tornado = True
except ImportError:
has_tornado = False
# Messages are not available to clients until they have been flushed.
# By default is is 1000ms, see log.default.flush.interval.ms in
# server.properties
MESSAGE_DELAY_SECS = (1000 * 2) / 1000
def get_unique_topic(name):
return '{0}-{1}'.format(time.time(), name)
class TestKafkaBlocking(unittest.TestCase):
def test_kafka(self):
kafka = Kafka()
topic = get_unique_topic('test-kafka')
start_offset = 0
input_messages = ['message0', 'message1', 'message2']
kafka.produce(topic, input_messages)
time.sleep(MESSAGE_DELAY_SECS)
fetch_results = kafka.fetch(topic, start_offset)
output_messages = []
offsets = []
for offset, output_message in fetch_results:
output_messages.append(output_message)
offsets.append(offset)
self.assertEquals(input_messages, output_messages)
actual_latest_offsets = kafka.offsets(topic, LATEST_OFFSET,
max_offsets=1)
self.assertEquals(len(actual_latest_offsets), 1)
expected_latest_offset = offsets[-1] + Lengths.MESSAGE_HEADER \
+ len(output_messages[-1])
self.assertEquals(expected_latest_offset, actual_latest_offsets[0])
actual_earliest_offsets = kafka.offsets(topic, EARLIEST_OFFSET,
max_offsets=1)
self.assertEquals(len(actual_earliest_offsets), 1)
self.assertEquals(0, actual_earliest_offsets[0])
def test_cant_connect(self):
kafka = Kafka(host=str(time.time()))
topic = get_unique_topic('test-cant-connect')
self.assertRaises(ConnectionFailure, kafka.produce, topic,
'wont appear')
if has_tornado:
class TestKafkaTornado(AsyncTestCase, LogTrapTestCase):
def test_kafka_tornado(self):
kafka = KafkaTornado(io_loop=self.io_loop)
topic = get_unique_topic('test-kafka-tornado')
start_offset = 0
input_messages = ['message0', 'message1', 'message2']
kafka.produce(topic, input_messages, callback=self.stop)
self.wait()
# By default messages may not be available for up to 1 second.
time.sleep(MESSAGE_DELAY_SECS)
kafka.fetch(topic, start_offset,
callback=self.stop)
fetch_results = self.wait()
output_messages = []
offsets = []
for offset, output_message in fetch_results:
output_messages.append(output_message)
offsets.append(offset)
self.assertEquals(input_messages, output_messages)
kafka.offsets(topic, LATEST_OFFSET,
max_offsets=1, callback=self.stop)
actual_latest_offsets = self.wait()
self.assertEquals(len(actual_latest_offsets), 1)
expected_latest_offset = offsets[-1] + Lengths.MESSAGE_HEADER \
+ len(output_messages[-1])
self.assertEquals(expected_latest_offset,
actual_latest_offsets[0])
kafka.offsets(topic, EARLIEST_OFFSET,
max_offsets=1, callback=self.stop)
actual_earliest_offsets = self.wait()
self.assertEquals(len(actual_earliest_offsets), 1)
self.assertEquals(0, actual_earliest_offsets[0])
def test_cant_connect(self):
kafka = KafkaTornado(host=str(time.time()), io_loop=self.io_loop)
topic = get_unique_topic('test-cant-connect')
self.assertRaises(ConnectionFailure, kafka.produce, topic,
'wont appear')
class TestTopic(unittest.TestCase):
# Contents of self.dogs_queue after setUp:
# [(0, 'Rusty'), (14, 'Patty'), (28, 'Jack'), (41, 'Clyde')]
def setUp(self):
self.k = Kafka()
self.topic_name = get_unique_topic('test-kafka-topic')
input_messages = ['Rusty', 'Patty', 'Jack', 'Clyde']
self.k.produce(self.topic_name, input_messages)
# If you don't do this sleep, then you can get into a condition where
# a fetch immediately after a produce will cause a state where the
# produce is duplicated (it really gets that way in Kafka).
time.sleep(MESSAGE_DELAY_SECS)
self.dogs_queue = self.k.topic(self.topic_name)
# print list(self.k.fetch(self.topic_name, 0))
# print self.topic_name
def test_offset_queries(self):
self.assertEqual(self.dogs_queue.earliest_offset(), 0)
self.assertEqual(self.dogs_queue.latest_offset(), 55)
self.assertRaises(OffsetOutOfRange, self.dogs_queue.poll(100).next)
self.assertRaises(InvalidOffset, self.dogs_queue.poll(22).next)
def test_end_offset_iteration(self):
dogs = self.dogs_queue.poll(0, end_offset=28, poll_interval=None)
status, messages = dogs.next()
self.assertEqual(status.start_offset, 0)
self.assertEqual(status.next_offset, 41)
self.assertEqual(status.last_offset_read, 28)
self.assertEqual(status.messages_read, 3)
self.assertEqual(status.bytes_read, 14)
self.assertEqual(status.num_fetches, 1)
self.assertEqual(messages, ['Rusty', 'Patty', 'Jack'])
self.assertRaises(StopIteration, dogs.next)
if __name__ == '__main__':
logging.basicConfig(
format='%(asctime)s,%(msecs)03d %(levelname)-5.5s [%(name)s] %(filename)s:%(lineno)s %(message)s',
level=logging.DEBUG
)
unittest.main()