11import pytest
22from client import ByteForgeClient
3+ import socket
4+ import threading
5+ import time
36
47
58@pytest .fixture
@@ -22,3 +25,79 @@ def test_get_after_delete(client):
2225
2326 response = client .get ("volt" )
2427 assert response == "Get command was successful but key dont exist"
28+
29+
30+
31+ def test_unknown_command (client ):
32+ response = client ._send ("FOOBAR volt 3333" )
33+ assert response == "No command was received"
34+
35+
36+ def test_concurrent_clients_distinct_keys ():
37+ # Kept comfortably under the rate limiter's burst capacity (10),
38+ # since earlier tests in the session may have already spent tokens
39+ # that haven't fully refilled yet.
40+ num_clients = 5
41+ results : list [str | None ] = [None ] * num_clients
42+ errors : list [str | None ] = [None ] * num_clients
43+
44+ def worker (i ):
45+ try :
46+ c = ByteForgeClient ()
47+ key = f"vault{ i } "
48+ value = str (1000 + i )
49+ c .insert (key , value )
50+ results [i ] = c .get (key )
51+ c .delete (key )
52+ c .close ()
53+ except OSError as e :
54+ errors [i ] = str (e )
55+
56+ threads = [threading .Thread (target = worker , args = (i ,)) for i in range (num_clients )]
57+ for t in threads :
58+ t .start ()
59+ for t in threads :
60+ t .join ()
61+
62+ for i in range (num_clients ):
63+ assert errors [i ] is None , f"client { i } failed: { errors [i ]} "
64+ expected_value = str (1000 + i )
65+ assert results [i ] == f"Get command was successful: { expected_value } "
66+
67+
68+ def test_cross_client_visibility_after_delete ():
69+ client_a = ByteForgeClient ()
70+ client_b = ByteForgeClient ()
71+
72+ try :
73+ assert client_a .insert ("shared" , "42" ) == "Insert command was successful"
74+ assert client_a .delete ("shared" ) == "Delete command was successful"
75+
76+ response = client_b .get ("shared" )
77+ assert response == "Get command was successful but key dont exist"
78+ finally :
79+ client_a .close ()
80+ client_b .close ()
81+
82+
83+ def test_rate_limiter_blocks_excess_connections ():
84+ host , port = "127.0.0.1" , 6625
85+ max_attempts = 50
86+ blocked = False
87+
88+ for _ in range (max_attempts ):
89+ try :
90+ s = socket .create_connection ((host , port ), timeout = 1.0 )
91+ s .sendall (b"GET volt\n " )
92+ data = s .recv (1024 )
93+ s .shutdown (socket .SHUT_RDWR )
94+ s .close ()
95+
96+ if data == b"" :
97+ blocked = True
98+ break
99+ except (ConnectionResetError , ConnectionAbortedError , BrokenPipeError , OSError ):
100+ blocked = True
101+ break
102+ time .sleep (0.2 )
103+ assert blocked , f"Expected at least one blocked connection within { max_attempts } attempts"
0 commit comments