Skip to content

Commit 6de26a7

Browse files
committed
feat: switch to quartz for time testing
Signed-off-by: Spike Curtis <[email protected]>
1 parent b9d65a1 commit 6de26a7

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ replace tailscale.com => github.com/coder/tailscale v1.1.1-0.20240522100209-5cd2
77

88
require (
99
cdr.dev/slog v1.6.2-0.20240126064726-20367d4aede6
10-
github.com/benbjohnson/clock v1.3.5
1110
github.com/breml/rootcerts v0.2.11
1211
github.com/coder/coder/v2 v2.10.1-0.20240528221537-02c36868b20b
12+
github.com/coder/quartz v0.1.0
1313
github.com/fatih/color v1.17.0
1414
github.com/go-chi/chi/v5 v5.1.0
1515
github.com/google/uuid v1.6.0
@@ -54,6 +54,7 @@ require (
5454
github.com/aws/aws-sdk-go-v2/service/sts v1.28.4 // indirect
5555
github.com/aws/smithy-go v1.20.2 // indirect
5656
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
57+
github.com/benbjohnson/clock v1.3.5 // indirect
5758
github.com/beorn7/perks v1.0.1 // indirect
5859
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
5960
github.com/cespare/xxhash/v2 v2.2.0 // indirect

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ github.com/coder/coder/v2 v2.10.1-0.20240528221537-02c36868b20b h1:9cZlY9BnE0zYV
124124
github.com/coder/coder/v2 v2.10.1-0.20240528221537-02c36868b20b/go.mod h1:o40ZZM8FESNC5hDp8/S92OSaPb/aV0jSySsh270z3Yc=
125125
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx9n47SZOKOpgSE1bbJzlE4qPVs=
126126
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc=
127+
github.com/coder/quartz v0.1.0 h1:cLL+0g5l7xTf6ordRnUMMiZtRE8Sq5LxpghS63vEXrQ=
128+
github.com/coder/quartz v0.1.0/go.mod h1:vsiCc+AHViMKH2CQpGIpFgdHIEQsxwm8yCscqKmzbRA=
127129
github.com/coder/retry v1.5.1 h1:iWu8YnD8YqHs3XwqrqsjoBTAVqT9ml6z9ViJ2wlMiqc=
128130
github.com/coder/retry v1.5.1/go.mod h1:blHMk9vs6LkoRT9ZHyuZo360cufXEhrxqvEzeMtRGoY=
129131
github.com/coder/serpent v0.7.0 h1:zGpD2GlF3lKIVkMjNGKbkip88qzd5r/TRcc30X/SrT0=

logger.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"sync"
88
"time"
99

10-
"github.com/benbjohnson/clock"
1110
"github.com/fatih/color"
1211
"github.com/google/uuid"
1312
appsv1 "k8s.io/api/apps/v1"
@@ -21,6 +20,7 @@ import (
2120
"github.com/coder/coder/v2/agent/proto"
2221
"github.com/coder/coder/v2/codersdk"
2322
"github.com/coder/coder/v2/codersdk/agentsdk"
23+
"github.com/coder/quartz"
2424

2525
// *Never* remove this. Certificates are not bundled as part
2626
// of the container, so this is necessary for all connections
@@ -30,7 +30,7 @@ import (
3030

3131
type podEventLoggerOptions struct {
3232
client kubernetes.Interface
33-
clock clock.Clock
33+
clock quartz.Clock
3434
coderURL *url.URL
3535

3636
logger slog.Logger
@@ -50,7 +50,7 @@ func newPodEventLogger(ctx context.Context, opts podEventLoggerOptions) (*podEve
5050
opts.logDebounce = 30 * time.Second
5151
}
5252
if opts.clock == nil {
53-
opts.clock = clock.New()
53+
opts.clock = quartz.NewReal()
5454
}
5555

5656
logCh := make(chan agentLog, 512)
@@ -389,7 +389,7 @@ type agentLog struct {
389389
type logQueuer struct {
390390
mu sync.Mutex
391391
logger slog.Logger
392-
clock clock.Clock
392+
clock quartz.Clock
393393
q chan agentLog
394394

395395
coderURL *url.URL
@@ -525,7 +525,7 @@ func (l *logQueuer) loggerTimeout(agentToken string) {
525525
type agentLoggerLifecycle struct {
526526
scriptLogger agentsdk.ScriptLogger
527527

528-
closeTimer *clock.Timer
528+
closeTimer *quartz.Timer
529529
close func()
530530
}
531531

logger_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"testing"
1212
"time"
1313

14-
"github.com/benbjohnson/clock"
1514
"github.com/go-chi/chi/v5"
1615
"github.com/hashicorp/yamux"
1716
"github.com/stretchr/testify/require"
@@ -30,6 +29,7 @@ import (
3029
"github.com/coder/coder/v2/codersdk"
3130
"github.com/coder/coder/v2/codersdk/agentsdk"
3231
"github.com/coder/coder/v2/testutil"
32+
"github.com/coder/quartz"
3333
)
3434

3535
func TestReplicaSetEvents(t *testing.T) {
@@ -43,7 +43,7 @@ func TestReplicaSetEvents(t *testing.T) {
4343
namespace := "test-namespace"
4444
client := fake.NewSimpleClientset()
4545

46-
cMock := clock.NewMock()
46+
cMock := quartz.NewMock(t)
4747
reporter, err := newPodEventLogger(ctx, podEventLoggerOptions{
4848
client: client,
4949
coderURL: agentURL,
@@ -140,7 +140,7 @@ func TestPodEvents(t *testing.T) {
140140
namespace := "test-namespace"
141141
client := fake.NewSimpleClientset()
142142

143-
cMock := clock.NewMock()
143+
cMock := quartz.NewMock(t)
144144
reporter, err := newPodEventLogger(ctx, podEventLoggerOptions{
145145
client: client,
146146
coderURL: agentURL,
@@ -284,7 +284,7 @@ func Test_logQueuer(t *testing.T) {
284284
api := newFakeAgentAPI(t)
285285
agentURL, err := url.Parse(api.server.URL)
286286
require.NoError(t, err)
287-
clock := clock.NewMock()
287+
clock := quartz.NewMock(t)
288288
ttl := time.Second
289289

290290
ch := make(chan agentLog)
@@ -300,7 +300,7 @@ func Test_logQueuer(t *testing.T) {
300300
},
301301
}
302302

303-
ctx, cancel := context.WithCancel(context.Background())
303+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
304304
defer cancel()
305305
go lq.work(ctx)
306306

@@ -335,7 +335,7 @@ func Test_logQueuer(t *testing.T) {
335335
logs = testutil.RequireRecvCtx(ctx, t, api.logs)
336336
require.Len(t, logs, 1)
337337

338-
clock.Add(2 * ttl)
338+
clock.Advance(ttl)
339339
// wait for the client to disconnect
340340
_ = testutil.RequireRecvCtx(ctx, t, api.disconnect)
341341
})

0 commit comments

Comments
 (0)