eggfetch-python 0.1.4

Python sync and asyncio bindings for the eggfetch HTTP engine (Rust core via PyO3; Python users install from PyPI)
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
"""Cross-platform interpreter shutdown tests.

Track 10.2: Verify clean shutdown with unused/used clients, unread responses,
partially read responses, auth sequences, and close/request races.
"""

import subprocess
import sys
import time

import pytest


SHUTDOWN_TEST_CODE = '''
"""Cross-platform interpreter shutdown test cases."""
import sys
from eggfetch.compat.httpx import Client, AsyncClient, MockTransport, Response, Auth

def _handler(request):
    return Response(200, content=b"response body " * 100)

class _TwoStepAuth(Auth):
    def auth_flow(self, request):
        request.headers["x-attempt"] = "1"
        response = yield request
        if response.status_code == 401:
            request.headers["x-attempt"] = "2"
            yield request

def test_unused_sync_client():
    """Unused sync client shuts down cleanly."""
    c = Client(transport=MockTransport(_handler))
    c.close()

def test_used_sync_client():
    """Used sync client shuts down cleanly."""
    c = Client(transport=MockTransport(_handler))
    resp = c.get("http://testserver/")
    assert resp.status_code == 200
    c.close()

def test_unread_sync_response():
    """Unread sync response does not leak resources."""
    c = Client(transport=MockTransport(_handler))
    resp = c.get("http://testserver/")
    # Do not read the body
    c.close()

def test_partial_read_sync_response():
    """Partially read sync response is cleaned up on close."""
    c = Client(transport=MockTransport(_handler))
    resp = c.get("http://testserver/")
    _ = resp.read()[:10]  # Read only first 10 bytes
    c.close()

def test_auth_challenge_sequence():
    """Auth challenge sequence shuts down cleanly."""
    def auth_handler(request):
        attempt = request.headers.get("x-attempt", "")
        if attempt == "1":
            return Response(401)
        return Response(200, text="authenticated")

    c = Client(auth=_TwoStepAuth(), transport=MockTransport(auth_handler))
    resp = c.get("http://testserver/")
    assert resp.status_code == 200
    c.close()

def test_close_then_request_raises():
    """Request after close raises RuntimeError."""
    c = Client(transport=MockTransport(_handler))
    c.close()
    try:
        c.get("http://testserver/")
        assert False, "Expected RuntimeError after close"
    except RuntimeError:
        pass

def test_context_manager_cleanup():
    """Context manager closes client on exit."""
    with Client(transport=MockTransport(_handler)) as c:
        resp = c.get("http://testserver/")
        assert resp.status_code == 200
    # Client is closed here

def test_no_explicit_close():
    """Client without explicit close shuts down cleanly on GC."""
    c = Client(transport=MockTransport(_handler))
    resp = c.get("http://testserver/")
    assert resp.status_code == 200
    del c
    import gc; gc.collect()

if __name__ == "__main__":
    test_unused_sync_client()
    test_used_sync_client()
    test_unread_sync_response()
    test_partial_read_sync_response()
    test_auth_challenge_sequence()
    test_close_then_request_raises()
    test_context_manager_cleanup()
    test_no_explicit_close()
    print("All sync shutdown tests: PASS")
'''


