nutshell icon indicating copy to clipboard operation
nutshell copied to clipboard

wait for uvicorn server to bind before running tests

Open conduition opened this issue 1 year ago • 2 comments

Previously we had a simple time.sleep(1) call after server.start() which was present to give the Mint's HTTP server time to spin up during test runs. This meant that if the server took longer than 1s to start on a dev's machine for any reason (even intermittently) then tests would fail due to connection errors.

The fix is to use a simple repeated polling check which allows the test runner to start only once the server is confirmed listening.

Drive-by: remove an unnecessary duplicate settings.mint_host assignment to the 'all interfaces' IP 0.0.0.0.

conduition avatar Aug 01 '24 15:08 conduition

Fixed an infinite loop bug. Tests now pass on my machine

conduition avatar Aug 05 '24 01:08 conduition

Correction: a couple of tests are still failing.

test_pay_invoice_internal fails with:

wallet = <cashu.wallet.lightning.lightning.LightningWallet object at 0x755ebd9c7ca0>

    @pytest.mark.asyncio
    @pytest.mark.skipif(is_regtest, reason="only works with FakeWallet")
    async def test_pay_invoice_internal(wallet: LightningWallet):
        # fill wallet
        invoice = await wallet.create_invoice(64)
        assert invoice.payment_request
        assert invoice.checking_id
        await wallet.get_invoice_status(invoice.checking_id)
        assert wallet.available_balance >= 64
    
        # pay invoice
        invoice2 = await wallet.create_invoice(16)
        assert invoice2.payment_request
        status = await wallet.pay_invoice(invoice2.payment_request)
    
>       assert status.ok
E       AssertionError: assert False
E        +  where False = PaymentResponse(ok=False, checking_id=None, fee=None, preimage=None, error_message='Mint Error: mint quote already paid (Code: 11000)').ok

tests/test_wallet_lightning.py:101: AssertionError

And test_p2pk_locktime_with_refund_pubkey fails with:

wallet1 = <cashu.wallet.wallet.Wallet object at 0x755ebe6eca30>, wallet2 = <cashu.wallet.wallet.Wallet object at 0x755ebe53a5f0>

    @pytest.mark.asyncio
    async def test_p2pk_locktime_with_refund_pubkey(wallet1: Wallet, wallet2: Wallet):
        invoice = await wallet1.request_mint(64)
        await pay_if_regtest(invoice.bolt11)
        await wallet1.mint(64, id=invoice.id)
        pubkey_wallet2 = await wallet2.create_p2pk_pubkey()  # receiver side
        # sender side
        garbage_pubkey = PrivateKey().pubkey
        assert garbage_pubkey
        secret_lock = await wallet1.create_p2pk_lock(
            garbage_pubkey.serialize().hex(),  # create lock to unspendable pubkey
            locktime_seconds=2,  # locktime
            tags=Tags([["refund", pubkey_wallet2]]),  # refund pubkey
        )  # sender side
        _, send_proofs = await wallet1.swap_to_send(
            wallet1.proofs, 8, secret_lock=secret_lock
        )
        send_proofs_copy = copy.deepcopy(send_proofs)
        # receiver side: can't redeem since we used a garbage pubkey
        # and locktime has not passed
>       await assert_err(
            wallet2.redeem(send_proofs),
            "Mint Error: no valid signature provided for input.",
        )

tests/test_wallet_p2pk.py:176: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

f = <coroutine object Wallet.redeem at 0x755ebd827e60>, msg = 'Mint Error: no valid signature provided for input.'

    async def assert_err(f, msg):
        """Compute f() and expect an error message 'msg'."""
        try:
            await f
        except Exception as exc:
            if msg not in str(exc.args[0]):
                raise Exception(f"Expected error: {msg}, got: {exc.args[0]}")
            return
>       raise Exception(f"Expected error: {msg}, got no error")
E       Exception: Expected error: Mint Error: no valid signature provided for input., got no error

tests/test_wallet_p2pk.py:31: Exception

These failures don't seem to be related to this PR.

conduition avatar Aug 05 '24 01:08 conduition