File: notest_connection_async.py

package info (click to toggle)
python-homematicip 1.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,796 kB
  • sloc: python: 15,164; makefile: 17; sh: 4
file content (272 lines) | stat: -rw-r--r-- 8,325 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import asyncio

import aiohttp
import pytest

from homematicip.aio.connection import AsyncConnection
from homematicip.base.base_connection import (
    ATTR_AUTH_TOKEN,
    ATTR_CLIENT_AUTH,
    HmipConnectionError,
    HmipServerCloseError,
    HmipWrongHttpStatusError,
)
from tests.fake_hmip_server import FakeConnectionHmip, FakeLookupHmip, FakeWebsocketHmip
from tests.helpers import mockreturn


@pytest.fixture
async def fake_lookup_server(event_loop):
    server = FakeLookupHmip(
        loop=event_loop, base_url="lookup.homematic.com", port=48335
    )
    connector = await server.start()
    async with aiohttp.ClientSession(connector=connector, loop=event_loop) as session:
        connection = AsyncConnection(event_loop, session=session)
        yield connection
    await server.stop()


@pytest.fixture
async def fake_server(event_loop):
    server = FakeConnectionHmip(
        loop=event_loop, base_url="test.homematic.com", port=None
    )
    connector = await server.start()
    async with aiohttp.ClientSession(connector=connector, loop=event_loop) as session:
        connection = AsyncConnection(event_loop, session=session)
        connection.headers[ATTR_AUTH_TOKEN] = ""
        connection.headers[ATTR_CLIENT_AUTH] = ""
        yield connection
    await server.stop()


@pytest.fixture
async def fake_websocket_server(event_loop):
    server = FakeWebsocketHmip(loop=event_loop, base_url="ws.homematic.com")
    return server


@pytest.mark.asyncio
async def test_init(fake_lookup_server):
    fake_lookup_server.set_auth_token("auth_token")
    await fake_lookup_server.init("accesspoint_id")
    assert (
        fake_lookup_server.urlWebSocket == FakeLookupHmip.host_response["urlWebSocket"]
    )


@pytest.mark.asyncio
async def test_post_200(fake_server):
    """Test response with status 200."""
    resp = await fake_server.api_call(
        "https://test.homematic.com/go_200_json", body={}, full_url=True
    )
    assert resp == FakeConnectionHmip.js_response


@pytest.mark.asyncio
async def test_post_200_no_json(fake_server):
    resp = await fake_server.api_call(
        "https://test.homematic.com/go_200_no_json", body=[], full_url=True
    )

    assert resp is True


@pytest.mark.asyncio
async def test_post_404_alt(fake_server):
    with pytest.raises(HmipWrongHttpStatusError):
        await fake_server.api_call(
            "https://test.homematic.com/go_404", body={}, full_url=True
        )


@pytest.mark.asyncio
async def test_post_404(monkeypatch, async_connection):
    monkeypatch.setattr(
        async_connection._websession, "post", mockreturn(return_status=404)
    )
    with pytest.raises(HmipWrongHttpStatusError):
        await async_connection.api_call("https://test", full_url=True)


@pytest.mark.asyncio
async def test_post_exhaustive_timeout(monkeypatch, async_connection):
    monkeypatch.setattr(
        async_connection._websession, "post", mockreturn(exception=asyncio.TimeoutError)
    )
    with pytest.raises(HmipConnectionError):
        await async_connection.api_call("https://test", full_url=True)


@pytest.mark.asyncio
async def test_websocket_exhaustive_timeout(monkeypatch, async_connection):
    async def raise_timeout(*args, **kwargs):
        raise asyncio.TimeoutError

    monkeypatch.setattr(async_connection._websession, "ws_connect", raise_timeout)
    async_connection._restCallTimout = 0.01
    with pytest.raises(HmipConnectionError):
        await async_connection.ws_connect(None)


async def start_fake_server(loop, base_url):
    fake_ws = FakeWebsocketHmip(loop=loop, base_url=base_url)
    connector = await fake_ws.start()
    return connector


