Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed compression level to constant #103

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ccx_messaging/publishers/kafka_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from ccx_messaging.error import CCXMessagingError

log = logging.getLogger(__name__)

BEST_COMPRESSION = 9

class KafkaPublisher(Publisher):
"""
Expand Down Expand Up @@ -70,7 +70,7 @@ def __init__(self, outgoing_topic: str, kafka_broker_config: dict = None, **kwar
def produce(self, outgoing_message: bytes):
"""Send the message though the Kafka producer."""
if self.compression:
self.producer.produce(self.topic, gzip.compress(outgoing_message,compresslevel=9))
self.producer.produce(self.topic, gzip.compress(outgoing_message,compresslevel=BEST_COMPRESSION))
else:
self.producer.produce(self.topic, outgoing_message)
self.producer.poll(0)
Expand Down
16 changes: 14 additions & 2 deletions test/publishers/kafka_publisher_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@
}
),
]
BEST_COMPRESSION = 9

def timeStampMasking(message):
message=list(message)
message[4] = 0
message[5] = 0
message[6] = 0
message[7] = 0
message = bytes(message)
return message

def test_init():
"""Check that init creates a valid object."""
Expand All @@ -125,15 +135,17 @@ def test_init_compression():
@pytest.mark.parametrize("input", VALID_INPUT_MSG)
def test_compressing_enabled(input):
input = bytes(json.dumps(input) + "\n",'utf-8')
expected_output = gzip.compress(input)
expected_output = timeStampMasking(gzip.compress(input,compresslevel=BEST_COMPRESSION))
kakfa_config = {
"bootstrap.servers": "kafka:9092",
"compression" : "gzip"
}
pub = KafkaPublisher(outgoing_topic="topic-name", **kakfa_config)
pub.producer = MagicMock()
pub.produce(input)
pub.producer.produce.assert_called_with("topic-name",expected_output)
outgoing_topic = pub.producer.produce.call_args[0][0]
outgoing_message = timeStampMasking(pub.producer.produce.call_args[0][1])
assert outgoing_message == expected_output and outgoing_topic == "topic-name"

@pytest.mark.parametrize("input", VALID_INPUT_MSG)
def test_compressing_disabled(input):
Expand Down