| 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 pytest |
| 18 |
import zmq |
| 19 |
|
| 20 |
from cylc.flow.exceptions import CylcError |
| 21 |
from cylc.flow.network import ZMQSocketBase |
| 22 |
|
| 23 |
from .key_setup import setup_keys |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture(scope='module') |
| 27 |
def myflow(mod_flow, mod_one_conf): |
| 28 |
return mod_flow(mod_one_conf) |
| 29 |
|
| 30 |
|
| 31 |
def test_single_port(myflow, port_range): |
| 32 |
"""Test server on a single port and port in use exception.""" |
| 33 |
context = zmq.Context() |
| 34 |
setup_keys(myflow) # auth keys are required for comms |
| 35 |
serv1 = ZMQSocketBase( |
| 36 |
zmq.REP, context=context, workflow=myflow, bind=True) |
| 37 |
serv2 = ZMQSocketBase( |
| 38 |
zmq.REP, context=context, workflow=myflow, bind=True) |
| 39 |
|
| 40 |
serv1._socket_bind(*port_range) |
| 41 |
port = serv1.port |
| 42 |
|
| 43 |
with pytest.raises(CylcError, match=r"Address already in use"): |
| 44 |
serv2._socket_bind(port, port) |
| 45 |
|
| 46 |
serv2.stop() |
| 47 |
serv1.stop() |
| 48 |
context.destroy() |
| 49 |
|
| 50 |
|
| 51 |
def test_start(myflow, port_range): |
| 52 |
"""Test socket start.""" |
| 53 |
setup_keys(myflow) # auth keys are required for comms |
| 54 |
publisher = ZMQSocketBase(zmq.PUB, workflow=myflow, bind=True) |
| 55 |
assert publisher.loop is None |
| 56 |
assert publisher.port is None |
| 57 |
publisher.start(*port_range) |
| 58 |
assert publisher.loop is not None |
| 59 |
assert publisher.port is not None |
| 60 |
publisher.stop() |
| 61 |
|
| 62 |
|
| 63 |
def test_stop(myflow, port_range): |
| 64 |
"""Test socket/thread stop.""" |
| 65 |
setup_keys(myflow) # auth keys are required for comms |
| 66 |
publisher = ZMQSocketBase(zmq.PUB, workflow=myflow, bind=True) |
| 67 |
publisher.start(*port_range) |
| 68 |
assert not publisher.socket.closed |
| 69 |
publisher.stop() |
| 70 |
assert publisher.socket.closed |