Skip to content

Commit 22c75d5

Browse files
authored
fix: allow subscription changes during experiment notifications (#164)
1 parent 1ecf363 commit 22c75d5

2 files changed

Lines changed: 83 additions & 1 deletion

File tree

‎growthbook/growthbook.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,8 @@ def _fireSubscriptions(self, experiment: Experiment[Any], result: Result[Any]) -
15161516
"experiment": experiment,
15171517
"result": result,
15181518
}
1519-
for cb in self._subscriptions:
1519+
# Subscription changes take effect on the next notification.
1520+
for cb in self._subscriptions.copy():
15201521
try:
15211522
cb(experiment, result)
15221523
except Exception:

‎tests/test_growthbook.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,87 @@ def test_fires_subscriptions_correctly():
13291329
gb.destroy()
13301330

13311331

1332+
def test_subscription_can_unsubscribe_itself():
1333+
gb = GrowthBook(attributes={"id": "user-1"})
1334+
calls = []
1335+
observed = []
1336+
1337+
def once(experiment, result):
1338+
calls.append((experiment.key, result))
1339+
unsubscribe()
1340+
1341+
unsubscribe = gb.subscribe(once)
1342+
gb.subscribe(lambda experiment, result: observed.append((experiment.key, result)))
1343+
try:
1344+
first = gb.run(Experiment(key="first", variations=[0, 1]))
1345+
second = gb.run(Experiment(key="second", variations=[0, 1]))
1346+
1347+
assert first.inExperiment is True
1348+
assert second.inExperiment is True
1349+
assert calls == [("first", first)]
1350+
assert observed == [("first", first), ("second", second)]
1351+
finally:
1352+
gb.destroy()
1353+
1354+
1355+
def test_subscription_can_destroy_growthbook():
1356+
gb = GrowthBook(attributes={"id": "user-1"})
1357+
calls = []
1358+
1359+
def destroy(experiment, result):
1360+
calls.append((experiment.key, result))
1361+
gb.destroy()
1362+
1363+
gb.subscribe(destroy)
1364+
try:
1365+
result = gb.run(Experiment(key="first", variations=[0, 1]))
1366+
gb.run(Experiment(key="second", variations=[0, 1]))
1367+
1368+
assert result.inExperiment is True
1369+
assert calls == [("first", result)]
1370+
finally:
1371+
gb.destroy()
1372+
1373+
1374+
def test_subscription_added_during_notification_waits_for_next_change():
1375+
gb = GrowthBook(attributes={"id": "user-1"})
1376+
calls = []
1377+
1378+
def added(experiment, result):
1379+
calls.append(experiment.key)
1380+
1381+
gb.subscribe(lambda experiment, result: gb.subscribe(added))
1382+
try:
1383+
first = Experiment(key="first", variations=[0, 1])
1384+
assert gb.run(first).inExperiment is True
1385+
assert calls == []
1386+
1387+
gb.run(first)
1388+
assert calls == []
1389+
1390+
assert gb.run(Experiment(key="second", variations=[0, 1])).inExperiment is True
1391+
assert calls == ["second"]
1392+
finally:
1393+
gb.destroy()
1394+
1395+
1396+
def test_subscription_error_does_not_prevent_other_notifications():
1397+
gb = GrowthBook(attributes={"id": "user-1"})
1398+
calls = []
1399+
1400+
def raises(experiment, result):
1401+
raise ValueError("subscriber failed")
1402+
1403+
gb.subscribe(raises)
1404+
gb.subscribe(lambda experiment, result: calls.append(result))
1405+
try:
1406+
result = gb.run(Experiment(key="first", variations=[0, 1]))
1407+
assert result.inExperiment is True
1408+
assert calls == [result]
1409+
finally:
1410+
gb.destroy()
1411+
1412+
13321413
def test_stores_assigned_variations_in_the_user():
13331414
gb = GrowthBook(
13341415
attributes={

0 commit comments

Comments
 (0)