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

Replace kafka-python with kafka-python-ng on Jupyter notebooks #704

Merged
merged 2 commits into from
Sep 20, 2024

Conversation

atheo89
Copy link
Member

@atheo89 atheo89 commented Sep 19, 2024

Related to: https://issues.redhat.com/browse/RHOAIENG-4475

Description

Replace kafka-python with kafka-python-ng as it is the same api but from different working fork: https://github.com/wbarnha/kafka-python-ng

How Has This Been Tested?

  • Spin up a jupyter notebook generated from this PR
    podman run -it -p 8888:8888 ghcr.io/atheo89/notebooks/workbench-images:jupyter-trustyai-ubi9-python-3.11-RHOAIENG-4475_dad8405dfc6b16ff6318147ce47ce656b479056e
  • Run the following mockup code to test kafka python api functionality
from kafka import KafkaProducer, KafkaConsumer
from unittest import mock
import json

# Mocking KafkaProducer to simulate sending a message
@mock.patch('kafka.producer.KafkaProducer.send')
@mock.patch('kafka.producer.KafkaProducer.__init__', return_value=None)
def test_producer(mock_producer_init, mock_send):
    # Create a mock producer instance
    producer = KafkaProducer(value_serializer=lambda v: json.dumps(v).encode('utf-8'))

    # Simulate sending a message
    test_message = {'key': 'value'}
    producer.send('test-topic', value=test_message)

    # Assert that the send method was called with the correct topic and value
    mock_send.assert_called_once_with('test-topic', value=test_message)
    print("Producer test passed: Message sent successfully.")

# Mocking KafkaConsumer to simulate receiving a message
@mock.patch('kafka.consumer.KafkaConsumer.__iter__', return_value=iter([mock.Mock(value=json.dumps({'key': 'value'}).encode('utf-8'))]))
@mock.patch('kafka.consumer.KafkaConsumer.__init__', return_value=None)
def test_consumer(mock_consumer_init, mock_consumer_iter):
    # Create a mock consumer instance
    consumer = KafkaConsumer(value_deserializer=lambda v: json.loads(v.decode('utf-8')))

    # Simulate consuming a message
    for message in consumer:
        print(f"Consumer test passed: Received message - {message.value}")
        break

if __name__ == '__main__':
    test_producer()
    test_consumer()

expected output

Producer test passed: Message sent successfully.
Consumer test passed: Received message - {'key': 'value'}

Merge criteria:

  • The commits are squashed in a cohesive manner and have meaningful messages.
  • Testing instructions have been added in the PR body (for PRs involving changes that are not immediately obvious).
  • The developer has manually tested the changes and verified that the changes work

@jstourac
Copy link
Member

/lgtm

Copy link
Contributor

openshift-ci bot commented Sep 20, 2024

[APPROVALNOTIFIER] This PR is APPROVED

Approval requirements bypassed by manually added approval.

This pull-request has been approved by:

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@atheo89
Copy link
Member Author

atheo89 commented Sep 20, 2024

/override ci/prow/rocm-notebooks-e2e-tests
/override ci/prow/notebooks-ubi9-e2e-tests
/override ci/prow/images

Copy link
Contributor

openshift-ci bot commented Sep 20, 2024

@atheo89: Overrode contexts on behalf of atheo89: ci/prow/images, ci/prow/notebooks-ubi9-e2e-tests, ci/prow/rocm-notebooks-e2e-tests

In response to this:

/override ci/prow/rocm-notebooks-e2e-tests
/override ci/prow/notebooks-ubi9-e2e-tests
/override ci/prow/images

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@openshift-merge-bot openshift-merge-bot bot merged commit fd483ad into opendatahub-io:main Sep 20, 2024
19 checks passed
@atheo89 atheo89 deleted the RHOAIENG-4475 branch October 23, 2024 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants