tests/test_txbatcher.py: fix race in network.next_tx()
With `txbatcher.SLEEP_INTERVAL = 0.01`, on my laptop, the batcher called try_broadcasting() even before next_tx() effectively awaited _tx_event. This resulted in the test failing due to timeout. Basically, if the txbatcher.SLEEP_INTERVAL was too low, the 2-4 event loop iterations needed to await _tx_event.wait() took too long. (note that the exact number of event loop iterations needed depends on the python version and the OS)
This commit is contained in:
@@ -20,13 +20,14 @@ from .test_wallet_vertical import WalletIntegrityHelper
|
|||||||
class MockNetwork(Logger):
|
class MockNetwork(Logger):
|
||||||
|
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
|
Logger.__init__(self)
|
||||||
self.config = config
|
self.config = config
|
||||||
self.fee_estimates = FeeTimeEstimates()
|
self.fee_estimates = FeeTimeEstimates()
|
||||||
self.asyncio_loop = util.get_asyncio_loop()
|
self.asyncio_loop = util.get_asyncio_loop()
|
||||||
self.interface = None
|
self.interface = None
|
||||||
self.relay_fee = 1000
|
self.relay_fee = 1000
|
||||||
self.wallets = []
|
self.wallets = []
|
||||||
self._tx_event = asyncio.Event()
|
self._tx_queue = asyncio.Queue()
|
||||||
|
|
||||||
def get_local_height(self):
|
def get_local_height(self):
|
||||||
return 42
|
return 42
|
||||||
@@ -41,14 +42,12 @@ class MockNetwork(Logger):
|
|||||||
for w in self.wallets:
|
for w in self.wallets:
|
||||||
w.adb.receive_tx_callback(tx, TX_HEIGHT_UNCONFIRMED)
|
w.adb.receive_tx_callback(tx, TX_HEIGHT_UNCONFIRMED)
|
||||||
|
|
||||||
self._tx_event.set()
|
self._tx_queue.put_nowait(tx)
|
||||||
self._tx = tx
|
|
||||||
self._tx_event.clear()
|
|
||||||
return tx.txid()
|
return tx.txid()
|
||||||
|
|
||||||
async def next_tx(self):
|
async def next_tx(self):
|
||||||
await util.wait_for2(self._tx_event.wait(), timeout=10)
|
tx = await util.wait_for2(self._tx_queue.get(), timeout=10)
|
||||||
return self._tx
|
return tx
|
||||||
|
|
||||||
|
|
||||||
WALLET_SEED = 'cause carbon luggage air humble mistake melt paper supreme sense gravity void'
|
WALLET_SEED = 'cause carbon luggage air humble mistake melt paper supreme sense gravity void'
|
||||||
@@ -103,6 +102,7 @@ class TestTxBatcher(ElectrumTestCase):
|
|||||||
# fund wallet
|
# fund wallet
|
||||||
funding_tx = Transaction(FUNDING_TX)
|
funding_tx = Transaction(FUNDING_TX)
|
||||||
await self.network.try_broadcasting(funding_tx, 'funding')
|
await self.network.try_broadcasting(funding_tx, 'funding')
|
||||||
|
await self.network.next_tx()
|
||||||
assert wallet.adb.get_transaction(funding_tx.txid()) is not None
|
assert wallet.adb.get_transaction(funding_tx.txid()) is not None
|
||||||
self.logger.info(f'wallet balance {wallet.get_balance()}')
|
self.logger.info(f'wallet balance {wallet.get_balance()}')
|
||||||
# payment 1 -> tx1(output1)
|
# payment 1 -> tx1(output1)
|
||||||
@@ -145,6 +145,7 @@ class TestTxBatcher(ElectrumTestCase):
|
|||||||
# fund wallet
|
# fund wallet
|
||||||
funding_tx = Transaction(FUNDING_TX)
|
funding_tx = Transaction(FUNDING_TX)
|
||||||
await self.network.try_broadcasting(funding_tx, 'funding')
|
await self.network.try_broadcasting(funding_tx, 'funding')
|
||||||
|
await self.network.next_tx()
|
||||||
assert wallet.adb.get_transaction(funding_tx.txid()) is not None
|
assert wallet.adb.get_transaction(funding_tx.txid()) is not None
|
||||||
self.logger.info(f'wallet balance1 {wallet.get_balance()}')
|
self.logger.info(f'wallet balance1 {wallet.get_balance()}')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user