@@ -10,6 +10,7 @@ import (
10
10
11
11
"github.com/lightninglabs/aperture/l402"
12
12
"github.com/lightninglabs/loop/swapserverrpc"
13
+ "github.com/lightningnetwork/lnd/lntypes"
13
14
"github.com/stretchr/testify/require"
14
15
"google.golang.org/grpc"
15
16
"google.golang.org/grpc/metadata"
@@ -111,7 +112,9 @@ func TestManager_ReservationNotification(t *testing.T) {
111
112
Client : mockClient ,
112
113
CurrentToken : func () (* l402.Token , error ) {
113
114
// Simulate successful fetching of L402
114
- return nil , nil
115
+ return & l402.Token {
116
+ Preimage : lntypes.Preimage {1 , 2 , 3 },
117
+ }, nil
115
118
},
116
119
})
117
120
@@ -208,7 +211,10 @@ func TestManager_Backoff(t *testing.T) {
208
211
mgr := NewManager (& Config {
209
212
Client : mockClient ,
210
213
CurrentToken : func () (* l402.Token , error ) {
211
- return & l402.Token {}, nil
214
+ // Simulate successful fetching of L402
215
+ return & l402.Token {
216
+ Preimage : lntypes.Preimage {1 , 2 , 3 },
217
+ }, nil
212
218
},
213
219
})
214
220
@@ -233,7 +239,7 @@ func TestManager_Backoff(t *testing.T) {
233
239
// - Attempt #3: ~3 seconds after that etc.
234
240
time .Sleep (5 * time .Second )
235
241
236
- // Cancel the contedt to stop the manager.
242
+ // Cancel the context to stop the manager.
237
243
cancel ()
238
244
wg .Wait ()
239
245
@@ -297,7 +303,10 @@ func TestManager_MinAliveConnTime(t *testing.T) {
297
303
Client : mockClient ,
298
304
MinAliveConnTime : minAlive ,
299
305
CurrentToken : func () (* l402.Token , error ) {
300
- return & l402.Token {}, nil
306
+ // Simulate successful fetching of L402
307
+ return & l402.Token {
308
+ Preimage : lntypes.Preimage {1 , 2 , 3 },
309
+ }, nil
301
310
},
302
311
})
303
312
@@ -345,3 +354,78 @@ func TestManager_MinAliveConnTime(t *testing.T) {
345
354
"Second attempt should occur ~2s after the first" ,
346
355
)
347
356
}
357
+
358
+ // TestManager_Backoff_Pending_Token verifies that the Manager backs off when
359
+ // the token is pending.
360
+ func TestManager_Backoff_Pending_Token (t * testing.T ) {
361
+ t .Parallel ()
362
+
363
+ // We'll tolerate a bit of jitter in the timing checks.
364
+ const tolerance = 300 * time .Millisecond
365
+
366
+ recvChan := make (chan * swapserverrpc.SubscribeNotificationsResponse )
367
+ recvErrChan := make (chan error )
368
+
369
+ mockStream := & mockSubscribeNotificationsClient {
370
+ recvChan : recvChan ,
371
+ recvErrChan : recvErrChan ,
372
+ }
373
+
374
+ // Create a new mock client that will fail to subscribe.
375
+ mockClient := & mockNotificationsClient {
376
+ mockStream : mockStream ,
377
+ // subscribeErr stays nil => would succeed on each call.
378
+ }
379
+
380
+ var tokenCalls []time.Time
381
+ // Manager with a successful CurrentToken so that it always tries
382
+ // to subscribe.
383
+ mgr := NewManager (& Config {
384
+ Client : mockClient ,
385
+ CurrentToken : func () (* l402.Token , error ) {
386
+ tokenCalls = append (tokenCalls , time .Now ())
387
+ if len (tokenCalls ) < 3 {
388
+ // Simulate a pending token.
389
+ return & l402.Token {}, nil
390
+ }
391
+
392
+ // Simulate successful fetching of L402
393
+ return & l402.Token {
394
+ Preimage : lntypes.Preimage {1 , 2 , 3 },
395
+ }, nil
396
+ },
397
+ })
398
+
399
+ // Run the manager in a background goroutine.
400
+ ctx , cancel := context .WithCancel (context .Background ())
401
+ defer cancel ()
402
+
403
+ var wg sync.WaitGroup
404
+ wg .Add (1 )
405
+ go func () {
406
+ defer wg .Done ()
407
+ // We ignore the returned error because the Manager returns
408
+ // nil on context cancel.
409
+ _ = mgr .Run (ctx )
410
+ }()
411
+
412
+ // Wait long enough to see at least 3 token calls, so we can see that
413
+ // we'll indeed backoff when the token is pending.
414
+ time .Sleep (5 * time .Second )
415
+
416
+ // Signal EOF so the subscription stops.
417
+ close (recvChan )
418
+
419
+ // Cancel the context to stop the manager.
420
+ cancel ()
421
+ wg .Wait ()
422
+
423
+ // Expect exactly 3 token calls.
424
+ require .Equal (t , 3 , len (tokenCalls ))
425
+
426
+ require .InDeltaf (
427
+ t , 3 * time .Second , tokenCalls [2 ].Sub (tokenCalls [0 ]),
428
+ float64 (tolerance ),
429
+ "Expected to backoff for at ~3 seconds due to pending token" ,
430
+ )
431
+ }
0 commit comments