Skip to content

Commit 88969df

Browse files
authored
Merge pull request #3262 from buildkite/agent-worker-ping-interval-for-testing
`AgentWorker` has `noWaitBetweenPingsForTesting` field
2 parents 2a5d335 + 3ad0294 commit 88969df

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

agent/agent_worker.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ type AgentWorker struct {
104104
stateMtx sync.Mutex
105105
state agentWorkerState
106106
currentJobID string
107+
108+
// disable the delay between pings, to speed up certain testing scenarios
109+
noWaitBetweenPingsForTesting bool
107110
}
108111

109112
type agentWorkerState string
@@ -294,6 +297,14 @@ func (a *AgentWorker) runPingLoop(ctx context.Context, idleMonitor *IdleMonitor)
294297
pingTicker := time.NewTicker(pingInterval)
295298
defer pingTicker.Stop()
296299

300+
// testTriggerCh will normally block forever, and so will not affect the for/select loop.
301+
var testTriggerCh chan struct{}
302+
if a.noWaitBetweenPingsForTesting {
303+
// a closed channel will unblock the for/select instantly, for zero-delay ping loop testing.
304+
testTriggerCh = make(chan struct{})
305+
close(testTriggerCh)
306+
}
307+
297308
first := make(chan struct{}, 1)
298309
first <- struct{}{}
299310

@@ -314,6 +325,8 @@ func (a *AgentWorker) runPingLoop(ctx context.Context, idleMonitor *IdleMonitor)
314325
for {
315326
setStat("😴 Waiting until next ping interval tick")
316327
select {
328+
case <-testTriggerCh:
329+
// instant receive from closed chan when noWaitBetweenPingsForTesting is true
317330
case <-first:
318331
// continue below
319332
case <-pingTicker.C:
@@ -329,6 +342,8 @@ func (a *AgentWorker) runPingLoop(ctx context.Context, idleMonitor *IdleMonitor)
329342
jitter := rand.N(pingInterval)
330343
setStat(fmt.Sprintf("🫨 Jittering for %v", jitter))
331344
select {
345+
case <-testTriggerCh:
346+
// instant receive from closed chan when noWaitBetweenPingsForTesting is true
332347
case <-time.After(jitter):
333348
// continue below
334349
case <-a.stop:

agent/agent_worker_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ func TestAgentWorker_Start_AcquireJob_Pause_Unpause(t *testing.T) {
377377
},
378378
},
379379
)
380+
worker.noWaitBetweenPingsForTesting = true
380381

381382
idleMonitor := NewIdleMonitor(1)
382383

@@ -544,6 +545,7 @@ func TestAgentWorker_DisconnectAfterJob_Start_Pause_Unpause(t *testing.T) {
544545
},
545546
},
546547
)
548+
worker.noWaitBetweenPingsForTesting = true
547549

548550
idleMonitor := NewIdleMonitor(1)
549551

0 commit comments

Comments
 (0)