async def start_async_client_connection(connector, loop, base_url, url):
    session = aiohttp.ClientSession(connector=connector, loop=loop)
    connection = AsyncConnection(loop, session)
    connection._urlWebSocket = "ws://" + base_url + url
    return connection


async def ws_listen(future, connection):
    def parser(*args, **kwargs):
        future.set_result(args)

    ws_loop = await connection.ws_connect(parser)
    try:
        await ws_loop
    except HmipServerCloseError as err:
        future.set_exception(HmipServerCloseError)
    except HmipConnectionError as err:
        future.set_exception(HmipConnectionError)


async def do_test(future, loop, url, base_url="ws.homematic.com"):
    connector = await start_fake_server(loop, base_url)
    connection = await start_async_client_connection(connector, loop, base_url, url)
    await ws_listen(future, connection)


# async def do_test(future, loop, url, base_url='ws.homematic.com'):
#     """Setup the fake websocket server."""
#     try:
#         connector = await start_fake_server(loop,base_url)
#         async with aiohttp.ClientSession(connector=connector, loop=loop) as session:
#
#             def parser(*args, **kwargs):
#                 future.set_result(args)
#
#             connection = AsyncConnection(loop, session)
#             connection._urlWebSocket = 'wss://' + base_url + url
#             ws_loop = await connection.ws_connect(parser)
#             try:
#                 await ws_loop
#             except HmipServerCloseError as err:
#                 future.set_exception(HmipServerCloseError)
#             except HmipConnectionError as err:
#                 future.set_exception(HmipConnectionError)
#
#     except CancelledError as err:
#         print("task cancelled.")


def finish_all(loop):
    async def finish():
        all_finished = False
        while not all_finished:
            await asyncio.sleep(1)
            all_finished = True
            _all_tasks = [
                _task
                for _task in asyncio.Task.all_tasks(loop)
                if not asyncio.Task.current_task(loop) == _task
            ]
            for task in _all_tasks:
                task.cancel()
                _done = task.done()
                all_finished = all_finished and _done

    loop.run_until_complete(finish())
    print("finished")


# todo: tests break. fix this.


def test_ws_message():
    loop = asyncio.get_event_loop()
    future = asyncio.Future()
    asyncio.ensure_future(do_test(future, loop, "/"))
    loop.run_until_complete(future)

    _res = future.result()

    assert _res[1] == "abc"

    finish_all(loop)
    # loop.close()


def test_ws_ping_pong():
    loop = asyncio.get_event_loop()
    future = asyncio.Future()
    asyncio.ensure_future(do_test(future, loop, "/nopong"))

    with pytest.raises(HmipConnectionError):
        loop.run_until_complete(future)
        # _res = future.result()
    finish_all(loop)


def test_ws_server_shutdown():
    loop = asyncio.get_event_loop()
    future = asyncio.Future()
    asyncio.ensure_future(do_test(future, loop, "/servershutdown"))

    with pytest.raises(HmipServerCloseError):
        loop.run_until_complete(future)
    finish_all(loop)


def test_ws_client_shutdown():
    loop = asyncio.get_event_loop()
    future = asyncio.Future()

    async def close_client(connection):
        await asyncio.sleep(2)
        await connection.close_websocket_connection()
        return

    async def start(url, base_url="ws.homematic.com"):
        connector = await start_fake_server(loop, base_url)
        connection = await start_async_client_connection(connector, loop, base_url, url)
        asyncio.ensure_future(ws_listen(future, connection))
        await close_client(connection)

    loop.run_until_complete(start("/clientclose"))
    finish_all(loop)


def test_ws_recover():
    loop = asyncio.get_event_loop()
    result = []

    async def start(url, base_url="ws.homematic.com"):
        retry = 0
        connector = await start_fake_server(loop, base_url)
        connection = await start_async_client_connection(connector, loop, base_url, url)

        def parser(*args, **kwargs):
            nonlocal result
            result.append(args)

        while retry < 2:
            retry += 1
            ws_loop = await connection.ws_connect(parser)
            try:
                await ws_loop
            except HmipServerCloseError as err:
                pass
            except HmipConnectionError as err:
                pass
            except Exception as err:
                pass

    loop.run_until_complete(start("/recover"))
    assert True