Use httptest to serve wasm test files.

This picks a port automatically, so avoids any conflicts that might
arise from running the tests in parallel.
Этот коммит содержится в:
Elliott Sales de Andrade 2020-12-22 17:56:53 -05:00 коммит произвёл Ron Evans
родитель 5bae55d755
коммит f3bdebe2a6
6 изменённых файлов: 40 добавлений и 34 удалений

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

@ -11,6 +11,9 @@ func TestChan(t *testing.T) {
t.Parallel()
wasmTmpDir, server, cleanup := startServer(t)
defer cleanup()
err := run("tinygo build -o " + wasmTmpDir + "/chan.wasm -target wasm testdata/chan.go")
if err != nil {
t.Fatal(err)
@ -20,7 +23,7 @@ func TestChan(t *testing.T) {
defer cancel()
err = chromedp.Run(ctx,
chromedp.Navigate("http://localhost:8826/run?file=chan.wasm"),
chromedp.Navigate(server.URL+"/run?file=chan.wasm"),
waitLog(`1
2
4

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

@ -13,6 +13,9 @@ func TestEvent(t *testing.T) {
t.Parallel()
wasmTmpDir, server, cleanup := startServer(t)
defer cleanup()
err := run("tinygo build -o " + wasmTmpDir + "/event.wasm -target wasm testdata/event.go")
if err != nil {
t.Fatal(err)
@ -23,7 +26,7 @@ func TestEvent(t *testing.T) {
var log1, log2 string
err = chromedp.Run(ctx,
chromedp.Navigate("http://localhost:8826/run?file=event.wasm"),
chromedp.Navigate(server.URL+"/run?file=event.wasm"),
chromedp.WaitVisible("#log"),
chromedp.Sleep(time.Second),
chromedp.InnerHTML("#log", &log1),

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

@ -20,6 +20,9 @@ func TestFmt(t *testing.T) {
t.Parallel()
wasmTmpDir, server, cleanup := startServer(t)
defer cleanup()
err := run("tinygo build -o " + wasmTmpDir + "/fmt.wasm -target wasm testdata/fmt.go")
if err != nil {
t.Fatal(err)
@ -30,7 +33,7 @@ func TestFmt(t *testing.T) {
var log1 string
err = chromedp.Run(ctx,
chromedp.Navigate("http://localhost:8826/run?file=fmt.wasm"),
chromedp.Navigate(server.URL+"/run?file=fmt.wasm"),
chromedp.Sleep(time.Second),
chromedp.InnerHTML("#log", &log1),
waitLog(`did not panic`),

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

@ -13,6 +13,9 @@ func TestFmtprint(t *testing.T) {
t.Parallel()
wasmTmpDir, server, cleanup := startServer(t)
defer cleanup()
err := run("tinygo build -o " + wasmTmpDir + "/fmtprint.wasm -target wasm testdata/fmtprint.go")
if err != nil {
t.Fatal(err)
@ -23,7 +26,7 @@ func TestFmtprint(t *testing.T) {
var log1 string
err = chromedp.Run(ctx,
chromedp.Navigate("http://localhost:8826/run?file=fmtprint.wasm"),
chromedp.Navigate(server.URL+"/run?file=fmtprint.wasm"),
chromedp.Sleep(time.Second),
chromedp.InnerHTML("#log", &log1),
waitLog(`test from fmtprint 1

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

@ -13,6 +13,9 @@ func TestLog(t *testing.T) {
t.Parallel()
wasmTmpDir, server, cleanup := startServer(t)
defer cleanup()
err := run("tinygo build -o " + wasmTmpDir + "/log.wasm -target wasm testdata/log.go")
if err != nil {
t.Fatal(err)
@ -23,7 +26,7 @@ func TestLog(t *testing.T) {
var log1 string
err = chromedp.Run(ctx,
chromedp.Navigate("http://localhost:8826/run?file=log.wasm"),
chromedp.Navigate(server.URL+"/run?file=log.wasm"),
chromedp.Sleep(time.Second),
chromedp.InnerHTML("#log", &log1),
waitLogRe(`^..../../.. ..:..:.. log 1

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

@ -3,11 +3,11 @@ package wasm
import (
"context"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"regexp"
@ -20,29 +20,6 @@ import (
"github.com/chromedp/chromedp"
)
var addr = flag.String("addr", ":8826", "Host:port to listen on for wasm test server")
var wasmTmpDir string // set in TestMain to a temp directory for build output
func TestMain(m *testing.M) {
flag.Parse()
os.Exit(func() int {
var err error
wasmTmpDir, err = ioutil.TempDir("", "wasm_test")
if err != nil {
log.Fatalf("unable to create temp dir: %v", err)
}
defer os.RemoveAll(wasmTmpDir) // cleanup even on panic and before os.Exit
startServer(wasmTmpDir)
return m.Run()
}())
}
func run(cmdline string) error {
args := strings.Fields(cmdline)
return runargs(args...)
@ -70,7 +47,12 @@ func chromectx(timeout time.Duration) (context.Context, context.CancelFunc) {
return ctx, cancel
}
func startServer(tmpDir string) {
func startServer(t *testing.T) (string, *httptest.Server, func()) {
// In Go 1.15, all this can be replaced by t.TempDir()
tmpDir, err := ioutil.TempDir("", "wasm_test")
if err != nil {
t.Fatalf("unable to create temp dir: %v", err)
}
fsh := http.FileServer(http.Dir(tmpDir))
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -140,11 +122,20 @@ if (wasmSupported) {
fsh.ServeHTTP(w, r)
})
log.Printf("Starting server at %q for dir: %s", *addr, tmpDir)
go func() {
log.Fatal(http.ListenAndServe(*addr, h))
}()
server := httptest.NewServer(h)
t.Logf("Started server at %q for dir: %s", server.URL, tmpDir)
// In Go 1.14+, this can be replaced by t.Cleanup()
cleanup := func() {
err := os.RemoveAll(tmpDir)
if err != nil {
t.Error(err)
}
server.Close()
}
return tmpDir, server, cleanup
}
// waitLog blocks until the log output equals the text provided (ignoring whitespace before and after)