Skip to content

Commit 03c4b2a

Browse files
authored
derp/derphttp: test improvements (tailscale#16723)
Update some logging to help future failures. Improve test shutdown concurrency issues. Fixes tailscale#16722 Signed-off-by: Mike O'Driscoll <[email protected]>
1 parent d122f03 commit 03c4b2a

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

derp/derphttp/derphttp_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ func TestBreakWatcherConnRecv(t *testing.T) {
286286
defer func() { retryInterval = origRetryInterval }()
287287

288288
var wg sync.WaitGroup
289-
defer wg.Wait()
290289
// Make the watcher server
291290
serverPrivateKey1 := key.NewNode()
292291
_, s1 := newTestServer(t, serverPrivateKey1)
@@ -298,14 +297,15 @@ func TestBreakWatcherConnRecv(t *testing.T) {
298297
defer s2.Close()
299298

300299
// Make the watcher (but it is not connected yet)
301-
watcher1 := newWatcherClient(t, serverPrivateKey1, serverURL2)
302-
defer watcher1.Close()
300+
watcher := newWatcherClient(t, serverPrivateKey1, serverURL2)
301+
defer watcher.Close()
303302

304303
ctx, cancel := context.WithCancel(context.Background())
305-
defer cancel()
306304

307305
watcherChan := make(chan int, 1)
306+
defer close(watcherChan)
308307
errChan := make(chan error, 1)
308+
defer close(errChan)
309309

310310
// Start the watcher thread (which connects to the watched server)
311311
wg.Add(1) // To avoid using t.Logf after the test ends. See https://golang.org/issue/40343
@@ -323,7 +323,7 @@ func TestBreakWatcherConnRecv(t *testing.T) {
323323
errChan <- err
324324
}
325325

326-
watcher1.RunWatchConnectionLoop(ctx, serverPrivateKey1.Public(), t.Logf, add, remove, notifyErr)
326+
watcher.RunWatchConnectionLoop(ctx, serverPrivateKey1.Public(), t.Logf, add, remove, notifyErr)
327327
}()
328328

329329
timer := time.NewTimer(5 * time.Second)
@@ -335,7 +335,7 @@ func TestBreakWatcherConnRecv(t *testing.T) {
335335
select {
336336
case peers := <-watcherChan:
337337
if peers != 1 {
338-
t.Fatal("wrong number of peers added during watcher connection")
338+
t.Fatalf("wrong number of peers added during watcher connection: have %d, want 1", peers)
339339
}
340340
case err := <-errChan:
341341
if !strings.Contains(err.Error(), "use of closed network connection") {
@@ -344,12 +344,13 @@ func TestBreakWatcherConnRecv(t *testing.T) {
344344
case <-timer.C:
345345
t.Fatalf("watcher did not process the peer update")
346346
}
347-
watcher1.breakConnection(watcher1.client)
348-
// re-establish connection by sending a packet
349-
watcher1.ForwardPacket(key.NodePublic{}, key.NodePublic{}, []byte("bogus"))
350-
351347
timer.Reset(5 * time.Second)
348+
watcher.breakConnection(watcher.client)
349+
// re-establish connection by sending a packet
350+
watcher.ForwardPacket(key.NodePublic{}, key.NodePublic{}, []byte("bogus"))
352351
}
352+
cancel() // Cancel the context to stop the watcher loop.
353+
wg.Wait()
353354
}
354355

355356
// Test that a watcher connection successfully reconnects and processes peer
@@ -364,7 +365,6 @@ func TestBreakWatcherConn(t *testing.T) {
364365
defer func() { retryInterval = origRetryInterval }()
365366

366367
var wg sync.WaitGroup
367-
defer wg.Wait()
368368
// Make the watcher server
369369
serverPrivateKey1 := key.NewNode()
370370
_, s1 := newTestServer(t, serverPrivateKey1)
@@ -380,7 +380,6 @@ func TestBreakWatcherConn(t *testing.T) {
380380
defer watcher1.Close()
381381

382382
ctx, cancel := context.WithCancel(context.Background())
383-
defer cancel()
384383

385384
watcherChan := make(chan int, 1)
386385
breakerChan := make(chan bool, 1)
@@ -396,8 +395,12 @@ func TestBreakWatcherConn(t *testing.T) {
396395
peers++
397396
// Signal that the watcher has run
398397
watcherChan <- peers
398+
select {
399+
case <-ctx.Done():
400+
return
399401
// Wait for breaker to run
400-
<-breakerChan
402+
case <-breakerChan:
403+
}
401404
}
402405
remove := func(m derp.PeerGoneMessage) { t.Logf("remove: %v", m.Peer.ShortString()); peers-- }
403406
notifyError := func(err error) {
@@ -416,7 +419,7 @@ func TestBreakWatcherConn(t *testing.T) {
416419
select {
417420
case peers := <-watcherChan:
418421
if peers != 1 {
419-
t.Fatal("wrong number of peers added during watcher connection")
422+
t.Fatalf("wrong number of peers added during watcher connection have %d, want 1", peers)
420423
}
421424
case err := <-errorChan:
422425
if !strings.Contains(err.Error(), "use of closed network connection") {
@@ -433,6 +436,9 @@ func TestBreakWatcherConn(t *testing.T) {
433436

434437
timer.Reset(5 * time.Second)
435438
}
439+
watcher1.Close()
440+
cancel()
441+
wg.Wait()
436442
}
437443

438444
func noopAdd(derp.PeerPresentMessage) {}

0 commit comments

Comments
 (0)