@@ -17,7 +17,7 @@ def find_available_port(start_port=9090, max_attempts=10):
17
17
port = start_port + i
18
18
try :
19
19
with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
20
- s .bind ((' localhost' , port ))
20
+ s .bind ((" localhost" , port ))
21
21
return port
22
22
except OSError :
23
23
continue
@@ -74,7 +74,7 @@ def test_metrics_server_startup(standalone_metrics_server):
74
74
"""Test that metrics server starts correctly."""
75
75
assert standalone_metrics_server .port is not None
76
76
assert standalone_metrics_server .port > 0
77
-
77
+
78
78
# Test that metrics endpoint is accessible
79
79
response = wait_for_server (f"http://localhost:{ standalone_metrics_server .port } /metrics" )
80
80
assert response .status_code == 200
@@ -84,70 +84,64 @@ def test_metrics_server_startup(standalone_metrics_server):
84
84
def test_metrics_server_with_authentication ():
85
85
"""Test metrics server with authentication enabled."""
86
86
port = find_available_port (9092 )
87
-
87
+
88
88
# Create a server app with authentication
89
89
with patch .dict ("os.environ" , {"JUPYTER_SERVER_METRICS_PORT" : str (port )}):
90
90
app = ServerApp ()
91
91
app .metrics_port = port
92
92
app .authenticate_prometheus = True
93
93
app .initialize ([])
94
-
94
+
95
95
# Start the app
96
96
app .start_app ()
97
-
97
+
98
98
# Wait for both servers to be ready
99
99
time .sleep (1.0 )
100
-
100
+
101
101
try :
102
102
# Get the token
103
103
token = app .identity_provider .token
104
-
104
+
105
105
# Test metrics endpoint with token
106
- response = wait_for_server (
107
- f"http://localhost:{ port } /metrics?token={ token } " ,
108
- timeout = 5
109
- )
106
+ response = wait_for_server (f"http://localhost:{ port } /metrics?token={ token } " , timeout = 5 )
110
107
assert response .status_code == 200
111
108
assert "jupyter_server_info" in response .text
112
-
109
+
113
110
# Test without token should fail
114
111
try :
115
112
response = requests .get (f"http://localhost:{ port } /metrics" , timeout = 2 )
116
113
assert response .status_code == 403
117
114
except requests .exceptions .ConnectionError :
118
115
# Server might not be ready yet, which is also acceptable
119
116
pass
120
-
117
+
121
118
finally :
122
119
app .stop ()
123
120
124
121
125
122
def test_metrics_server_without_authentication ():
126
123
"""Test metrics server without authentication."""
127
124
port = find_available_port (9093 )
128
-
125
+
129
126
# Create a server app without authentication
130
127
with patch .dict ("os.environ" , {"JUPYTER_SERVER_METRICS_PORT" : str (port )}):
131
128
app = ServerApp ()
132
129
app .metrics_port = port
133
130
app .authenticate_prometheus = False
134
131
app .initialize ([])
135
-
132
+
136
133
# Start the app
137
134
app .start_app ()
138
-
135
+
139
136
# Wait for both servers to be ready
140
137
time .sleep (1.0 )
141
-
138
+
142
139
try :
143
140
# Test metrics endpoint without token should work
144
- response = wait_for_server (
145
- f"http://localhost:{ port } /metrics" ,
146
- timeout = 5
147
- )
141
+ response = wait_for_server (f"http://localhost:{ port } /metrics" , timeout = 5 )
148
142
assert response .status_code == 200
149
143
assert "jupyter_server_info" in response .text
150
-
144
+
151
145
finally :
152
146
app .stop ()
153
147
@@ -156,20 +150,20 @@ def test_metrics_server_port_conflict():
156
150
"""Test that metrics server handles port conflicts gracefully."""
157
151
# Use a port that's likely to be in use
158
152
port = 8888 # Default Jupyter port
159
-
153
+
160
154
# Create a server app that should fail to start metrics server
161
155
with patch .dict ("os.environ" , {"JUPYTER_SERVER_METRICS_PORT" : str (port )}):
162
156
app = ServerApp ()
163
157
app .metrics_port = port
164
158
app .initialize ([])
165
-
159
+
166
160
# Start the app - should not crash
167
161
app .start_app ()
168
-
162
+
169
163
try :
170
164
# The app should still be running even if metrics server failed
171
165
assert app .http_server is not None
172
-
166
+
173
167
finally :
174
168
app .stop ()
175
169
@@ -180,22 +174,21 @@ def test_metrics_server_disabled():
180
174
app = ServerApp ()
181
175
app .metrics_port = 0
182
176
app .initialize ([])
183
-
177
+
184
178
# Start the app
185
179
app .start_app ()
186
-
180
+
187
181
# Wait for server to be ready
188
182
time .sleep (0.5 )
189
-
183
+
190
184
try :
191
185
# Metrics should be available on main server
192
186
token = app .identity_provider .token
193
187
response = wait_for_server (
194
- f"http://localhost:{ app .port } /metrics?token={ token } " ,
195
- timeout = 5
188
+ f"http://localhost:{ app .port } /metrics?token={ token } " , timeout = 5
196
189
)
197
190
assert response .status_code == 200
198
191
assert "jupyter_server_info" in response .text
199
-
192
+
200
193
finally :
201
194
app .stop ()
0 commit comments