コード例 #1
0
def test__KafkaProducer_should_successfully_connect_to_kafka_broker(mocker):
    class StubbedProducerWithNoConnection:
        def poll(self, interval):
            raise Exception

    mocker.patch.object(kafka, "create_producer")
    kafka.create_producer.return_value = StubbedProducerWithNoConnection()
    with pytest.raises(kafka.NoProducerInstantiatedError) as excinfo:
        kafka_producer = kafka.KafkaProducer(
            raise_exception_on_failed_connection=True)
        kafka_producer.instantiate_producer()
    assert excinfo.typename == "NoProducerInstantiatedError"
コード例 #2
0
def test__KafkaProducer_should_successfully_connect_to_kafka_broker(mocker):
    class StubbedProducer:
        def poll(self, interval):
            return True

    mocker.patch.object(kafka, "create_producer")
    kafka.create_producer.return_value = StubbedProducer()

    kafka_producer = kafka.KafkaProducer()
    kafka_producer.instantiate_producer()

    assert kafka_producer.producer is not None
コード例 #3
0
def test__KafkaProducer_should_successfully_publish_dict_as_msg(mocker):
    class StubbedProducer:
        def produce(self, msg):
            return None

    in_dict = {'someKey': 'some value'}

    mocker.patch.object(kafka, "create_producer")
    kafka.create_producer.return_value = StubbedProducer()
    kafka_producer = kafka.KafkaProducer()
    published_dict = kafka_producer.publish_dict_as_json(in_dict)

    assert published_dict == '{"someKey": "some value"}'
コード例 #4
0
def test__KafkaProducer_should_raise_an_exception_if_it_cant_publish(mocker):
    in_dict = {'someKey': 'some value'}

    class StubbedProducerWithNoConnection:
        def poll(self, interval):
            raise Exception

    mocker.patch.object(kafka, "create_producer")
    kafka.create_producer.return_value = StubbedProducerWithNoConnection()

    with pytest.raises(kafka.NoProducerInstantiatedError) as excinfo:
        kafka_producer = kafka.KafkaProducer(
            raise_exception_on_failed_connection=True)
        published_dict = kafka_producer.publish_dict_as_json(in_dict)

    assert excinfo.typename == "NoProducerInstantiatedError"