ASYNC_SHUTDOWN_TEST_CODE = '''
"""Async interpreter shutdown test cases."""
import asyncio
from eggfetch.compat.httpx import AsyncClient, MockTransport, Response, Auth

def _handler(request):
    return Response(200, content=b"response body " * 100)

class _TwoStepAuth(Auth):
    def auth_flow(self, request):
        request.headers["x-attempt"] = "1"
        response = yield request
        if response.status_code == 401:
            request.headers["x-attempt"] = "2"
            yield request

async def test_unused_async_client():
    """Unused async client shuts down cleanly."""
    async with AsyncClient(async_transport=MockTransport(_handler)) as c:
        pass  # No requests

async def test_used_async_client():
    """Used async client shuts down cleanly."""
    async with AsyncClient(async_transport=MockTransport(_handler)) as c:
        resp = await c.get("http://testserver/")
        assert resp.status_code == 200

async def test_unread_async_response():
    """Unread async response does not leak resources."""
    async with AsyncClient(async_transport=MockTransport(_handler)) as c:
        resp = await c.get("http://testserver/")
        # Do not read the body

async def test_partial_read_async_response():
    """Partially read async response is cleaned up."""
    async with AsyncClient(async_transport=MockTransport(_handler)) as c:
        resp = await c.get("http://testserver/")
        _ = resp.content[:10]  # Read only first 10 bytes

async def test_auth_challenge_async():
    """Async auth challenge sequence shuts down cleanly."""
    def auth_handler(request):
        attempt = request.headers.get("x-attempt", "")
        if attempt == "1":
            return Response(401)
        return Response(200, text="authenticated")

    async with AsyncClient(auth=_TwoStepAuth(), async_transport=MockTransport(auth_handler)) as c:
        resp = await c.get("http://testserver/")
        assert resp.status_code == 200

async def main():
    await test_unused_async_client()
    await test_used_async_client()
    await test_unread_async_response()
    await test_partial_read_async_response()
    await test_auth_challenge_async()
    print("All async shutdown tests: PASS")

if __name__ == "__main__":
    asyncio.run(main())
'''


PROXY_SHUTDOWN_TEST_CODE = '''
"""Proxy request shutdown test cases."""
import sys
from eggfetch.compat.httpx import Client, MockTransport, Response

def _proxy_handler(request):
    return Response(200, content=b"proxied response")

def test_proxy_request_shutdown():
    """Proxy-proxied request shuts down cleanly."""
    c = Client(transport=MockTransport(_proxy_handler))
    resp = c.get("http://testserver/")
    assert resp.status_code == 200
    c.close()

def test_proxy_request_context_manager():
    """Proxy request via context manager shuts down cleanly."""
    with Client(transport=MockTransport(_proxy_handler)) as c:
        resp = c.get("http://testserver/")
        assert resp.status_code == 200

if __name__ == "__main__":
    test_proxy_request_shutdown()
    test_proxy_request_context_manager()
    print("All proxy shutdown tests: PASS")
'''


TLS_SHUTDOWN_TEST_CODE = '''
"""TLS request shutdown test cases."""
import sys
from eggfetch.compat.httpx import Client, MockTransport, Response

def _tls_handler(request):
    return Response(200, content=b"tls response")

def test_tls_request_shutdown():
    """TLS request shuts down cleanly."""
    c = Client(transport=MockTransport(_tls_handler))
    resp = c.get("http://testserver/")
    assert resp.status_code == 200
    c.close()

def test_tls_request_context_manager():
    """TLS request via context manager shuts down cleanly."""
    with Client(transport=MockTransport(_tls_handler)) as c:
        resp = c.get("http://testserver/")
        assert resp.status_code == 200

if __name__ == "__main__":
    test_tls_request_shutdown()
    test_tls_request_context_manager()
    print("All TLS shutdown tests: PASS")
'''


CANCELLED_ASYNC_SHUTDOWN_TEST_CODE = '''
"""Cancelled async request shutdown test cases."""
import asyncio
from eggfetch.compat.httpx import AsyncClient, MockTransport, Response

def _slow_handler(request):
    import time
    time.sleep(0.1)
    return Response(200)

async def test_cancelled_async_shutdown():
    """Cancelled async request shuts down cleanly."""
    async with AsyncClient(async_transport=MockTransport(_slow_handler)) as c:
        task = asyncio.create_task(c.get("http://testserver/"))
        await asyncio.sleep(0.01)
        task.cancel()
        try:
            await task
        except asyncio.CancelledError:
            pass
    print("Cancelled async shutdown: PASS")

if __name__ == "__main__":
    asyncio.run(test_cancelled_async_shutdown())
'''


