| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import socket |
| 6 |
import pytest |
| 7 |
from scalene.launchbrowser import is_port_available |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture |
| 11 |
def free_port(): |
| 12 |
"""Find a free port for testing.""" |
| 13 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 14 |
s.bind(("", 0)) |
| 15 |
return s.getsockname()[1] |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def occupied_port(): |
| 20 |
"""Create and occupy a port for testing.""" |
| 21 |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 22 |
s.bind(("", 0)) |
| 23 |
port = s.getsockname()[1] |
| 24 |
s.listen(1) |
| 25 |
yield port |
| 26 |
s.close() |
| 27 |
|
| 28 |
|
| 29 |
def test_is_port_available_with_free_port(free_port): |
| 30 |
"""Test that is_port_available returns True for a free port.""" |
| 31 |
assert is_port_available(free_port) == True |
| 32 |
|
| 33 |
|
| 34 |
def test_is_port_available_with_occupied_port(occupied_port): |
| 35 |
"""Test that is_port_available returns False for an occupied port.""" |
| 36 |
assert is_port_available(occupied_port) == False |