Skip to content

Commit fa17236

Browse files
committed
Merge #1534: Fixes websocket test in test_wallet_rpc.py
e3681f7 Fixes websocket test in test_wallet_rpc.py (Adam Gibson) Pull request description: Prior to this commit, the test of the reception of a notification via the websocket would fail intermittently because the notification was being sent without waiting for the setup of the websocket connection to complete. After this commit, we fire the notification event only after the setup and authentication check of the new websocket connection is complete. ACKs for top commit: roshii: utACK e3681f7 Tree-SHA512: b207a0a7d0d909dd523f2b344794c9f55bb08fdc1021ef41639a166c547947ce9b3f11ddb949c27f7dd607119f4d567e05dc1eae2aa760955eae96e139a02664
2 parents 3e4a69b + e3681f7 commit fa17236

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

jmclient/test/test_wallet_rpc.py

+29-11
Original file line numberDiff line numberDiff line change
@@ -180,32 +180,50 @@ class WalletRPCTestBaseFB(WalletRPCTestBase):
180180
# we are using fresh (empty) wallets for these tests
181181
wallet_structure = [0, 0, 0, 0, 0]
182182

183+
class ClientNotifTestProto(ClientTProtocol):
184+
185+
def sendAuth(self):
186+
task.deferLater(reactor, self.factory.delay,
187+
self.factory.callbackfn)
188+
super().sendAuth()
189+
190+
class ClientNotifTestFactory(WebSocketClientFactory):
191+
def __init__(self, *args, **kwargs):
192+
if "delay" in kwargs:
193+
self.delay = kwargs.pop("delay", None)
194+
if "callbackfn" in kwargs:
195+
self.callbackfn = kwargs.pop("callbackfn", None)
196+
super().__init__(*args, **kwargs)
197+
183198
class TrialTestWRPC_WS(WalletRPCTestBase, unittest.TestCase):
184199
""" class for testing websocket subscriptions/events etc.
185200
"""
201+
186202
def test_notif(self):
187203
# simulate the daemon already having created
188204
# a valid token (which it usually does when
189205
# starting the WalletService:
190206
self.daemon.wss_factory.valid_token = encoded_token
191-
self.client_factory = WebSocketClientFactory(
192-
"ws://127.0.0.1:"+str(self.wss_port))
193-
self.client_factory.protocol = ClientTProtocol
207+
# once the websocket connection is established, and auth
208+
# is sent, our custom clientfactory will fire the tx
209+
# notification via the callback passed as argument here;
210+
# and we wait for the receipt in the code below:
211+
self.client_factory = ClientNotifTestFactory(
212+
"ws://127.0.0.1:"+str(self.wss_port),
213+
delay=0.1, callbackfn=self.fire_tx_notif)
214+
self.client_factory.protocol = ClientNotifTestProto
194215
self.client_connector = connectWS(self.client_factory)
195-
d = task.deferLater(reactor, 0.1, self.fire_tx_notif)
196-
# create a small delay between the instruction to send
197-
# the notification, and the checking of its receipt,
198-
# otherwise the client will be queried before the notification
199-
# arrived. We will try a few times before giving up.
200216
self.attempt_receipt_counter = 0
201-
d.addCallback(self.wait_to_receive)
202-
return d
217+
return task.deferLater(reactor, 0.0, self.wait_to_receive)
203218

204-
def wait_to_receive(self, res):
219+
def wait_to_receive(self):
205220
d = task.deferLater(reactor, 0.1, self.checkNotifs)
206221
return d
207222

208223
def checkNotifs(self):
224+
# We wait and monitor if the notification has been received,
225+
# but give up after 10 attempts spaced by 0.2 seconds each.
226+
# It should usually succeed on the first try.
209227
if self.attempt_receipt_counter > 10:
210228
assert False
211229
if not self.client_factory.notifs == 1:

0 commit comments

Comments
 (0)