Skip to content

Commit d65e2c8

Browse files
committed
"fix" dashboard test
1 parent a14812c commit d65e2c8

File tree

5 files changed

+48
-12
lines changed

5 files changed

+48
-12
lines changed

cli/exp_scaletest.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,13 +1151,12 @@ func (r *RootCmd) scaletestDashboard() *clibase.Cmd {
11511151
userClient.SetSessionToken(userTokResp.Key)
11521152

11531153
config := dashboard.Config{
1154-
Interval: interval,
1155-
Jitter: jitter,
1156-
Trace: tracingEnabled,
1157-
Logger: logger.Named(name),
1158-
Headless: headless,
1159-
ActionFunc: dashboard.ClickRandomElement,
1160-
RandIntn: rndGen.Intn,
1154+
Interval: interval,
1155+
Jitter: jitter,
1156+
Trace: tracingEnabled,
1157+
Logger: logger.Named(name),
1158+
Headless: headless,
1159+
RandIntn: rndGen.Intn,
11611160
}
11621161
//nolint:gocritic
11631162
logger.Info(ctx, "runner config", slog.F("interval", interval), slog.F("jitter", jitter), slog.F("headless", headless), slog.F("trace", tracingEnabled))

scaletest/dashboard/chromedp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ var defaultTargets = []Target{
8888
},
8989
}
9090

91-
// ClickRandomElement returns an action that will click an element from defaultTargets.
91+
// clickRandomElement returns an action that will click an element from defaultTargets.
9292
// If no elements are found, an error is returned.
9393
// If more than one element is found, one is chosen at random.
9494
// The label of the clicked element is returned.
95-
func ClickRandomElement(ctx context.Context, log slog.Logger, randIntn func(int) int, deadline time.Time) (Label, Action, error) {
95+
func clickRandomElement(ctx context.Context, log slog.Logger, randIntn func(int) int, deadline time.Time) (Label, Action, error) {
9696
var xpath Selector
9797
var found bool
9898
var err error

scaletest/dashboard/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ type Config struct {
2222
Headless bool `json:"headless"`
2323
// ActionFunc is a function that returns an action to run.
2424
ActionFunc func(ctx context.Context, log slog.Logger, randIntn func(int) int, deadline time.Time) (Label, Action, error) `json:"-"`
25+
// WaitLoaded is a function that waits for the page to be loaded.
26+
WaitLoaded func(ctx context.Context, deadline time.Time) error
27+
// Screenshot is a function that takes a screenshot.
28+
Screenshot func(ctx context.Context, filename string) (string, error)
2529
// RandIntn is a function that returns a random number between 0 and n-1.
2630
RandIntn func(int) int `json:"-"`
2731
}

scaletest/dashboard/run.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dashboard
22

33
import (
44
"context"
5+
"errors"
56
"io"
67
"time"
78

@@ -25,6 +26,15 @@ var (
2526

2627
func NewRunner(client *codersdk.Client, metrics Metrics, cfg Config) *Runner {
2728
client.Trace = cfg.Trace
29+
if cfg.WaitLoaded == nil {
30+
cfg.WaitLoaded = waitForWorkspacesPageLoaded
31+
}
32+
if cfg.ActionFunc == nil {
33+
cfg.ActionFunc = clickRandomElement
34+
}
35+
if cfg.Screenshot == nil {
36+
cfg.Screenshot = screenshot
37+
}
2838
return &Runner{
2939
client: client,
3040
cfg: cfg,
@@ -33,6 +43,16 @@ func NewRunner(client *codersdk.Client, metrics Metrics, cfg Config) *Runner {
3343
}
3444

3545
func (r *Runner) Run(ctx context.Context, _ string, _ io.Writer) error {
46+
err := r._run(ctx)
47+
// If the context deadline exceeded, don't return an error.
48+
// This just means the test finished.
49+
if err == nil || errors.Is(err, context.DeadlineExceeded) {
50+
return nil
51+
}
52+
return err
53+
}
54+
55+
func (r *Runner) _run(ctx context.Context) error {
3656
if r.client == nil {
3757
return xerrors.Errorf("client is nil")
3858
}
@@ -55,7 +75,7 @@ func (r *Runner) Run(ctx context.Context, _ string, _ io.Writer) error {
5575
defer t.Stop()
5676
r.cfg.Logger.Info(ctx, "waiting for workspaces page to load")
5777
loadWorkspacePageDeadline := time.Now().Add(r.cfg.Interval)
58-
if err := waitForWorkspacesPageLoaded(cdpCtx, loadWorkspacePageDeadline); err != nil {
78+
if err := r.cfg.WaitLoaded(cdpCtx, loadWorkspacePageDeadline); err != nil {
5979
return xerrors.Errorf("wait for workspaces page to load: %w", err)
6080
}
6181
for {
@@ -73,7 +93,7 @@ func (r *Runner) Run(ctx context.Context, _ string, _ io.Writer) error {
7393
l, act, err := r.cfg.ActionFunc(cdpCtx, r.cfg.Logger, r.cfg.RandIntn, actionCompleteByDeadline)
7494
if err != nil {
7595
r.cfg.Logger.Error(ctx, "calling ActionFunc", slog.Error(err))
76-
sPath, sErr := screenshot(cdpCtx, me.Username)
96+
sPath, sErr := r.cfg.Screenshot(cdpCtx, me.Username)
7797
if sErr != nil {
7898
r.cfg.Logger.Error(ctx, "screenshot failed", slog.Error(sErr))
7999
}
@@ -88,7 +108,7 @@ func (r *Runner) Run(ctx context.Context, _ string, _ io.Writer) error {
88108
r.metrics.IncErrors(string(l))
89109
//nolint:gocritic
90110
r.cfg.Logger.Error(ctx, "action failed", slog.F("label", l), slog.Error(err))
91-
sPath, sErr := screenshot(cdpCtx, me.Username+"-"+string(l))
111+
sPath, sErr := r.cfg.Screenshot(cdpCtx, me.Username+"-"+string(l))
92112
if sErr != nil {
93113
r.cfg.Logger.Error(ctx, "screenshot failed", slog.Error(sErr))
94114
}

scaletest/dashboard/run_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"math/rand"
66
"runtime"
77
"sync"
8+
"sync/atomic"
89
"testing"
910
"time"
1011

@@ -47,17 +48,29 @@ func Test_Run(t *testing.T) {
4748
IgnoreErrors: true,
4849
})
4950
m := &testMetrics{}
51+
var (
52+
waitLoadedCalled atomic.Bool
53+
screenshotCalled atomic.Bool
54+
)
5055
cfg := dashboard.Config{
5156
Interval: 500 * time.Millisecond,
5257
Jitter: 100 * time.Millisecond,
5358
Logger: log,
5459
Headless: true,
60+
WaitLoaded: func(_ context.Context, _ time.Time) error {
61+
waitLoadedCalled.Store(true)
62+
return nil
63+
},
5564
ActionFunc: func(_ context.Context, _ slog.Logger, rnd func(int) int, _ time.Time) (dashboard.Label, dashboard.Action, error) {
5665
if rnd(2) == 0 {
5766
return "fails", failAction, nil
5867
}
5968
return "succeeds", successAction, nil
6069
},
70+
Screenshot: func(_ context.Context, name string) (string, error) {
71+
screenshotCalled.Store(true)
72+
return "/fake/path/to/" + name + ".png", nil
73+
},
6174
RandIntn: rg.Intn,
6275
}
6376
r := dashboard.NewRunner(client, m, cfg)

0 commit comments

Comments
 (0)