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

publish_all() method for Topic [API-1529] #570

Merged
merged 8 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions examples/topic/topic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ def on_message(event):
topic.publish("Message " + str(i))
time.sleep(0.1)

topic.publish_all(["m1", "m2", "m3", "m4", "m5"])
time.sleep(1)

client.shutdown()
18 changes: 18 additions & 0 deletions hazelcast/protocol/codec/topic_publish_all_codec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from hazelcast.protocol.client_message import OutboundMessage, REQUEST_HEADER_SIZE, create_initial_buffer
from hazelcast.protocol.builtin import StringCodec
from hazelcast.protocol.builtin import ListMultiFrameCodec
from hazelcast.protocol.builtin import DataCodec

# hex: 0x040400
_REQUEST_MESSAGE_TYPE = 263168
# hex: 0x040401
_RESPONSE_MESSAGE_TYPE = 263169

_REQUEST_INITIAL_FRAME_SIZE = REQUEST_HEADER_SIZE


def encode_request(name, messages):
buf = create_initial_buffer(_REQUEST_INITIAL_FRAME_SIZE, _REQUEST_MESSAGE_TYPE)
StringCodec.encode(buf, name)
ListMultiFrameCodec.encode(buf, messages, DataCodec.encode, True)
return OutboundMessage(buf, False)
31 changes: 30 additions & 1 deletion hazelcast/proxy/topic.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import typing

from hazelcast.errors import NullPointerError
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
from hazelcast.future import Future
from hazelcast.protocol.codec import (
topic_add_message_listener_codec,
topic_publish_codec,
topic_publish_all_codec,
topic_remove_message_listener_codec,
)
from hazelcast.proxy.base import PartitionSpecificProxy, TopicMessage
Expand Down Expand Up @@ -57,7 +59,7 @@ def handle(item_data, publish_time, uuid):
)

def publish(self, message: MessageType) -> Future[None]:
"""Publishes the message to all subscribers of this topic
"""Publishes the message to all subscribers of this topic.

Args:
message: The message to be published.
Expand All @@ -70,6 +72,27 @@ def publish(self, message: MessageType) -> Future[None]:
request = topic_publish_codec.encode_request(self.name, message_data)
return self._invoke(request)

def publish_all(self, messages: typing.Sequence[MessageType]) -> Future[None]:
"""Publishes the messages to all subscribers of this topic.

Args:
messages: The messages to be published.
"""
try:
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
if messages is None:
raise NullPointerError("Null message is not allowed!")
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
data_list = []
for m in messages:
data = self._to_data(m)
if data is None:
raise NullPointerError("Null message is not allowed!")
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
data_list.append(data)
except SchemaNotReplicatedError as e:
return self._send_schema_and_retry(e, self.publish_all, messages)

request = topic_publish_all_codec.encode_request(self.name, data_list)
return self._invoke(request)

def remove_listener(self, registration_id: str) -> Future[bool]:
"""Stops receiving messages for the given message listener.

Expand Down Expand Up @@ -107,6 +130,12 @@ def publish( # type: ignore[override]
) -> None:
return self._wrapped.publish(message).result()

def publish_all( # type: ignore[override]
self,
messages: typing.Sequence[MessageType]
) -> None:
return self._wrapped.publish_all(messages).result()

def remove_listener( # type: ignore[override]
self,
registration_id: str,
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/backward_compatible/proxy/topic_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from hazelcast.errors import NullPointerError
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
from tests.base import SingleMemberTestCase
from tests.util import random_string, event_collector

Expand Down Expand Up @@ -44,3 +45,24 @@ def assert_event():

def test_str(self):
self.assertTrue(str(self.topic).startswith("Topic"))

def test_publish_all(self):
collector = event_collector()
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
self.topic.add_listener(on_message=collector)

messages = ["message1", "message2", "message3"]
self.topic.publish_all(messages)

def assert_event():
self.assertEqual(len(collector.events), 3)

self.assertTrueEventually(assert_event, 5)

def test_publish_all_none_argument(self):
with self.assertRaises(NullPointerError):
self.topic.publish_all(None)

def test_publish_all_none_message(self):
messages = ["message1", None, "message3"]
mdumandag marked this conversation as resolved.
Show resolved Hide resolved
with self.assertRaises(NullPointerError):
self.topic.publish_all(messages)