A Kafka mock library that is designed to be used in integration tests for applications using librdkafka.
Project description
Embedded Kafka (Kafka Simulator) for Python
Embedded Kafka is a mocking library for the confluent_kafka library used for Apache Kafka. This library allows integration tests to utilize Producer and Consumer instances without an actual connection to a Kafka Cluster.
Project Overview
The main component of this project is a process called KafkaSimulator which simulates the behavior of an actual Kafka Cluster, within the bounds of implementation limitations. The current version includes a KProducer class that acts as a mock for the Producer from the confluent_kafka package. A Consumer class is still under development.
Table of Contents
Installation
Official Release
pip install kafka_mocha
or using your favorite package manager, e.g. poetry:
poetry add kafka_mocha
Prerelease or Development Version
From GitHub (development version):
pip install git+https://github.com/Effiware/kafka-mocha@develop
or as published (prerelease) version:
poetry add kafka_mocha --allow-prereleases
Usage
Starting Kafka Simulator
Kafka Simulator is automatically run whenever any instance of KProdcer (e.g. via mock_producer) is created.
So there is no need to manually start it.
Upon default logging settings a custom start-up messages might be visible:
INFO kafka_simulator > Kafka Simulator initialized
INFO ticking_thread > Buffer for KProducer(4368687344): ticking initialized
INFO buffer_handler > Buffer for KProducer(4368687344) has been primed, size: 300, timeout: 2
INFO kafka_simulator > Kafka Simulator initialized
INFO kafka_simulator > Handle producers has been primed
INFO kafka_simulator > Kafka Simulator initialized
INFO ticking_thread > Buffer for KProducer(4368687344): ticking started
KProducer
To use the KProducer class in your tests, you need to import it from the kafka_simulator package:
from confluent_kafka import Producer
from kafka_mocha import mock_producer
# Example usage
@mock_producer
def producer_factory(conf):
return Producer(conf)
producer = producer_factory({'bootstrap.servers': 'localhost:9092'})
producer.produce(topic='test-topic', value='Test message')
producer.flush()
The KProducer class replicates the interface and behavior of the Producer class from the confluent_kafka library.
Contributing
We welcome contributions! Before posting your first PR, please see our contributing guidelines for more details.
Also, bear in mind that this project uses Poetry for dependency management. If you are not familiar with it, please first read the Poetry documentation and:
- Setup poetry environment (recommended)
- Don't overwrite the
pyproject.tomlfile manually (Poetry will do it for you) - Don't recreate the
poetry.lock(unless you know what you are doing)
Cloning the repository
git clone git@github.com:Effiware/kafka-mocha.git
cd kafka-mocha
Installing dependencies
Default (and recommended) way:
poetry install --with test
Standard way:
poetry export -f requirements.txt --output requirements.txt
pip install -r requirements.txt
Running tests
Currently, test configuration is set up to run with pytest and kept in pytest.ini file. You can run them with:
poetry run pytest tests
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kafka_mocha-0.1.0a5.tar.gz.
File metadata
- Download URL: kafka_mocha-0.1.0a5.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
179fc2637483471a3be32526cd349e7fc0645090c3800856f489381c7a12ac0a
|
|
| MD5 |
eb1a6eab5dec3663901c3b379ceccb90
|
|
| BLAKE2b-256 |
349f1be6bc8521a3324cf2dc5703cf1919c3e31693b49f11e7538e3e3e15784e
|
Provenance
The following attestation bundles were made for kafka_mocha-0.1.0a5.tar.gz:
Publisher:
pypi-publish.yml on Effiware/kafka-mocha
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kafka_mocha-0.1.0a5.tar.gz -
Subject digest:
179fc2637483471a3be32526cd349e7fc0645090c3800856f489381c7a12ac0a - Sigstore transparency entry: 161815068
- Sigstore integration time:
-
Permalink:
Effiware/kafka-mocha@325eb081da92a33ac8cca43d684049ed80d9333a -
Branch / Tag:
refs/tags/0.1.0a5 - Owner: https://github.com/Effiware
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@325eb081da92a33ac8cca43d684049ed80d9333a -
Trigger Event:
release
-
Statement type:
File details
Details for the file kafka_mocha-0.1.0a5-py3-none-any.whl.
File metadata
- Download URL: kafka_mocha-0.1.0a5-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f1828b4b4387ddcf7f4ae319c7f060d885d5541450c13ca58705f3f7cc91f599
|
|
| MD5 |
7fb2e86708d881c2257214515ba32886
|
|
| BLAKE2b-256 |
77db9a79e5ab69a97c89dc7c405140bbfec5bfa645014d62daba5e71a862adda
|
Provenance
The following attestation bundles were made for kafka_mocha-0.1.0a5-py3-none-any.whl:
Publisher:
pypi-publish.yml on Effiware/kafka-mocha
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
kafka_mocha-0.1.0a5-py3-none-any.whl -
Subject digest:
f1828b4b4387ddcf7f4ae319c7f060d885d5541450c13ca58705f3f7cc91f599 - Sigstore transparency entry: 161815069
- Sigstore integration time:
-
Permalink:
Effiware/kafka-mocha@325eb081da92a33ac8cca43d684049ed80d9333a -
Branch / Tag:
refs/tags/0.1.0a5 - Owner: https://github.com/Effiware
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-publish.yml@325eb081da92a33ac8cca43d684049ed80d9333a -
Trigger Event:
release
-
Statement type: