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
=== modified file 'src/maasserver/tests/test_listener.py'
--- src/maasserver/tests/test_listener.py 2016-11-17 11:46:08 +0000
+++ src/maasserver/tests/test_listener.py 2017-03-23 17:06:45 +0000
@@ -53,6 +53,7 @@
53from twisted.internet.defer import (53from twisted.internet.defer import (
54 CancelledError,54 CancelledError,
55 Deferred,55 Deferred,
56 DeferredQueue,
56 inlineCallbacks,57 inlineCallbacks,
57)58)
58from twisted.logger import LogLevel59from twisted.logger import LogLevel
@@ -111,17 +112,51 @@
111 @wait_for_reactor112 @wait_for_reactor
112 @inlineCallbacks113 @inlineCallbacks
113 def test__handles_missing_system_handler_on_notification(self):114 def test__handles_missing_system_handler_on_notification(self):
114 listener = PostgresListenerService()115 # Captured notifications from the database will go here.
116 notices = DeferredQueue()
117
118 class PostgresListenerServiceSpy(PostgresListenerService):
119 """Send notices off to `notices` right after processing."""
120 def doRead(self):
121 try:
122 self.connection.connection.poll()
123 except:
124 self.loseConnection(Failure(error.ConnectionLost()))
125 else:
126 # Copy the pending notices now but don't put them in the
127 # queue until after the real doRead has processed them.
128 notifies = list(self.connection.connection.notifies)
129 try:
130 return super().doRead()
131 finally:
132 for notice in notifies:
133 notices.put(notice)
134
135 listener = PostgresListenerServiceSpy()
115 # Change notifications to a frozenset. This makes sure that136 # Change notifications to a frozenset. This makes sure that
116 # the system message does not go into the queue. Instead if should137 # the system message does not go into the queue. Instead if should
117 # call the handler directly in `doRead`.138 # call the handler directly in `doRead`.
118 listener.notifications = frozenset()139 listener.notifications = frozenset()
119 yield listener.startService()140 yield listener.startService()
120 yield deferToDatabase(listener.registerChannel, "sys_test")141
121 listener.listeners["sys_test"] = []142 # Use a randomised channel name even though LISTEN/NOTIFY is
143 # per-database and _not_ per-cluster.
144 channel = factory.make_name("sys_test", sep="_").lower()
145 self.assertTrue(listener.isSystemChannel(channel))
146 payload = factory.make_name("payload")
147
148 yield deferToDatabase(listener.registerChannel, channel)
149 listener.listeners[channel] = []
122 try:150 try:
123 yield deferToDatabase(self.send_notification, "sys_test", 1)151 # Notify our channel with a payload and wait for it to come back.
124 self.assertFalse("sys_test" in listener.listeners)152 yield deferToDatabase(self.send_notification, channel, payload)
153 while True:
154 notice = yield notices.get()
155 if notice.channel == channel:
156 self.assertThat(notice.payload, Equals(payload))
157 # Our channel has been deleted from the listeners map.
158 self.assertFalse(channel in listener.listeners)
159 break
125 finally:160 finally:
126 yield listener.stopService()161 yield listener.stopService()
127162