Skip to content

Commit b561c3b

Browse files
committed
test: fire-and-forget CLN swap rpc
1 parent 7ab245e commit b561c3b

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

test/recovery_test.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"testing"
99
"time"
1010

11-
"github.com/elementsproject/glightning/jrpc2"
1211
"github.com/elementsproject/peerswap/clightning"
1312
"github.com/elementsproject/peerswap/premium"
1413
"github.com/elementsproject/peerswap/swap"
@@ -19,28 +18,45 @@ type clnSocketProvider interface {
1918
GetDataDir() string
2019
}
2120

22-
func notifyCLN(t *testing.T, node clnSocketProvider, method jrpc2.Method) {
21+
func fireAndForgetCLNRPC(t *testing.T, node clnSocketProvider, method string, params any) {
2322
t.Helper()
2423

2524
require := requireNew(t)
2625

2726
socketPath := filepath.Join(node.GetDataDir(), "lightning-rpc")
2827
conn, err := net.DialTimeout("unix", socketPath, 1*time.Second)
2928
require.NoError(err)
30-
defer conn.Close()
31-
32-
req := &jrpc2.Request{
33-
// No id means this is treated as a JSON-RPC notification, and no
34-
// response is expected. This avoids leaving a long-running pending RPC
35-
// request in glightning when the node is intentionally stopped mid-test.
36-
Id: nil,
37-
Method: method,
29+
t.Cleanup(func() { _ = conn.Close() })
30+
31+
rawParams, err := json.Marshal(params)
32+
require.NoError(err)
33+
34+
req := struct {
35+
JSONRPC string `json:"jsonrpc"`
36+
ID int `json:"id"`
37+
Method string `json:"method"`
38+
Params json.RawMessage `json:"params"`
39+
}{
40+
JSONRPC: "2.0",
41+
ID: 1,
42+
Method: method,
43+
Params: rawParams,
3844
}
3945
payload, err := json.Marshal(req)
4046
require.NoError(err)
4147

4248
_, err = conn.Write(append(payload, '\n', '\n'))
4349
require.NoError(err)
50+
51+
// Drain any responses to avoid backpressure, but never block the test.
52+
go func() {
53+
buf := make([]byte, 4096)
54+
for {
55+
if _, err := conn.Read(buf); err != nil {
56+
return
57+
}
58+
}
59+
}()
4460
}
4561

4662
// Test_RestoreFromPassedCSV checks the following scenario: A swap is initiated
@@ -92,7 +108,7 @@ func Test_RestoreFromPassedCSV(t *testing.T) {
92108
asset := "btc"
93109

94110
// Do swap.
95-
notifyCLN(t, lightningds[0], &clightning.SwapOut{
111+
fireAndForgetCLNRPC(t, lightningds[0], "peerswap-swap-out", &clightning.SwapOut{
96112
SatAmt: params.swapAmt,
97113
ShortChannelId: params.scid,
98114
Asset: asset,
@@ -225,7 +241,7 @@ func Test_Recover_PassedSwap_BTC(t *testing.T) {
225241
asset := "btc"
226242

227243
// Do swap.
228-
notifyCLN(t, lightningds[0], &clightning.SwapOut{
244+
fireAndForgetCLNRPC(t, lightningds[0], "peerswap-swap-out", &clightning.SwapOut{
229245
SatAmt: params.swapAmt,
230246
ShortChannelId: params.scid,
231247
Asset: asset,
@@ -331,7 +347,7 @@ func Test_Recover_PassedSwap_LBTC(t *testing.T) {
331347
asset := "lbtc"
332348

333349
// Do swap.
334-
notifyCLN(t, lightningds[0], &clightning.SwapOut{
350+
fireAndForgetCLNRPC(t, lightningds[0], "peerswap-swap-out", &clightning.SwapOut{
335351
SatAmt: params.swapAmt,
336352
ShortChannelId: params.scid,
337353
Asset: asset,

0 commit comments

Comments
 (0)