@@ -17,7 +17,7 @@ def find_available_port(start_port=9090, max_attempts=10):
1717 port = start_port + i
1818 try :
1919 with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as s :
20- s .bind ((' localhost' , port ))
20+ s .bind ((" localhost" , port ))
2121 return port
2222 except OSError :
2323 continue
@@ -74,7 +74,7 @@ def test_metrics_server_startup(standalone_metrics_server):
7474 """Test that metrics server starts correctly."""
7575 assert standalone_metrics_server .port is not None
7676 assert standalone_metrics_server .port > 0
77-
77+
7878 # Test that metrics endpoint is accessible
7979 response = wait_for_server (f"http://localhost:{ standalone_metrics_server .port } /metrics" )
8080 assert response .status_code == 200
@@ -84,70 +84,64 @@ def test_metrics_server_startup(standalone_metrics_server):
8484def test_metrics_server_with_authentication ():
8585 """Test metrics server with authentication enabled."""
8686 port = find_available_port (9092 )
87-
87+
8888 # Create a server app with authentication
8989 with patch .dict ("os.environ" , {"JUPYTER_SERVER_METRICS_PORT" : str (port )}):
9090 app = ServerApp ()
9191 app .metrics_port = port
9292 app .authenticate_prometheus = True
9393 app .initialize ([])
94-
94+
9595 # Start the app
9696 app .start_app ()
97-
97+
9898 # Wait for both servers to be ready
9999 time .sleep (1.0 )
100-
100+
101101 try :
102102 # Get the token
103103 token = app .identity_provider .token
104-
104+
105105 # 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 )
110107 assert response .status_code == 200
111108 assert "jupyter_server_info" in response .text
112-
109+
113110 # Test without token should fail
114111 try :
115112 response = requests .get (f"http://localhost:{ port } /metrics" , timeout = 2 )
116113 assert response .status_code == 403
117114 except requests .exceptions .ConnectionError :
118115 # Server might not be ready yet, which is also acceptable
119116 pass
120-
117+
121118 finally :
122119 app .stop ()
123120
124121
125122def test_metrics_server_without_authentication ():
126123 """Test metrics server without authentication."""
127124 port = find_available_port (9093 )
128-
125+
129126 # Create a server app without authentication
130127 with patch .dict ("os.environ" , {"JUPYTER_SERVER_METRICS_PORT" : str (port )}):
131128 app = ServerApp ()
132129 app .metrics_port = port
133130 app .authenticate_prometheus = False
134131 app .initialize ([])
135-
132+
136133 # Start the app
137134 app .start_app ()
138-
135+
139136 # Wait for both servers to be ready
140137 time .sleep (1.0 )
141-
138+
142139 try :
143140 # 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 )
148142 assert response .status_code == 200
149143 assert "jupyter_server_info" in response .text
150-
144+
151145 finally :
152146 app .stop ()
153147
@@ -156,20 +150,20 @@ def test_metrics_server_port_conflict():
156150 """Test that metrics server handles port conflicts gracefully."""
157151 # Use a port that's likely to be in use
158152 port = 8888 # Default Jupyter port
159-
153+
160154 # Create a server app that should fail to start metrics server
161155 with patch .dict ("os.environ" , {"JUPYTER_SERVER_METRICS_PORT" : str (port )}):
162156 app = ServerApp ()
163157 app .metrics_port = port
164158 app .initialize ([])
165-
159+
166160 # Start the app - should not crash
167161 app .start_app ()
168-
162+
169163 try :
170164 # The app should still be running even if metrics server failed
171165 assert app .http_server is not None
172-
166+
173167 finally :
174168 app .stop ()
175169
@@ -180,22 +174,21 @@ def test_metrics_server_disabled():
180174 app = ServerApp ()
181175 app .metrics_port = 0
182176 app .initialize ([])
183-
177+
184178 # Start the app
185179 app .start_app ()
186-
180+
187181 # Wait for server to be ready
188182 time .sleep (0.5 )
189-
183+
190184 try :
191185 # Metrics should be available on main server
192186 token = app .identity_provider .token
193187 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
196189 )
197190 assert response .status_code == 200
198191 assert "jupyter_server_info" in response .text
199-
192+
200193 finally :
201194 app .stop ()
0 commit comments