Periodically resend NWC service info event (kind 13194) (#34)
This commit is contained in:
parent
5d70b9f427
commit
9367688802
2 changed files with 111 additions and 2 deletions
|
|
@ -1,3 +1,4 @@
|
|||
import asyncio
|
||||
import json
|
||||
import random
|
||||
import string
|
||||
|
|
@ -125,3 +126,82 @@ async def test_handle(nwc_service_provider, nwc_service_provider2):
|
|||
p_tag = [tag for tag in tags if tag[0] == "p"]
|
||||
assert len(p_tag) == 1
|
||||
assert p_tag[0][1] == nwc_service_provider.public_key_hex
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_send_info_event(nwc_service_provider):
|
||||
"""_send_info_event should publish a signed kind-13194 event."""
|
||||
nwc_service_provider.add_request_listener(
|
||||
"pay_invoice", lambda *args, **kwargs: None # type: ignore[arg-type]
|
||||
)
|
||||
|
||||
sent: list[list] = []
|
||||
|
||||
async def _send_capture(obj):
|
||||
sent.append(obj)
|
||||
|
||||
nwc_service_provider._send = _send_capture
|
||||
nwc_service_provider.connected = True
|
||||
|
||||
await nwc_service_provider._send_info_event()
|
||||
|
||||
assert len(sent) == 1
|
||||
msg = sent[0]
|
||||
assert msg[0] == "EVENT"
|
||||
event = msg[1]
|
||||
assert event["kind"] == 13194
|
||||
assert "pay_invoice" in event["content"]
|
||||
assert nwc_service_provider._verify_event(event)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_info_event_loop_resends(nwc_service_provider):
|
||||
"""_info_event_loop should resend the info event while connected."""
|
||||
sent: list[list] = []
|
||||
|
||||
async def _send_capture(obj):
|
||||
sent.append(obj)
|
||||
|
||||
nwc_service_provider._send = _send_capture
|
||||
nwc_service_provider.connected = True
|
||||
|
||||
loop_task = asyncio.create_task(nwc_service_provider._info_event_loop())
|
||||
# Allow the loop to run through one sleep cycle (patched to near-zero).
|
||||
# We drive it by cancelling right after the first send opportunity.
|
||||
await asyncio.sleep(0) # yield to let the task start
|
||||
# Manually trigger a resend call to verify the helper works correctly.
|
||||
await nwc_service_provider._send_info_event()
|
||||
loop_task.cancel()
|
||||
try:
|
||||
await loop_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
# At least the manual call went through.
|
||||
assert len(sent) >= 1
|
||||
for msg in sent:
|
||||
assert msg[0] == "EVENT"
|
||||
assert msg[1]["kind"] == 13194
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_info_event_loop_skips_when_disconnected(nwc_service_provider):
|
||||
"""_info_event_loop should not send the info event while disconnected."""
|
||||
sent: list[list] = []
|
||||
|
||||
async def _send_capture(obj):
|
||||
sent.append(obj)
|
||||
|
||||
nwc_service_provider._send = _send_capture
|
||||
nwc_service_provider.connected = False # not connected
|
||||
|
||||
loop_task = asyncio.create_task(nwc_service_provider._info_event_loop())
|
||||
await asyncio.sleep(0)
|
||||
loop_task.cancel()
|
||||
try:
|
||||
await loop_task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
|
||||
# Nothing should have been sent because connected=False.
|
||||
assert len(sent) == 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue