/ justapi / crates / justapi-py / tests / test_circuit_breaker_new.py
swadhinbiswas test(py): add comprehensive test suite for Python bindings · 7/8/2026
f693697
61 lines · 1.7 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
54
55
56
57
58
59
60
61
import urllib.request
from urllib.error import HTTPError
import multiprocessing
import time
from justapi import JustAPIApp

def run_server():
    app = JustAPIApp()
    app.enable_circuit_breaker(failure_threshold=2, reset_timeout_ms=5000)

    # Route that always fails
    @app.get("/flaky")
    def flaky(req):
        raise Exception("Random failure!")

    # Route that works
    @app.get("/ok")
    def ok(req):
        return {"status": "ok"}

    app.run("127.0.0.1:8088")

def fetch(url):
    try:
        urllib.request.urlopen(url)
        return 200
    except HTTPError as e:
        return e.code
    except Exception as e:
        return 0

def test_circuit_breaker():
    p = multiprocessing.Process(target=run_server)
    p.start()
    time.sleep(1.5)  # Wait for server to start

    try:
        # Request 1: fails, should return 500 (Python exception caught and mapped to 500)
        status1 = fetch("http://127.0.0.1:8088/flaky")
        assert status1 == 500, f"Expected 500, got {status1}"

        # Request 2: fails, should return 500
        status2 = fetch("http://127.0.0.1:8088/flaky")
        assert status2 == 500, f"Expected 500, got {status2}"

        # Request 3: Circuit is now OPEN! Should return 503
        status3 = fetch("http://127.0.0.1:8088/flaky")
        assert status3 == 503, f"Expected 503 Circuit Open, got {status3}"

        # Make sure another route is unaffected
        status_ok = fetch("http://127.0.0.1:8088/ok")
        assert status_ok == 200, f"Expected 200, got {status_ok}"

        print("Circuit breaker tested successfully!")
    finally:
        p.terminate()
        p.join()

if __name__ == "__main__":
    test_circuit_breaker()