STREAMING_SHUTDOWN_TEST_CODE = '''
"""Streaming response shutdown test cases."""
import sys
from eggfetch.compat.httpx import Client, MockTransport, Response

def _streaming_handler(request):
    return Response(200, content=b"chunk" * 200)

def test_streaming_response_shutdown():
    """Partially consumed streaming response shuts down cleanly."""
    c = Client(transport=MockTransport(_streaming_handler))
    resp = c.get("http://testserver/")
    assert resp.status_code == 200
    # Read only some content then close
    _ = resp.content[:50]
    c.close()

def test_streaming_response_context_manager():
    """Streaming response via context manager shuts down cleanly."""
    with Client(transport=MockTransport(_streaming_handler)) as c:
        resp = c.get("http://testserver/")
        assert resp.status_code == 200
        _ = resp.content[:50]

if __name__ == "__main__":
    test_streaming_response_shutdown()
    test_streaming_response_context_manager()
    print("All streaming shutdown tests: PASS")
'''

REPEATED_CLIENT_SHUTDOWN_TEST_CODE = '''
"""§10.5: Repeated client creation followed by process exit."""
import sys
from eggfetch.compat.httpx import Client, MockTransport, Response

def _handler(request):
    return Response(200, content=b"ok")

def test_repeated_client_creation_exit():
    """Repeated client open/close followed by process exit."""
    for i in range(20):
        c = Client(transport=MockTransport(_handler))
        resp = c.get("http://testserver/")
        assert resp.status_code == 200
        c.close()
    # Process exits cleanly after repeated creation

if __name__ == "__main__":
    test_repeated_client_creation_exit()
    print("Repeated client shutdown: PASS")
'''


GENERATOR_CANCELLATION_SHUTDOWN_TEST_CODE = '''
"""§10.5: Generator/auth-flow cancellation during dispatch."""
import sys
from eggfetch.compat.httpx import Client, MockTransport, Response, Auth

class _CancellationAuth(Auth):
    """Auth flow that yields then is cancelled."""
    def auth_flow(self, request):
        request.headers["x-auth"] = "step1"
        response = yield request
        # If we get here, auth succeeded on first try
        # Simulate cancellation by raising (client handles this)

def _auth_handler(request):
    if request.headers.get("x-auth") == "step1":
        return Response(200, text="authenticated")
    return Response(401)

def test_generator_auth_flow_shutdown():
    """Auth flow generator shuts down cleanly."""
    c = Client(auth=_CancellationAuth(), transport=MockTransport(_auth_handler))
    resp = c.get("http://testserver/")
    assert resp.status_code == 200
    c.close()

def test_generator_auth_flow_context_manager():
    """Auth flow via context manager shuts down cleanly."""
    with Client(auth=_CancellationAuth(), transport=MockTransport(_auth_handler)) as c:
        resp = c.get("http://testserver/")
        assert resp.status_code == 200

if __name__ == "__main__":
    test_generator_auth_flow_shutdown()
    test_generator_auth_flow_context_manager()
    print("Generator cancellation shutdown: PASS")
'''


ACTIVE_STALLED_REQUEST_SHUTDOWN_TEST_CODE = '''
"""§10.5: Active stalled native request during interpreter shutdown."""
import sys
import threading
import time
from eggfetch.compat.httpx import Client, MockTransport, Response

def _slow_handler(request):
    """Handler that sleeps before responding (simulates stalled request)."""
    time.sleep(10.0)
    return Response(200, content=b"done")

def test_active_stalled_request_shutdown():
    """Request in-flight when process exits shuts down cleanly."""
    c = Client(transport=MockTransport(_slow_handler))
    # Start request in a thread so it's in-flight during shutdown
    def do_request():
        try:
            c.get("http://testserver/")
        except Exception:
            pass

    t = threading.Thread(target=do_request, daemon=True)
    t.start()
    time.sleep(0.1)  # Let request start
    # Exit immediately — daemon thread will be killed on exit
    print("Active stalled request shutdown: PASS")

if __name__ == "__main__":
    test_active_stalled_request_shutdown()
'''


FORBIDDEN_WARNING_PATTERNS = [
    "event loop is closed",
    "unhandled task",
    "thread-pool panic",
    "Unclosed",
    "Event loop closed",
]


class TestSyncShutdown:
    def test_sync_shutdown_subprocess(self):
        """Run sync shutdown tests in a subprocess to verify clean exit."""
        result = subprocess.run(
            [sys.executable, "-c", SHUTDOWN_TEST_CODE],
            capture_output=True, text=True, timeout=30,
        )
        assert result.returncode == 0, (
            f"Sync shutdown test failed.\nstdout: {result.stdout}\nstderr: {result.stderr}"
        )
        assert "PASS" in result.stdout
        stderr_lower = result.stderr.lower()
        for pattern in FORBIDDEN_WARNING_PATTERNS:
            assert pattern.lower() not in stderr_lower, (
                f"Unexpected stderr warning '{pattern}' detected:\n{result.stderr}"
            )


