6
6
from urllib .parse import urljoin
7
7
8
8
9
- @pytest .fixture ()
10
- def server_port ():
11
- sock = socket .socket ()
12
- sock .bind (('' , 0 ))
13
- return sock .getsockname ()[1 ]
9
+ class ServerManager :
10
+
11
+ def __init__ (self , base_dir , port ):
12
+ self .data_dir = base_dir / 'data'
13
+ self .log_file = base_dir / 'server.log'
14
+ self .port = port
15
+ self .process = None
16
+
17
+ def start (self ):
18
+ command = [
19
+ 'valgrind' ,
20
+ 'zig-out/bin/fpindex' ,
21
+ '--dir' , str (self .data_dir ),
22
+ '--port' , str (self .port ),
23
+ '--log-level' , 'debug' ,
24
+ ]
25
+ self .process = subprocess .Popen (
26
+ command ,
27
+ stdin = subprocess .DEVNULL ,
28
+ stdout = subprocess .DEVNULL ,
29
+ stderr = self .log_file .open ('w' ),
30
+ )
31
+
32
+ def stop (self ):
33
+ if self .process is not None :
34
+ if self .process .returncode is None :
35
+ self .process .terminate ()
36
+ try :
37
+ self .process .wait (timeout = 1.0 )
38
+ except subprocess .TimeoutExpired :
39
+ self .process .kill ()
40
+ self .process .wait ()
41
+
42
+ def error_log (self ):
43
+ for line in self .log_file .read_text ().splitlines ():
44
+ yield line
45
+
46
+
47
+ @pytest .fixture (scope = 'session' )
48
+ def server (tmp_path_factory ):
49
+ srv = ServerManager (base_dir = tmp_path_factory .mktemp ('srv' ), port = 14502 )
50
+ srv .start ()
51
+ yield srv
52
+ srv .stop ()
53
+ for line in srv .error_log ():
54
+ print (line )
14
55
15
56
16
- @pytest .fixture ()
17
- def server (server_port , tmp_path ):
18
- data_dir = tmp_path / 'data'
19
- stderr = tmp_path / 'server.stderr.log'
20
- command = [
21
- 'zig-out/bin/fpindex' ,
22
- '--dir' , str (data_dir ),
23
- '--port' , str (server_port ),
24
- '--log-level' , 'debug' ,
25
- ]
26
- process = subprocess .Popen (
27
- command ,
28
- stdin = subprocess .DEVNULL ,
29
- stdout = subprocess .DEVNULL ,
30
- stderr = stderr .open ('w' ),
31
- )
32
- yield
33
- if process .returncode is None :
34
- process .terminate ()
35
- process .wait ()
36
- for line in stderr .read_text ().splitlines ():
37
- print (line )
57
+ index_no = 1
38
58
39
59
40
60
@pytest .fixture
41
- def index_name ():
42
- return 'testidx'
61
+ def index_name (request ):
62
+ global index_no
63
+ index_no += 1
64
+ return f't{ index_no :03d} '
43
65
44
66
45
67
class Client :
@@ -48,18 +70,23 @@ def __init__(self, session, base_url):
48
70
self .base_url = base_url
49
71
50
72
def head (self , url , ** kwargs ):
73
+ kwargs .setdefault ('timeout' , 1 )
51
74
return self .session .head (urljoin (self .base_url , url ), ** kwargs )
52
75
53
76
def get (self , url , ** kwargs ):
77
+ kwargs .setdefault ('timeout' , 1 )
54
78
return self .session .get (urljoin (self .base_url , url ), ** kwargs )
55
79
56
80
def put (self , url , ** kwargs ):
81
+ kwargs .setdefault ('timeout' , 1 )
57
82
return self .session .put (urljoin (self .base_url , url ), ** kwargs )
58
83
59
84
def post (self , url , ** kwargs ):
85
+ kwargs .setdefault ('timeout' , 1 )
60
86
return self .session .post (urljoin (self .base_url , url ), ** kwargs )
61
87
62
88
def delete (self , url , ** kwargs ):
89
+ kwargs .setdefault ('timeout' , 1 )
63
90
return self .session .delete (urljoin (self .base_url , url ), ** kwargs )
64
91
65
92
@@ -74,17 +101,28 @@ def check_port(port_no):
74
101
return sock .connect_ex (('127.0.0.1' , port_no )) == 0
75
102
76
103
77
- @pytest .fixture
78
- def client (session , server_port , server ):
79
- deadline = time .time () + 1
80
- while not check_port (server_port ):
104
+ def wait_for_ready (port , timeout ):
105
+ deadline = time .time () + timeout
106
+ while not check_port (port ):
81
107
if time .time () > deadline :
82
108
raise TimeoutError ()
83
- time .sleep (0.01 )
84
- return Client (session , f'http://localhost:{ server_port } ' )
109
+ time .sleep (timeout / 100.0 )
110
+
111
+
112
+ @pytest .fixture
113
+ def client (session , server ):
114
+ wait_for_ready (server .port , 1 )
115
+ return Client (session , f'http://localhost:{ server .port } ' )
85
116
86
117
87
118
@pytest .fixture ()
88
119
def create_index (client , index_name ):
89
120
req = client .put (f'/{ index_name } ' )
90
121
req .raise_for_status ()
122
+
123
+
124
+ @pytest .fixture (autouse = True )
125
+ def delete_index (client , index_name ):
126
+ yield
127
+ req = client .delete (f'/{ index_name } ' )
128
+ req .raise_for_status ()
0 commit comments