/ justapi / tests / test_batching.py
swadhinbiswas test: add end-to-end integration test suite with gRPC and proto stubs · 7/9/2026
43e015f
53 lines · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import asyncio
import time
from justapi.app import JustAPIApp, adaptive_batch
import urllib.request
import threading

app = JustAPIApp()

@app.post("/predict")
@adaptive_batch(max_size=3, window_ms=50)
def predict(requests):
    # Log the size of the batch
    print(f"Received batch of size {len(requests)}")
    # Simulate ML model computation
    time.sleep(0.1)
    
    results = []
    for req in requests:
        body = req.get("body", b"").decode("utf-8")
        results.append({"status": 200, "body": f"Processed {body}"})
    return results

def test_batching():
    def run_server():
        try:
            app.run("127.0.0.1:8081")
        except Exception:
            pass

    server_thread = threading.Thread(target=run_server, daemon=True)
    server_thread.start()

    time.sleep(1)

    def make_request(idx):
        req = urllib.request.Request("http://127.0.0.1:8081/predict", data=f"req-{idx}".encode("utf-8"), method="POST")
        try:
            with urllib.request.urlopen(req) as response:
                return response.read().decode("utf-8")
        except Exception as e:
            return str(e)

    threads = []
    for i in range(5):
        t = threading.Thread(target=lambda i=i: print(f"Response {i}: {make_request(i)}"))
        threads.append(t)
        t.start()

    for t in threads:
        t.join()

    print("Done")