Skip to content

Commit 3b9d365

Browse files
committed
avoid race in test
1 parent 24f6614 commit 3b9d365

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

coderd/agentapi/tailnet_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package agentapi_test
22

33
import (
44
"context"
5+
"sync"
56
"testing"
67
"time"
78

@@ -45,10 +46,13 @@ func TestStreamDERPMaps(t *testing.T) {
4546
t.Run("OK", func(t *testing.T) {
4647
t.Parallel()
4748

49+
derpMapMu := sync.Mutex{}
4850
derpMap := tailcfg.DERPMap{}
4951
api := &agentapi.TailnetAPI{
5052
Ctx: context.Background(),
5153
DerpMapFn: func() *tailcfg.DERPMap {
54+
derpMapMu.Lock()
55+
defer derpMapMu.Unlock()
5256
derp := (&derpMap).Clone()
5357
return derp
5458
},
@@ -91,14 +95,18 @@ func TestStreamDERPMaps(t *testing.T) {
9195
require.Equal(t, tailnet.DERPMapToProto(&derpMap), gotMap)
9296

9397
// Update the map, should get an update.
98+
derpMapMu.Lock()
9499
derpMap.Regions = map[int]*tailcfg.DERPRegion{
95100
1: {},
96101
}
102+
derpMapMu.Unlock()
97103
gotMap = <-maps
98104
require.Equal(t, tailnet.DERPMapToProto(&derpMap), gotMap)
99105

100106
// Update the map again, should get an update.
107+
derpMapMu.Lock()
101108
derpMap.Regions = nil
109+
derpMapMu.Unlock()
102110
gotMap = <-maps
103111
require.Equal(t, tailnet.DERPMapToProto(&derpMap), gotMap)
104112

0 commit comments

Comments
 (0)