Debian Codegraph @

tests/test_aio_context.py

raw ◀ ▶
1 Debian patch touch this file
1 import tempfile
2
3 import pytest
4
5
6 def test_aio_context(context_maker):
7 ctx = context_maker()
8 assert ctx is not None
9
10 ctx = context_maker(1)
11 assert ctx is not None
12
•13 ctx = context_maker(128)
14 assert ctx is not None
15
16
17 def test_context_repr_does_not_crash(context_maker):
18 ctx = context_maker()
19 assert isinstance(repr(ctx), str)
20
21
22 def test_operation_repr_does_not_crash(backend):
23 with tempfile.NamedTemporaryFile() as f:
24 fd = f.fileno()
25 ops = [
26 backend.Operation.read(16, fd, 0),
27 backend.Operation.write(b"hello", fd, 0),
28 backend.Operation.fsync(fd),
29 backend.Operation.fdsync(fd),
30 ]
31 for op in ops:
32 assert isinstance(repr(op), str)
33
34
35 def test_write_operation_properties_before_submission(backend):
36 """.fileno/.offset/.nbytes/.payload for a freshly constructed write
37 Operation must reflect the constructor arguments identically across
38 every backend, even before it's ever submitted - unlike read (see
39 below), nothing here depends on backend-specific buffer allocation
40 strategy."""
41 with tempfile.NamedTemporaryFile() as f:
42 fd = f.fileno()
43 payload = b"the quick brown fox"
44 op = backend.Operation.write(payload, fd, 7)
45
46 assert op.fileno == fd
47 assert op.offset == 7
48 assert op.nbytes == len(payload)
49 assert bytes(op.payload) == payload
50
51
52 def test_read_operation_nbytes_before_submission(backend):
53 """Only .nbytes is guaranteed consistent for an unsubmitted read across
54 backends - .payload's pre-submission content is an implementation
55 detail (some backends pre-allocate a same-size buffer, python_aio
56 starts with an empty BytesIO instead), so it's deliberately not
57 asserted here."""
58 with tempfile.NamedTemporaryFile() as f:
59 fd = f.fileno()
60 op = backend.Operation.read(16, fd, 3)
61
62 assert op.fileno == fd
63 assert op.offset == 3
64 assert op.nbytes == 16
65
66
67 @pytest.mark.parametrize("ctor_name", ["fsync", "fdsync"])
68 def test_sync_operation_properties(backend, ctor_name):
69 with tempfile.NamedTemporaryFile() as f:
70 fd = f.fileno()
71 op = getattr(backend.Operation, ctor_name)(fd)
72
73 assert op.fileno == fd
74 assert op.offset == 0
75 assert op.nbytes == 0
76 # Some backends return None, others an empty buffer - both falsy.
77 assert not op.payload