Blame 0007-tests-improve-wasm-tests-slightly.patch

03e3a66
From f5ece39b3353ae389ab3da90163b6a49dda1d4aa Mon Sep 17 00:00:00 2001
03e3a66
From: Ayke van Laethem <aykevanlaethem@gmail.com>
03e3a66
Date: Sat, 20 Nov 2021 01:06:02 +0100
03e3a66
Subject: [PATCH 7/9] tests: improve wasm tests slightly
03e3a66
03e3a66
These wasm tests weren't passing in GitHub Actions and also weren't
03e3a66
passing on my laptop. I'm not sure why, I think there are a few race
03e3a66
conditions that are going on.
03e3a66
03e3a66
This commit attempts to fix this at least to a degree:
03e3a66
03e3a66
  - The context deadline is increased from 5 seconds to 10 seconds.
03e3a66
  - The tests are not running in parallel anymore.
03e3a66
  - Some `Sleep` calls were removed, they do not appear to be necessary
03e3a66
    (and if they were, sleeping is the wrong solution to solve race
03e3a66
    conditions).
03e3a66
03e3a66
Overall the tests are taking a few seconds more, but on the other hand
03e3a66
they seem to be passing more reliable. At least for me, on my laptop
03e3a66
(and hopefully also in CI).
03e3a66
03e3a66
Signed-off-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
03e3a66
---
03e3a66
 tests/wasm/chan_test.go     |  7 ++-----
03e3a66
 tests/wasm/event_test.go    |  9 ++-------
03e3a66
 tests/wasm/fmt_test.go      |  8 ++------
03e3a66
 tests/wasm/fmtprint_test.go |  8 ++------
03e3a66
 tests/wasm/log_test.go      |  8 ++------
03e3a66
 tests/wasm/setup_test.go    | 13 ++++++-------
03e3a66
 6 files changed, 16 insertions(+), 37 deletions(-)
03e3a66
03e3a66
diff --git a/tests/wasm/chan_test.go b/tests/wasm/chan_test.go
03e3a66
index e44d7ebe..fe981974 100644
03e3a66
--- a/tests/wasm/chan_test.go
03e3a66
+++ b/tests/wasm/chan_test.go
03e3a66
@@ -2,23 +2,20 @@ package wasm
03e3a66
 
03e3a66
 import (
03e3a66
 	"testing"
03e3a66
-	"time"
03e3a66
 
03e3a66
 	"github.com/chromedp/chromedp"
03e3a66
 )
03e3a66
 
