run all tests as separate tabs on one browser

This vastly speeds up 'go test' on my laptop from ~10s to ~3s, as we
save a lot of time spinning up new Chrome browser processes.

In practice, each tab is a separate process anyway, but there's a lot of
added overhead if we're firing up the entire browser, particularly with
an empty user data dir.

This makes 'go test' racy now, as Browser doesn't support creating tabs
concurrently right now. Follow-up commits will fix that, with the help
of 'go test -race' after this commit.
This commit is contained in:
Daniel Martí 2019-04-01 14:20:41 +01:00
parent 661ef78880
commit 117274bc5d

View File

@ -11,7 +11,7 @@ import (
var ( var (
testdataDir string testdataDir string
allocCtx context.Context browserCtx context.Context
allocOpts = []ExecAllocatorOption{ allocOpts = []ExecAllocatorOption{
NoFirstRun, NoFirstRun,
@ -22,7 +22,9 @@ var (
) )
func testAllocate(t *testing.T, path string) (_ context.Context, cancel func()) { func testAllocate(t *testing.T, path string) (_ context.Context, cancel func()) {
ctx, cancel := NewContext(allocCtx) // Same browser, new tab; not needing to start new chrome browsers for
// each test gives a huge speed-up.
ctx, cancel := NewContext(browserCtx)
// Only navigate if we want a path, otherwise leave the blank page. // Only navigate if we want a path, otherwise leave the blank page.
if path != "" { if path != "" {
@ -61,12 +63,17 @@ func TestMain(m *testing.M) {
allocOpts = append(allocOpts, NoSandbox) allocOpts = append(allocOpts, NoSandbox)
} }
ctx, cancel := NewAllocator(context.Background(), WithExecAllocator(allocOpts...)) allocCtx, cancel := NewAllocator(context.Background(), WithExecAllocator(allocOpts...))
allocCtx = ctx
// start the browser
browserCtx, _ = NewContext(allocCtx)
if err := Run(browserCtx, Tasks{}); err != nil {
panic(err)
}
code := m.Run() code := m.Run()
cancel() cancel()
FromContext(ctx).Allocator.Wait() FromContext(allocCtx).Allocator.Wait()
os.Exit(code) os.Exit(code)
} }