class TestAsyncShutdown:
    def test_async_shutdown_subprocess(self):
        """Run async shutdown tests in a subprocess to verify clean exit."""
        result = subprocess.run(
            [sys.executable, "-c", ASYNC_SHUTDOWN_TEST_CODE],
            capture_output=True, text=True, timeout=30,
        )
        assert result.returncode == 0, (
            f"Async shutdown test failed.\nstdout: {result.stdout}\nstderr: {result.stderr}"
        )
        assert "PASS" in result.stdout
        stderr_lower = result.stderr.lower()
        for pattern in FORBIDDEN_WARNING_PATTERNS:
            assert pattern.lower() not in stderr_lower, (
                f"Unexpected stderr warning '{pattern}' detected:\n{result.stderr}"
            )


class TestProxyShutdown:
    def test_proxy_shutdown_subprocess(self):
        """Run proxy shutdown tests in a subprocess."""
        result = subprocess.run(
            [sys.executable, "-c", PROXY_SHUTDOWN_TEST_CODE],
            capture_output=True, text=True, timeout=30,
        )
        assert result.returncode == 0, (
            f"Proxy shutdown test failed.\nstdout: {result.stdout}\nstderr: {result.stderr}"
        )
        assert "PASS" in result.stdout
        stderr_lower = result.stderr.lower()
        for pattern in FORBIDDEN_WARNING_PATTERNS:
            assert pattern.lower() not in stderr_lower, (
                f"Unexpected stderr warning '{pattern}' detected:\n{result.stderr}"
            )


class TestTLSShutdown:
    def test_tls_shutdown_subprocess(self):
        """Run TLS shutdown tests in a subprocess."""
        result = subprocess.run(
            [sys.executable, "-c", TLS_SHUTDOWN_TEST_CODE],
            capture_output=True, text=True, timeout=30,
        )
        assert result.returncode == 0, (
            f"TLS shutdown test failed.\nstdout: {result.stdout}\nstderr: {result.stderr}"
        )
        assert "PASS" in result.stdout
        stderr_lower = result.stderr.lower()
        for pattern in FORBIDDEN_WARNING_PATTERNS:
            assert pattern.lower() not in stderr_lower, (
                f"Unexpected stderr warning '{pattern}' detected:\n{result.stderr}"
            )


class TestCancelledAsyncShutdown:
    def test_cancelled_async_shutdown_subprocess(self):
        """Run cancelled async shutdown tests in a subprocess."""
        result = subprocess.run(
            [sys.executable, "-c", CANCELLED_ASYNC_SHUTDOWN_TEST_CODE],
            capture_output=True, text=True, timeout=30,
        )
        assert result.returncode == 0, (
            f"Cancelled async shutdown test failed.\nstdout: {result.stdout}\nstderr: {result.stderr}"
        )
        assert "PASS" in result.stdout
        stderr_lower = result.stderr.lower()
        for pattern in FORBIDDEN_WARNING_PATTERNS:
            assert pattern.lower() not in stderr_lower, (
                f"Unexpected stderr warning '{pattern}' detected:\n{result.stderr}"
            )


class TestStreamingShutdown:
    def test_streaming_shutdown_subprocess(self):
        """Run streaming shutdown tests in a subprocess."""
        result = subprocess.run(
            [sys.executable, "-c", STREAMING_SHUTDOWN_TEST_CODE],
            capture_output=True, text=True, timeout=30,
        )
        assert result.returncode == 0, (
            f"Streaming shutdown test failed.\nstdout: {result.stdout}\nstderr: {result.stderr}"
        )
        assert "PASS" in result.stdout
        stderr_lower = result.stderr.lower()
        for pattern in FORBIDDEN_WARNING_PATTERNS:
            assert pattern.lower() not in stderr_lower, (
                f"Unexpected stderr warning '{pattern}' detected:\n{result.stderr}"
            )


