Skip to content

chore(scaletest/dashboard): stub out initChromeDPCtx in unit tests #13650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions scaletest/dashboard/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dashboard

import (
"context"
"net/url"
"time"

"cdr.dev/slog"
Expand All @@ -28,6 +29,8 @@ type Config struct {
Screenshot func(ctx context.Context, filename string) (string, error)
// RandIntn is a function that returns a random number between 0 and n-1.
RandIntn func(int) int `json:"-"`
// InitChromeDPCtx is a function that initializes ChromeDP into the given context.Context.
InitChromeDPCtx func(ctx context.Context, log slog.Logger, u *url.URL, sessionToken string, headless bool) (context.Context, context.CancelFunc, error) `json:"-"`
}

func (c Config) Validate() error {
Expand Down
5 changes: 4 additions & 1 deletion scaletest/dashboard/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func NewRunner(client *codersdk.Client, metrics Metrics, cfg Config) *Runner {
if cfg.RandIntn == nil {
cfg.RandIntn = rand.Intn
}
if cfg.InitChromeDPCtx == nil {
cfg.InitChromeDPCtx = initChromeDPCtx
}
return &Runner{
client: client,
cfg: cfg,
Expand Down Expand Up @@ -70,7 +73,7 @@ func (r *Runner) runUntilDeadlineExceeded(ctx context.Context) error {
return xerrors.Errorf("user has no organizations")
}

cdpCtx, cdpCancel, err := initChromeDPCtx(ctx, r.cfg.Logger, r.client.URL, r.client.SessionToken(), r.cfg.Headless)
cdpCtx, cdpCancel, err := r.cfg.InitChromeDPCtx(ctx, r.cfg.Logger, r.client.URL, r.client.SessionToken(), r.cfg.Headless)
if err != nil {
return xerrors.Errorf("init chromedp ctx: %w", err)
}
Expand Down
7 changes: 7 additions & 0 deletions scaletest/dashboard/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dashboard_test
import (
"context"
"math/rand"
"net/url"
"runtime"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -52,6 +53,7 @@ func Test_Run(t *testing.T) {
waitLoadedCalled atomic.Bool
screenshotCalled atomic.Bool
)
cancelDone := make(chan struct{})
cfg := dashboard.Config{
Interval: 500 * time.Millisecond,
Jitter: 100 * time.Millisecond,
Expand All @@ -72,6 +74,9 @@ func Test_Run(t *testing.T) {
return "/fake/path/to/" + name + ".png", nil
},
RandIntn: rg.Intn,
InitChromeDPCtx: func(ctx context.Context, _ slog.Logger, _ *url.URL, _ string, _ bool) (context.Context, context.CancelFunc, error) {
return ctx, func() { close(cancelDone) }, nil
},
}
r := dashboard.NewRunner(client, m, cfg)
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
Expand All @@ -84,6 +89,8 @@ func Test_Run(t *testing.T) {
err, ok := <-done
assert.True(t, ok)
require.NoError(t, err)
_, ok = <-cancelDone
require.False(t, ok, "cancel should have been called")

for _, dur := range m.ObservedDurations["succeeds"] {
assert.NotZero(t, dur)
Expand Down
Loading