Skip to content

Commit

Permalink
test: fix flaky regtest (lnbits#2616)
Browse files Browse the repository at this point in the history
* add log for invoice success
* add internal flag
* sleeping inside the tasks to not block
* sleep was wrong decrease wait time
  • Loading branch information
dni authored Jul 31, 2024
1 parent b417051 commit ffba71c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,28 @@ dev:
poetry run lnbits --reload

test-wallets:
LNBITS_DATA_FOLDER="./tests/data" \
LNBITS_BACKEND_WALLET_CLASS="FakeWallet" \
PYTHONUNBUFFERED=1 \
DEBUG=true \
poetry run pytest tests/wallets

test-unit:
LNBITS_DATA_FOLDER="./tests/data" \
LNBITS_BACKEND_WALLET_CLASS="FakeWallet" \
PYTHONUNBUFFERED=1 \
DEBUG=true \
poetry run pytest tests/unit

test-api:
LNBITS_DATA_FOLDER="./tests/data" \
LNBITS_BACKEND_WALLET_CLASS="FakeWallet" \
PYTHONUNBUFFERED=1 \
DEBUG=true \
poetry run pytest tests/api

test-regtest:
LNBITS_DATA_FOLDER="./tests/data" \
PYTHONUNBUFFERED=1 \
DEBUG=true \
poetry run pytest tests/regtest
Expand Down
2 changes: 1 addition & 1 deletion lnbits/core/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class PaymentKwargs(TypedDict):
)
if wallet and updated:
await send_payment_notification(wallet, updated)
logger.debug(f"payment successful {payment.checking_id}")
logger.success(f"payment successful {payment.checking_id}")
elif payment.checking_id is None and payment.ok is False:
# payment failed
logger.debug(f"payment failed {temp_id}, {payment.error_message}")
Expand Down
4 changes: 2 additions & 2 deletions lnbits/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async def http_exception_handler(request: Request, exc: HTTPException):
def register_payment_error_handler(app: FastAPI):
@app.exception_handler(PaymentError)
async def payment_error_handler(request: Request, exc: PaymentError):
logger.error(f"PaymentError: {exc.message}, {exc.status}")
logger.error(f"{exc.message}, {exc.status}")
return JSONResponse(
status_code=520,
content={"detail": exc.message, "status": exc.status},
Expand All @@ -100,7 +100,7 @@ async def payment_error_handler(request: Request, exc: PaymentError):
def register_invoice_error_handler(app: FastAPI):
@app.exception_handler(InvoiceError)
async def invoice_error_handler(request: Request, exc: InvoiceError):
logger.error(f"InvoiceError: {exc.message}, Status: {exc.status}")
logger.error(f"{exc.message}, Status: {exc.status}")
return JSONResponse(
status_code=520,
content={"detail": exc.message, "status": exc.status},
Expand Down
13 changes: 6 additions & 7 deletions lnbits/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ async def internal_invoice_listener():
"""
while settings.lnbits_running:
checking_id = await internal_invoice_queue.get()
logger.info("> got internal payment notification", checking_id)
create_task(invoice_callback_dispatcher(checking_id))
logger.info(f"got an internal payment notification {checking_id}")
create_task(invoice_callback_dispatcher(checking_id, is_internal=True))


async def invoice_listener():
Expand All @@ -123,7 +123,7 @@ async def invoice_listener():
"""
funding_source = get_funding_source()
async for checking_id in funding_source.paid_invoices_stream():
logger.info("> got a payment notification", checking_id)
logger.info(f"got a payment notification {checking_id}")
create_task(invoice_callback_dispatcher(checking_id))


Expand Down Expand Up @@ -171,23 +171,22 @@ async def check_pending_payments():
await asyncio.sleep(60 * 30) # every 30 minutes


async def invoice_callback_dispatcher(checking_id: str):
async def invoice_callback_dispatcher(checking_id: str, is_internal: bool = False):
"""
Takes an incoming payment, checks its status, and dispatches it to
invoice_listeners from core and extensions.
"""
payment = await get_standalone_payment(checking_id, incoming=True)
if payment and payment.is_in:
logger.trace(
f"invoice listeners: sending invoice callback for payment {checking_id}"
)
status = await payment.check_status()
await update_payment_details(
checking_id=payment.checking_id,
fee=status.fee_msat,
preimage=status.preimage,
status=PaymentState.SUCCESS,
)
internal = "internal" if is_internal else ""
logger.success(f"{internal} invoice {checking_id} settled")
for name, send_chan in invoice_listeners.items():
logger.trace(f"invoice listeners: sending to `{name}`")
await send_chan.put(payment)
Expand Down
35 changes: 21 additions & 14 deletions tests/regtest/test_real_invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,22 @@ async def test_create_real_invoice(client, adminkey_headers_from, inkey_headers_
assert not payment_status["paid"]

async def listen():
found_checking_id = False
async for checking_id in get_funding_source().paid_invoices_stream():
if checking_id == invoice["checking_id"]:
found_checking_id = True
return
assert found_checking_id
# wait for the backend to update the payment status
await asyncio.sleep(1)
return checking_id

async def pay():
await asyncio.sleep(3)
# wait a sec to paid_invoices_stream to start listening
await asyncio.sleep(1)
pay_real_invoice(invoice["payment_request"])
return True

checking_id, paid = await asyncio.gather(listen(), pay())
assert paid
assert checking_id == invoice["payment_hash"]

await asyncio.gather(listen(), pay())
await asyncio.sleep(3)
response = await client.get(
f'/api/v1/payments/{invoice["payment_hash"]}', headers=inkey_headers_from
)
Expand Down Expand Up @@ -296,22 +299,26 @@ async def test_receive_real_invoice_set_pending_and_check_state(
assert not payment_status["paid"]

async def listen():
found_checking_id = False
async for checking_id in get_funding_source().paid_invoices_stream():
if checking_id == invoice["checking_id"]:
found_checking_id = True
return
assert found_checking_id
# wait for the backend to update the payment status
await asyncio.sleep(1)
return checking_id

async def pay():
await asyncio.sleep(3)
# wait a sec to paid_invoices_stream to start listening
await asyncio.sleep(1)
pay_real_invoice(invoice["payment_request"])
return True

checking_id, paid = await asyncio.gather(listen(), pay())
assert paid
assert checking_id == invoice["payment_hash"]

await asyncio.gather(listen(), pay())
await asyncio.sleep(3)
response = await client.get(
f'/api/v1/payments/{invoice["payment_hash"]}', headers=inkey_headers_from
)
assert response.status_code < 300
payment_status = response.json()
assert payment_status["paid"]

Expand Down

0 comments on commit ffba71c

Please sign in to comment.