| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
import pytest |
| 6 |
import socket |
| 7 |
from scalene.scalene_jupyter import ScaleneJupyter |
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture(scope="function") |
| 11 |
def free_port(): |
| 12 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 13 |
s.bind(("", 0)) |
| 14 |
return s.getsockname()[1] |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture(scope="function") |
| 18 |
def occupied_port(free_port): |
| 19 |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: |
| 20 |
s.bind(("", free_port)) |
| 21 |
yield free_port |
| 22 |
|
| 23 |
|
| 24 |
def test_find_available_port(free_port): |
| 25 |
|
| 26 |
port = ScaleneJupyter.find_available_port(free_port, free_port) |
| 27 |
assert port == free_port |
| 28 |
|
| 29 |
|
| 30 |
def test_find_available_port_with_occupied_port(occupied_port): |
| 31 |
|
| 32 |
port = ScaleneJupyter.find_available_port(occupied_port, occupied_port + 1) |
| 33 |
assert port == occupied_port + 1 |
| 34 |
|
| 35 |
|
| 36 |
def test_no_available_ports(occupied_port): |
| 37 |
|
| 38 |
port = ScaleneJupyter.find_available_port(occupied_port, occupied_port) |
| 39 |
assert port is None |