Debian Codegraph @

tests/integration/network/test_replier.py

raw
1 # THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE.
2 # Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import asyncio
18
19 import pytest
20
21 from cylc.flow import __version__ as CYLC_VERSION
22 from cylc.flow.network import deserialize
23 from cylc.flow.network.client import WorkflowRuntimeClient
24 from cylc.flow.scheduler import Scheduler
25
26
27 async def test_listener(one: Scheduler, start):
28 """Test listener."""
29 async with start(one):
30 # Test listener handles an invalid message from client
31 # (without directly calling listener):
32 client = WorkflowRuntimeClient(one.workflow)
33 client.socket.send_string(r'Not JSON')
34 res = deserialize(
35 (await client.socket.recv()).decode()
36 )
37 assert res['error']
38 assert 'data' not in res
39 # Check other fields are present:
40 assert res['cylc_version'] == CYLC_VERSION
41
42 one.server.replier.queue.put('STOP')
43 async with asyncio.timeout(2):
44 # wait for the server to consume the STOP item from the queue
45 while not one.server.replier.queue.empty():
46 await asyncio.sleep(0.01)
47 # ensure the server is "closed"
48 one.server.replier.queue.put('foobar')
49 with pytest.raises(ValueError):
50 one.server.replier.listener()