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.
Этот коммит содержится в:
родитель
5bae55d755
коммит
f3bdebe2a6
6 изменённых файлов: 40 добавлений и 34 удалений
|
@ -11,6 +11,9 @@ func TestChan(t *testing.T) {
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
wasmTmpDir, server, cleanup := startServer(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
err := run("tinygo build -o " + wasmTmpDir + "/chan.wasm -target wasm testdata/chan.go")
|
err := run("tinygo build -o " + wasmTmpDir + "/chan.wasm -target wasm testdata/chan.go")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -20,7 +23,7 @@ func TestChan(t *testing.T) {
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
err = chromedp.Run(ctx,
|
err = chromedp.Run(ctx,
|
||||||
chromedp.Navigate("http://localhost:8826/run?file=chan.wasm"),
|
chromedp.Navigate(server.URL+"/run?file=chan.wasm"),
|
||||||
waitLog(`1
|
waitLog(`1
|
||||||
2
|
2
|
||||||
4
|
4
|
||||||
|
|
|
@ -13,6 +13,9 @@ func TestEvent(t *testing.T) {
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
wasmTmpDir, server, cleanup := startServer(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
err := run("tinygo build -o " + wasmTmpDir + "/event.wasm -target wasm testdata/event.go")
|
err := run("tinygo build -o " + wasmTmpDir + "/event.wasm -target wasm testdata/event.go")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -23,7 +26,7 @@ func TestEvent(t *testing.T) {
|
||||||
|
|
||||||
var log1, log2 string
|
var log1, log2 string
|
||||||
err = chromedp.Run(ctx,
|
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.WaitVisible("#log"),
|
||||||
chromedp.Sleep(time.Second),
|
chromedp.Sleep(time.Second),
|
||||||
chromedp.InnerHTML("#log", &log1),
|
chromedp.InnerHTML("#log", &log1),
|
||||||
|
|
|
@ -20,6 +20,9 @@ func TestFmt(t *testing.T) {
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
wasmTmpDir, server, cleanup := startServer(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
err := run("tinygo build -o " + wasmTmpDir + "/fmt.wasm -target wasm testdata/fmt.go")
|
err := run("tinygo build -o " + wasmTmpDir + "/fmt.wasm -target wasm testdata/fmt.go")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -30,7 +33,7 @@ func TestFmt(t *testing.T) {
|
||||||
|
|
||||||
var log1 string
|
var log1 string
|
||||||
err = chromedp.Run(ctx,
|
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.Sleep(time.Second),
|
||||||
chromedp.InnerHTML("#log", &log1),
|
chromedp.InnerHTML("#log", &log1),
|
||||||
waitLog(`did not panic`),
|
waitLog(`did not panic`),
|
||||||
|
|
|
@ -13,6 +13,9 @@ func TestFmtprint(t *testing.T) {
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
wasmTmpDir, server, cleanup := startServer(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
err := run("tinygo build -o " + wasmTmpDir + "/fmtprint.wasm -target wasm testdata/fmtprint.go")
|
err := run("tinygo build -o " + wasmTmpDir + "/fmtprint.wasm -target wasm testdata/fmtprint.go")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -23,7 +26,7 @@ func TestFmtprint(t *testing.T) {
|
||||||
|
|
||||||
var log1 string
|
var log1 string
|
||||||
err = chromedp.Run(ctx,
|
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.Sleep(time.Second),
|
||||||
chromedp.InnerHTML("#log", &log1),
|
chromedp.InnerHTML("#log", &log1),
|
||||||
waitLog(`test from fmtprint 1
|
waitLog(`test from fmtprint 1
|
||||||
|
|
|
@ -13,6 +13,9 @@ func TestLog(t *testing.T) {
|
||||||
|
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
wasmTmpDir, server, cleanup := startServer(t)
|
||||||
|
defer cleanup()
|
||||||
|
|
||||||
err := run("tinygo build -o " + wasmTmpDir + "/log.wasm -target wasm testdata/log.go")
|
err := run("tinygo build -o " + wasmTmpDir + "/log.wasm -target wasm testdata/log.go")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -23,7 +26,7 @@ func TestLog(t *testing.T) {
|
||||||
|
|
||||||
var log1 string
|
var log1 string
|
||||||
err = chromedp.Run(ctx,
|
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.Sleep(time.Second),
|
||||||
chromedp.InnerHTML("#log", &log1),
|
chromedp.InnerHTML("#log", &log1),
|
||||||
waitLogRe(`^..../../.. ..:..:.. log 1
|
waitLogRe(`^..../../.. ..:..:.. log 1
|
||||||
|
|
|
@ -3,11 +3,11 @@ package wasm
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
@ -20,29 +20,6 @@ import (
|
||||||
"github.com/chromedp/chromedp"
|
"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 {
|
func run(cmdline string) error {
|
||||||
args := strings.Fields(cmdline)
|
args := strings.Fields(cmdline)
|
||||||
return runargs(args...)
|
return runargs(args...)
|
||||||
|
@ -70,7 +47,12 @@ func chromectx(timeout time.Duration) (context.Context, context.CancelFunc) {
|
||||||
return ctx, cancel
|
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))
|
fsh := http.FileServer(http.Dir(tmpDir))
|
||||||
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -140,11 +122,20 @@ if (wasmSupported) {
|
||||||
fsh.ServeHTTP(w, r)
|
fsh.ServeHTTP(w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Printf("Starting server at %q for dir: %s", *addr, tmpDir)
|
server := httptest.NewServer(h)
|
||||||
go func() {
|
t.Logf("Started server at %q for dir: %s", server.URL, tmpDir)
|
||||||
log.Fatal(http.ListenAndServe(*addr, h))
|
|
||||||
}()
|
|
||||||
|
|
||||||
|
// 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)
|
// waitLog blocks until the log output equals the text provided (ignoring whitespace before and after)
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче