-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
507 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ | |
# OS files | ||
.DS_Store | ||
|
||
# test file | ||
trame_net.log | ||
|
||
# Editor directories and files | ||
.idea | ||
.vscode | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ dev = [ | |
"pre-commit", | ||
"ruff", | ||
"pytest", | ||
"pytest-asyncio", | ||
] | ||
|
||
[build-system] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import asyncio | ||
import pytest | ||
|
||
import multiprocessing | ||
import time | ||
from concurrent.futures import ProcessPoolExecutor | ||
|
||
from trame.app import get_server, asynchronous | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_thread_state_sync(): | ||
running_states = [] | ||
value_changes = [] | ||
|
||
MULTI_PROCESS_MANAGER = multiprocessing.Manager() | ||
SPAWN = multiprocessing.get_context("spawn") | ||
PROCESS_EXECUTOR = ProcessPoolExecutor(1, mp_context=SPAWN) | ||
|
||
loop = asyncio.get_event_loop() | ||
queue = MULTI_PROCESS_MANAGER.Queue() | ||
|
||
server = get_server("test_thread_state_sync") | ||
server.state.running = False | ||
server.state.a = 0 | ||
|
||
@server.state.change("running") | ||
def on_running_change(running, **_): | ||
running_states.append(running) | ||
|
||
@server.state.change("a") | ||
def on_a_change(a, **_): | ||
value_changes.append(a) | ||
|
||
server.start(exec_mode="task", port=0) | ||
assert await server.ready | ||
|
||
def exec_in_thread(queue): | ||
with asynchronous.StateQueue(queue) as state: | ||
assert state.queue is queue | ||
|
||
state.running = True | ||
|
||
state.update( | ||
{ | ||
"b": 10, | ||
"c": 20, | ||
} | ||
) | ||
|
||
for i in range(10): | ||
time.sleep(0.1) | ||
state.a = i | ||
assert state.a == i | ||
assert state["a"] == i | ||
|
||
state.running = False | ||
|
||
asynchronous.decorate_task( | ||
loop.run_in_executor( | ||
PROCESS_EXECUTOR, | ||
exec_in_thread(queue), | ||
) | ||
) | ||
asynchronous.create_state_queue_monitor_task(server, queue) | ||
|
||
previous_size = len(value_changes) | ||
while len(value_changes) < 10: | ||
await asyncio.sleep(0.15) | ||
assert len(value_changes) > previous_size | ||
previous_size = len(value_changes) | ||
|
||
assert running_states == [False, True, False] | ||
|
||
assert server.state.b == 10 | ||
assert server.state.c == 20 | ||
|
||
await server.stop() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_task_decorator(): | ||
bg_update = "idle" | ||
|
||
@asynchronous.task | ||
async def run_something(): | ||
nonlocal bg_update | ||
bg_update = "ok" | ||
|
||
run_something() | ||
assert bg_update == "idle" | ||
await asyncio.sleep(0.1) | ||
assert bg_update == "ok" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
import pytest | ||
|
||
from trame.app import get_server, get_client, asynchronous | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_client_connection(): | ||
server = get_server("test_client_connection") | ||
server.start(exec_mode="task", port=0) | ||
assert await server.ready | ||
assert server.running | ||
|
||
url = f"ws://localhost:{server.port}/ws" | ||
client = get_client(url) | ||
asynchronous.create_task(client.connect(secret="wslink-secret")) | ||
await asyncio.sleep(0.1) | ||
|
||
# should be a noop | ||
await client.connect() | ||
assert client.connected == 2 | ||
|
||
@client.change("a") | ||
def on_change(a, **_): | ||
assert a == 2 | ||
|
||
@server.trigger("add") | ||
def server_method(*args): | ||
result = 0 | ||
for v in args: | ||
result += v | ||
return result | ||
|
||
with server.state as state: | ||
state.a = 2 | ||
|
||
await server.network_completion | ||
await asyncio.sleep(0.1) # wait for client network | ||
|
||
assert server.state.a == client.state.a | ||
|
||
with client.state as state: | ||
state.b = {"a": 1, "b": 2, "_filter": ["b"]} | ||
|
||
await asyncio.sleep(0.1) # wait for client network | ||
assert server.state.b == {"a": 1, "_filter": ["b"]} | ||
|
||
assert await client.call_trigger("add", [1, 2, 3]) == 6 | ||
assert await client.call_trigger("add") == 0 | ||
|
||
await asyncio.sleep(0.1) | ||
await client.diconnect() | ||
await asyncio.sleep(0.5) | ||
await server.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from trame.app import get_server | ||
from trame.ui.html import DivLayout | ||
from trame.widgets import html | ||
|
||
|
||
def test_namespace_template(): | ||
server = get_server("test_namespace_template") | ||
child_server = server.create_child_server(prefix="child_") | ||
child_server.state.a = 10 | ||
|
||
layout = DivLayout(child_server) | ||
with layout: | ||
html.Div("{{ a }}") | ||
|
||
assert layout.html == "<div >\n<div >\n{{ child_a }}\n</div>\n</div>" | ||
assert child_server.translator("a") == "child_a" |
Oops, something went wrong.