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 Jun 22, 2023
1 parent 42bc287 commit 07702be
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions browser/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"

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

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

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

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

// Initialize browser registry and start listening for events
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()
}

0 comments on commit 07702be

Please sign in to comment.