summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorSean Quah <8349537+squahtx@users.noreply.github.com>2022-03-04 13:10:05 +0000
committerGitHub <noreply@github.com>2022-03-04 13:10:05 +0000
commit158e0937eb56f14dd851549939037de499526fd2 (patch)
tree187339c2e41c026a2784a5467400bf32f1d77991 /tests
parentRemove backwards compatibility with RelationPaginationToken. (#12138) (diff)
downloadsynapse-158e0937eb56f14dd851549939037de499526fd2.tar.xz
Add test for `ObservableDeferred`'s cancellation behaviour (#12149)
Signed-off-by: Sean Quah <seanq@element.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/util/test_async_helpers.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/tests/util/test_async_helpers.py b/tests/util/test_async_helpers.py
index 362014f4cb..ff53ce114b 100644
--- a/tests/util/test_async_helpers.py
+++ b/tests/util/test_async_helpers.py
@@ -100,6 +100,34 @@ class ObservableDeferredTest(TestCase):
         self.assertEqual(str(results[0].value), "gah!", "observer 1 errback result")
         self.assertEqual(str(results[1].value), "gah!", "observer 2 errback result")
 
+    def test_cancellation(self):
+        """Test that cancelling an observer does not affect other observers."""
+        origin_d: "Deferred[int]" = Deferred()
+        observable = ObservableDeferred(origin_d, consumeErrors=True)
+
+        observer1 = observable.observe()
+        observer2 = observable.observe()
+        observer3 = observable.observe()
+
+        self.assertFalse(observer1.called)
+        self.assertFalse(observer2.called)
+        self.assertFalse(observer3.called)
+
+        # cancel the second observer
+        observer2.cancel()
+        self.assertFalse(observer1.called)
+        self.failureResultOf(observer2, CancelledError)
+        self.assertFalse(observer3.called)
+
+        # other observers resolve as normal
+        origin_d.callback(123)
+        self.assertEqual(observer1.result, 123, "observer 1 callback result")
+        self.assertEqual(observer3.result, 123, "observer 3 callback result")
+
+        # additional observers resolve as normal
+        observer4 = observable.observe()
+        self.assertEqual(observer4.result, 123, "observer 4 callback result")
+
 
 class TimeoutDeferredTest(TestCase):
     def setUp(self):