Skip to content

Commit 0396c7b

Browse files
authored
feat: add support for system event (#264)
* feat: add support for event * push to coveralls only once * force test to fail * remove failure test
1 parent 685eda7 commit 0396c7b

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

.github/workflows/ci.yml

+2-14
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,11 @@ jobs:
3434
with:
3535
version: latest
3636

37-
- name: Setup Local Supabase
38-
run: |
39-
supabase start --workdir tests
40-
supabase db reset --workdir tests
41-
supabase status --workdir tests -o env > tests/.env \
42-
--override-name auth.anon_key=SUPABASE_ANON_KEY \
43-
--override-name api.url=SUPABASE_URL
44-
45-
- name: Wait for Supabase to be ready
46-
run: |
47-
echo "Waiting for 5 seconds to ensure Supabase is fully initialized..."
48-
sleep 5
49-
5037
- name: Run Tests
51-
run: make run_tests || make run_tests
38+
run: make run_tests
5239

5340
- name: Upload coverage to Coveralls
41+
if: ${{ matrix.python-version }} == "3.12"
5442
uses: coverallsapp/github-action@v2
5543
with:
5644
github-token: ${{ secrets.GITHUB_TOKEN }}

Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,12 @@ tests_pre_commit:
1212

1313
run_tests: tests
1414

15-
tests_only:
15+
setup_test_infra:
16+
supabase start --workdir tests
17+
supabase db reset --workdir tests
18+
supabase status --workdir tests -o env > tests/.env \
19+
--override-name auth.anon_key=SUPABASE_ANON_KEY \
20+
--override-name api.url=SUPABASE_URL
21+
22+
tests_only: setup_test_infra
1623
poetry run pytest --cov=./ --cov-report=xml --cov-report=html -vv

pyproject.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "2.1.0" # {x-release-please-version}
44
description = ""
55
authors = [
66
"Joel Lee <[email protected]>",
7-
"Andrew Smith <[email protected]>"
7+
"Andrew Smith <[email protected]>",
88
]
99
license = "MIT"
1010
readme = "README.md"
@@ -33,3 +33,7 @@ pytest-cov = "^5.0.0"
3333
[build-system]
3434
requires = ["poetry-core>=1.0.0"]
3535
build-backend = "poetry.core.masonry.api"
36+
37+
[tool.pytest.ini_options]
38+
asyncio_mode = "strict"
39+
asyncio_default_fixture_loop_scope = "function"

realtime/_async/channel.py

+11
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,17 @@ def on_postgres_changes(
388388
callback=lambda payload, _: callback(payload),
389389
)
390390

391+
def on_system(
392+
self, callback: Callable[[Dict[str, Any], None]]
393+
) -> AsyncRealtimeChannel:
394+
"""
395+
Set up a listener for system events.
396+
397+
:param callback: The callback function to execute when a system event is received.
398+
:return: The Channel instance for method chaining.
399+
"""
400+
return self._on("system", callback=lambda payload, _: callback(payload))
401+
391402
# Presence methods
392403
async def track(self, user_status: Dict[str, Any]) -> None:
393404
"""

realtime/_async/client.py

-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ async def _flush_send_buffer(self):
167167
await callback()
168168
self.send_buffer = []
169169

170-
@ensure_connection
171170
async def close(self) -> None:
172171
"""
173172
Close the WebSocket connection.

tests/test_connection.py

+5
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,16 @@ def delete_callback(payload):
135135
delete_event.set()
136136

137137
subscribed_event = asyncio.Event()
138+
system_event = asyncio.Event()
138139

139140
await channel.on_postgres_changes(
140141
"*", all_changes_callback, table="todos"
141142
).on_postgres_changes("INSERT", insert_callback, table="todos").on_postgres_changes(
142143
"UPDATE", update_callback, table="todos"
143144
).on_postgres_changes(
144145
"DELETE", delete_callback, table="todos"
146+
).on_system(
147+
lambda _: system_event.set()
145148
).subscribe(
146149
lambda state, error: (
147150
subscribed_event.set()
@@ -150,6 +153,8 @@ def delete_callback(payload):
150153
)
151154
)
152155

156+
await asyncio.wait_for(system_event.wait(), 10)
157+
153158
# Wait for the channel to be subscribed
154159
await asyncio.wait_for(subscribed_event.wait(), 10)
155160

0 commit comments

Comments
 (0)