@@ -43,23 +43,25 @@ def encoded_frame_json(encoded_frame_binary):
4343def test_websocket_camera_init_default ():
4444 """Test WebSocketCamera initialization with default parameters."""
4545 camera = WebSocketCamera ()
46- assert camera .host == "0.0.0.0"
46+ assert camera .address == "ws:// 0.0.0.0:8080 "
4747 assert camera .port == 8080
4848 assert camera .timeout == 3
4949 assert camera .frame_format == "binary"
5050 assert camera .resolution == (640 , 480 )
5151 assert camera .fps == 10
52+ assert camera .status == "disconnected"
5253
5354
5455def test_websocket_camera_init_custom ():
5556 """Test WebSocketCamera initialization with custom parameters."""
5657 camera = WebSocketCamera (host = "127.0.0.1" , port = 9090 , timeout = 30 , frame_format = "json" , resolution = (1920 , 1080 ), fps = 30 )
57- assert camera .host == "127.0.0.1"
58+ assert camera .address == "ws:// 127.0.0.1:9090 "
5859 assert camera .port == 9090
5960 assert camera .timeout == 30
6061 assert camera .frame_format == "json"
6162 assert camera .resolution == (1920 , 1080 )
6263 assert camera .fps == 30
64+ assert camera .status == "disconnected"
6365
6466
6567def test_websocket_camera_start_stop ():
@@ -73,13 +75,17 @@ def test_websocket_camera_start_stop():
7375 pytest .fail ("Camera start failed" )
7476
7577 assert camera .is_started ()
78+ # Starting does not coincide with being connected in case of WebSocketCamera
79+ # as that depends on client activity
80+ assert camera .status == "disconnected"
7681
7782 try :
7883 camera .stop ()
7984 except Exception :
8085 pytest .fail ("Camera stop failed" )
8186
8287 assert not camera .is_started ()
88+ assert camera .status == "disconnected"
8389
8490
8591def test_websocket_camera_parse_message_binary (sample_frame , encoded_frame_binary ):
@@ -161,7 +167,7 @@ def test_websocket_camera_read_frame_empty_queue():
161167async def test_websocket_camera_capture_frame (encoded_frame_binary ):
162168 """Test capturing frame from WebSocket camera."""
163169 with WebSocketCamera (port = 0 , frame_format = "binary" ) as camera :
164- async with websockets .connect (f" { camera .protocol } :// { camera . host } : { camera . port } " ) as ws :
170+ async with websockets .connect (camera .address ) as ws :
165171 # Skip welcome message
166172 await ws .recv ()
167173
@@ -183,15 +189,15 @@ async def test_websocket_camera_single_client():
183189
184190 try :
185191 # Connect first client
186- async with websockets .connect (f" { camera .protocol } :// { camera . host } : { camera . port } " ) as ws1 :
192+ async with websockets .connect (camera .address ) as ws1 :
187193 # First client should receive welcome message
188194 welcome = await ws1 .recv ()
189195 message = json .loads (welcome )
190196 assert message ["status" ] == "connected"
191197
192198 # Try to connect second client while first is connected
193199 try :
194- async with websockets .connect (f" { camera .protocol } :// { camera . host } : { camera . port } " ) as ws2 :
200+ async with websockets .connect (camera .address ) as ws2 :
195201 # Second client should receive rejection message
196202 rejection = await asyncio .wait_for (ws2 .recv (), timeout = 1.0 )
197203 message = json .loads (rejection )
@@ -207,7 +213,7 @@ async def test_websocket_camera_single_client():
207213async def test_websocket_camera_welcome_message ():
208214 """Test that welcome message is sent to connected client."""
209215 with WebSocketCamera (port = 0 ) as camera :
210- async with websockets .connect (f" { camera .protocol } :// { camera . host } : { camera . port } " ) as ws :
216+ async with websockets .connect (camera .address ) as ws :
211217 # Should receive welcome message
212218 welcome = await asyncio .wait_for (ws .recv (), timeout = 1.0 )
213219 message = json .loads (welcome )
@@ -220,7 +226,7 @@ async def test_websocket_camera_welcome_message():
220226async def test_websocket_camera_receives_frames (encoded_frame_binary ):
221227 """Test that server receives and queues frames from client."""
222228 with WebSocketCamera (port = 0 , frame_format = "binary" ) as camera :
223- async with websockets .connect (f" { camera .protocol } :// { camera . host } : { camera . port } " ) as ws :
229+ async with websockets .connect (camera .address ) as ws :
224230 # Skip welcome message
225231 await ws .recv ()
226232
@@ -241,7 +247,7 @@ async def test_websocket_camera_disconnects_client_on_stop():
241247 camera .start ()
242248
243249 try :
244- async with websockets .connect (f" { camera .protocol } :// { camera . host } : { camera . port } " ) as ws :
250+ async with websockets .connect (camera .address ) as ws :
245251 # Client connected, receive welcome message
246252 welcome = await ws .recv ()
247253 message = json .loads (welcome )
@@ -280,7 +286,7 @@ def test_websocket_camera_stop_without_client():
280286async def test_websocket_camera_backpressure (sample_frame ):
281287 """Test that old frames are dropped when new frames arrive faster than they're consumed."""
282288 with WebSocketCamera (port = 0 , frame_format = "binary" ) as camera :
283- async with websockets .connect (f" { camera .protocol } :// { camera . host } : { camera . port } " ) as ws :
289+ async with websockets .connect (camera .address ) as ws :
284290 await ws .recv () # Skip welcome message
285291
286292 _ , buffer1 = cv2 .imencode (".jpg" , np .ones ((480 , 640 , 3 ), dtype = np .uint8 ) * 1 )
@@ -349,7 +355,7 @@ def event_listener(event_type, data):
349355
350356 # This should emit connection and disconnection events
351357 async def client_task ():
352- async with websockets .connect (f" { camera .protocol } :// { camera . host } : { camera . port } " ):
358+ async with websockets .connect (camera .address ):
353359 pass
354360
355361 # Run client concurrently to properly test event handling
@@ -432,7 +438,7 @@ def event_listener(event_type, data):
432438
433439 # This should emit a connection event but no disconnection event
434440 async def client_task ():
435- async with websockets .connect (f" { camera .protocol } :// { camera . host } : { camera . port } " ):
441+ async with websockets .connect (camera .address ):
436442 pass
437443 await can_close .wait ()
438444
0 commit comments