|
| 1 | +"""Test port binding logic.""" |
| 2 | +from uuid import uuid4 |
| 3 | + |
| 4 | +import psutil |
| 5 | +import pytest |
| 6 | +from receptor_affinity import utils |
| 7 | +from receptor_affinity.exceptions import NodeUnavailableError |
| 8 | +from receptor_affinity.mesh import Node |
| 9 | + |
| 10 | + |
| 11 | +def test_invalid_listen_scheme(): |
| 12 | + """Start a node, and give it a listen address with an invalid scheme. |
| 13 | +
|
| 14 | + The node should fail to start. See `receptor #93`_. |
| 15 | +
|
| 16 | + .. NOTE:: This test should be extended to check that a traceback isn't printed to stdout or |
| 17 | + stderr. |
| 18 | +
|
| 19 | + .. _receptor #93: https://github.com/project-receptor/receptor/issues/93 |
| 20 | + """ |
| 21 | + node = Node(str(uuid4()), listen=f'harglebargle://127.0.0.1:{utils.random_port()}') |
| 22 | + with pytest.raises(NodeUnavailableError): |
| 23 | + node.start() |
| 24 | + |
| 25 | + |
| 26 | +def test_listen_fragment(): |
| 27 | + """Start a node, and give it a listen address with a fragment. |
| 28 | +
|
| 29 | + The node should fail to start. See `receptor #153`_. |
| 30 | +
|
| 31 | + .. _receptor #153: https://github.com/project-receptor/receptor/issues/153 |
| 32 | + """ |
| 33 | + node = Node(str(uuid4()), listen=f'receptor://127.0.0.1:{utils.random_port()}#frag') |
| 34 | + with pytest.raises(NodeUnavailableError): |
| 35 | + node.start() |
| 36 | + |
| 37 | + |
| 38 | +def test_listen_path(): |
| 39 | + """Start a node, and give it a listen address with a path. |
| 40 | +
|
| 41 | + The node should fail to start. See `receptor #153`_. |
| 42 | +
|
| 43 | + .. _receptor #153: https://github.com/project-receptor/receptor/issues/153 |
| 44 | + """ |
| 45 | + node = Node(str(uuid4()), listen=f'receptor://127.0.0.1:{utils.random_port()}/path') |
| 46 | + with pytest.raises(NodeUnavailableError): |
| 47 | + node.start() |
| 48 | + |
| 49 | + |
| 50 | +def test_listen_query(): |
| 51 | + """Start a node, and give it a listen address with a query. |
| 52 | +
|
| 53 | + The node should fail to start. See `receptor #153`_. |
| 54 | +
|
| 55 | + .. _receptor #153: https://github.com/project-receptor/receptor/issues/153 |
| 56 | + """ |
| 57 | + node = Node(str(uuid4()), listen=f'receptor://127.0.0.1:{utils.random_port()}?key=val') |
| 58 | + with pytest.raises(NodeUnavailableError): |
| 59 | + node.start() |
| 60 | + |
| 61 | + |
| 62 | +def test_no_port_given(): |
| 63 | + """Start a node, and don't specify a port on which to listen. |
| 64 | +
|
| 65 | + Assert that it listens on port 8888. Older versions of receptor would listen on a random port. |
| 66 | + See: `receptor #138`_. |
| 67 | +
|
| 68 | + .. _receptor #138: https://github.com/project-receptor/receptor/issues/138 |
| 69 | + """ |
| 70 | + node = Node(str(uuid4()), listen=f'receptor://127.0.0.1') |
| 71 | + node.start() |
| 72 | + try: |
| 73 | + conns = psutil.Process(node.pid).connections() |
| 74 | + assert len(conns) == 1 |
| 75 | + assert conns[0].laddr.port == 8888 |
| 76 | + finally: |
| 77 | + node.stop() |
0 commit comments