21
21
from rerun_bindings import DatasetEntry
22
22
23
23
HOST = "localhost"
24
- PORT = None # Will be set dynamically
25
- CATALOG_URL = None # Will be set dynamically
26
24
DATASET_NAME = "dataset"
27
25
28
26
DATASET_FILEPATH = pathlib .Path (__file__ ).parent .parent .parent .parent / "tests" / "assets" / "rrd" / "dataset"
@@ -85,12 +83,12 @@ def shutdown_process(process: subprocess.Popen[str]) -> None:
85
83
print (f"Error during cleanup: { e } " )
86
84
87
85
88
- def wait_for_server_ready (timeout : int = 30 ) -> None :
86
+ def wait_for_server_ready (port : int , timeout : int = 30 ) -> None :
89
87
def is_port_open () -> bool :
90
88
sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
91
89
sock .settimeout (1 )
92
90
try :
93
- result = sock .connect_ex ((HOST , PORT ))
91
+ result = sock .connect_ex ((HOST , port ))
94
92
return result == 0
95
93
finally :
96
94
sock .close ()
@@ -102,7 +100,7 @@ def is_port_open() -> bool:
102
100
break
103
101
time .sleep (0.1 )
104
102
else :
105
- raise TimeoutError (f"Server port { PORT } not ready within { timeout } s" )
103
+ raise TimeoutError (f"Server port { port } not ready within { timeout } s" )
106
104
107
105
108
106
class ServerInstance :
@@ -125,22 +123,20 @@ def server_instance() -> Generator[ServerInstance, None, None]:
125
123
# Find a free port dynamically
126
124
sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
127
125
sock .bind ((HOST , 0 ))
128
- global PORT
129
- PORT = sock .getsockname ()[1 ]
126
+ port = sock .getsockname ()[1 ]
130
127
sock .close ()
131
128
132
- global CATALOG_URL
133
- CATALOG_URL = f"rerun+http://{ HOST } :{ PORT } "
129
+ catalog_url = f"rerun+http://{ HOST } :{ port } "
134
130
135
131
cmd = ["python" , "-m" , "rerun" , "server" , "--dataset" , str (DATASET_FILEPATH ), "--table" , str (TABLE_FILEPATH )]
136
132
server_process = subprocess .Popen (cmd , env = env , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
137
133
138
134
try :
139
- wait_for_server_ready ()
135
+ wait_for_server_ready (port )
140
136
except Exception as e :
141
137
print (f"Error during waiting for server to start: { e } " )
142
138
143
- client = CatalogClient (CATALOG_URL )
139
+ client = CatalogClient (catalog_url )
144
140
dataset = client .get_dataset (name = DATASET_NAME )
145
141
146
142
resource = ServerInstance (server_process , client , dataset )
0 commit comments