Skip to content

Commit

Permalink
feat: switch to quartz for time testing
Browse files Browse the repository at this point in the history
Signed-off-by: Spike Curtis <spike@coder.com>
  • Loading branch information
spikecurtis committed Jul 16, 2024
1 parent b9d65a1 commit 6de26a7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ replace tailscale.com => github.com/coder/tailscale v1.1.1-0.20240522100209-5cd2

require (
cdr.dev/slog v1.6.2-0.20240126064726-20367d4aede6
github.com/benbjohnson/clock v1.3.5
github.com/breml/rootcerts v0.2.11
github.com/coder/coder/v2 v2.10.1-0.20240528221537-02c36868b20b
github.com/coder/quartz v0.1.0
github.com/fatih/color v1.17.0
github.com/go-chi/chi/v5 v5.1.0
github.com/google/uuid v1.6.0
Expand Down Expand Up @@ -54,6 +54,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.28.4 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/benbjohnson/clock v1.3.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ github.com/coder/coder/v2 v2.10.1-0.20240528221537-02c36868b20b h1:9cZlY9BnE0zYV
github.com/coder/coder/v2 v2.10.1-0.20240528221537-02c36868b20b/go.mod h1:o40ZZM8FESNC5hDp8/S92OSaPb/aV0jSySsh270z3Yc=
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx9n47SZOKOpgSE1bbJzlE4qPVs=
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc=
github.com/coder/quartz v0.1.0 h1:cLL+0g5l7xTf6ordRnUMMiZtRE8Sq5LxpghS63vEXrQ=
github.com/coder/quartz v0.1.0/go.mod h1:vsiCc+AHViMKH2CQpGIpFgdHIEQsxwm8yCscqKmzbRA=
github.com/coder/retry v1.5.1 h1:iWu8YnD8YqHs3XwqrqsjoBTAVqT9ml6z9ViJ2wlMiqc=
github.com/coder/retry v1.5.1/go.mod h1:blHMk9vs6LkoRT9ZHyuZo360cufXEhrxqvEzeMtRGoY=
github.com/coder/serpent v0.7.0 h1:zGpD2GlF3lKIVkMjNGKbkip88qzd5r/TRcc30X/SrT0=
Expand Down
10 changes: 5 additions & 5 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"
"time"

"github.com/benbjohnson/clock"
"github.com/fatih/color"
"github.com/google/uuid"
appsv1 "k8s.io/api/apps/v1"
Expand All @@ -21,6 +20,7 @@ import (
"github.com/coder/coder/v2/agent/proto"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
"github.com/coder/quartz"

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

type podEventLoggerOptions struct {
client kubernetes.Interface
clock clock.Clock
clock quartz.Clock
coderURL *url.URL

logger slog.Logger
Expand All @@ -50,7 +50,7 @@ func newPodEventLogger(ctx context.Context, opts podEventLoggerOptions) (*podEve
opts.logDebounce = 30 * time.Second
}
if opts.clock == nil {
opts.clock = clock.New()
opts.clock = quartz.NewReal()
}

logCh := make(chan agentLog, 512)
Expand Down Expand Up @@ -389,7 +389,7 @@ type agentLog struct {
type logQueuer struct {
mu sync.Mutex
logger slog.Logger
clock clock.Clock
clock quartz.Clock
q chan agentLog

coderURL *url.URL
Expand Down Expand Up @@ -525,7 +525,7 @@ func (l *logQueuer) loggerTimeout(agentToken string) {
type agentLoggerLifecycle struct {
scriptLogger agentsdk.ScriptLogger

closeTimer *clock.Timer
closeTimer *quartz.Timer
close func()
}

Expand Down
12 changes: 6 additions & 6 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"testing"
"time"

"github.com/benbjohnson/clock"
"github.com/go-chi/chi/v5"
"github.com/hashicorp/yamux"
"github.com/stretchr/testify/require"
Expand All @@ -30,6 +29,7 @@ import (
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/codersdk/agentsdk"
"github.com/coder/coder/v2/testutil"
"github.com/coder/quartz"
)

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

cMock := clock.NewMock()
cMock := quartz.NewMock(t)
reporter, err := newPodEventLogger(ctx, podEventLoggerOptions{
client: client,
coderURL: agentURL,
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestPodEvents(t *testing.T) {
namespace := "test-namespace"
client := fake.NewSimpleClientset()

cMock := clock.NewMock()
cMock := quartz.NewMock(t)
reporter, err := newPodEventLogger(ctx, podEventLoggerOptions{
client: client,
coderURL: agentURL,
Expand Down Expand Up @@ -284,7 +284,7 @@ func Test_logQueuer(t *testing.T) {
api := newFakeAgentAPI(t)
agentURL, err := url.Parse(api.server.URL)
require.NoError(t, err)
clock := clock.NewMock()
clock := quartz.NewMock(t)
ttl := time.Second

ch := make(chan agentLog)
Expand All @@ -300,7 +300,7 @@ func Test_logQueuer(t *testing.T) {
},
}

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
go lq.work(ctx)

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

clock.Add(2 * ttl)
clock.Advance(ttl)
// wait for the client to disconnect
_ = testutil.RequireRecvCtx(ctx, t, api.disconnect)
})
Expand Down

0 comments on commit 6de26a7

Please sign in to comment.