Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakekaplan committed Sep 6, 2024
1 parent 40fa4c3 commit fbe7ed0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/prefect/events/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ConnectionClosed,
ConnectionClosedError,
ConnectionClosedOK,
InvalidHandshake,
)
from websockets.sync.client import connect as sync_connect

Expand Down Expand Up @@ -296,23 +297,21 @@ async def __aexit__(

def sync_raise_for_connection_error(self):
try:
with self._sync_connect() as ws:
pong = ws.ping()
pong.wait()
except Exception as e:
with self._sync_connect():
pass
except (OSError, InvalidHandshake, TimeoutError) as e:
raise RuntimeError(
f"Unable to establish connection to {self._events_socket_url!r}. "
f"Unable to connect to {self._events_socket_url!r}. "
f"Check your network to ensure websocket connections can be made to the API."
) from e

async def raise_for_connection_error(self):
try:
async with self._deferred_connect() as ws:
pong = ws.ping()
await pong
except Exception as e:
async with self._deferred_connect():
pass
except (OSError, InvalidHandshake, asyncio.TimeoutError) as e:
raise RuntimeError(
f"Unable to establish connection to {self._events_socket_url!r}. "
f"Unable to connect to {self._events_socket_url!r}. "
f"Check your network to ensure websocket connections can be made to the API."
) from e

Expand Down
14 changes: 14 additions & 0 deletions tests/events/client/test_events_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ async def test_constructs_cloud_client(cloud_settings):
assert isinstance(get_events_client(), PrefectCloudEventsClient)


def test_sync_raise_for_connection_error_bad_url():
client = PrefectEventsClient(api_url="http://notarealurl")
with pytest.raises(RuntimeError) as e:
client.sync_raise_for_connection_error()
assert "Unable to connect to 'ws://notarealurl/events/in'" in str(e.value)


async def test_raise_for_connection_error_bad_url():
client = PrefectEventsClient(api_url="http://notarealurl")
with pytest.raises(RuntimeError) as e:
await client.raise_for_connection_error()
assert "Unable to connect to 'ws://notarealurl/events/in'" in str(e.value)


async def test_events_client_can_emit_when_ephemeral_enabled(
example_event_1: Event, monkeypatch: pytest.MonkeyPatch, ephemeral_settings
):
Expand Down

0 comments on commit fbe7ed0

Please sign in to comment.