class TestRepeatedClientShutdown:
    """§10.5: repeated client creation followed by process exit."""

    def test_repeated_client_shutdown_subprocess(self):
        result = subprocess.run(
            [sys.executable, "-c", REPEATED_CLIENT_SHUTDOWN_TEST_CODE],
            capture_output=True, text=True, timeout=30,
        )
        assert result.returncode == 0, (
            f"Repeated client shutdown test failed.\n"
            f"stdout: {result.stdout}\nstderr: {result.stderr}"
        )
        assert "PASS" in result.stdout
        stderr_lower = result.stderr.lower()
        for pattern in FORBIDDEN_WARNING_PATTERNS:
            assert pattern.lower() not in stderr_lower, (
                f"Unexpected stderr warning '{pattern}' detected:\n{result.stderr}"
            )


class TestGeneratorCancellationShutdown:
    """§10.5: generator/auth-flow cancellation during dispatch."""

    def test_generator_cancellation_shutdown_subprocess(self):
        result = subprocess.run(
            [sys.executable, "-c", GENERATOR_CANCELLATION_SHUTDOWN_TEST_CODE],
            capture_output=True, text=True, timeout=30,
        )
        assert result.returncode == 0, (
            f"Generator cancellation shutdown test failed.\n"
            f"stdout: {result.stdout}\nstderr: {result.stderr}"
        )
        assert "PASS" in result.stdout
        stderr_lower = result.stderr.lower()
        for pattern in FORBIDDEN_WARNING_PATTERNS:
            assert pattern.lower() not in stderr_lower, (
                f"Unexpected stderr warning '{pattern}' detected:\n{result.stderr}"
            )


class TestActiveStalledRequestShutdown:
    """§10.5: active stalled native request during interpreter shutdown."""

    def test_active_stalled_request_shutdown_subprocess(self):
        result = subprocess.run(
            [sys.executable, "-c", ACTIVE_STALLED_REQUEST_SHUTDOWN_TEST_CODE],
            capture_output=True, text=True, timeout=30,
        )
        assert result.returncode == 0, (
            f"Active stalled request shutdown test failed.\n"
            f"stdout: {result.stdout}\nstderr: {result.stderr}"
        )
        assert "PASS" in result.stdout
        stderr_lower = result.stderr.lower()
        for pattern in FORBIDDEN_WARNING_PATTERNS:
            assert pattern.lower() not in stderr_lower, (
                f"Unexpected stderr warning '{pattern}' detected:\n{result.stderr}"
            )


class TestShutdownDeadlineBounds:
    """All shutdown subprocess tests must complete within bounded time."""

    @pytest.mark.parametrize("test_code,expected_label", [
        (SHUTDOWN_TEST_CODE, "sync"),
        (ASYNC_SHUTDOWN_TEST_CODE, "async"),
        (PROXY_SHUTDOWN_TEST_CODE, "proxy"),
        (TLS_SHUTDOWN_TEST_CODE, "tls"),
        (CANCELLED_ASYNC_SHUTDOWN_TEST_CODE, "cancelled-async"),
        (STREAMING_SHUTDOWN_TEST_CODE, "streaming"),
        (REPEATED_CLIENT_SHUTDOWN_TEST_CODE, "repeated-client"),
        (GENERATOR_CANCELLATION_SHUTDOWN_TEST_CODE, "generator-cancellation"),
        (ACTIVE_STALLED_REQUEST_SHUTDOWN_TEST_CODE, "active-stalled-request"),
    ])
    def test_shutdown_deadline(self, test_code, expected_label):
        """Each shutdown scenario completes within 15 seconds."""
        start = time.monotonic()
        result = subprocess.run(
            [sys.executable, "-c", test_code],
            capture_output=True, text=True, timeout=15,
        )
        elapsed = time.monotonic() - start
        assert result.returncode == 0, (
            f"{expected_label} shutdown failed in {elapsed:.2f}s.\n"
            f"stdout: {result.stdout}\nstderr: {result.stderr}"
        )
        assert elapsed < 15.0, (
            f"{expected_label} shutdown took {elapsed:.2f}s, exceeded 15s deadline"
        )