Skip to content

Commit 99d6d18

Browse files
committed
test: Wrap provisionerd channel closes in sync.Once (#1181)
This caused a few flakes, so figured I'd tackle all of them: https://github.com/coder/coder/runs/6167856950?check_suite_focus=true#step:9:246
1 parent 6996ff5 commit 99d6d18

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

provisionerd/provisionerd_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func TestProvisionerd(t *testing.T) {
8484
t.Run("CloseCancelsJob", func(t *testing.T) {
8585
t.Parallel()
8686
completeChan := make(chan struct{})
87+
var completed sync.Once
8788
var closer io.Closer
8889
var closerMutex sync.Mutex
8990
closerMutex.Lock()
@@ -105,7 +106,9 @@ func TestProvisionerd(t *testing.T) {
105106
},
106107
updateJob: noopUpdateJob,
107108
failJob: func(ctx context.Context, job *proto.FailedJob) (*proto.Empty, error) {
108-
close(completeChan)
109+
completed.Do(func() {
110+
close(completeChan)
111+
})
109112
return &proto.Empty{}, nil
110113
},
111114
}), nil
@@ -432,6 +435,8 @@ func TestProvisionerd(t *testing.T) {
432435

433436
t.Run("Shutdown", func(t *testing.T) {
434437
t.Parallel()
438+
var updated sync.Once
439+
var completed sync.Once
435440
updateChan := make(chan struct{})
436441
completeChan := make(chan struct{})
437442
server := createProvisionerd(t, func(ctx context.Context) (proto.DRPCProvisionerDaemonClient, error) {
@@ -453,12 +458,16 @@ func TestProvisionerd(t *testing.T) {
453458
updateJob: func(ctx context.Context, update *proto.UpdateJobRequest) (*proto.UpdateJobResponse, error) {
454459
if len(update.Logs) > 0 && update.Logs[0].Source == proto.LogSource_PROVISIONER {
455460
// Close on a log so we know when the job is in progress!
456-
close(updateChan)
461+
updated.Do(func() {
462+
close(updateChan)
463+
})
457464
}
458465
return &proto.UpdateJobResponse{}, nil
459466
},
460467
failJob: func(ctx context.Context, job *proto.FailedJob) (*proto.Empty, error) {
461-
close(completeChan)
468+
completed.Do(func() {
469+
close(completeChan)
470+
})
462471
return &proto.Empty{}, nil
463472
},
464473
}), nil
@@ -642,6 +651,7 @@ func TestProvisionerd(t *testing.T) {
642651

643652
t.Run("ReconnectAndComplete", func(t *testing.T) {
644653
t.Parallel()
654+
var completed sync.Once
645655
var second atomic.Bool
646656
failChan := make(chan struct{})
647657
failedChan := make(chan struct{})
@@ -650,7 +660,9 @@ func TestProvisionerd(t *testing.T) {
650660
client := createProvisionerDaemonClient(t, provisionerDaemonTestServer{
651661
acquireJob: func(ctx context.Context, _ *proto.Empty) (*proto.AcquiredJob, error) {
652662
if second.Load() {
653-
close(completeChan)
663+
completed.Do(func() {
664+
close(completeChan)
665+
})
654666
return &proto.AcquiredJob{}, nil
655667
}
656668
return &proto.AcquiredJob{

0 commit comments

Comments
 (0)