mirror of
https://github.com/coder/coder.git
synced 2026-09-21 12:44:32 +08:00
chore(scaletest/dashboard): stub out initChromeDPCtx in unit tests (#13650)
This commit is contained in:
@@ -2,6 +2,7 @@ package dashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"cdr.dev/slog"
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package dashboard_test
|
||||
import (
|
||||
"context"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -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,
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user