Skip to content

Commit 7ab245e

Browse files
committed
test: avoid glightning pending RPC during CLN stop
1 parent 39a2229 commit 7ab245e

1 file changed

Lines changed: 51 additions & 36 deletions

File tree

test/recovery_test.go

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,48 @@
11
package test
22

33
import (
4+
"encoding/json"
45
"math"
6+
"net"
7+
"path/filepath"
58
"testing"
9+
"time"
610

11+
"github.com/elementsproject/glightning/jrpc2"
712
"github.com/elementsproject/peerswap/clightning"
813
"github.com/elementsproject/peerswap/premium"
914
"github.com/elementsproject/peerswap/swap"
1015
"github.com/elementsproject/peerswap/testframework"
1116
)
1217

18+
type clnSocketProvider interface {
19+
GetDataDir() string
20+
}
21+
22+
func notifyCLN(t *testing.T, node clnSocketProvider, method jrpc2.Method) {
23+
t.Helper()
24+
25+
require := requireNew(t)
26+
27+
socketPath := filepath.Join(node.GetDataDir(), "lightning-rpc")
28+
conn, err := net.DialTimeout("unix", socketPath, 1*time.Second)
29+
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,
38+
}
39+
payload, err := json.Marshal(req)
40+
require.NoError(err)
41+
42+
_, err = conn.Write(append(payload, '\n', '\n'))
43+
require.NoError(err)
44+
}
45+
1346
// Test_RestoreFromPassedCSV checks the following scenario: A swap is initiated
1447
// and the opening tx is broadcasted. The maker node then goes offline before
1548
// the opening tx is confirmed such that the taker node can not pay the invoice.
@@ -59,18 +92,12 @@ func Test_RestoreFromPassedCSV(t *testing.T) {
5992
asset := "btc"
6093

6194
// Do swap.
62-
go func() {
63-
// We need to run this in a go routine as the Request call is blocking and sometimes does not return.
64-
var response map[string]interface{}
65-
if err := lightningds[0].Rpc.Request(&clightning.SwapOut{
66-
SatAmt: params.swapAmt,
67-
ShortChannelId: params.scid,
68-
Asset: asset,
69-
PremiumLimitRatePPM: params.premiumLimitRatePPM,
70-
}, &response); err != nil {
71-
t.Errorf("SwapOut request failed: %v", err)
72-
}
73-
}()
95+
notifyCLN(t, lightningds[0], &clightning.SwapOut{
96+
SatAmt: params.swapAmt,
97+
ShortChannelId: params.scid,
98+
Asset: asset,
99+
PremiumLimitRatePPM: params.premiumLimitRatePPM,
100+
})
74101

75102
var premiumAmt uint64
76103
if params.swapType == swap.SWAPTYPE_OUT {
@@ -198,18 +225,12 @@ func Test_Recover_PassedSwap_BTC(t *testing.T) {
198225
asset := "btc"
199226

200227
// Do swap.
201-
go func() {
202-
// We need to run this in a go routine as the Request call is blocking and sometimes does not return.
203-
var response map[string]interface{}
204-
if err := lightningds[0].Rpc.Request(&clightning.SwapOut{
205-
SatAmt: params.swapAmt,
206-
ShortChannelId: params.scid,
207-
Asset: asset,
208-
PremiumLimitRatePPM: params.premiumLimitRatePPM,
209-
}, &response); err != nil {
210-
t.Errorf("SwapOut request failed: %v", err)
211-
}
212-
}()
228+
notifyCLN(t, lightningds[0], &clightning.SwapOut{
229+
SatAmt: params.swapAmt,
230+
ShortChannelId: params.scid,
231+
Asset: asset,
232+
PremiumLimitRatePPM: params.premiumLimitRatePPM,
233+
})
213234

214235
var premiumAmt uint64
215236
if params.swapType == swap.SWAPTYPE_OUT {
@@ -310,18 +331,12 @@ func Test_Recover_PassedSwap_LBTC(t *testing.T) {
310331
asset := "lbtc"
311332

312333
// Do swap.
313-
go func() {
314-
// We need to run this in a go routine as the Request call is blocking and sometimes does not return.
315-
var response map[string]interface{}
316-
if err := lightningds[0].Rpc.Request(&clightning.SwapOut{
317-
SatAmt: params.swapAmt,
318-
ShortChannelId: params.scid,
319-
Asset: asset,
320-
PremiumLimitRatePPM: params.premiumLimitRatePPM,
321-
}, &response); err != nil {
322-
t.Errorf("SwapOut request failed: %v", err)
323-
}
324-
}()
334+
notifyCLN(t, lightningds[0], &clightning.SwapOut{
335+
SatAmt: params.swapAmt,
336+
ShortChannelId: params.scid,
337+
Asset: asset,
338+
PremiumLimitRatePPM: params.premiumLimitRatePPM,
339+
})
325340

326341
var premiumAmt uint64
327342
if params.swapType == swap.SWAPTYPE_OUT {

0 commit comments

Comments
 (0)