File: test_client.py

package info (click to toggle)
python-trame-server 3.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 480 kB
  • sloc: python: 4,075; javascript: 5; sh: 4; makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,468 bytes parent folder | download
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
import asyncio

import pytest
from trame.app import asynchronous, get_client, get_server


@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"))

    for _ in range(10):
        if client.connected == 2:
            break
        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()