Skip to content

Commit c072f70

Browse files
authored
Merge pull request #53 from ellemouton/serverConnStatus
multi: server connection status
2 parents 8028a4a + 1c03f18 commit c072f70

File tree

9 files changed

+177
-54
lines changed

9 files changed

+177
-54
lines changed

cmd/wasm-client/lnd_conn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
func mailboxRPCConnection(mailboxServer, pairingPhrase string,
1717
localStatic keychain.SingleKeyECDH, remoteStatic *btcec.PublicKey,
1818
onRemoteStatic func(key *btcec.PublicKey) error,
19-
onAuthData func(data []byte) error) (func() mailbox.ConnStatus,
19+
onAuthData func(data []byte) error) (func() mailbox.ClientStatus,
2020
func() (*grpc.ClientConn, error), error) {
2121

2222
words := strings.Split(pairingPhrase, " ")

cmd/wasm-client/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ type wasmClient struct {
140140

141141
lndConn *grpc.ClientConn
142142

143-
statusChecker func() mailbox.ConnStatus
143+
statusChecker func() mailbox.ClientStatus
144144

145145
mac *macaroon.Macaroon
146146

itest/client_harness.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type clientHarness struct {
1919

2020
grpcConn *grpc.ClientConn
2121
clientConn mockrpc.MockServiceClient
22+
client *mailbox.Client
2223

2324
passphraseEntropy []byte
2425
localStatic keychain.SingleKeyECDH
@@ -65,6 +66,7 @@ func (c *clientHarness) start() error {
6566
if err != nil {
6667
return err
6768
}
69+
c.client = transportConn
6870

6971
noiseConn := mailbox.NewNoiseGrpcConn(connData)
7072

itest/connection_test.go

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ package itest
33
import (
44
"context"
55
"crypto/rand"
6+
"time"
67

78
"github.com/lightninglabs/lightning-node-connect/itest/mockrpc"
9+
"github.com/lightninglabs/lightning-node-connect/mailbox"
10+
"github.com/lightningnetwork/lnd/lntest/wait"
811
"github.com/stretchr/testify/require"
912
)
1013

1114
var (
1215
defaultMessage = []byte("some default message")
1316
)
1417

18+
const defaultTimeout = 30 * time.Second
19+
1520
// testHappyPath ensures that client and server are able to communicate
1621
// as expected in the case where no connections are dropped.
1722
func testHappyPath(t *harnessTest) {
@@ -28,8 +33,11 @@ func testHappyPath(t *harnessTest) {
2833
// testHashmailServerReconnect tests that client and server are able to
2934
// continue with their communication after the hashmail server restarts.
3035
func testHashmailServerReconnect(t *harnessTest) {
31-
ctx := context.Background()
36+
// Assert that the server and client are connected.
37+
assertServerStatus(t, mailbox.ServerStatusInUse)
38+
assertClientStatus(t, mailbox.ClientStatusConnected)
3239

40+
ctx := context.Background()
3341
resp, err := t.client.clientConn.MockServiceMethod(
3442
ctx, &mockrpc.Request{Req: defaultMessage},
3543
)
@@ -40,19 +48,31 @@ func testHashmailServerReconnect(t *harnessTest) {
4048
require.NoError(t.t, t.hmserver.stop())
4149
t.t.Logf("")
4250

51+
// Check that the client and server status are updated appropriately.
52+
assertServerStatus(t, mailbox.ServerStatusNotConnected)
53+
assertClientStatus(t, mailbox.ClientStatusNotConnected)
54+
4355
// Restart hashmail server
4456
require.NoError(t.t, t.hmserver.start())
4557

58+
// Check that the client and server successfully reconnect.
59+
assertServerStatus(t, mailbox.ServerStatusInUse)
60+
assertClientStatus(t, mailbox.ClientStatusConnected)
61+
4662
resp, err = t.client.clientConn.MockServiceMethod(
4763
ctx, &mockrpc.Request{Req: defaultMessage},
4864
)
4965
require.NoError(t.t, err)
5066
require.Equal(t.t, len(defaultMessage)*10, len(resp.Resp))
5167
}
5268

69+
// testClientReconnect tests that the client and server are able to reestablish
70+
// their connection if the client disconnects and reconnects.
5371
func testClientReconnect(t *harnessTest) {
54-
ctx := context.Background()
72+
// Assert that the server and client are connected.
73+
assertServerStatus(t, mailbox.ServerStatusInUse)
5574

75+
ctx := context.Background()
5676
resp, err := t.client.clientConn.MockServiceMethod(
5777
ctx, &mockrpc.Request{Req: defaultMessage},
5878
)
@@ -62,34 +82,50 @@ func testClientReconnect(t *harnessTest) {
6282
// Stop the client.
6383
require.NoError(t.t, t.client.cleanup())
6484

85+
// Check that the server status is updated appropriately.
86+
assertServerStatus(t, mailbox.ServerStatusIdle)
87+
6588
// Restart the client.
6689
require.NoError(t.t, t.client.start())
6790

91+
// Check that the client and server successfully reconnect.
92+
assertServerStatus(t, mailbox.ServerStatusInUse)
93+
6894
resp, err = t.client.clientConn.MockServiceMethod(
6995
ctx, &mockrpc.Request{Req: defaultMessage},
7096
)
7197
require.NoError(t.t, err)
7298
require.Equal(t.t, len(defaultMessage)*10, len(resp.Resp))
7399
}
74100

101+
// testServerReconnect tests that the client and server are able to reestablish
102+
// their connection if the server disconnects and reconnects.
75103
func testServerReconnect(t *harnessTest) {
76-
ctx := context.Background()
104+
// Assert that the server and client are connected.
105+
assertClientStatus(t, mailbox.ClientStatusConnected)
77106

107+
ctx := context.Background()
78108
resp, err := t.client.clientConn.MockServiceMethod(
79109
ctx, &mockrpc.Request{Req: defaultMessage},
80110
)
81111
require.NoError(t.t, err)
82112
require.Equal(t.t, len(defaultMessage)*10, len(resp.Resp))
83113

84114
t.server.stop()
115+
116+
// Assert that the client status is updated appropriately.
117+
assertClientStatus(t, mailbox.ClientStatusSessionNotFound)
118+
85119
require.NoError(t.t, t.server.start())
86120

87121
select {
88122
case err := <-t.server.errChan:
89123
if err != nil {
90124
t.Fatalf("could not start server: %v", err)
91125
}
92-
default:
126+
127+
case <-time.After(defaultTimeout):
128+
t.Fatalf("could not start server in time")
93129
}
94130

95131
// To replicate how the browser's behaviour, we retry this call a few
@@ -102,10 +138,16 @@ func testServerReconnect(t *harnessTest) {
102138
break
103139
}
104140
}
141+
105142
require.NoError(t.t, err)
106143
require.Equal(t.t, len(defaultMessage)*10, len(resp.Resp))
144+
145+
// Assert that the client's status has been correctly updated.
146+
assertClientStatus(t, mailbox.ClientStatusConnected)
107147
}
108148

149+
// testLargeResponse tests that the client and server can successfully send
150+
// a large amount of data back and forth reliably.
109151
func testLargeResponse(t *harnessTest) {
110152
ctx := context.Background()
111153

@@ -119,3 +161,20 @@ func testLargeResponse(t *harnessTest) {
119161
require.NoError(t.t, err)
120162
require.Equal(t.t, len(req)*10, len(resp.Resp))
121163
}
164+
165+
func assertServerStatus(t *harnessTest, status mailbox.ServerStatus) {
166+
err := wait.Predicate(func() bool {
167+
t.server.statusMu.Lock()
168+
defer t.server.statusMu.Unlock()
169+
170+
return t.server.status == status
171+
}, defaultTimeout)
172+
require.NoError(t.t, err)
173+
}
174+
175+
func assertClientStatus(t *harnessTest, status mailbox.ClientStatus) {
176+
err := wait.Predicate(func() bool {
177+
return t.client.client.ConnStatus() == status
178+
}, defaultTimeout)
179+
require.NoError(t.t, err)
180+
}

itest/server_harness.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ type serverHarness struct {
2525

2626
tlsConfig *tls.Config
2727

28+
status mailbox.ServerStatus
29+
statusMu sync.Mutex
30+
2831
errChan chan error
2932
wg sync.WaitGroup
3033
}
@@ -74,7 +77,11 @@ func (s *serverHarness) start() error {
7477
)
7578

7679
mailboxServer, err := mailbox.NewServer(
77-
s.serverHost, connData, grpc.WithTransportCredentials(
80+
s.serverHost, connData, func(status mailbox.ServerStatus) {
81+
s.statusMu.Lock()
82+
s.status = status
83+
s.statusMu.Unlock()
84+
}, grpc.WithTransportCredentials(
7885
credentials.NewTLS(s.tlsConfig),
7986
),
8087
)

mailbox/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Client struct {
1414

1515
connData *ConnData
1616

17-
status ConnStatus
17+
status ClientStatus
1818
statusMu sync.Mutex
1919

2020
sid [64]byte
@@ -33,7 +33,7 @@ func NewClient(ctx context.Context, connData *ConnData) (*Client, error) {
3333
return &Client{
3434
ctx: ctx,
3535
connData: connData,
36-
status: ConnStatusNotConnected,
36+
status: ClientStatusNotConnected,
3737
sid: sid,
3838
}, nil
3939
}
@@ -73,7 +73,7 @@ func (c *Client) Dial(_ context.Context, serverHost string) (net.Conn, error) {
7373

7474
if c.mailboxConn == nil {
7575
mailboxConn, err := NewClientConn(
76-
c.ctx, c.sid, serverHost, func(status ConnStatus) {
76+
c.ctx, c.sid, serverHost, func(status ClientStatus) {
7777
c.statusMu.Lock()
7878
c.status = status
7979
c.statusMu.Unlock()
@@ -95,7 +95,7 @@ func (c *Client) Dial(_ context.Context, serverHost string) (net.Conn, error) {
9595
}
9696

9797
// ConnStatus returns last determined client connection status.
98-
func (c *Client) ConnStatus() ConnStatus {
98+
func (c *Client) ConnStatus() ClientStatus {
9999
c.statusMu.Lock()
100100
defer c.statusMu.Unlock()
101101

0 commit comments

Comments
 (0)