-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_lifecycle.py
More file actions
604 lines (485 loc) · 18.6 KB
/
Copy pathtest_api_lifecycle.py
File metadata and controls
604 lines (485 loc) · 18.6 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
"""Deterministic retry, upload, and shutdown lifecycle regressions."""
from __future__ import annotations
import json
import asyncio
import threading
import time
from concurrent.futures import CancelledError
from dataclasses import dataclass
from pathlib import Path
from types import SimpleNamespace
from typing import Callable, Optional
import pytest
from google.genai.errors import APIError, ServerError
import eventcalendar.core.api_client as api_client_module
from eventcalendar.core.api_client import CalendarAPIClient
from eventcalendar.core.retry import RetryCategory, classify_retry
from eventcalendar.core.submission_runtime import (
CancellableNetworkRuntime,
SynchronousNetworkRuntime,
)
from eventcalendar.exceptions.errors import CalendarAPIError, ImageProcessingError
def _valid_event() -> dict:
return {
"title": "Review",
"start_time": "10:00",
"end_time": "11:00",
"date": "2026-07-30",
"timezone": "UTC",
}
def _bare_client(sdk, *, max_retries: int = 3, base_delay: float = 0) -> CalendarAPIClient:
client = object.__new__(CalendarAPIClient)
client.api_key_masked = "AIza...test"
client.max_retries = max_retries
client.base_delay = base_delay
client._closed = False
client._close_lock = threading.Lock()
client._active_operations = 0
client._transport_close_started = False
client._types = SimpleNamespace(
UploadFileConfig=lambda **kwargs: SimpleNamespace(**kwargs),
DeleteFileConfig=lambda **kwargs: SimpleNamespace(**kwargs),
)
client.client = sdk
client._network_runtime = SynchronousNetworkRuntime(sdk)
client.generation_config = object()
return client
@dataclass
class _ProcessedImage:
path: str
mime_type: str
on_cleanup: Callable[[], None]
def cleanup(self) -> None:
self.on_cleanup()
@pytest.mark.parametrize(
("error", "retryable", "category"),
[
(APIError(401, {"status": "UNAUTHENTICATED"}), False, RetryCategory.CREDENTIALS),
(APIError(403, {"status": "PERMISSION_DENIED"}), False, RetryCategory.PERMISSION),
(APIError(429, {"status": "RESOURCE_EXHAUSTED"}), True, RetryCategory.RATE_LIMIT),
(ServerError(503, {"status": "UNAVAILABLE"}), True, RetryCategory.SERVER),
(TimeoutError("socket timed out"), True, RetryCategory.TIMEOUT),
(Exception("Quota exceeded for the day"), False, RetryCategory.QUOTA),
(Exception("unknown vendor wording"), False, RetryCategory.UNKNOWN),
(TypeError("bad call site"), False, RetryCategory.UNKNOWN),
(APIError(429, {"status": "RESOURCE_EXHAUSTED", "message": "requests per day"}), False, RetryCategory.QUOTA),
],
)
def test_retry_decisions_prefer_typed_status_and_keep_legacy_fallback(
error: Exception,
retryable: bool,
category: RetryCategory,
) -> None:
decision = classify_retry(error)
assert decision.retryable is retryable
assert decision.category == category
assert decision.reason
def test_upload_retries_are_per_file_and_preprocessing_is_not_repeated(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
for name in ("one.png", "two.png", "invalid.png", "exhausted.png"):
(tmp_path / name).write_bytes(b"test image fixture")
attempts: dict[str, int] = {}
cleanup_counts: dict[str, int] = {}
class Files:
def upload(self, *, file: str, config):
del config
name = Path(file).name
attempts[name] = attempts.get(name, 0) + 1
if name == "two.png" and attempts[name] == 1:
raise ServerError(503, {"status": "UNAVAILABLE"})
if name == "exhausted.png":
raise ServerError(503, {"status": "UNAVAILABLE"})
return SimpleNamespace(name=f"files/{name}")
def delete(self, *, name: str, config) -> None:
del name, config
sdk = SimpleNamespace(files=Files(), close=lambda: None)
client = _bare_client(sdk)
def preprocess(path: str, mime_type: Optional[str]):
name = Path(path).name
if name == "invalid.png":
raise ImageProcessingError(path, "invalid image contents")
cleanup_counts.setdefault(name, 0)
return _ProcessedImage(
path,
mime_type or "image/png",
lambda name=name: cleanup_counts.__setitem__(name, cleanup_counts[name] + 1),
)
monkeypatch.setattr(api_client_module, "preprocess_image_for_upload", preprocess)
image_data = [
(str(tmp_path / name), "image/png", None)
for name in ("one.png", "two.png", "invalid.png", "exhausted.png")
]
batch = client._prepare_images(image_data, None)
assert [uploaded.name for uploaded in batch.contents] == ["files/one.png", "files/two.png"]
assert attempts == {"one.png": 1, "two.png": 2, "exhausted.png": 3}
assert cleanup_counts == {"one.png": 1, "two.png": 1, "exhausted.png": 1}
assert len(batch.failures) == 2
assert any("invalid image contents" in failure for failure in batch.failures)
assert any("Failed after 3 attempts" in failure for failure in batch.failures)
def test_cancellation_during_upload_backoff_cleans_processed_and_remote_files(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
for name in ("uploaded.png", "retrying.png"):
(tmp_path / name).write_bytes(b"test image fixture")
upload_attempts: list[str] = []
deleted: list[str] = []
cleaned: list[str] = []
class Files:
def upload(self, *, file: str, config):
del config
name = Path(file).name
upload_attempts.append(name)
if name == "retrying.png":
raise ServerError(503, {"status": "UNAVAILABLE"})
return SimpleNamespace(name=f"files/{name}")
def delete(self, *, name: str, config) -> None:
del config
deleted.append(name)
class CancelDuringWait:
waits: list[float]
def __init__(self) -> None:
self.waits = []
def is_set(self) -> bool:
return False
def wait(self, delay: float) -> bool:
self.waits.append(delay)
return True
sdk = SimpleNamespace(files=Files(), close=lambda: None)
client = _bare_client(sdk, base_delay=2)
cancel = CancelDuringWait()
monkeypatch.setattr(
api_client_module,
"preprocess_image_for_upload",
lambda path, mime: _ProcessedImage(
path,
mime or "image/png",
lambda path=path: cleaned.append(Path(path).name),
),
)
with pytest.raises(CancelledError):
client._prepare_images(
[
(str(tmp_path / "uploaded.png"), "image/png", None),
(str(tmp_path / "retrying.png"), "image/png", None),
],
cancel, # type: ignore[arg-type]
)
assert upload_attempts == ["uploaded.png", "retrying.png"]
assert cancel.waits == [2]
assert cleaned == ["uploaded.png", "retrying.png"]
assert len(deleted) == 2
assert all(name.startswith("files/eventcalendar-") for name in deleted)
def test_close_during_generation_waits_for_remote_cleanup_and_closes_once(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
image_path = tmp_path / "event.png"
image_path.write_bytes(b"test image fixture")
generation_started = threading.Event()
release_generation = threading.Event()
order: list[str] = []
class Files:
def upload(self, *, file: str, config):
del file, config
order.append("upload")
return SimpleNamespace(name="files/event.png")
def delete(self, *, name: str, config) -> None:
del config
assert name.startswith("files/eventcalendar-")
order.append("delete")
class Models:
def generate_content(self, **kwargs):
del kwargs
order.append("generation-start")
generation_started.set()
assert release_generation.wait(5), "test did not release blocked generation"
order.append("generation-finish")
return SimpleNamespace(text=json.dumps([_valid_event()]))
class SDK:
files = Files()
models = Models()
def close(self) -> None:
order.append("close")
client = _bare_client(SDK())
monkeypatch.setattr(
api_client_module,
"preprocess_image_for_upload",
lambda path, mime: _ProcessedImage(path, mime or "image/png", lambda: None),
)
results = []
failures: list[BaseException] = []
def run_extract() -> None:
try:
results.append(client.extract_events("Review", [(str(image_path), "image/png", None)]))
except BaseException as exc: # Captured for assertion in the test thread.
failures.append(exc)
worker = threading.Thread(target=run_extract, name="blocked_generation_test")
worker.start()
assert generation_started.wait(5)
try:
client.close()
assert "close" not in order
with pytest.raises(CalendarAPIError, match="closed"):
client.extract_events("New work", [])
with pytest.raises(CalendarAPIError, match="closed"):
client.upload_to_gemini(str(image_path), "image/png")
finally:
release_generation.set()
worker.join(5)
assert not worker.is_alive()
assert failures == []
assert len(results) == 1
assert order == ["upload", "generation-start", "generation-finish", "delete", "close"]
client.close()
assert order.count("close") == 1
def test_transport_close_failure_is_terminal_and_idempotent() -> None:
calls = []
class SDK:
def close(self) -> None:
calls.append("close")
raise RuntimeError("close failed")
client = _bare_client(SDK())
client.close()
client.close()
assert calls == ["close"]
with pytest.raises(CalendarAPIError, match="closed"):
client.extract_events("new work", [])
def test_inline_images_collapse_submission_to_one_sdk_call(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
from google.genai import types
paths = []
for index in range(8):
path = tmp_path / f"image-{index}.png"
path.write_bytes(b"small-image")
paths.append((str(path), "image/png", None))
calls = {"generate": 0}
class Files:
def upload(self, **_kwargs):
raise AssertionError("small transient images must not use the File API")
def delete(self, **_kwargs):
raise AssertionError("inline images own no remote cleanup")
class Models:
def generate_content(self, **kwargs):
calls["generate"] += 1
assert len(kwargs["contents"]) == 9
return SimpleNamespace(text=json.dumps([_valid_event()]))
sdk = SimpleNamespace(files=Files(), models=Models(), close=lambda: None)
client = _bare_client(sdk)
client._types = types
monkeypatch.setattr(
api_client_module,
"preprocess_image_for_upload",
lambda path, mime: _ProcessedImage(path, mime or "image/png", lambda: None),
)
result = client.extract_events("Review", paths)
assert calls == {"generate": 1}
assert result.metrics.inline_images == 8
assert result.metrics.uploaded_images == 0
assert result.metrics.sdk_calls == 1
def test_active_async_request_is_cancelled_promptly() -> None:
from google.genai import types
started = threading.Event()
coroutine_cancelled = threading.Event()
class AsyncModels:
async def generate_content(self, **_kwargs):
started.set()
try:
await asyncio.sleep(60)
finally:
coroutine_cancelled.set()
class AsyncClient:
models = AsyncModels()
async def aclose(self) -> None:
pass
class SyncModels:
def generate_content(self, **_kwargs):
raise AssertionError("production-shaped clients must use cancellable async I/O")
class SDK:
aio = AsyncClient()
models = SyncModels()
def close(self) -> None:
pass
client = _bare_client(SDK())
client._types = types
client._network_runtime = CancellableNetworkRuntime(client.client)
cancel = threading.Event()
failures: list[BaseException] = []
worker = threading.Thread(
target=lambda: _capture_failure(
failures,
lambda: client.extract_events("Review", [], cancel_event=cancel),
)
)
worker.start()
assert started.wait(1)
cancelled_at = time.perf_counter()
cancel.set()
worker.join(1)
assert not worker.is_alive()
assert time.perf_counter() - cancelled_at < 0.25
assert len(failures) == 1 and isinstance(failures[0], CancelledError)
assert coroutine_cancelled.wait(1)
client.close()
def test_end_to_end_deadline_cancels_active_transport(
monkeypatch: pytest.MonkeyPatch,
) -> None:
from dataclasses import replace
from google.genai import types
from eventcalendar.exceptions.errors import RequestDeadlineExceeded
transport_cancelled = threading.Event()
class AsyncModels:
async def generate_content(self, **_kwargs):
try:
await asyncio.sleep(60)
finally:
transport_cancelled.set()
class AsyncClient:
models = AsyncModels()
async def aclose(self) -> None:
pass
class SDK:
aio = AsyncClient()
models = SimpleNamespace(generate_content=lambda **_kwargs: None)
def close(self) -> None:
pass
monkeypatch.setattr(
api_client_module,
"API_CONFIG",
replace(
api_client_module.API_CONFIG,
request_deadline_seconds=0.10,
generation_timeout_seconds=30.0,
),
)
client = _bare_client(SDK())
client._types = types
client._network_runtime = CancellableNetworkRuntime(client.client)
started = time.perf_counter()
with pytest.raises(RequestDeadlineExceeded):
client.extract_events("Review", [])
elapsed = time.perf_counter() - started
assert elapsed < 0.30
assert transport_cancelled.wait(1)
client.close()
def _capture_failure(failures: list[BaseException], operation: Callable[[], object]) -> None:
try:
operation()
except BaseException as exc:
failures.append(exc)
def test_success_status_is_not_emitted_before_cleanup_finishes(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
image_path = tmp_path / "large.png"
image_path.write_bytes(b"image")
cleanup_started = threading.Event()
release_cleanup = threading.Event()
statuses: list[str] = []
class Files:
def upload(self, **_kwargs):
return SimpleNamespace(name="files/large")
def delete(self, **_kwargs):
cleanup_started.set()
assert release_cleanup.wait(2)
class Models:
def generate_content(self, **_kwargs):
return SimpleNamespace(text=json.dumps([_valid_event()]))
client = _bare_client(SimpleNamespace(files=Files(), models=Models(), close=lambda: None))
monkeypatch.setattr(
api_client_module,
"preprocess_image_for_upload",
lambda path, mime: _ProcessedImage(path, mime or "image/png", lambda: None),
)
results = []
worker = threading.Thread(
target=lambda: results.append(
client.extract_events("Review", [(str(image_path), "image/png", None)], statuses.append)
)
)
worker.start()
assert cleanup_started.wait(1)
assert statuses[-1] == "Cleaning up temporary uploads..."
assert not any(status.startswith("Successfully") for status in statuses)
release_cleanup.set()
worker.join(2)
assert len(results) == 1
assert statuses[-1].startswith("Successfully")
def test_accepted_upload_with_lost_response_is_reconciled_and_deleted(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
from google.genai import types
image_path = tmp_path / "overflow.png"
with image_path.open("wb") as output:
output.truncate(12 * 1024 * 1024 + 1)
calls = {"upload": 0, "get": 0, "delete": []}
class Files:
def upload(self, *, file, config):
del file, config
calls["upload"] += 1
raise TimeoutError("response lost after server acceptance")
def get(self, *, name, config):
del config
calls["get"] += 1
return types.File(name=name, mime_type="image/png")
def delete(self, *, name, config):
del config
calls["delete"].append(name)
class Models:
def generate_content(self, **_kwargs):
return SimpleNamespace(text=json.dumps([_valid_event()]))
client = _bare_client(SimpleNamespace(files=Files(), models=Models(), close=lambda: None))
client._types = types
monkeypatch.setattr(
api_client_module,
"preprocess_image_for_upload",
lambda path, mime: _ProcessedImage(path, mime or "image/png", lambda: None),
)
result = client.extract_events(
"Review",
[(str(image_path), "image/png", None)],
)
assert calls["upload"] == 1
assert calls["get"] == 1
assert len(calls["delete"]) == 1
assert calls["delete"][0].startswith("files/eventcalendar-")
assert result.metrics.reconciliation_attempts == 1
def test_transient_remote_delete_is_retried_once(
monkeypatch: pytest.MonkeyPatch,
tmp_path: Path,
) -> None:
from google.genai import types
image_path = tmp_path / "overflow.png"
with image_path.open("wb") as output:
output.truncate(12 * 1024 * 1024 + 1)
delete_attempts = []
class Files:
def upload(self, *, file, config):
del file
return types.File(name=config.name, mime_type="image/png")
def delete(self, *, name, config):
del config
delete_attempts.append(name)
if len(delete_attempts) == 1:
raise TimeoutError("transient cleanup timeout")
class Models:
def generate_content(self, **_kwargs):
return SimpleNamespace(text=json.dumps([_valid_event()]))
client = _bare_client(SimpleNamespace(files=Files(), models=Models(), close=lambda: None))
client._types = types
monkeypatch.setattr(
api_client_module,
"preprocess_image_for_upload",
lambda path, mime: _ProcessedImage(path, mime or "image/png", lambda: None),
)
result = client.extract_events(
"Review",
[(str(image_path), "image/png", None)],
)
assert len(delete_attempts) == 2
assert result.metrics.cleanup_attempts == 2