@@ -3,15 +3,20 @@ package itest
3
3
import (
4
4
"context"
5
5
"crypto/rand"
6
+ "time"
6
7
7
8
"github.com/lightninglabs/lightning-node-connect/itest/mockrpc"
9
+ "github.com/lightninglabs/lightning-node-connect/mailbox"
10
+ "github.com/lightningnetwork/lnd/lntest/wait"
8
11
"github.com/stretchr/testify/require"
9
12
)
10
13
11
14
var (
12
15
defaultMessage = []byte ("some default message" )
13
16
)
14
17
18
+ const defaultTimeout = 30 * time .Second
19
+
15
20
// testHappyPath ensures that client and server are able to communicate
16
21
// as expected in the case where no connections are dropped.
17
22
func testHappyPath (t * harnessTest ) {
@@ -28,8 +33,11 @@ func testHappyPath(t *harnessTest) {
28
33
// testHashmailServerReconnect tests that client and server are able to
29
34
// continue with their communication after the hashmail server restarts.
30
35
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 )
32
39
40
+ ctx := context .Background ()
33
41
resp , err := t .client .clientConn .MockServiceMethod (
34
42
ctx , & mockrpc.Request {Req : defaultMessage },
35
43
)
@@ -40,19 +48,31 @@ func testHashmailServerReconnect(t *harnessTest) {
40
48
require .NoError (t .t , t .hmserver .stop ())
41
49
t .t .Logf ("" )
42
50
51
+ // Check that the client and server status are updated appropriately.
52
+ assertServerStatus (t , mailbox .ServerStatusNotConnected )
53
+ assertClientStatus (t , mailbox .ClientStatusNotConnected )
54
+
43
55
// Restart hashmail server
44
56
require .NoError (t .t , t .hmserver .start ())
45
57
58
+ // Check that the client and server successfully reconnect.
59
+ assertServerStatus (t , mailbox .ServerStatusInUse )
60
+ assertClientStatus (t , mailbox .ClientStatusConnected )
61
+
46
62
resp , err = t .client .clientConn .MockServiceMethod (
47
63
ctx , & mockrpc.Request {Req : defaultMessage },
48
64
)
49
65
require .NoError (t .t , err )
50
66
require .Equal (t .t , len (defaultMessage )* 10 , len (resp .Resp ))
51
67
}
52
68
69
+ // testClientReconnect tests that the client and server are able to reestablish
70
+ // their connection if the client disconnects and reconnects.
53
71
func testClientReconnect (t * harnessTest ) {
54
- ctx := context .Background ()
72
+ // Assert that the server and client are connected.
73
+ assertServerStatus (t , mailbox .ServerStatusInUse )
55
74
75
+ ctx := context .Background ()
56
76
resp , err := t .client .clientConn .MockServiceMethod (
57
77
ctx , & mockrpc.Request {Req : defaultMessage },
58
78
)
@@ -62,34 +82,50 @@ func testClientReconnect(t *harnessTest) {
62
82
// Stop the client.
63
83
require .NoError (t .t , t .client .cleanup ())
64
84
85
+ // Check that the server status is updated appropriately.
86
+ assertServerStatus (t , mailbox .ServerStatusIdle )
87
+
65
88
// Restart the client.
66
89
require .NoError (t .t , t .client .start ())
67
90
91
+ // Check that the client and server successfully reconnect.
92
+ assertServerStatus (t , mailbox .ServerStatusInUse )
93
+
68
94
resp , err = t .client .clientConn .MockServiceMethod (
69
95
ctx , & mockrpc.Request {Req : defaultMessage },
70
96
)
71
97
require .NoError (t .t , err )
72
98
require .Equal (t .t , len (defaultMessage )* 10 , len (resp .Resp ))
73
99
}
74
100
101
+ // testServerReconnect tests that the client and server are able to reestablish
102
+ // their connection if the server disconnects and reconnects.
75
103
func testServerReconnect (t * harnessTest ) {
76
- ctx := context .Background ()
104
+ // Assert that the server and client are connected.
105
+ assertClientStatus (t , mailbox .ClientStatusConnected )
77
106
107
+ ctx := context .Background ()
78
108
resp , err := t .client .clientConn .MockServiceMethod (
79
109
ctx , & mockrpc.Request {Req : defaultMessage },
80
110
)
81
111
require .NoError (t .t , err )
82
112
require .Equal (t .t , len (defaultMessage )* 10 , len (resp .Resp ))
83
113
84
114
t .server .stop ()
115
+
116
+ // Assert that the client status is updated appropriately.
117
+ assertClientStatus (t , mailbox .ClientStatusSessionNotFound )
118
+
85
119
require .NoError (t .t , t .server .start ())
86
120
87
121
select {
88
122
case err := <- t .server .errChan :
89
123
if err != nil {
90
124
t .Fatalf ("could not start server: %v" , err )
91
125
}
92
- default :
126
+
127
+ case <- time .After (defaultTimeout ):
128
+ t .Fatalf ("could not start server in time" )
93
129
}
94
130
95
131
// To replicate how the browser's behaviour, we retry this call a few
@@ -102,10 +138,16 @@ func testServerReconnect(t *harnessTest) {
102
138
break
103
139
}
104
140
}
141
+
105
142
require .NoError (t .t , err )
106
143
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 )
107
147
}
108
148
149
+ // testLargeResponse tests that the client and server can successfully send
150
+ // a large amount of data back and forth reliably.
109
151
func testLargeResponse (t * harnessTest ) {
110
152
ctx := context .Background ()
111
153
@@ -119,3 +161,20 @@ func testLargeResponse(t *harnessTest) {
119
161
require .NoError (t .t , err )
120
162
require .Equal (t .t , len (req )* 10 , len (resp .Resp ))
121
163
}
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
+ }
0 commit comments