Merge lp:~allenap/maas/spurious-listener-test into lp:~maas-committers/maas/trunk

Proposed by Gavin Panella
Status: Merged
Approved by: Gavin Panella
Approved revision: no longer in the source branch.
Merged at revision: 5848
Proposed branch: lp:~allenap/maas/spurious-listener-test
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 67 lines (+40/-5)
1 file modified
src/maasserver/tests/test_listener.py (+40/-5)
To merge this branch: bzr merge lp:~allenap/maas/spurious-listener-test
Reviewer Review Type Date Requested Status
Blake Rouse (community) Approve
Review via email: mp+320838@code.launchpad.net

Commit message

Fix spuriously failing test test__handles_missing_system_handler_on_notification.

To post a comment you must log in.
Revision history for this message
Blake Rouse (blake-rouse) wrote :

Nicely done! Thanks for the fix.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/tests/test_listener.py'
2--- src/maasserver/tests/test_listener.py 2016-11-17 11:46:08 +0000
3+++ src/maasserver/tests/test_listener.py 2017-03-23 17:06:45 +0000
4@@ -53,6 +53,7 @@
5 from twisted.internet.defer import (
6 CancelledError,
7 Deferred,
8+ DeferredQueue,
9 inlineCallbacks,
10 )
11 from twisted.logger import LogLevel
12@@ -111,17 +112,51 @@
13 @wait_for_reactor
14 @inlineCallbacks
15 def test__handles_missing_system_handler_on_notification(self):
16- listener = PostgresListenerService()
17+ # Captured notifications from the database will go here.
18+ notices = DeferredQueue()
19+
20+ class PostgresListenerServiceSpy(PostgresListenerService):
21+ """Send notices off to `notices` right after processing."""
22+ def doRead(self):
23+ try:
24+ self.connection.connection.poll()
25+ except:
26+ self.loseConnection(Failure(error.ConnectionLost()))
27+ else:
28+ # Copy the pending notices now but don't put them in the
29+ # queue until after the real doRead has processed them.
30+ notifies = list(self.connection.connection.notifies)
31+ try:
32+ return super().doRead()
33+ finally:
34+ for notice in notifies:
35+ notices.put(notice)
36+
37+ listener = PostgresListenerServiceSpy()
38 # Change notifications to a frozenset. This makes sure that
39 # the system message does not go into the queue. Instead if should
40 # call the handler directly in `doRead`.
41 listener.notifications = frozenset()
42 yield listener.startService()
43- yield deferToDatabase(listener.registerChannel, "sys_test")
44- listener.listeners["sys_test"] = []
45+
46+ # Use a randomised channel name even though LISTEN/NOTIFY is
47+ # per-database and _not_ per-cluster.
48+ channel = factory.make_name("sys_test", sep="_").lower()
49+ self.assertTrue(listener.isSystemChannel(channel))
50+ payload = factory.make_name("payload")
51+
52+ yield deferToDatabase(listener.registerChannel, channel)
53+ listener.listeners[channel] = []
54 try:
55- yield deferToDatabase(self.send_notification, "sys_test", 1)
56- self.assertFalse("sys_test" in listener.listeners)
57+ # Notify our channel with a payload and wait for it to come back.
58+ yield deferToDatabase(self.send_notification, channel, payload)
59+ while True:
60+ notice = yield notices.get()
61+ if notice.channel == channel:
62+ self.assertThat(notice.payload, Equals(payload))
63+ # Our channel has been deleted from the listeners map.
64+ self.assertFalse(channel in listener.listeners)
65+ break
66 finally:
67 yield listener.stopService()
68