Skip to content

Commit c9b3a79

Browse files
committed
Remove forked from test_transport and generalize server to be module level
1 parent 1cba56a commit c9b3a79

File tree

1 file changed

+56
-18
lines changed

1 file changed

+56
-18
lines changed

tests/test_transport.py

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,40 @@
4848
class CapturingServer(WSGIServer):
4949
def __init__(self, host="127.0.0.1", port=0, ssl_context=None):
5050
WSGIServer.__init__(self, host, port, self, ssl_context=ssl_context)
51-
self.code = 204
51+
self.code = {}
5252
self.headers = {}
53-
self.captured = []
53+
self._captured = {}
54+
self.active_pytest_request = None
5455

5556
def respond_with(self, code=200, headers=None):
56-
self.code = code
57-
if headers:
58-
self.headers = headers
57+
assert self.active_pytest_request is not None
58+
self.code[self.active_pytest_request] = code
59+
self.headers[self.active_pytest_request] = headers or None
60+
61+
@property
62+
def captured(self):
63+
if self.active_pytest_request is not None:
64+
return self._captured[self.active_pytest_request]
65+
else:
66+
return []
5967

6068
def clear_captured(self):
61-
del self.captured[:]
69+
assert self.active_pytest_request is not None
70+
del self._captured[self.active_pytest_request][:]
71+
72+
def set_pytest_request(self, pytest_request):
73+
self.active_pytest_request = pytest_request
74+
self._captured[self.active_pytest_request] = []
75+
76+
def clear_pytest_request(self):
77+
if self.active_pytest_request is not None:
78+
if self.active_pytest_request in self._captured:
79+
del self._captured[self.active_pytest_request]
80+
if self.active_pytest_request in self.code:
81+
del self.code[self.active_pytest_request]
82+
if self.active_pytest_request in self.headers:
83+
del self.headers[self.active_pytest_request]
84+
self.active_pytest_request = None
6285

6386
def __call__(self, environ, start_response):
6487
"""
@@ -82,25 +105,41 @@ def __call__(self, environ, start_response):
82105
else:
83106
envelope = Envelope.deserialize_from(rdr)
84107

85-
self.captured.append(
86-
CapturedData(
87-
path=request.path,
88-
event=event,
89-
envelope=envelope,
90-
compressed=compressed,
108+
if self.active_pytest_request is not None:
109+
self._captured[self.active_pytest_request].append(
110+
CapturedData(
111+
path=request.path,
112+
event=event,
113+
envelope=envelope,
114+
compressed=compressed,
115+
)
91116
)
92-
)
93117

94-
response = Response(status=self.code)
95-
response.headers.extend(self.headers)
118+
response = Response(status=self.code.get(self.active_pytest_request, 204))
119+
response.headers.extend(self.headers.get(self.active_pytest_request, {}))
96120
return response(environ, start_response)
97121

98122

99-
@pytest.fixture
100-
def capturing_server(request):
123+
server = None
124+
125+
126+
@pytest.fixture(scope="module", autouse=True)
127+
def make_capturing_server(request):
128+
global server
101129
server = CapturingServer()
102130
server.start()
103131
request.addfinalizer(server.stop)
132+
133+
134+
@pytest.fixture
135+
def capturing_server(request):
136+
global server
137+
server.set_pytest_request(request)
138+
139+
@request.addfinalizer
140+
def cleanup():
141+
server.clear_pytest_request()
142+
104143
return server
105144

106145

@@ -129,7 +168,6 @@ def mock_transaction_envelope(span_count):
129168
return envelope
130169

131170

132-
@pytest.mark.forked
133171
@pytest.mark.parametrize("debug", (True, False))
134172
@pytest.mark.parametrize("client_flush_method", ["close", "flush"])
135173
@pytest.mark.parametrize("use_pickle", (True, False))

0 commit comments

Comments
 (0)