From 117274bc5da3ad6c297e097e278bbf945930dd87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Mon, 1 Apr 2019 14:20:41 +0100 Subject: [PATCH] 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. --- chromedp_test.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/chromedp_test.go b/chromedp_test.go index e2baa44..cd343de 100644 --- a/chromedp_test.go +++ b/chromedp_test.go @@ -11,7 +11,7 @@ import ( var ( testdataDir string - allocCtx context.Context + browserCtx context.Context allocOpts = []ExecAllocatorOption{ NoFirstRun, @@ -22,7 +22,9 @@ var ( ) 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. if path != "" { @@ -61,12 +63,17 @@ func TestMain(m *testing.M) { allocOpts = append(allocOpts, NoSandbox) } - ctx, cancel := NewAllocator(context.Background(), WithExecAllocator(allocOpts...)) - allocCtx = ctx + allocCtx, cancel := NewAllocator(context.Background(), WithExecAllocator(allocOpts...)) + + // start the browser + browserCtx, _ = NewContext(allocCtx) + if err := Run(browserCtx, Tasks{}); err != nil { + panic(err) + } code := m.Run() cancel() - FromContext(ctx).Allocator.Wait() + FromContext(allocCtx).Allocator.Wait() os.Exit(code) }