Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Refactor Linearizer, convert methods to async and use an async context manager #12357

Merged
merged 18 commits into from
Apr 5, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add comments to Linearizer tests
Signed-off-by: Sean Quah <seanq@element.io>
  • Loading branch information
Sean Quah committed Apr 1, 2022
commit 071618db91a6a73a1c9252d1646257e120cd4f14
17 changes: 14 additions & 3 deletions tests/util/test_linearizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_linearizer(self):
d2 = linearizer.queue(key)
self.assertFalse(d2.called)

# Once the first task is done, the second task can continue.
with cm1:
self.assertFalse(d2.called)

Expand All @@ -56,13 +57,14 @@ def test_linearizer_is_queued(self):
d1 = linearizer.queue(key)
cm1 = yield d1

# Since d1 gets called immediately, "is_queued" should return false.
# Since the first task acquires the lock immediately, "is_queued" should return
# false.
self.assertFalse(linearizer.is_queued(key))

d2 = linearizer.queue(key)
self.assertFalse(d2.called)

# Now d2 is queued up behind successful completion of cm1
# Now the second task is queued up behind the first.
self.assertTrue(linearizer.is_queued(key))

with cm1:
Expand All @@ -71,7 +73,7 @@ def test_linearizer_is_queued(self):
# cm1 still not done, so d2 still queued.
self.assertTrue(linearizer.is_queued(key))

# And now d2 is called and nothing is in the queue again
# And now the second task acquires the lock and nothing is in the queue again.
self.assertFalse(linearizer.is_queued(key))

with (yield d2):
Expand Down Expand Up @@ -118,24 +120,28 @@ def test_multiple_entries(self):
d3 = limiter.queue(key)
cm3 = yield d3

# These next two tasks have to wait.
d4 = limiter.queue(key)
self.assertFalse(d4.called)

d5 = limiter.queue(key)
self.assertFalse(d5.called)

# Once the first task completes, the fourth task can continue.
with cm1:
self.assertFalse(d4.called)
self.assertFalse(d5.called)

cm4 = yield d4
self.assertFalse(d5.called)

# Once the third task completes, the fifth task can continue.
with cm3:
self.assertFalse(d5.called)

cm5 = yield d5

# Make all tasks finish.
with cm2:
pass

Expand All @@ -145,6 +151,7 @@ def test_multiple_entries(self):
with cm5:
pass

# The next task shouldn't have to wait.
d6 = limiter.queue(key)
with (yield d6):
pass
Expand All @@ -159,12 +166,15 @@ def test_cancellation(self):
d1 = linearizer.queue(key)
cm1 = yield d1

# Create a second task, waiting for the first task.
d2 = linearizer.queue(key)
self.assertFalse(d2.called)

# Create a third task, waiting for the second task.
d3 = linearizer.queue(key)
self.assertFalse(d3.called)

# Cancel the waiting second task.
d2.cancel()

with cm1:
Expand All @@ -177,5 +187,6 @@ def test_cancellation(self):
except CancelledError:
pass

# The third task should continue running.
with (yield d3):
pass