Skip to content

Commit

Permalink
Merge #1534: Fixes websocket test in test_wallet_rpc.py
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kristapsk committed Aug 20, 2023
2 parents 3e4a69b + e3681f7 commit fa17236
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions jmclient/test/test_wallet_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,32 +180,50 @@ class WalletRPCTestBaseFB(WalletRPCTestBase):
# we are using fresh (empty) wallets for these tests
wallet_structure = [0, 0, 0, 0, 0]

class ClientNotifTestProto(ClientTProtocol):

def sendAuth(self):
task.deferLater(reactor, self.factory.delay,
self.factory.callbackfn)
super().sendAuth()

class ClientNotifTestFactory(WebSocketClientFactory):
def __init__(self, *args, **kwargs):
if "delay" in kwargs:
self.delay = kwargs.pop("delay", None)
if "callbackfn" in kwargs:
self.callbackfn = kwargs.pop("callbackfn", None)
super().__init__(*args, **kwargs)

class TrialTestWRPC_WS(WalletRPCTestBase, unittest.TestCase):
""" class for testing websocket subscriptions/events etc.
"""

def test_notif(self):
# simulate the daemon already having created
# a valid token (which it usually does when
# starting the WalletService:
self.daemon.wss_factory.valid_token = encoded_token
self.client_factory = WebSocketClientFactory(
"ws://127.0.0.1:"+str(self.wss_port))
self.client_factory.protocol = ClientTProtocol
# once the websocket connection is established, and auth
# is sent, our custom clientfactory will fire the tx
# notification via the callback passed as argument here;
# and we wait for the receipt in the code below:
self.client_factory = ClientNotifTestFactory(
"ws://127.0.0.1:"+str(self.wss_port),
delay=0.1, callbackfn=self.fire_tx_notif)
self.client_factory.protocol = ClientNotifTestProto
self.client_connector = connectWS(self.client_factory)
d = task.deferLater(reactor, 0.1, self.fire_tx_notif)
# create a small delay between the instruction to send
# the notification, and the checking of its receipt,
# otherwise the client will be queried before the notification
# arrived. We will try a few times before giving up.
self.attempt_receipt_counter = 0
d.addCallback(self.wait_to_receive)
return d
return task.deferLater(reactor, 0.0, self.wait_to_receive)

def wait_to_receive(self, res):
def wait_to_receive(self):
d = task.deferLater(reactor, 0.1, self.checkNotifs)
return d

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

0 comments on commit fa17236

Please sign in to comment.