| 1 |
"""Tests for event bus.""" |
| 2 |
|
| 3 |
from unittest.mock import AsyncMock |
| 4 |
|
| 5 |
import pytest |
| 6 |
from syrupy import SnapshotAssertion |
| 7 |
|
| 8 |
from hass_nabucasa.events import CloudEventBus |
| 9 |
from hass_nabucasa.events.bus import EventBusError |
| 10 |
from hass_nabucasa.events.types import ( |
| 11 |
CloudEventType, |
| 12 |
RelayerConnectedEvent, |
| 13 |
RelayerDisconnectedEvent, |
| 14 |
) |
| 15 |
from tests.common import extract_log_messages |
| 16 |
|
| 17 |
|
| 18 |
async def test_subscribe_and_publish( |
| 19 |
caplog: pytest.LogCaptureFixture, |
| 20 |
snapshot: SnapshotAssertion, |
| 21 |
): |
| 22 |
"""Test basic subscription and publishing.""" |
| 23 |
subscriber = AsyncMock() |
| 24 |
event_bus = CloudEventBus() |
| 25 |
|
| 26 |
unsubscribe = event_bus.subscribe( |
| 27 |
event_type=CloudEventType.RELAYER_CONNECTED, handler=subscriber |
| 28 |
) |
| 29 |
|
| 30 |
await event_bus.publish(event=RelayerConnectedEvent()) |
| 31 |
|
| 32 |
assert len(subscriber.call_args_list) == 1 |
| 33 |
|
| 34 |
unsubscribe() |
| 35 |
|
| 36 |
assert subscriber.call_args_list == snapshot |
| 37 |
assert extract_log_messages(caplog) == snapshot |
| 38 |
|
| 39 |
|
| 40 |
async def test_publish_without_data( |
| 41 |
caplog: pytest.LogCaptureFixture, |
| 42 |
snapshot: SnapshotAssertion, |
| 43 |
): |
| 44 |
"""Test publishing without extra fields.""" |
| 45 |
subscriber = AsyncMock() |
| 46 |
event_bus = CloudEventBus() |
| 47 |
|
| 48 |
event_bus.subscribe(event_type=CloudEventType.RELAYER_CONNECTED, handler=subscriber) |
| 49 |
|
| 50 |
event = RelayerConnectedEvent() |
| 51 |
await event_bus.publish(event=event) |
| 52 |
|
| 53 |
assert len(subscriber.call_args_list) == 1 |
| 54 |
assert subscriber.call_args_list[0][0][0].type == CloudEventType.RELAYER_CONNECTED |
| 55 |
assert extract_log_messages(caplog) == snapshot |
| 56 |
|
| 57 |
|
| 58 |
async def test_publish_without_subscribers( |
| 59 |
caplog: pytest.LogCaptureFixture, |
| 60 |
snapshot: SnapshotAssertion, |
| 61 |
): |
| 62 |
"""Test publishing without any subscribers doesn't raise.""" |
| 63 |
event_bus = CloudEventBus() |
| 64 |
|
| 65 |
event = RelayerConnectedEvent() |
| 66 |
await event_bus.publish(event=event) |
| 67 |
|
| 68 |
assert extract_log_messages(caplog) == snapshot |
| 69 |
|
| 70 |
|
| 71 |
async def test_unsubscribe( |
| 72 |
caplog: pytest.LogCaptureFixture, |
| 73 |
snapshot: SnapshotAssertion, |
| 74 |
): |
| 75 |
"""Test unsubscribing from events.""" |
| 76 |
subscriber = AsyncMock() |
| 77 |
event_bus = CloudEventBus() |
| 78 |
|
| 79 |
unsubscribe = event_bus.subscribe( |
| 80 |
event_type=CloudEventType.RELAYER_CONNECTED, handler=subscriber |
| 81 |
) |
| 82 |
|
| 83 |
await event_bus.publish(event=RelayerConnectedEvent()) |
| 84 |
assert len(subscriber.call_args_list) == 1 |
| 85 |
|
| 86 |
unsubscribe() |
| 87 |
|
| 88 |
await event_bus.publish(event=RelayerConnectedEvent()) |
| 89 |
assert len(subscriber.call_args_list) == 1 |
| 90 |
|
| 91 |
assert extract_log_messages(caplog) == snapshot |
| 92 |
|
| 93 |
|
| 94 |
async def test_unsubscribe_multiple_times( |
| 95 |
caplog: pytest.LogCaptureFixture, |
| 96 |
snapshot: SnapshotAssertion, |
| 97 |
): |
| 98 |
"""Test that unsubscribing multiple times doesn't raise.""" |
| 99 |
subscriber = AsyncMock() |
| 100 |
event_bus = CloudEventBus() |
| 101 |
|
| 102 |
unsubscribe = event_bus.subscribe( |
| 103 |
event_type=CloudEventType.RELAYER_CONNECTED, handler=subscriber |
| 104 |
) |
| 105 |
|
| 106 |
unsubscribe() |
| 107 |
unsubscribe() |
| 108 |
|
| 109 |
await event_bus.publish(event=RelayerConnectedEvent()) |
| 110 |
assert len(subscriber.call_args_list) == 0 |
| 111 |
|
| 112 |
assert extract_log_messages(caplog) == snapshot |
| 113 |
|
| 114 |
|
| 115 |
async def test_error_handling( |
| 116 |
caplog: pytest.LogCaptureFixture, |
| 117 |
snapshot: SnapshotAssertion, |
| 118 |
): |
| 119 |
"""Test that errors in handlers don't stop other handlers.""" |
| 120 |
failing_subscriber = AsyncMock(side_effect=ValueError("Test error")) |
| 121 |
working_subscriber = AsyncMock() |
| 122 |
event_bus = CloudEventBus() |
| 123 |
|
| 124 |
event_bus.subscribe( |
| 125 |
event_type=CloudEventType.RELAYER_CONNECTED, handler=failing_subscriber |
| 126 |
) |
| 127 |
event_bus.subscribe( |
| 128 |
event_type=CloudEventType.RELAYER_CONNECTED, handler=working_subscriber |
| 129 |
) |
| 130 |
|
| 131 |
await event_bus.publish(event=RelayerConnectedEvent()) |
| 132 |
|
| 133 |
assert len(failing_subscriber.call_args_list) == 1 |
| 134 |
assert len(working_subscriber.call_args_list) == 1 |
| 135 |
assert extract_log_messages(caplog) == snapshot |
| 136 |
|
| 137 |
|
| 138 |
async def test_multiple_event_types( |
| 139 |
caplog: pytest.LogCaptureFixture, |
| 140 |
snapshot: SnapshotAssertion, |
| 141 |
): |
| 142 |
"""Test subscribing to different event types.""" |
| 143 |
connected_subscriber = AsyncMock() |
| 144 |
disconnected_subscriber = AsyncMock() |
| 145 |
event_bus = CloudEventBus() |
| 146 |
|
| 147 |
event_bus.subscribe( |
| 148 |
event_type=CloudEventType.RELAYER_CONNECTED, handler=connected_subscriber |
| 149 |
) |
| 150 |
event_bus.subscribe( |
| 151 |
event_type=CloudEventType.RELAYER_DISCONNECTED, handler=disconnected_subscriber |
| 152 |
) |
| 153 |
|
| 154 |
await event_bus.publish(event=RelayerConnectedEvent()) |
| 155 |
await event_bus.publish(event=RelayerDisconnectedEvent()) |
| 156 |
|
| 157 |
assert len(connected_subscriber.call_args_list) == 1 |
| 158 |
assert len(disconnected_subscriber.call_args_list) == 1 |
| 159 |
assert extract_log_messages(caplog) == snapshot |
| 160 |
|
| 161 |
|
| 162 |
async def test_multiple_subscribers( |
| 163 |
caplog: pytest.LogCaptureFixture, |
| 164 |
snapshot: SnapshotAssertion, |
| 165 |
): |
| 166 |
"""Test multiple subscribers to the same event.""" |
| 167 |
subscriber1 = AsyncMock() |
| 168 |
subscriber2 = AsyncMock() |
| 169 |
subscriber3 = AsyncMock() |
| 170 |
event_bus = CloudEventBus() |
| 171 |
|
| 172 |
event_bus.subscribe( |
| 173 |
event_type=CloudEventType.RELAYER_CONNECTED, handler=subscriber1 |
| 174 |
) |
| 175 |
event_bus.subscribe( |
| 176 |
event_type=CloudEventType.RELAYER_CONNECTED, handler=subscriber2 |
| 177 |
) |
| 178 |
event_bus.subscribe( |
| 179 |
event_type=CloudEventType.RELAYER_CONNECTED, handler=subscriber3 |
| 180 |
) |
| 181 |
|
| 182 |
await event_bus.publish(event=RelayerConnectedEvent()) |
| 183 |
|
| 184 |
assert len(subscriber1.call_args_list) == 1 |
| 185 |
assert len(subscriber2.call_args_list) == 1 |
| 186 |
assert len(subscriber3.call_args_list) == 1 |
| 187 |
assert extract_log_messages(caplog) == snapshot |
| 188 |
|
| 189 |
|
| 190 |
async def test_event_timestamp( |
| 191 |
caplog: pytest.LogCaptureFixture, |
| 192 |
snapshot: SnapshotAssertion, |
| 193 |
): |
| 194 |
"""Test that events have timestamps.""" |
| 195 |
subscriber = AsyncMock() |
| 196 |
event_bus = CloudEventBus() |
| 197 |
|
| 198 |
event_bus.subscribe(event_type=CloudEventType.RELAYER_CONNECTED, handler=subscriber) |
| 199 |
|
| 200 |
await event_bus.publish(event=RelayerConnectedEvent()) |
| 201 |
|
| 202 |
assert len(subscriber.call_args_list) == 1 |
| 203 |
assert subscriber.call_args_list[0][0][0].timestamp is not None |
| 204 |
assert extract_log_messages(caplog) == snapshot |
| 205 |
|
| 206 |
|
| 207 |
async def test_subscribe_to_multiple_event_types( |
| 208 |
caplog: pytest.LogCaptureFixture, |
| 209 |
snapshot: SnapshotAssertion, |
| 210 |
): |
| 211 |
"""Test subscribing to multiple event types with a single handler.""" |
| 212 |
subscriber = AsyncMock() |
| 213 |
event_bus = CloudEventBus() |
| 214 |
|
| 215 |
unsubscribe = event_bus.subscribe( |
| 216 |
event_type=[ |
| 217 |
CloudEventType.RELAYER_CONNECTED, |
| 218 |
CloudEventType.RELAYER_DISCONNECTED, |
| 219 |
], |
| 220 |
handler=subscriber, |
| 221 |
) |
| 222 |
|
| 223 |
await event_bus.publish(event=RelayerConnectedEvent()) |
| 224 |
await event_bus.publish(event=RelayerDisconnectedEvent()) |
| 225 |
|
| 226 |
assert len(subscriber.call_args_list) == 2 |
| 227 |
assert subscriber.call_args_list[0][0][0].type == CloudEventType.RELAYER_CONNECTED |
| 228 |
assert ( |
| 229 |
subscriber.call_args_list[1][0][0].type == CloudEventType.RELAYER_DISCONNECTED |
| 230 |
) |
| 231 |
|
| 232 |
unsubscribe() |
| 233 |
|
| 234 |
await event_bus.publish(event=RelayerConnectedEvent()) |
| 235 |
assert len(subscriber.call_args_list) == 2 |
| 236 |
|
| 237 |
assert extract_log_messages(caplog) == snapshot |
| 238 |
|
| 239 |
|
| 240 |
async def test_subscribe_to_invalid_event_type(): |
| 241 |
"""Test that subscribing to an invalid event type raises an error.""" |
| 242 |
subscriber = AsyncMock() |
| 243 |
event_bus = CloudEventBus() |
| 244 |
|
| 245 |
with pytest.raises(EventBusError, match="Unknown event type: invalid_event"): |
| 246 |
event_bus.subscribe(event_type="invalid_event", handler=subscriber) # type: ignore[arg-type] |