Skip to content

Commit

Permalink
Add browser registry test
Browse files Browse the repository at this point in the history
  • Loading branch information
ka3de committed Jul 5, 2023
1 parent 2c2f71d commit 764f815
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
8 changes: 8 additions & 0 deletions browser/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"

"github.com/grafana/xk6-browser/api"
"github.com/grafana/xk6-browser/chromium"
Expand Down Expand Up @@ -164,6 +165,8 @@ type browserRegistry struct {
m map[int64]api.Browser

buildFn browserBuildFunc

stopped atomic.Bool // testing purposes
}

type browserBuildFunc func(ctx context.Context) (api.Browser, error)
Expand Down Expand Up @@ -236,6 +239,7 @@ func (r *browserRegistry) handleIterEvents(eventsCh <-chan *k6event.Event, unsub
// unsubscribe for the VU events and exit the loop in order to reduce unuseful overhead.
if !isBrowserIter(r.vu) {
unsubscribeFn()
r.stop()
e.Done()
return
}
Expand Down Expand Up @@ -318,6 +322,10 @@ func (r *browserRegistry) clear() {
}
}

func (r *browserRegistry) stop() {
r.stopped.Store(true)
}

func isBrowserIter(vu k6modules.VU) bool {
opts := k6ext.GetScenarioOpts(vu.Context(), vu)
_, ok := opts["type"] // Check if browser type option is set
Expand Down
92 changes: 92 additions & 0 deletions browser/registry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package browser

import (
"context"
"errors"
"strconv"
"sync"
Expand All @@ -10,6 +11,9 @@ import (
"github.com/stretchr/testify/require"

"github.com/grafana/xk6-browser/env"
"github.com/grafana/xk6-browser/k6ext/k6test"

k6event "go.k6.io/k6/event"
)

func TestPidRegistry(t *testing.T) {
Expand Down Expand Up @@ -182,3 +186,91 @@ func TestIsRemoteBrowser(t *testing.T) {
require.Equal(t, "WS_URL_2", wsURL)
})
}

func TestBrowserRegistry(t *testing.T) {
t.Parallel()

remoteRegistry, err := newRemoteRegistry(func(key string) (string, bool) {
// No env vars
return "", false
})
require.NoError(t, err)

t.Run("init_and_close_browsers_on_iter_events", func(t *testing.T) {
t.Parallel()

vu := k6test.NewVU(t)
browserRegistry := newBrowserRegistry(vu, remoteRegistry, &pidRegistry{})

vu.ActivateVU()

// Send a few IterStart events
vu.StartIteration(t, k6test.WithIteration(0))
vu.StartIteration(t, k6test.WithIteration(1))
vu.StartIteration(t, k6test.WithIteration(2))

// Verify browsers are initialized
browserRegistry.mu.RLock()
assert.Equal(t, 3, len(browserRegistry.m))
browserRegistry.mu.RUnlock()

// Send IterEnd events
vu.EndIteration(t, k6test.WithIteration(0))
vu.EndIteration(t, k6test.WithIteration(1))
vu.EndIteration(t, k6test.WithIteration(2))

// Verify there are no browsers left
browserRegistry.mu.RLock()
assert.Equal(t, 0, len(browserRegistry.m))
browserRegistry.mu.RUnlock()
})

t.Run("close_browsers_on_exit_event", func(t *testing.T) {
t.Parallel()

vu := k6test.NewVU(t)
browserRegistry := newBrowserRegistry(vu, remoteRegistry, &pidRegistry{})

vu.ActivateVU()

// Send a few IterStart events
vu.StartIteration(t, k6test.WithIteration(0))
vu.StartIteration(t, k6test.WithIteration(1))
vu.StartIteration(t, k6test.WithIteration(2))

// Verify browsers are initialized
browserRegistry.mu.RLock()
assert.Equal(t, 3, len(browserRegistry.m))
browserRegistry.mu.RUnlock()

// Send Exit event
events, ok := vu.EventsField.Global.(*k6event.System)
require.True(t, ok, "want *k6event.System; got %T", events)
waitDone := events.Emit(&k6event.Event{
Type: k6event.Exit,
})
require.NoError(t, waitDone(context.Background()), "error waiting on Exit done")

// Verify there are no browsers left
browserRegistry.mu.RLock()
assert.Equal(t, 0, len(browserRegistry.m))
browserRegistry.mu.RUnlock()
})

t.Run("unsubscribe_on_non_browser_vu", func(t *testing.T) {
t.Parallel()

vu := k6test.NewVU(t)
browserRegistry := newBrowserRegistry(vu, remoteRegistry, &pidRegistry{})

vu.ActivateVU()

// Unset browser type option in scenario options in order to represent that VU is not
// a browser test VU
delete(vu.StateField.Options.Scenarios["default"].GetScenarioOptions().Browser, "type")

vu.StartIteration(t)

assert.True(t, browserRegistry.stopped.Load())
})
}

0 comments on commit 764f815

Please sign in to comment.