From 1b9bb32ca0fb93855219c29fce1a11b9b020d158 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Thu, 12 Dec 2024 07:54:43 +0100 Subject: [PATCH] test: fix lndrest on regtest (#2811) * fix: lndrest on regtest * fix some race conditions on lndrest --- .github/workflows/ci.yml | 2 +- lnbits/nodes/lndrest.py | 12 +++++++++--- tests/regtest/conftest.py | 5 ----- tests/regtest/helpers.py | 3 ++- tests/regtest/test_real_invoice.py | 3 ++- tests/regtest/test_x_node_api.py | 5 +++++ 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 92feb296f..d37f61882 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -75,7 +75,7 @@ jobs: strategy: matrix: python-version: ["3.10"] - backend-wallet-class: ["LndWallet", "CoreLightningWallet", "CoreLightningRestWallet", "LNbitsWallet", "EclairWallet"] + backend-wallet-class: ["LndRestWallet", "LndWallet", "CoreLightningWallet", "CoreLightningRestWallet", "LNbitsWallet", "EclairWallet"] with: custom-pytest: "poetry run pytest tests/regtest" python-version: ${{ matrix.python-version }} diff --git a/lnbits/nodes/lndrest.py b/lnbits/nodes/lndrest.py index d6d079719..ac337b510 100644 --- a/lnbits/nodes/lndrest.py +++ b/lnbits/nodes/lndrest.py @@ -159,9 +159,15 @@ class LndRestNode(Node): timeout=None, ) as stream: async for chunk in stream.aiter_text(): - if chunk: - chunk = json.loads(chunk) - logger.info(f"LND Channel close update: {chunk['result']}") + if not chunk: + continue + chunk = json.loads(chunk) + if "error" in chunk: + raise HTTPException( + status_code=HTTPStatus.INTERNAL_SERVER_ERROR, + detail=chunk["error"].get("message"), + ) + logger.info(f"LND Channel close update: {chunk.get('result')}") async def close_channel( self, diff --git a/tests/regtest/conftest.py b/tests/regtest/conftest.py index ac61052a5..cea47f680 100644 --- a/tests/regtest/conftest.py +++ b/tests/regtest/conftest.py @@ -3,11 +3,6 @@ import pytest from .helpers import get_hold_invoice, get_real_invoice -@pytest.fixture(scope="session") -def anyio_backend(): - return "asyncio" - - @pytest.fixture(scope="function") async def hold_invoice(): invoice = get_hold_invoice(100) diff --git a/tests/regtest/helpers.py b/tests/regtest/helpers.py index 70bcbf0eb..da8d47e6a 100644 --- a/tests/regtest/helpers.py +++ b/tests/regtest/helpers.py @@ -20,7 +20,8 @@ docker_lightning_cli = [ docker_bitcoin_cli = [ "docker", "exec", - "lnbits-bitcoind-1-1" "bitcoin-cli", + "lnbits-bitcoind-1", + "bitcoin-cli", "-rpcuser=lnbits", "-rpcpassword=lnbits", "-regtest", diff --git a/tests/regtest/test_real_invoice.py b/tests/regtest/test_real_invoice.py index 46e6f28b6..688ceec8c 100644 --- a/tests/regtest/test_real_invoice.py +++ b/tests/regtest/test_real_invoice.py @@ -57,6 +57,7 @@ async def test_pay_real_invoice( await asyncio.sleep(1) balance = await get_node_balance_sats() + # TODO: maybe take fee into consideration? assert prev_balance - balance == 100 @@ -158,7 +159,7 @@ async def test_pay_hold_invoice_check_pending( headers=adminkey_headers_from, ) ) - await asyncio.sleep(1) + await asyncio.sleep(3) # get payment hash from the invoice invoice_obj = bolt11.decode(invoice["payment_request"]) settle_invoice(preimage) diff --git a/tests/regtest/test_x_node_api.py b/tests/regtest/test_x_node_api.py index 0df07f1ec..bff5d5b28 100644 --- a/tests/regtest/test_x_node_api.py +++ b/tests/regtest/test_x_node_api.py @@ -102,6 +102,8 @@ async def test_node_payments(node_client, real_invoice, adminkey_headers_from): @pytest.mark.anyio async def test_channel_management(node_client): async def get_channels(): + # lndrest is slow / async with channel commands + await asyncio.sleep(3) response = await node_client.get("/node/api/v1/channels") assert response.status_code == 200 return parse_obj_as(list[NodeChannel], response.json()) @@ -134,6 +136,7 @@ async def test_channel_management(node_client): ) assert response.status_code == 200 created = ChannelPoint(**response.json()) + data = await get_channels() assert any( channel.point == created and channel.state == ChannelState.PENDING @@ -145,6 +148,8 @@ async def test_channel_management(node_client): # left for testing mine_blocks(5) + await asyncio.sleep(1) + @pytest.mark.anyio async def test_peer_management(node_client):