Skip to content

Commit 95f509b

Browse files
wasm test suite (#1116)
* wasm: add test suite using headlless chrome
1 parent dda576e commit 95f509b

File tree

13 files changed

+447
-0
lines changed

13 files changed

+447
-0
lines changed

.circleci/config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ commands:
3737
sudo tar -C /usr/local -xf node-v10.15.1-linux-x64.tar.xz
3838
sudo ln -s /usr/local/node-v10.15.1-linux-x64/bin/node /usr/bin/node
3939
rm node-v10.15.1-linux-x64.tar.xz
40+
install-chrome:
41+
steps:
42+
- run:
43+
name: "Install Chrome"
44+
command: |
45+
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
46+
sudo apt install ./google-chrome-stable_current_amd64.deb
4047
llvm-source-linux:
4148
steps:
4249
- restore_cache:
@@ -71,6 +78,7 @@ commands:
7178
- apt-dependencies:
7279
llvm: "<<parameters.llvm>>"
7380
- install-node
81+
- install-chrome
7482
- restore_cache:
7583
keys:
7684
- go-cache-v2-{{ checksum "go.mod" }}-{{ .Environment.CIRCLE_PREVIOUS_BUILD_NUM }}
@@ -88,6 +96,7 @@ commands:
8896
- run: go test -v -tags=llvm<<parameters.llvm>> ./cgo ./compileopts ./interp ./transform .
8997
- run: make gen-device -j4
9098
- run: make smoketest
99+
- run: make wasmtest
91100
- save_cache:
92101
key: go-cache-v2-{{ checksum "go.mod" }}-{{ .Environment.CIRCLE_BUILD_NUM }}
93102
paths:

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ endif
310310
$(TINYGO) build -o wasm.wasm -target=wasm examples/wasm/export
311311
$(TINYGO) build -o wasm.wasm -target=wasm examples/wasm/main
312312

313+
wasmtest:
314+
$(GO) test ./tests/wasm
315+
313316
build/release: tinygo gen-device wasi-libc
314317
@mkdir -p build/release/tinygo/bin
315318
@mkdir -p build/release/tinygo/lib/clang/include

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ go 1.11
44

55
require (
66
github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2
7+
github.com/chromedp/cdproto v0.0.0-20200116234248-4da64dd111ac
8+
github.com/chromedp/chromedp v0.5.3
79
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf
810
github.com/marcinbor85/gohex v0.0.0-20180128172054-7a43cd876e46
911
go.bug.st/serial v1.0.0

go.sum

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2 h1:oMCHnXa6CCCafdPDbMh/lWRhRByN0VFLvv+g+ayx1SI=
22
github.com/blakesmith/ar v0.0.0-20150311145944-8bd4349a67f2/go.mod h1:PkYb9DJNAwrSvRx5DYA+gUcOIgTGVMNkfSCbZM8cWpI=
3+
github.com/chromedp/cdproto v0.0.0-20200116234248-4da64dd111ac h1:T7V5BXqnYd55Hj/g5uhDYumg9Fp3rMTS6bykYtTIFX4=
4+
github.com/chromedp/cdproto v0.0.0-20200116234248-4da64dd111ac/go.mod h1:PfAWWKJqjlGFYJEidUM6aVIWPr0EpobeyVWEEmplX7g=
5+
github.com/chromedp/chromedp v0.5.3 h1:F9LafxmYpsQhWQBdCs+6Sret1zzeeFyHS5LkRF//Ffg=
6+
github.com/chromedp/chromedp v0.5.3/go.mod h1:YLdPtndaHQ4rCpSpBG+IPpy9JvX0VD+7aaLxYgYj28w=
37
github.com/creack/goselect v0.1.1 h1:tiSSgKE1eJtxs1h/VgGQWuXUP0YS4CDIFMp6vaI1ls0=
48
github.com/creack/goselect v0.1.1/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
59
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
10+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee h1:s+21KNqlpePfkah2I+gwHF8xmJWRjooY+5248k6m4A0=
11+
github.com/gobwas/httphead v0.0.0-20180130184737-2c6c146eadee/go.mod h1:L0fX3K22YWvt/FAX9NnzrNzcI4wNYi9Yku4O0LKYflo=
12+
github.com/gobwas/pool v0.2.0 h1:QEmUOlnSjWtnpRGHF3SauEiOsy82Cup83Vf2LcMlnc8=
13+
github.com/gobwas/pool v0.2.0/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
14+
github.com/gobwas/ws v1.0.2 h1:CoAavW/wd/kulfZmSIBt6p24n4j7tHgNVCjsfHVNUbo=
15+
github.com/gobwas/ws v1.0.2/go.mod h1:szmBTxLgaFppYjEmNtny/v3w89xOydFnnZMcgRRu/EM=
616
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
717
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf h1:7+FW5aGwISbqUtkfmIpZJGRgNFg2ioYPvFaUxdqpDsg=
818
github.com/google/shlex v0.0.0-20181106134648-c34317bd91bf/go.mod h1:RpwtwJQFrIEPstU94h88MWPXP2ektJZ8cZ0YntAmXiE=
19+
github.com/knq/sysutil v0.0.0-20191005231841-15668db23d08 h1:V0an7KRw92wmJysvFvtqtKMAPmvS5O0jtB0nYo6t+gs=
20+
github.com/knq/sysutil v0.0.0-20191005231841-15668db23d08/go.mod h1:dFWs1zEqDjFtnBXsd1vPOZaLsESovai349994nHx3e0=
21+
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
22+
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
923
github.com/marcinbor85/gohex v0.0.0-20180128172054-7a43cd876e46 h1:wXG2bA8fO7Vv7lLk2PihFMTqmbT173Tje39oKzQ50Mo=
1024
github.com/marcinbor85/gohex v0.0.0-20180128172054-7a43cd876e46/go.mod h1:Pb6XcsXyropB9LNHhnqaknG/vEwYztLkQzVCHv8sQ3M=
1125
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -27,6 +41,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
2741
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2842
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9 h1:ZBzSG/7F4eNKz2L3GE9o300RX0Az1Bw5HF7PDraD+qU=
2943
golang.org/x/sys v0.0.0-20191128015809-6d18c012aee9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
44+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3045
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3146
golang.org/x/tools v0.0.0-20190227180812-8dcc6e70cdef h1:ymc9FeDom3RIEA3coKokSllBB1hRcMT0tZ1W3Jf9Ids=
3247
golang.org/x/tools v0.0.0-20190227180812-8dcc6e70cdef/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=

tests/wasm/chan_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package wasm
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/chromedp/chromedp"
8+
)
9+
10+
func TestChan(t *testing.T) {
11+
12+
t.Parallel()
13+
14+
err := run("tinygo build -o " + wasmTmpDir + "/chan.wasm -target wasm testdata/chan.go")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
ctx, cancel := chromectx(5 * time.Second)
20+
defer cancel()
21+
22+
err = chromedp.Run(ctx,
23+
chromedp.Navigate("http://localhost:8826/run?file=chan.wasm"),
24+
waitLog(`1
25+
2
26+
4
27+
3
28+
true`),
29+
)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
}

tests/wasm/event_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// +build go1.14
2+
3+
package wasm
4+
5+
import (
6+
"testing"
7+
"time"
8+
9+
"github.com/chromedp/chromedp"
10+
)
11+
12+
func TestEvent(t *testing.T) {
13+
14+
t.Parallel()
15+
16+
err := run("tinygo build -o " + wasmTmpDir + "/event.wasm -target wasm testdata/event.go")
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
21+
ctx, cancel := chromectx(5 * time.Second)
22+
defer cancel()
23+
24+
var log1, log2 string
25+
err = chromedp.Run(ctx,
26+
chromedp.Navigate("http://localhost:8826/run?file=event.wasm"),
27+
chromedp.WaitVisible("#log"),
28+
chromedp.Sleep(time.Second),
29+
chromedp.InnerHTML("#log", &log1),
30+
waitLog(`1
31+
4`),
32+
chromedp.Click("#testbtn"),
33+
chromedp.Sleep(time.Second),
34+
chromedp.InnerHTML("#log", &log2),
35+
waitLog(`1
36+
4
37+
2
38+
3
39+
true`),
40+
)
41+
t.Logf("log1: %s", log1)
42+
t.Logf("log2: %s", log2)
43+
if err != nil {
44+
t.Fatal(err)
45+
}
46+
47+
}

tests/wasm/fmt_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// +build go1.14
2+
3+
package wasm
4+
5+
// NOTE: this should work in go1.13 but panics with:
6+
// panic: syscall/js: call of Value.Get on string
7+
// which is coming from here: https://github.com/golang/go/blob/release-branch.go1.13/src/syscall/js/js.go#L252
8+
// But I'm not sure how import "fmt" results in this.
9+
// To reproduce, install Go 1.13.x and change the build tag above
10+
// to go1.13 and run this test.
11+
12+
import (
13+
"testing"
14+
"time"
15+
16+
"github.com/chromedp/chromedp"
17+
)
18+
19+
func TestFmt(t *testing.T) {
20+
21+
t.Parallel()
22+
23+
err := run("tinygo build -o " + wasmTmpDir + "/fmt.wasm -target wasm testdata/fmt.go")
24+
if err != nil {
25+
t.Fatal(err)
26+
}
27+
28+
ctx, cancel := chromectx(5 * time.Second)
29+
defer cancel()
30+
31+
var log1 string
32+
err = chromedp.Run(ctx,
33+
chromedp.Navigate("http://localhost:8826/run?file=fmt.wasm"),
34+
chromedp.Sleep(time.Second),
35+
chromedp.InnerHTML("#log", &log1),
36+
waitLog(`did not panic`),
37+
)
38+
t.Logf("log1: %s", log1)
39+
if err != nil {
40+
t.Fatal(err)
41+
}
42+
43+
}

tests/wasm/fmtprint_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// +build go1.14
2+
3+
package wasm
4+
5+
import (
6+
"testing"
7+
"time"
8+
9+
"github.com/chromedp/chromedp"
10+
)
11+
12+
func TestFmtprint(t *testing.T) {
13+
14+
t.Parallel()
15+
16+
err := run("tinygo build -o " + wasmTmpDir + "/fmtprint.wasm -target wasm testdata/fmtprint.go")
17+
if err != nil {
18+
t.Fatal(err)
19+
}
20+
21+
ctx, cancel := chromectx(5 * time.Second)
22+
defer cancel()
23+
24+
var log1 string
25+
err = chromedp.Run(ctx,
26+
chromedp.Navigate("http://localhost:8826/run?file=fmtprint.wasm"),
27+
chromedp.Sleep(time.Second),
28+
chromedp.InnerHTML("#log", &log1),
29+
waitLog(`test from fmtprint 1
30+
test from fmtprint 2
31+
test from fmtprint 3
32+
test from fmtprint 4`),
33+
)
34+
t.Logf("log1: %s", log1)
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
39+
}

0 commit comments

Comments
 (0)