Skip to content

Commit fca879d

Browse files
committed
test_commands: add unittest for add_peer cli
1 parent 95ba7e7 commit fca879d

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/test_commands.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import binascii
23
import datetime
34
import os.path
@@ -740,3 +741,38 @@ async def test_export_lightning_preimage(self, *mock_args):
740741
assert await cmds.export_lightning_preimage(payment_hash=payment_hash.hex(), wallet=w) == preimage.hex()
741742
assert await cmds.export_lightning_preimage(payment_hash=os.urandom(32).hex(), wallet=w) is None
742743

744+
@mock.patch.object(wallet.Abstract_Wallet, 'save_db')
745+
@mock.patch('electrum.commands.LN_P2P_NETWORK_TIMEOUT', 0.001)
746+
async def test_add_peer(self, *mock_args):
747+
w = restore_wallet_from_text__for_unittest(
748+
'disagree rug lemon bean unaware square alone beach tennis exhibit fix mimic',
749+
path='if_this_exists_mocking_failed_648151893',
750+
config=self.config)['wallet']
751+
cmds = Commands(config=self.config)
752+
753+
# Mock the network and lnworker
754+
mock_lnworker = mock.Mock()
755+
w.lnworker = mock_lnworker
756+
mock_peer = mock.Mock()
757+
mock_peer.initialized = asyncio.Future()
758+
connection_string = "[email protected]:9735"
759+
called = False
760+
async def lnworker_add_peer(*args, **kwargs):
761+
assert args[0] == connection_string
762+
nonlocal called
763+
called += 1
764+
return mock_peer
765+
mock_lnworker.add_peer = lnworker_add_peer
766+
767+
# check if add_peer times out if peer doesn't initialize (LN_P2P_NETWORK_TIMEOUT is 0.001s)
768+
with self.assertRaises(UserFacingException):
769+
await cmds.add_peer(connection_string=connection_string, wallet=w)
770+
# check if add_peer called lnworker.add_peer
771+
assert called == 1
772+
773+
mock_peer.initialized = asyncio.Future()
774+
mock_peer.initialized.set_result(True)
775+
# check if add_peer returns True if peer is initialized
776+
result = await cmds.add_peer(connection_string=connection_string, wallet=w)
777+
assert called == 2
778+
self.assertTrue(result)

0 commit comments

Comments
 (0)