tests: improve wasm tests slightly

These wasm tests weren't passing in GitHub Actions and also weren't
passing on my laptop. I'm not sure why, I think there are a few race
conditions that are going on.

This commit attempts to fix this at least to a degree:

  - The context deadline is increased from 5 seconds to 10 seconds.
  - The tests are not running in parallel anymore.
  - Some `Sleep` calls were removed, they do not appear to be necessary
    (and if they were, sleeping is the wrong solution to solve race
    conditions).

Overall the tests are taking a few seconds more, but on the other hand
they seem to be passing more reliable. At least for me, on my laptop
(and hopefully also in CI).
Этот коммит содержится в:
Ayke van Laethem 2021-11-20 01:06:02 +01:00 коммит произвёл Ron Evans
родитель 470cbd5f53
коммит 1d2c17753a
6 изменённых файлов: 16 добавлений и 37 удалений

Просмотреть файл

@ -2,23 +2,20 @@ package wasm
import ( import (
"testing" "testing"
"time"
"github.com/chromedp/chromedp" "github.com/chromedp/chromedp"
) )
func TestChan(t *testing.T) { func TestChan(t *testing.T) {
t.Parallel()
wasmTmpDir, server := startServer(t) wasmTmpDir, server := startServer(t)
err := run("tinygo build -o " + wasmTmpDir + "/chan.wasm -target wasm testdata/chan.go") err := run(t, "tinygo build -o "+wasmTmpDir+"/chan.wasm -target wasm testdata/chan.go")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ctx, cancel := chromectx(5 * time.Second) ctx, cancel := chromectx()
defer cancel() defer cancel()
err = chromedp.Run(ctx, err = chromedp.Run(ctx,

Просмотреть файл

@ -2,35 +2,30 @@ package wasm
import ( import (
"testing" "testing"
"time"
"github.com/chromedp/chromedp" "github.com/chromedp/chromedp"
) )
func TestEvent(t *testing.T) { func TestEvent(t *testing.T) {
t.Parallel()
wasmTmpDir, server := startServer(t) wasmTmpDir, server := startServer(t)
err := run("tinygo build -o " + wasmTmpDir + "/event.wasm -target wasm testdata/event.go") err := run(t, "tinygo build -o "+wasmTmpDir+"/event.wasm -target wasm testdata/event.go")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ctx, cancel := chromectx(5 * time.Second) ctx, cancel := chromectx()
defer cancel() defer cancel()
var log1, log2 string var log1, log2 string
err = chromedp.Run(ctx, err = chromedp.Run(ctx,
chromedp.Navigate(server.URL+"/run?file=event.wasm"), chromedp.Navigate(server.URL+"/run?file=event.wasm"),
chromedp.WaitVisible("#log"), chromedp.WaitVisible("#log"),
chromedp.Sleep(time.Second),
chromedp.InnerHTML("#log", &log1), chromedp.InnerHTML("#log", &log1),
waitLog(`1 waitLog(`1
4`), 4`),
chromedp.Click("#testbtn"), chromedp.Click("#testbtn"),
chromedp.Sleep(time.Second),
chromedp.InnerHTML("#log", &log2), chromedp.InnerHTML("#log", &log2),
waitLog(`1 waitLog(`1
4 4

Просмотреть файл

@ -2,29 +2,25 @@ package wasm
import ( import (
"testing" "testing"
"time"
"github.com/chromedp/chromedp" "github.com/chromedp/chromedp"
) )
func TestFmt(t *testing.T) { func TestFmt(t *testing.T) {
t.Parallel()
wasmTmpDir, server := startServer(t) wasmTmpDir, server := startServer(t)
err := run("tinygo build -o " + wasmTmpDir + "/fmt.wasm -target wasm testdata/fmt.go") err := run(t, "tinygo build -o "+wasmTmpDir+"/fmt.wasm -target wasm testdata/fmt.go")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ctx, cancel := chromectx(5 * time.Second) ctx, cancel := chromectx()
defer cancel() defer cancel()
var log1 string var log1 string
err = chromedp.Run(ctx, err = chromedp.Run(ctx,
chromedp.Navigate(server.URL+"/run?file=fmt.wasm"), chromedp.Navigate(server.URL+"/run?file=fmt.wasm"),
chromedp.Sleep(time.Second),
chromedp.InnerHTML("#log", &log1), chromedp.InnerHTML("#log", &log1),
waitLog(`did not panic`), waitLog(`did not panic`),
) )

Просмотреть файл

@ -2,29 +2,25 @@ package wasm
import ( import (
"testing" "testing"
"time"
"github.com/chromedp/chromedp" "github.com/chromedp/chromedp"
) )
func TestFmtprint(t *testing.T) { func TestFmtprint(t *testing.T) {
t.Parallel()
wasmTmpDir, server := startServer(t) wasmTmpDir, server := startServer(t)
err := run("tinygo build -o " + wasmTmpDir + "/fmtprint.wasm -target wasm testdata/fmtprint.go") err := run(t, "tinygo build -o "+wasmTmpDir+"/fmtprint.wasm -target wasm testdata/fmtprint.go")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ctx, cancel := chromectx(5 * time.Second) ctx, cancel := chromectx()
defer cancel() defer cancel()
var log1 string var log1 string
err = chromedp.Run(ctx, err = chromedp.Run(ctx,
chromedp.Navigate(server.URL+"/run?file=fmtprint.wasm"), chromedp.Navigate(server.URL+"/run?file=fmtprint.wasm"),
chromedp.Sleep(time.Second),
chromedp.InnerHTML("#log", &log1), chromedp.InnerHTML("#log", &log1),
waitLog(`test from fmtprint 1 waitLog(`test from fmtprint 1
test from fmtprint 2 test from fmtprint 2

Просмотреть файл

@ -2,29 +2,25 @@ package wasm
import ( import (
"testing" "testing"
"time"
"github.com/chromedp/chromedp" "github.com/chromedp/chromedp"
) )
func TestLog(t *testing.T) { func TestLog(t *testing.T) {
t.Parallel()
wasmTmpDir, server := startServer(t) wasmTmpDir, server := startServer(t)
err := run("tinygo build -o " + wasmTmpDir + "/log.wasm -target wasm testdata/log.go") err := run(t, "tinygo build -o "+wasmTmpDir+"/log.wasm -target wasm testdata/log.go")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
ctx, cancel := chromectx(5 * time.Second) ctx, cancel := chromectx()
defer cancel() defer cancel()
var log1 string var log1 string
err = chromedp.Run(ctx, err = chromedp.Run(ctx,
chromedp.Navigate(server.URL+"/run?file=log.wasm"), chromedp.Navigate(server.URL+"/run?file=log.wasm"),
chromedp.Sleep(time.Second),
chromedp.InnerHTML("#log", &log1), chromedp.InnerHTML("#log", &log1),
waitLogRe(`^..../../.. ..:..:.. log 1 waitLogRe(`^..../../.. ..:..:.. log 1
..../../.. ..:..:.. log 2 ..../../.. ..:..:.. log 2

Просмотреть файл

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os/exec" "os/exec"
@ -18,29 +17,29 @@ import (
"github.com/chromedp/chromedp" "github.com/chromedp/chromedp"
) )
func run(cmdline string) error { func run(t *testing.T, cmdline string) error {
args := strings.Fields(cmdline) args := strings.Fields(cmdline)
return runargs(args...) return runargs(t, args...)
} }
func runargs(args ...string) error { func runargs(t *testing.T, args ...string) error {
cmd := exec.Command(args[0], args[1:]...) cmd := exec.Command(args[0], args[1:]...)
b, err := cmd.CombinedOutput() b, err := cmd.CombinedOutput()
log.Printf("Command: %s; err=%v; full output:\n%s", strings.Join(args, " "), err, b) t.Logf("Command: %s; err=%v; full output:\n%s", strings.Join(args, " "), err, b)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
func chromectx(timeout time.Duration) (context.Context, context.CancelFunc) { func chromectx() (context.Context, context.CancelFunc) {
var ctx context.Context var ctx context.Context
// looks for locally installed Chrome // looks for locally installed Chrome
ctx, _ = chromedp.NewContext(context.Background()) ctx, _ = chromedp.NewContext(context.Background())
ctx, cancel := context.WithTimeout(ctx, timeout) ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
return ctx, cancel return ctx, cancel
} }