Skip to content

Commit e1d0c79

Browse files
committed
Clearing envelopes between tests
1 parent fdd627c commit e1d0c79

File tree

1 file changed

+53
-41
lines changed

1 file changed

+53
-41
lines changed

tests/integrations/aws_lambda/test_aws_lambda.py

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,27 @@ def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
7979
},
8080
)
8181

82+
@classmethod
83+
def wait_for_stack(cls, timeout=30, port=SAM_PORT):
84+
"""
85+
Wait for SAM to be ready, with timeout.
86+
"""
87+
start_time = time.time()
88+
while True:
89+
if time.time() - start_time > timeout:
90+
raise TimeoutError(
91+
"SAM failed to start within {} seconds".format(timeout)
92+
)
8293

83-
def wait_for_sam(timeout=30, port=SAM_PORT):
84-
"""
85-
Wait for SAM to be ready, with timeout.
86-
"""
87-
start_time = time.time()
88-
while True:
89-
if time.time() - start_time > timeout:
90-
raise TimeoutError("SAM failed to start within {} seconds".format(timeout))
91-
92-
try:
93-
# Try to connect to SAM
94-
response = requests.get(f"http://127.0.0.1:{port}/")
95-
if response.status_code == 200 or response.status_code == 404:
96-
return
94+
try:
95+
# Try to connect to SAM
96+
response = requests.get(f"http://127.0.0.1:{port}/")
97+
if response.status_code == 200 or response.status_code == 404:
98+
return
9799

98-
except requests.exceptions.ConnectionError:
99-
time.sleep(1)
100-
continue
100+
except requests.exceptions.ConnectionError:
101+
time.sleep(1)
102+
continue
101103

102104

103105
class SentryTestServer:
@@ -153,20 +155,14 @@ def clear_envelopes(self):
153155

154156

155157
@pytest.fixture(scope="session", autouse=True)
156-
def sentry_test_server():
158+
def test_environment():
159+
print("Setting up AWS Lambda test infrastructure")
160+
161+
# Setup dummy relay to capture envelopes
157162
server = SentryTestServer()
158163
server.start()
159-
160164
time.sleep(1) # Give it a moment to start up
161165

162-
yield server
163-
164-
165-
@pytest.fixture(scope="session", autouse=True)
166-
def sam_stack():
167-
"""
168-
Create and deploy the SAM stack once for all tests
169-
"""
170166
# Create the SAM stack
171167
app = App()
172168
stack = DummyLambdaStack(app, "DummyLambdaStack", env={"region": SAM_REGION})
@@ -194,10 +190,21 @@ def sam_stack():
194190

195191
try:
196192
# Wait for SAM to be ready
197-
wait_for_sam()
198-
yield stack
193+
DummyLambdaStack.wait_for_stack()
194+
195+
def before_test():
196+
server.clear_envelopes()
197+
print("[TEST] Clearing envelopes before test")
198+
199+
yield {
200+
"stack": stack,
201+
"server": server,
202+
"before_test": before_test, # Add this function to the yielded dict
203+
}
199204

200205
finally:
206+
print("Tearing down AWS Lambda test infrastructure")
207+
201208
process.terminate()
202209
process.wait(timeout=5) # Give it time to shut down gracefully
203210

@@ -206,6 +213,11 @@ def sam_stack():
206213
process.kill()
207214

208215

216+
@pytest.fixture(autouse=True)
217+
def clear_before_test(test_environment):
218+
test_environment["before_test"]()
219+
220+
209221
@pytest.fixture
210222
def lambda_client():
211223
"""
@@ -220,31 +232,31 @@ def lambda_client():
220232
)
221233

222234

223-
def test_basic(lambda_client, sentry_test_server):
224-
sentry_test_server.clear_envelopes()
225-
235+
def test_basic(lambda_client, test_environment):
226236
response = lambda_client.invoke(
227237
FunctionName="BasicTestFunction", Payload=json.dumps({"name": "Ivana"})
228238
)
229239
result = json.loads(response["Payload"].read().decode())
230240
assert result == {"message": "Hello, Ivana!"}
231241

232-
print("envelopes:")
233-
print(sentry_test_server.envelopes)
242+
message, transaction = test_environment["server"].envelopes
243+
assert message["message"] == "[SENTRY MESSAGE] Hello, Ivana!"
244+
assert transaction["type"] == "transaction"
234245

235-
# assert sentry_test_server.envelopes == [{"message": "[SENTRY MESSAGE] Hello, Ivana!"}]
236246

237-
238-
def test_basic_2(lambda_client, sentry_test_server):
239-
sentry_test_server.clear_envelopes()
247+
def test_basic_2(lambda_client, test_environment):
248+
test_environment["server"].clear_envelopes()
240249

241250
response = lambda_client.invoke(
242251
FunctionName="BasicTestFunction", Payload=json.dumps({"name": "Neel"})
243252
)
244253
result = json.loads(response["Payload"].read().decode())
245254
assert result == {"message": "Hello, Neel!"}
246255

247-
print("envelopes2:")
248-
print(sentry_test_server.envelopes)
256+
message, transaction = test_environment["server"].envelopes
257+
assert message["message"] == "[SENTRY MESSAGE] Hello, Neel!"
258+
assert transaction["type"] == "transaction"
259+
249260

250-
# assert sentry_test_server.envelopes == [{"message": "[SENTRY MESSAGE] Hello, Neel!"}]
261+
# what is not working:
262+
# i should improve how the server are started and stopped at the beginning and end of the test session.

0 commit comments

Comments
 (0)