03e3a66
 func TestChan(t *testing.T) {
03e3a66
 
03e3a66
-	t.Parallel()
03e3a66
-
03e3a66
 	wasmTmpDir, server := startServer(t)
03e3a66
 
03e3a66
-	err := run("tinygo build -o " + wasmTmpDir + "/chan.wasm -target wasm testdata/chan.go")
03e3a66
+	err := run(t, "tinygo build -o "+wasmTmpDir+"/chan.wasm -target wasm testdata/chan.go")
03e3a66
 	if err != nil {
03e3a66
 		t.Fatal(err)
03e3a66
 	}
03e3a66
 
03e3a66
-	ctx, cancel := chromectx(5 * time.Second)
03e3a66
+	ctx, cancel := chromectx()
03e3a66
 	defer cancel()
03e3a66
 
03e3a66
 	err = chromedp.Run(ctx,
03e3a66
diff --git a/tests/wasm/event_test.go b/tests/wasm/event_test.go
03e3a66
index 038a500a..a29a01c7 100644
03e3a66
--- a/tests/wasm/event_test.go
03e3a66
+++ b/tests/wasm/event_test.go
03e3a66
@@ -2,35 +2,30 @@ package wasm
03e3a66
 
03e3a66
 import (
03e3a66
 	"testing"
03e3a66
-	"time"
03e3a66
 
03e3a66
 	"github.com/chromedp/chromedp"
03e3a66
 )
03e3a66
 
03e3a66
 func TestEvent(t *testing.T) {
03e3a66
 
03e3a66
-	t.Parallel()
03e3a66
-
03e3a66
 	wasmTmpDir, server := startServer(t)
03e3a66
 
03e3a66
-	err := run("tinygo build -o " + wasmTmpDir + "/event.wasm -target wasm testdata/event.go")
03e3a66
+	err := run(t, "tinygo build -o "+wasmTmpDir+"/event.wasm -target wasm testdata/event.go")
03e3a66
 	if err != nil {
03e3a66
 		t.Fatal(err)
03e3a66
 	}
03e3a66
 
03e3a66
-	ctx, cancel := chromectx(5 * time.Second)
03e3a66
+	ctx, cancel := chromectx()
03e3a66
 	defer cancel()
03e3a66
 
03e3a66
 	var log1, log2 string
03e3a66
 	err = chromedp.Run(ctx,
03e3a66
 		chromedp.Navigate(server.URL+"/run?file=event.wasm"),
03e3a66
 		chromedp.WaitVisible("#log"),
03e3a66
-		chromedp.Sleep(time.Second),
03e3a66
 		chromedp.InnerHTML("#log", &log1),
03e3a66
 		waitLog(`1
03e3a66
 4`),
03e3a66
 		chromedp.Click("#testbtn"),
03e3a66
-		chromedp.Sleep(time.Second),
03e3a66
 		chromedp.InnerHTML("#log", &log2),
03e3a66
 		waitLog(`1
03e3a66
 4
03e3a66
diff --git a/tests/wasm/fmt_test.go b/tests/wasm/fmt_test.go
03e3a66
index f9f2f77b..d3695f07 100644
03e3a66
--- a/tests/wasm/fmt_test.go
03e3a66
+++ b/tests/wasm/fmt_test.go
03e3a66
@@ -2,29 +2,25 @@ package wasm
03e3a66
 
03e3a66
 import (
03e3a66
 	"testing"
03e3a66
-	"time"
03e3a66
 
03e3a66
 	"github.com/chromedp/chromedp"
03e3a66
 )
03e3a66
 
03e3a66
 func TestFmt(t *testing.T) {
03e3a66
 
03e3a66
-	t.Parallel()
03e3a66
-
03e3a66
 	wasmTmpDir, server := startServer(t)
03e3a66
 
03e3a66
-	err := run("tinygo build -o " + wasmTmpDir + "/fmt.wasm -target wasm testdata/fmt.go")
03e3a66
+	err := run(t, "tinygo build -o "+wasmTmpDir+"/fmt.wasm -target wasm testdata/fmt.go")
03e3a66
 	if err != nil {
03e3a66
 		t.Fatal(err)
03e3a66
 	}
03e3a66
 
03e3a66
-	ctx, cancel := chromectx(5 * time.Second)
03e3a66
+	ctx, cancel := chromectx()
03e3a66
 	defer cancel()
03e3a66
 
03e3a66
 	var log1 string
03e3a66
 	err = chromedp.Run(ctx,
03e3a66
 		chromedp.Navigate(server.URL+"/run?file=fmt.wasm"),
03e3a66
-		chromedp.Sleep(time.Second),
03e3a66
 		chromedp.InnerHTML("#log", &log1),
03e3a66
 		waitLog(`did not panic`),
03e3a66
 	)
03e3a66
diff --git a/tests/wasm/fmtprint_test.go b/tests/wasm/fmtprint_test.go
03e3a66
index 90825ba0..3c750239 100644
03e3a66
--- a/tests/wasm/fmtprint_test.go
03e3a66
+++ b/tests/wasm/fmtprint_test.go
03e3a66
@@ -2,29 +2,25 @@ package wasm
03e3a66
 
03e3a66
 import (
03e3a66
 	"testing"
03e3a66
-	"time"
03e3a66
 
03e3a66
 	"github.com/chromedp/chromedp"
03e3a66
 )
03e3a66
 
03e3a66
 func TestFmtprint(t *testing.T) {
03e3a66
 
03e3a66
-	t.Parallel()
03e3a66
-
03e3a66
 	wasmTmpDir, server := startServer(t)
03e3a66
 
03e3a66
-	err := run("tinygo build -o " + wasmTmpDir + "/fmtprint.wasm -target wasm testdata/fmtprint.go")
03e3a66
+	err := run(t, "tinygo build -o "+wasmTmpDir+"/fmtprint.wasm -target wasm testdata/fmtprint.go")
03e3a66
 	if err != nil {
03e3a66
 		t.Fatal(err)
03e3a66
 	}
03e3a66
 
03e3a66
-	ctx, cancel := chromectx(5 * time.Second)
03e3a66
+	ctx, cancel := chromectx()
03e3a66
 	defer cancel()
03e3a66
 
03e3a66
 	var log1 string
03e3a66
 	err = chromedp.Run(ctx,
03e3a66
 		chromedp.Navigate(server.URL+"/run?file=fmtprint.wasm"),
03e3a66
-		chromedp.Sleep(time.Second),
03e3a66
 		chromedp.InnerHTML("#log", &log1),
03e3a66
 		waitLog(`test from fmtprint 1
03e3a66
 test from fmtprint 2
03e3a66
diff --git a/tests/wasm/log_test.go b/tests/wasm/log_test.go
03e3a66
index fae4c670..1f6c79fe 100644
03e3a66
--- a/tests/wasm/log_test.go
03e3a66
+++ b/tests/wasm/log_test.go
03e3a66
@@ -2,29 +2,25 @@ package wasm
03e3a66
 
03e3a66
 import (
03e3a66
 	"testing"
03e3a66
-	"time"
03e3a66
 
03e3a66
 	"github.com/chromedp/chromedp"
03e3a66
 )
03e3a66
 
03e3a66
 func TestLog(t *testing.T) {
03e3a66
 
03e3a66
-	t.Parallel()
03e3a66
-
03e3a66
 	wasmTmpDir, server := startServer(t)
03e3a66
 
03e3a66
-	err := run("tinygo build -o " + wasmTmpDir + "/log.wasm -target wasm testdata/log.go")
03e3a66
+	err := run(t, "tinygo build -o "+wasmTmpDir+"/log.wasm -target wasm testdata/log.go")
03e3a66
 	if err != nil {
03e3a66
 		t.Fatal(err)
03e3a66
 	}
03e3a66
 
03e3a66
-	ctx, cancel := chromectx(5 * time.Second)
03e3a66
+	ctx, cancel := chromectx()
03e3a66
 	defer cancel()
03e3a66
 
03e3a66
 	var log1 string
03e3a66
 	err = chromedp.Run(ctx,
03e3a66
 		chromedp.Navigate(server.URL+"/run?file=log.wasm"),
03e3a66
-		chromedp.Sleep(time.Second),
03e3a66
 		chromedp.InnerHTML("#log", &log1),
03e3a66
 		waitLogRe(`^..../../.. ..:..:.. log 1
03e3a66
 ..../../.. ..:..:.. log 2
03e3a66
diff --git a/tests/wasm/setup_test.go b/tests/wasm/setup_test.go
03e3a66
index 0071076c..e5a18daf 100644
03e3a66
--- a/tests/wasm/setup_test.go
03e3a66
+++ b/tests/wasm/setup_test.go
03e3a66
@@ -4,7 +4,6 @@ import (
03e3a66
 	"context"
03e3a66
 	"errors"
03e3a66
 	"fmt"
03e3a66
-	"log"
03e3a66
 	"net/http"
03e3a66
 	"net/http/httptest"
03e3a66
 	"os/exec"
03e3a66
@@ -18,29 +17,29 @@ import (
03e3a66
 	"github.com/chromedp/chromedp"
03e3a66
 )
03e3a66
 
03e3a66
-func run(cmdline string) error {
03e3a66
+func run(t *testing.T, cmdline string) error {
03e3a66
 	args := strings.Fields(cmdline)
03e3a66
-	return runargs(args...)
03e3a66
+	return runargs(t, args...)
03e3a66
 }
03e3a66
 
03e3a66
-func runargs(args ...string) error {
03e3a66
+func runargs(t *testing.T, args ...string) error {
03e3a66
 	cmd := exec.Command(args[0], args[1:]...)
03e3a66
 	b, err := cmd.CombinedOutput()
03e3a66
-	log.Printf("Command: %s; err=%v; full output:\n%s", strings.Join(args, " "), err, b)
03e3a66
+	t.Logf("Command: %s; err=%v; full output:\n%s", strings.Join(args, " "), err, b)
03e3a66
 	if err != nil {
03e3a66
 		return err
03e3a66
 	}
03e3a66
 	return nil
03e3a66
 }
03e3a66
 
03e3a66
-func chromectx(timeout time.Duration) (context.Context, context.CancelFunc) {
03e3a66
+func chromectx() (context.Context, context.CancelFunc) {
03e3a66
 
03e3a66
 	var ctx context.Context
03e3a66
 
03e3a66
 	// looks for locally installed Chrome
03e3a66
 	ctx, _ = chromedp.NewContext(context.Background())
03e3a66
 
03e3a66
-	ctx, cancel := context.WithTimeout(ctx, timeout)
03e3a66
+	ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
03e3a66
 
03e3a66
 	return ctx, cancel
03e3a66
 }
03e3a66
-- 
03e3a66
2.31.1
03e3a66