async def test_producer_and_consumer_same_group(fake_kafka_server): fake_kafka.FakeKafkaServer._init_topics(partitions_per_topic=2) producer = fake_kafka.AIOKafkaProducer() await producer.start() assert producer.started consumer1 = fake_kafka.AIOKafkaConsumer("my_topic", group_id='a') consumer2 = fake_kafka.AIOKafkaConsumer("my_topic", group_id='a') await consumer1.start() await consumer2.start() assert consumer1.started assert consumer2.started try: await producer.send_and_wait("my_topic", b"Super message1") await producer.send_and_wait("my_topic", b"Super message2") assert len(fake_kafka_server.topics['my_topic'][0]) == 1 assert fake_kafka_server.topics['my_topic'][0][0].value == b"Super message1" assert fake_kafka_server.topics['my_topic'][1][0].value == b"Super message2" ai = consumer1.__aiter__() msg = await ai.__anext__() assert msg.partition == 0 assert msg.offset == 0 assert msg.value == b"Super message1" ai = consumer2.__aiter__() msg = await ai.__anext__() assert msg.partition == 1 assert msg.offset == 0 assert msg.value == b"Super message2" finally: await consumer1.stop() await producer.stop() await consumer2.stop() assert consumer1.stopped assert consumer2.stopped assert producer.stopped
async def test_producer_and_consumer(fake_kafka_server): producer = fake_kafka.AIOKafkaProducer() await producer.start() assert producer.started consumer = fake_kafka.AIOKafkaConsumer("my_topic") await consumer.start() assert consumer.started try: await producer.send_and_wait("my_topic", b"Super message1") await producer.send_and_wait("my_topic", b"Super message2") assert len(fake_kafka_server.topics['my_topic'][0]) == 2 assert fake_kafka_server.topics['my_topic'][0][0].value == b"Super message1" assert fake_kafka_server.topics['my_topic'][0][1].value == b"Super message2" ai = consumer.__aiter__() msg = await ai.__anext__() assert msg.partition == 0 assert msg.offset == 0 assert msg.value == b"Super message1" msg = await ai.__anext__() assert msg.partition == 0 assert msg.offset == 1 assert msg.value == b"Super message2" finally: await consumer.stop() await producer.stop() assert consumer.stopped assert producer.stopped
async def test_producer(fake_kafka_server): producer = fake_kafka.AIOKafkaProducer() await producer.start() assert producer.started try: await producer.send_and_wait("my_topic", b"Super message") finally: await producer.stop() assert producer.stopped assert len(fake_kafka_server.topics['my_topic']) == 1 assert fake_kafka_server.topics['my_topic'][0][0].value == b"Super message"
async def produce_messages(n, key, partition, csv1): producer = fake_kafka.AIOKafkaProducer(bootstrap_servers=['http://127.0.0.1:8000'], use_websocket=True) await producer.start() if partition is not None: partition = int(partition) start = time.time() for i in range(n): await producer.send_and_wait("my_topic", "Super message", key=key, partition=partition) end = time.time() print("{} messages sent in {} s for {} ns/m or {} m/s".format(n, end - start, (end - start) * 1000000 / n, int(n / (end - start)))) await producer.stop() with open(csv1, 'a') as f: writer = csv.writer(f) writer.writerow([n, end - start, (end - start) * 1000000 / n, int(n / (end - start))])