Skip to content

Commit

Permalink
Add tests for feature flags in ASGI apps
Browse files Browse the repository at this point in the history
  • Loading branch information
imjoehaines committed Aug 11, 2023
1 parent 90b9ee7 commit cde7b7c
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions tests/integrations/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,70 @@ async def index(req):
assert len(exception2['stacktrace']) == 2
assert 'other_func' == exception2['stacktrace'][0]['method']
assert 'index' == exception2['stacktrace'][1]['method']

def test_feature_flags_dont_leak_between_requests(self):
count = 0
app = Starlette()

async def other_func():
nonlocal count
count += 1
bugsnag.add_feature_flag(str(count), 'b')

if count > 4:
bugsnag.clear_feature_flags()

raise ScaryException('fell winds!')

@app.route('/')
async def index(req):
nonlocal count
count += 1
bugsnag.add_feature_flag(str(count), 'a')

await other_func()
return PlainTextResponse('pineapple')

app = TestClient(BugsnagMiddleware(app))

with pytest.raises(Exception):
app.get('/')

assert self.sent_report_count == 1

payload = self.server.received[0]['json_body']
exception = payload['events'][0]['exceptions'][0]
feature_flags = payload['events'][0]['featureFlags']

assert exception['errorClass'] == 'tests.utils.ScaryException'
assert exception['message'] == 'fell winds!'
assert exception['stacktrace'][0]['method'] == 'other_func'
assert exception['stacktrace'][1]['method'] == 'index'

assert feature_flags == [
{'name': '1', 'variant': 'a'},
{'name': '2', 'variant': 'b'}
]

with pytest.raises(Exception):
app.get('/')

assert self.sent_report_count == 2

payload = self.server.received[1]['json_body']
feature_flags = payload['events'][0]['featureFlags']

assert feature_flags == [
{'name': '3', 'variant': 'a'},
{'name': '4', 'variant': 'b'}
]

with pytest.raises(Exception):
app.get('/')

assert self.sent_report_count == 3

payload = self.server.received[2]['json_body']
feature_flags = payload['events'][0]['featureFlags']

assert feature_flags == []

0 comments on commit cde7b7c

Please sign in to comment.