-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
108 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,36 @@ | ||
import contextlib | ||
import random | ||
import socket | ||
import time | ||
import typing | ||
|
||
HOST: typing.Final = "0.0.0.0" | ||
_min_port: typing.Final = 9000 | ||
_max_port: typing.Final = 9999 | ||
|
||
_used_ports: set[int] = set() | ||
|
||
|
||
def get_available_port() -> int: | ||
for port in range(_min_port, _max_port + 1): | ||
with contextlib.closing( | ||
socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
) as sock: | ||
try: | ||
sock.bind((HOST, port)) | ||
return port | ||
except OSError: | ||
continue | ||
|
||
raise Exception("failed to find available port") | ||
start_time = time.time() | ||
|
||
while True: | ||
if time.time() - start_time > 5: | ||
raise Exception("timeout finding available port") | ||
|
||
port = random.randint(9000, 9999) | ||
|
||
if port in _used_ports: | ||
continue | ||
|
||
if not _is_port_available(port): | ||
continue | ||
|
||
_used_ports.add(port) | ||
return port | ||
|
||
|
||
def _is_port_available(port: int) -> bool: | ||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | ||
try: | ||
s.bind((HOST, port)) | ||
return True | ||
except OSError: | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
tests/test_experimental/test_remote_state_middleware/cases/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters