Skip to content

Commit 313d4e0

Browse files
authored
chore(scaletest/dashboard): stub out initChromeDPCtx in unit tests (#13650)
1 parent 65b9f9b commit 313d4e0

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

scaletest/dashboard/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dashboard
22

33
import (
44
"context"
5+
"net/url"
56
"time"
67

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

3336
func (c Config) Validate() error {

scaletest/dashboard/run.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func NewRunner(client *codersdk.Client, metrics Metrics, cfg Config) *Runner {
3939
if cfg.RandIntn == nil {
4040
cfg.RandIntn = rand.Intn
4141
}
42+
if cfg.InitChromeDPCtx == nil {
43+
cfg.InitChromeDPCtx = initChromeDPCtx
44+
}
4245
return &Runner{
4346
client: client,
4447
cfg: cfg,
@@ -70,7 +73,7 @@ func (r *Runner) runUntilDeadlineExceeded(ctx context.Context) error {
7073
return xerrors.Errorf("user has no organizations")
7174
}
7275

73-
cdpCtx, cdpCancel, err := initChromeDPCtx(ctx, r.cfg.Logger, r.client.URL, r.client.SessionToken(), r.cfg.Headless)
76+
cdpCtx, cdpCancel, err := r.cfg.InitChromeDPCtx(ctx, r.cfg.Logger, r.client.URL, r.client.SessionToken(), r.cfg.Headless)
7477
if err != nil {
7578
return xerrors.Errorf("init chromedp ctx: %w", err)
7679
}

scaletest/dashboard/run_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dashboard_test
33
import (
44
"context"
55
"math/rand"
6+
"net/url"
67
"runtime"
78
"sync"
89
"sync/atomic"
@@ -52,6 +53,7 @@ func Test_Run(t *testing.T) {
5253
waitLoadedCalled atomic.Bool
5354
screenshotCalled atomic.Bool
5455
)
56+
cancelDone := make(chan struct{})
5557
cfg := dashboard.Config{
5658
Interval: 500 * time.Millisecond,
5759
Jitter: 100 * time.Millisecond,
@@ -72,6 +74,9 @@ func Test_Run(t *testing.T) {
7274
return "/fake/path/to/" + name + ".png", nil
7375
},
7476
RandIntn: rg.Intn,
77+
InitChromeDPCtx: func(ctx context.Context, _ slog.Logger, _ *url.URL, _ string, _ bool) (context.Context, context.CancelFunc, error) {
78+
return ctx, func() { close(cancelDone) }, nil
79+
},
7580
}
7681
r := dashboard.NewRunner(client, m, cfg)
7782
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
@@ -84,6 +89,8 @@ func Test_Run(t *testing.T) {
8489
err, ok := <-done
8590
assert.True(t, ok)
8691
require.NoError(t, err)
92+
_, ok = <-cancelDone
93+
require.False(t, ok, "cancel should have been called")
8794

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

0 commit comments

Comments
 (0)