Skip to content

Commit

Permalink
feat: switch to quartz for time testing (#48)
Browse files Browse the repository at this point in the history
Signed-off-by: Spike Curtis <spike@coder.com>
  • Loading branch information
spikecurtis authored Jul 16, 2024
1 parent 24d4ce5 commit 5046438
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ replace tailscale.com => github.com/coder/tailscale v1.1.1-0.20240702054557-aa55

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.20240703121105-f6639b788f7b
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 @@ -61,7 +61,6 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/charmbracelet/lipgloss v0.8.0 // indirect
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 // indirect
github.com/coder/quartz v0.1.0 // indirect
github.com/coder/retry v1.5.1 // indirect
github.com/coder/serpent v0.7.0 // indirect
github.com/coder/terraform-provider-coder v0.23.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/benbjohnson/clock v1.3.5 h1:VvXlSJBzZpA/zum6Sj74hxwYI2DIxRWuNIoXAzHZz5o=
github.com/benbjohnson/clock v1.3.5/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bool64/shared v0.1.5 h1:fp3eUhBsrSjNCQPcSdQqZxxh9bBwrYiZ+zOKFkM0/2E=
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 @@ -20,6 +19,7 @@ import (
"cdr.dev/slog"
"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 @@ -29,7 +29,7 @@ import (

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

logger slog.Logger
Expand All @@ -49,7 +49,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 @@ -388,7 +388,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 5046438

Please sign in to comment.