test_publish_subscribe.py 9.77 KB
Newer Older
Blazej's avatar
Blazej committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import asyncio
import dataclasses
import uuid
from typing import List

import pytest
from utils import event_plane, nats_server

from triton_distributed.icp import Event, EventTopic, NatsEventPlane

pytestmark = pytest.mark.pre_merge


@pytest.mark.asyncio
class TestEventPlaneFunctional:
    @pytest.mark.asyncio
    async def test_single_publisher_subscriber(self, nats_server, event_plane):
        print(f"Print loop test: {id(asyncio.get_running_loop())}")

        received_events: List[Event] = []

        async def callback(event):
            received_events.append(event)
            print(event)

        event_topic = EventTopic(["test", "event_topic"])
        event_type = "test_event"
        event = b"test_payload"

        await event_plane.subscribe(
            callback, event_topic=event_topic, event_type=event_type
        )
        event_metadata = await event_plane.publish(event, event_type, event_topic)

        # Allow time for message to propagate
        await asyncio.sleep(2)

        assert len(received_events) == 1
        assert received_events[0].event_id == event_metadata.event_id

    @pytest.mark.asyncio
    async def test_single_publisher_subscriber_iterator(self, nats_server, event_plane):
        print(f"Print loop test: {id(asyncio.get_running_loop())}")

        received_events: List[Event] = []

        event_topic = EventTopic(["test", "event_topic"])
        event_type = "test_event"
        event = b"test_payload"

        subscription = await event_plane.subscribe(
            event_topic=event_topic, event_type=event_type
        )
        event_metadata = await event_plane.publish(
            event, event_topic=event_topic, event_type=event_type
        )

        # Allow time for message to propagate
        await asyncio.sleep(2)

        async for x in subscription:
            print(x.timestamp)
            print(x.event_id)
            print(x.event_type)
            print(x.event_topic)
            print(x.payload)
            received_events.append(x)
            break

        assert len(received_events) == 1
        assert received_events[0].event_id == event_metadata.event_id

    @pytest.mark.asyncio
    async def test_default_subscription(self, nats_server, event_plane):
        print(f"Print loop test: {id(asyncio.get_running_loop())}")

        received_events: List[Event] = []

        event = b"test_payload"

        subscription = await event_plane.subscribe()
        event_metadata = await event_plane.publish(
            event,
        )

        # Allow time for message to propagate
        await asyncio.sleep(2)

        async for x in subscription:
            print(x.timestamp)
            print(x.event_id)
            print(x.event_type)
            print(x.event_topic)
            print(x.payload)
            received_events.append(x)
            break

        assert len(received_events) == 1
        assert received_events[0].event_id == event_metadata.event_id

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
    @pytest.mark.asyncio
    async def test_event_topic_list(self, nats_server, event_plane):
        print(f"Print loop test: {id(asyncio.get_running_loop())}")

        received_events: List[Event] = []

        event = b"test_payload"

        subscription = await event_plane.subscribe(event_topic="hello")
        event_metadata = await event_plane.publish(event, event_topic=["hello"])

        # Allow time for message to propagate
        await asyncio.sleep(2)

        async for x in subscription:
            print(x.timestamp)
            print(x.event_id)
            print(x.event_type)
            print(x.event_topic)
            print(x.payload)
            received_events.append(x)
            break

        assert len(received_events) == 1
        assert received_events[0].event_id == event_metadata.event_id

Blazej's avatar
Blazej committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
    @pytest.mark.asyncio
    async def test_custom_type(self, nats_server, event_plane):
        print(f"Print loop test: {id(asyncio.get_running_loop())}")

        received_events: List[Event] = []

        @dataclasses.dataclass
        class MyEvent:
            test: str
            index: int

        event = MyEvent("hello", 0)

        subscription = await event_plane.subscribe()
        event_metadata = await event_plane.publish(
            event,
        )

        # Allow time for message to propagate
        await asyncio.sleep(2)

        async for x in subscription:
            print(x.timestamp)
            print(x.event_id)
            print(x.event_type)
            print(x.event_topic)
            print(x.payload)
            print(x.typed_payload(MyEvent))
            received_events.append(x)
            break

        assert len(received_events) == 1
        assert received_events[0].event_id == event_metadata.event_id
        assert isinstance(received_events[0].typed_payload(MyEvent), type(event))
        assert isinstance(received_events[0].typed_payload(dict), dict)

    @pytest.mark.asyncio
    async def test_one_publisher_multiple_subscribers(self, nats_server):
        results_1: List[Event] = []
        results_2: List[Event] = []
        results_3: List[Event] = []

        async def callback_1(event):
            results_1.append(event)

        async def callback_2(event):
            results_2.append(event)

        async def callback_3(event):
            results_3.append(event)

        event_topic = EventTopic(["test"])
        event_type = "multi_event"
        event = b"multi_payload"

        # async with event_plane_context() as event_plane1:
        server_url = "tls://localhost:4222"

        component_id = uuid.uuid4()
        event_plane2 = NatsEventPlane(server_url, component_id)
        try:
            await event_plane2.connect()

            try:
                subscription1 = await event_plane2.subscribe(
                    callback_1, event_topic=event_topic
                )
                try:
                    subscription2 = await event_plane2.subscribe(
                        callback_2, event_topic=event_topic
                    )
                    try:
                        subscription3 = await event_plane2.subscribe(
                            callback_3, event_type=event_type
                        )

                        component_id = uuid.uuid4()
                        event_plane1 = NatsEventPlane(server_url, component_id)
                        try:
                            await event_plane1.connect()

                            ch1 = EventTopic(["test", "1"])
                            ch2 = EventTopic(["test", "2"])
                            await event_plane1.publish(event, event_type, ch1)
                            await event_plane1.publish(event, event_type, ch2)

                            # Allow time for message propagation
                            await asyncio.sleep(2)

                            assert len(results_1) == 2
                            assert len(results_2) == 2
                            assert len(results_3) == 2
                        finally:
                            await event_plane1.disconnect()
                    finally:
                        await subscription3.unsubscribe()
                finally:
                    await subscription2.unsubscribe()
            finally:
                await subscription1.unsubscribe()

        finally:
            await event_plane2.disconnect()

    @pytest.mark.asyncio
    async def test_context_manager(self, nats_server):
        """Test that context managers properly handle connection/disconnection and subscription/unsubscription."""
        received_events: List[Event] = []
        event_topic = EventTopic(["test", "event_topic"])
        event_type = "test_event"
        event = b"test_payload"

        # Test successful operation with context managers
        async with NatsEventPlane() as plane:
            assert plane.is_connected()

            async def callback(event):
                received_events.append(event)

            async with await plane.subscribe(
                callback, event_topic=event_topic, event_type=event_type
            ) as subscription:
                assert subscription._nc_sub is not None
                event_metadata = await plane.publish(event, event_type, event_topic)
                await asyncio.sleep(2)  # Allow time for message to propagate

            # After subscription context, should be unsubscribed
            assert subscription._nc_sub is None

        # After plane context, should be disconnected
        assert not plane.is_connected()
        assert len(received_events) == 1
        assert received_events[0].event_id == event_metadata.event_id

        # Test error handling in context managers
        with pytest.raises(RuntimeError):
            async with NatsEventPlane() as plane:
                async with await plane.subscribe(
                    callback, event_topic=event_topic, event_type=event_type
                ):
                    raise RuntimeError("Test error")
                # Should not reach here
                pytest.fail("Should have raised exception")
            # Should not reach here
            pytest.fail("Should have raised exception")

        # Even after error, resources should be cleaned up
        assert not plane.is_connected()