Skip to content

Commit 892ce54

Browse files
feat: Allow sending and receiving messages through the runner
This commit marks the first step in the lnprototest refactoring process, aimed at writing more readable tests with the runner. With this commit, it becomes possible to write tests similar to the following example. ``` def test_v2_init_is_first_msg(runner: Runner, namespaceoverride: Any) -> None: """Tests workflow runner --- connect ---> ln node runner <-- init ------ ln node """ runner.start() runner.connect(None, connprivkey="03") init_msg = runner.recv_msg() assert ( init_msg.messagetype.number == 16 ), f"received not an init msg but: {init_msg.to_str()}" runner.stop() ``` Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent b880666 commit 892ce54

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

lnprototest/clightning/clightning.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,14 +198,14 @@ def restart(self) -> None:
198198
self.bitcoind.restart()
199199
self.start(also_bitcoind=False)
200200

201-
def connect(self, _: Event, connprivkey: str) -> None:
202-
self.add_conn(
203-
RunnerConn(
204-
connprivkey,
205-
"0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
206-
self.lightning_port,
207-
)
201+
def connect(self, _: Event, connprivkey: str) -> RunnerConn:
202+
conn = RunnerConn(
203+
connprivkey,
204+
"0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
205+
self.lightning_port,
208206
)
207+
self.add_conn(conn)
208+
return conn
209209

210210
def getblockheight(self) -> int:
211211
return self.bitcoind.rpc.getblockcount()

lnprototest/event.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#! /usr/bin/python3
21
import logging
32
import traceback
43
import collections

lnprototest/runner.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import io
12
import logging
23
import shutil
34
import tempfile
@@ -6,6 +7,7 @@
67
import functools
78

89
import pyln
10+
from pyln.proto.message import Message
911

1012
from abc import ABC, abstractmethod
1113
from typing import Dict, Optional, List, Union, Any, Callable
@@ -15,6 +17,7 @@
1517
from .event import Event, MustNotMsg, ExpectMsg
1618
from .utils import privkey_expand
1719
from .keyset import KeySet
20+
from .namespace import namespace
1821

1922

2023
class Conn(object):
@@ -155,6 +158,28 @@ def is_running(self) -> bool:
155158
def connect(self, event: Event, connprivkey: str) -> None:
156159
pass
157160

161+
def send_msg(self, msg: Message) -> None:
162+
"""Send a message through the last connection"""
163+
missing = msg.missing_fields()
164+
if missing:
165+
raise SpecFileError(self, "Missing fields {}".format(missing))
166+
binmsg = io.BytesIO()
167+
msg.write(binmsg)
168+
self.last_conn.connection.send_message(msg.getvalue())
169+
170+
def recv_msg(
171+
self, timeout: int = 1000, skip_filter: Optional[int] = None
172+
) -> Message:
173+
"""Listen on the connection for incoming message.
174+
175+
If the {skip_filter} is specified, the message that
176+
match the filters are skipped.
177+
"""
178+
raw_msg = self.last_conn.connection.read_message()
179+
msg = Message.read(namespace(), io.BytesIO(raw_msg))
180+
self.add_stash(msg.messagetype.name, msg)
181+
return msg
182+
158183
@abstractmethod
159184
def check_final_error(
160185
self,

tests/test_v2_bolt1-01-init.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Any
2+
3+
from lnprototest.runner import Runner
4+
5+
6+
def test_v2_init_is_first_msg(runner: Runner, namespaceoverride: Any) -> None:
7+
"""Tests workflow
8+
9+
runner --- connect ---> ln node
10+
runner <-- init ------ ln node
11+
"""
12+
runner.start()
13+
14+
runner.connect(None, connprivkey="03")
15+
init_msg = runner.recv_msg()
16+
assert (
17+
init_msg.messagetype.number == 16
18+
), f"received not an init msg but: {init_msg.to_str()}"
19+
20+
runner.stop()

0 commit comments

Comments
 (0)