Skip to content

Commit 5046438

Browse files
authored
feat: switch to quartz for time testing (#48)
Signed-off-by: Spike Curtis <[email protected]>
1 parent 24d4ce5 commit 5046438

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

go.mod

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ replace tailscale.com => github.com/coder/tailscale v1.1.1-0.20240702054557-aa55
99

1010
require (
1111
cdr.dev/slog v1.6.2-0.20240126064726-20367d4aede6
12-
github.com/benbjohnson/clock v1.3.5
1312
github.com/breml/rootcerts v0.2.11
1413
github.com/coder/coder/v2 v2.10.1-0.20240703121105-f6639b788f7b
14+
github.com/coder/quartz v0.1.0
1515
github.com/fatih/color v1.17.0
1616
github.com/go-chi/chi/v5 v5.1.0
1717
github.com/google/uuid v1.6.0
@@ -61,7 +61,6 @@ require (
6161
github.com/cespare/xxhash/v2 v2.2.0 // indirect
6262
github.com/charmbracelet/lipgloss v0.8.0 // indirect
6363
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 // indirect
64-
github.com/coder/quartz v0.1.0 // indirect
6564
github.com/coder/retry v1.5.1 // indirect
6665
github.com/coder/serpent v0.7.0 // indirect
6766
github.com/coder/terraform-provider-coder v0.23.0 // indirect

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
9797
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
9898
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
9999
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
100-
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
101-
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
102100
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
103101
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
104102
github.com/bool64/shared v0.1.5 h1:fp3eUhBsrSjNCQPcSdQqZxxh9bBwrYiZ+zOKFkM0/2E=

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"
@@ -20,6 +19,7 @@ import (
2019
"cdr.dev/slog"
2120
"github.com/coder/coder/v2/codersdk"
2221
"github.com/coder/coder/v2/codersdk/agentsdk"
22+
"github.com/coder/quartz"
2323

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

3030
type podEventLoggerOptions struct {
3131
client kubernetes.Interface
32-
clock clock.Clock
32+
clock quartz.Clock
3333
coderURL *url.URL
3434

3535
logger slog.Logger
@@ -49,7 +49,7 @@ func newPodEventLogger(ctx context.Context, opts podEventLoggerOptions) (*podEve
4949
opts.logDebounce = 30 * time.Second
5050
}
5151
if opts.clock == nil {
52-
opts.clock = clock.New()
52+
opts.clock = quartz.NewReal()
5353
}
5454

5555
logCh := make(chan agentLog, 512)
@@ -388,7 +388,7 @@ type agentLog struct {
388388
type logQueuer struct {
389389
mu sync.Mutex
390390
logger slog.Logger
391-
clock clock.Clock
391+
clock quartz.Clock
392392
q chan agentLog
393393

394394
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)