Repository navigation
Expand file tree
/
Copy pathtest_util.py
More file actions
143 lines (126 loc) · 6.7 KB
/
Copy pathtest_util.py
File metadata and controls
143 lines (126 loc) · 6.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
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
# -*- coding: utf-8; -*-
import socketserver
from ...syntax import macros, test, test_raises, warn # noqa: F401
from ...test.fixtures import session, testset
from .fixtures import nettest
from ..util import ReceiveBuffer, ReuseAddrThreadingTCPServer, recvall, netstringify
def runtests():
with testset("netstringify"):
test[netstringify(b"hello world") == b"11:hello world,"]
with testset("sendall"):
server = lambda sock: recvall(1024, sock)
client = lambda sock: [sock.sendall(b"x" * 512), sock.sendall(b"x" * 512)]
test[len(nettest(server, client)) == 1024]
with testset("ReuseAddrThreadingTCPServer"):
# Direct regression guard for a latent bug that hit us once already:
# the class used to override `server_bind()` to set `SO_REUSEADDR`,
# but the override silently dropped the
# `self.server_address = self.socket.getsockname()` refresh from
# stdlib `TCPServer.server_bind`. That line is what updates
# `server_address` to reflect the kernel-assigned port when you
# bind to port 0. Without it, `server.server_address[1]` reports
# 0 even though the socket is listening on a real port.
#
# The fix (commit `4243ded`) was to delete the custom override
# and just set `allow_reuse_address = True` as a class attribute,
# letting stdlib's `server_bind` do both the sockopt AND the
# `server_address` refresh. This testset guards the fix.
class _NullHandler(socketserver.BaseRequestHandler):
def handle(self):
pass
with testset("bind to port 0 returns the actually-bound port"):
server = ReuseAddrThreadingTCPServer(("127.0.0.1", 0), _NullHandler)
try:
actual_port = server.server_address[1]
# The bug manifested as `actual_port == 0`; with the fix,
# we see a real kernel-assigned port (1024..65535 range,
# but we only check non-zero since the exact port is
# non-deterministic).
test[actual_port != 0]
test[actual_port > 0]
finally:
server.server_close()
with testset("allow_reuse_address is enabled"):
# The class attribute form: stdlib `TCPServer.server_bind`
# checks this and calls `setsockopt(SO_REUSEADDR, 1)` when
# truthy. We verify the class attribute is in place — the
# actual sockopt is exercised transitively by the fact that
# the integration tests run repeatedly without "Address
# already in use" errors.
test[ReuseAddrThreadingTCPServer.allow_reuse_address is True]
with testset("ReceiveBuffer"):
# `ReceiveBuffer` is a thin `BytesIO` wrapper with message-protocol
# semantics: append bytes as more arrive on a transport, `getvalue()`
# to inspect what's there, `set()` to replace (typically after a
# message boundary has been consumed and the remainder needs to
# stay in the buffer for the next message). It's part of
# `unpythonic.net.util.__all__` — a public API used internally by
# `MessageDecoder` and externally by at least one fleet-outside
# consumer (`raven.common.netutil`).
with testset("construction"):
# Default: empty buffer.
test[ReceiveBuffer().getvalue() == b""]
# Initial contents populate the buffer.
test[ReceiveBuffer(b"hello").getvalue() == b"hello"]
# `getvalue()` is non-destructive — calling twice returns
# the same bytes, buffer still has them afterwards.
buf = ReceiveBuffer(b"abc")
test[buf.getvalue() == b"abc"]
test[buf.getvalue() == b"abc"]
with testset("append"):
buf = ReceiveBuffer()
buf.append(b"hello")
test[buf.getvalue() == b"hello"]
# Multiple appends accumulate in order.
buf.append(b" ")
buf.append(b"world")
test[buf.getvalue() == b"hello world"]
# Empty append is a no-op.
buf.append(b"")
test[buf.getvalue() == b"hello world"]
# `append` returns self — chainable.
test[buf.append(b"!") is buf]
test[buf.getvalue() == b"hello world!"]
with testset("set replaces contents"):
buf = ReceiveBuffer(b"old contents")
buf.set(b"new")
test[buf.getvalue() == b"new"]
# set() with empty bytes clears the buffer.
buf.set(b"")
test[buf.getvalue() == b""]
# `set` also returns self.
test[ReceiveBuffer().set(b"x") is not None]
with testset("set + append: position is at end, not zero"):
# This is a subtle but documented property: `set(new_contents)`
# must leave the internal stream position at the *end* of the
# new contents, so a subsequent `append` continues from where
# `set` left off. The naive refactor `self._buffer =
# BytesIO(new_contents)` puts the position at 0 and the next
# write would overwrite — this test guards that regression.
buf = ReceiveBuffer()
buf.set(b"abc")
buf.append(b"def")
test[buf.getvalue() == b"abcdef"]
# Also via the __init__ path, which delegates to set().
buf2 = ReceiveBuffer(b"foo")
buf2.append(b"bar")
test[buf2.getvalue() == b"foobar"]
with testset("type errors"):
# `ReceiveBuffer` rejects non-`bytes` inputs strictly — `bytearray`,
# `memoryview`, and `str` are all refused even though `BytesIO`
# itself would accept some of them. This is deliberate: message-
# protocol code expects immutable `bytes` boundaries and a
# `bytearray` input could mutate under the buffer's feet.
test_raises[TypeError, ReceiveBuffer().append("not bytes")]
test_raises[TypeError, ReceiveBuffer().append(bytearray(b"nope"))]
test_raises[TypeError, ReceiveBuffer().append(memoryview(b"nope"))]
test_raises[TypeError, ReceiveBuffer().append(42)]
test_raises[TypeError, ReceiveBuffer().append(None)]
test_raises[TypeError, ReceiveBuffer().set("not bytes")]
test_raises[TypeError, ReceiveBuffer().set(bytearray(b"nope"))]
# Construction delegates to `set`, so the same check applies.
test_raises[TypeError, ReceiveBuffer("not bytes")]
test_raises[TypeError, ReceiveBuffer(bytearray(b"nope"))]
if __name__ == '__main__': # pragma: no cover
with session(__file__):
runtests()