Skip to content

Commit

Permalink
Better test
Browse files Browse the repository at this point in the history
  • Loading branch information
richardm-stripe committed Dec 1, 2023
1 parent d849362 commit b3d9149
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
7 changes: 5 additions & 2 deletions stripe/request_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ def __init__(
self.usage = usage

def payload(self):
return {
ret = {
"request_id": self.request_id,
"request_duration_ms": self.request_duration_ms,
"usage": self.usage,
}

if (self.usage is not None and len(self.usage) > 0):
ret["usage"] = self.usage
return ret
16 changes: 11 additions & 5 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ class MockServerRequestHandler(TestHandler):

def test_passes_client_telemetry_when_enabled(self):
class MockServerRequestHandler(TestHandler):
num_requests = 0

def do_request(self, req_num):
if req_num == 0:
time.sleep(31 / 1000) # 31 ms
Expand All @@ -174,7 +172,7 @@ def do_request(self, req_num):
200,
{
"Content-Type": "application/json; charset=utf-8",
"Request-Id": "req_1",
"Request-Id": "req_%s" % (req_num + 1),
},
None,
]
Expand All @@ -187,10 +185,11 @@ def do_request(self, req_num):
cus.description = "hello"
cus.save()

stripe.Customer.retrieve("cus_xyz")
stripe.Customer.retrieve("cus_xyz")

reqs = MockServerRequestHandler.get_requests(2)
assert MockServerRequestHandler.num_requests == 2
reqs = MockServerRequestHandler.get_requests(3)

# req 1
assert not reqs[0].headers.get("x-stripe-client-telemetry")
# req 2
Expand All @@ -208,6 +207,13 @@ def do_request(self, req_num):
usage = telemetry["last_request_metrics"]["usage"]
assert usage == ["save"]

# req 3
telemetry_raw = reqs[2].headers.get("x-stripe-client-telemetry")
assert telemetry_raw is not None
metrics = json.loads(telemetry_raw)["last_request_metrics"]
assert metrics["request_id"] == "req_2"
assert "usage" not in metrics

def test_uses_thread_local_client_telemetry(self):
class MockServerRequestHandler(TestHandler):
local_num_requests = 0
Expand Down

0 comments on commit b3d9149

Please sign in to comment.