Skip to content

Commit 9e9a5fd

Browse files
authored
chore(coderd/coderdtest): wait for provisioner daemons to be connected (#15936)
Fixes coder/internal#260
1 parent 962608c commit 9e9a5fd

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

coderd/coderdtest/coderdtest.go

+6
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ func NewTaggedProvisionerDaemon(t testing.TB, coderAPI *coderd.API, name string,
631631
assert.NoError(t, err)
632632
}()
633633

634+
connectedCh := make(chan struct{})
634635
daemon := provisionerd.New(func(dialCtx context.Context) (provisionerdproto.DRPCProvisionerDaemonClient, error) {
635636
return coderAPI.CreateInMemoryTaggedProvisionerDaemon(dialCtx, name, []codersdk.ProvisionerType{codersdk.ProvisionerTypeEcho}, provisionerTags)
636637
}, &provisionerd.Options{
@@ -640,7 +641,12 @@ func NewTaggedProvisionerDaemon(t testing.TB, coderAPI *coderd.API, name string,
640641
Connector: provisionerd.LocalProvisioners{
641642
string(database.ProvisionerTypeEcho): sdkproto.NewDRPCProvisionerClient(echoClient),
642643
},
644+
InitConnectionCh: connectedCh,
643645
})
646+
// Wait for the provisioner daemon to connect before continuing.
647+
// Users of this function tend to assume that the provisioner is connected
648+
// and ready to use when that may not strictly be the case.
649+
<-connectedCh
644650
closer := NewProvisionerDaemonCloser(daemon)
645651
t.Cleanup(func() {
646652
_ = closer.Close()

coderd/templateversions_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ func TestPostTemplateVersionsByOrganization(t *testing.T) {
172172
require.Equal(t, provisionersdk.ScopeOrganization, version.Job.Tags[provisionersdk.TagScope])
173173
if assert.Equal(t, version.Job.Status, codersdk.ProvisionerJobPending) {
174174
assert.NotNil(t, version.MatchedProvisioners)
175-
assert.Equal(t, version.MatchedProvisioners.Available, 1)
176-
assert.Equal(t, version.MatchedProvisioners.Count, 1)
175+
assert.Equal(t, 1, version.MatchedProvisioners.Available)
176+
assert.Equal(t, 1, version.MatchedProvisioners.Count)
177177
assert.True(t, version.MatchedProvisioners.MostRecentlySeen.Valid)
178178
}
179179

provisionerd/provisionerd.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type Options struct {
6060
UpdateInterval time.Duration
6161
LogBufferInterval time.Duration
6262
Connector Connector
63+
InitConnectionCh chan struct{} // only to be used in tests
6364
}
6465

6566
// New creates and starts a provisioner daemon.
@@ -84,6 +85,9 @@ func New(clientDialer Dialer, opts *Options) *Server {
8485
mets := NewMetrics(reg)
8586
opts.Metrics = &mets
8687
}
88+
if opts.InitConnectionCh == nil {
89+
opts.InitConnectionCh = make(chan struct{})
90+
}
8791

8892
ctx, ctxCancel := context.WithCancel(context.Background())
8993
daemon := &Server{
@@ -93,11 +97,12 @@ func New(clientDialer Dialer, opts *Options) *Server {
9397
clientDialer: clientDialer,
9498
clientCh: make(chan proto.DRPCProvisionerDaemonClient),
9599

96-
closeContext: ctx,
97-
closeCancel: ctxCancel,
98-
closedCh: make(chan struct{}),
99-
shuttingDownCh: make(chan struct{}),
100-
acquireDoneCh: make(chan struct{}),
100+
closeContext: ctx,
101+
closeCancel: ctxCancel,
102+
closedCh: make(chan struct{}),
103+
shuttingDownCh: make(chan struct{}),
104+
acquireDoneCh: make(chan struct{}),
105+
initConnectionCh: opts.InitConnectionCh,
101106
}
102107

103108
daemon.wg.Add(2)
@@ -115,6 +120,11 @@ type Server struct {
115120

116121
wg sync.WaitGroup
117122

123+
// initConnectionCh will receive when the daemon connects to coderd for the
124+
// first time.
125+
initConnectionCh chan struct{}
126+
initConnectionOnce sync.Once
127+
118128
// mutex protects all subsequent fields
119129
mutex sync.Mutex
120130
// closeContext is canceled when we start closing.
@@ -231,6 +241,9 @@ connectLoop:
231241
}
232242
p.opts.Logger.Info(p.closeContext, "successfully connected to coderd")
233243
retrier.Reset()
244+
p.initConnectionOnce.Do(func() {
245+
close(p.initConnectionCh)
246+
})
234247

235248
// serve the client until we are closed or it disconnects
236249
for {

0 commit comments

Comments
 (0)