Skip to content

Commit 5ced32a

Browse files
Ataba29razimograbi
authored andcommitted
Put all tests in 1 file because order is important
1 parent d7ba917 commit 5ced32a

2 files changed

Lines changed: 79 additions & 86 deletions

File tree

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +0,0 @@
1-
import socket
2-
import threading
3-
4-
import pytest
5-
from client import ByteForgeClient
6-
7-
8-
@pytest.fixture
9-
def client():
10-
c = ByteForgeClient()
11-
yield c
12-
c.close()
13-
14-
15-
def test_unknown_command(client):
16-
response = client._send("FOOBAR volt 3333")
17-
assert response == "No command was received"
18-
19-
20-
def test_concurrent_clients_distinct_keys():
21-
# Kept comfortably under the rate limiter's burst capacity (10),
22-
# since earlier tests in the session may have already spent tokens
23-
# that haven't fully refilled yet.
24-
num_clients = 5
25-
results: list[str | None] = [None] * num_clients
26-
errors: list[str | None] = [None] * num_clients
27-
28-
def worker(i):
29-
try:
30-
c = ByteForgeClient()
31-
key = f"vault{i}"
32-
value = str(1000 + i)
33-
c.insert(key, value)
34-
results[i] = c.get(key)
35-
c.delete(key)
36-
c.close()
37-
except OSError as e:
38-
errors[i] = str(e)
39-
40-
threads = [threading.Thread(target=worker, args=(i,)) for i in range(num_clients)]
41-
for t in threads:
42-
t.start()
43-
for t in threads:
44-
t.join()
45-
46-
for i in range(num_clients):
47-
assert errors[i] is None, f"client {i} failed: {errors[i]}"
48-
expected_value = str(1000 + i)
49-
assert results[i] == f"Get command was successful: {expected_value}"
50-
51-
52-
def test_cross_client_visibility_after_delete():
53-
client_a = ByteForgeClient()
54-
client_b = ByteForgeClient()
55-
56-
try:
57-
assert client_a.insert("shared", "42") == "Insert command was successful"
58-
assert client_a.delete("shared") == "Delete command was successful"
59-
60-
response = client_b.get("shared")
61-
assert response == "Get command was successful but key dont exist"
62-
finally:
63-
client_a.close()
64-
client_b.close()
65-
66-
67-
def test_rate_limiter_blocks_excess_connections():
68-
host, port = "127.0.0.1", 6625
69-
max_attempts = 50
70-
blocked = False
71-
72-
for _ in range(max_attempts):
73-
try:
74-
s = socket.create_connection((host, port), timeout=1.0)
75-
s.sendall(b"GET volt\n")
76-
data = s.recv(1024)
77-
s.close()
78-
79-
if data == b"":
80-
blocked = True
81-
break
82-
except (ConnectionResetError, ConnectionAbortedError, BrokenPipeError, OSError):
83-
blocked = True
84-
break
85-
86-
assert blocked, f"Expected at least one blocked connection within {max_attempts} attempts"

src/Tests/integration/test_basic_commands.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import pytest
22
from 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

Comments
 (0)