Skip to content

Commit 6449c93

Browse files
committed
Revert "fix(coderd/provisionerdserver): fix test flake in TestHeartbeat"
This reverts commit bb32c58.
1 parent bb32c58 commit 6449c93

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ type Options struct {
7272
// The default function just calls UpdateProvisionerDaemonLastSeenAt.
7373
// This is mainly used for testing.
7474
HeartbeatFn func(context.Context) error
75-
76-
// HeartbeatDone is used for testing.
77-
HeartbeatDone chan struct{}
7875
}
7976

8077
type server struct {
@@ -186,9 +183,6 @@ func NewServer(
186183
if options.HeartbeatInterval == 0 {
187184
options.HeartbeatInterval = DefaultHeartbeatInterval
188185
}
189-
if options.HeartbeatDone == nil {
190-
options.HeartbeatDone = make(chan struct{})
191-
}
192186

193187
s := &server{
194188
lifecycleCtx: lifecycleCtx,
@@ -219,7 +213,7 @@ func NewServer(
219213
s.heartbeatFn = s.defaultHeartbeat
220214
}
221215

222-
go s.heartbeatLoop(options.HeartbeatDone)
216+
go s.heartbeatLoop()
223217
return s, nil
224218
}
225219

@@ -233,21 +227,17 @@ func (s *server) timeNow() time.Time {
233227
}
234228

235229
// heartbeatLoop runs heartbeatOnce at the interval specified by HeartbeatInterval
236-
// until the lifecycle context is canceled. Done is closed on exit.
237-
func (s *server) heartbeatLoop(hbDone chan<- struct{}) {
230+
// until the lifecycle context is canceled.
231+
func (s *server) heartbeatLoop() {
238232
tick := time.NewTicker(time.Nanosecond)
239-
defer func() {
240-
close(hbDone)
241-
}()
233+
defer tick.Stop()
242234
for {
243235
select {
244236
case <-s.lifecycleCtx.Done():
245237
s.Logger.Debug(s.lifecycleCtx, "heartbeat loop canceled")
246-
tick.Stop()
247238
return
248239
case <-tick.C:
249240
if s.lifecycleCtx.Err() != nil {
250-
tick.Stop()
251241
return
252242
}
253243
start := s.timeNow()

coderd/provisionerdserver/provisionerdserver_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ func TestHeartbeat(t *testing.T) {
104104
ctx, cancel := context.WithCancel(context.Background())
105105
t.Cleanup(cancel)
106106
heartbeatChan := make(chan struct{})
107-
heartbeatDone := make(chan struct{})
108107
heartbeatFn := func(hbCtx context.Context) error {
109108
t.Logf("heartbeat")
110109
select {
@@ -118,7 +117,6 @@ func TestHeartbeat(t *testing.T) {
118117
//nolint:dogsled // 。:゚૮ ˶ˆ ﻌ ˆ˶ ა ゚:。
119118
_, _, _, _ = setup(t, false, &overrides{
120119
ctx: ctx,
121-
heartbeatDone: heartbeatDone,
122120
heartbeatFn: heartbeatFn,
123121
heartbeatInterval: testutil.IntervalFast,
124122
})
@@ -127,9 +125,17 @@ func TestHeartbeat(t *testing.T) {
127125
require.True(t, ok, "first heartbeat not received")
128126
_, ok = <-heartbeatChan
129127
require.True(t, ok, "second heartbeat not received")
130-
// Cancel the context. This should cause heartbeatDone to be closed.
131128
cancel()
132-
<-heartbeatDone
129+
// Close the channel to ensure we don't receive any more heartbeats.
130+
// The test will fail if we do.
131+
defer func() {
132+
if r := recover(); r != nil {
133+
t.Fatalf("heartbeat received after cancel: %v", r)
134+
}
135+
}()
136+
137+
close(heartbeatChan)
138+
<-time.After(testutil.IntervalMedium)
133139
}
134140

135141
func TestAcquireJob(t *testing.T) {
@@ -1721,7 +1727,6 @@ type overrides struct {
17211727
acquireJobLongPollDuration time.Duration
17221728
heartbeatFn func(ctx context.Context) error
17231729
heartbeatInterval time.Duration
1724-
heartbeatDone chan struct{}
17251730
}
17261731

17271732
func setup(t *testing.T, ignoreLogErrors bool, ov *overrides) (proto.DRPCProvisionerDaemonServer, database.Store, pubsub.Pubsub, database.ProvisionerDaemon) {
@@ -1746,9 +1751,6 @@ func setup(t *testing.T, ignoreLogErrors bool, ov *overrides) (proto.DRPCProvisi
17461751
if ov.heartbeatInterval == 0 {
17471752
ov.heartbeatInterval = testutil.IntervalMedium
17481753
}
1749-
if ov.heartbeatDone == nil {
1750-
ov.heartbeatDone = make(chan struct{})
1751-
}
17521754
if ov.deploymentValues != nil {
17531755
deploymentValues = ov.deploymentValues
17541756
}
@@ -1813,7 +1815,6 @@ func setup(t *testing.T, ignoreLogErrors bool, ov *overrides) (proto.DRPCProvisi
18131815
AcquireJobLongPollDur: pollDur,
18141816
HeartbeatInterval: ov.heartbeatInterval,
18151817
HeartbeatFn: ov.heartbeatFn,
1816-
HeartbeatDone: ov.heartbeatDone,
18171818
},
18181819
)
18191820
require.NoError(t, err)

0 commit comments

Comments
 (0)