browser_integration_native_test.go 2.57 KB
Newer Older
1 2 3 4 5 6
// +build !js

package websocket

import (
	"bufio"
7
	"context"
8 9 10 11
	"os"
	"os/exec"
	"path/filepath"
	"strings"
12 13 14
	"testing"

	ma "github.com/multiformats/go-multiaddr"
15 16 17
	"gitlab.dms3.io/p2p/go-p2p-core/sec/insecure"
	mplex "gitlab.dms3.io/p2p/go-p2p-mplex"
	tptu "gitlab.dms3.io/p2p/go-p2p-transport-upgrader"
18 19
)

20 21 22 23 24 25
var (
	wasmBrowserTestBin     = "wasmbrowsertest"
	wasmBrowserTestDir     = filepath.Join("tools", "bin")
	wasmBrowserTestPackage = "github.com/agnivade/wasmbrowsertest"
)

26 27 28
// TestInBrowser is a harness that allows us to use `go test` in order to run
// WebAssembly tests in a headless browser.
func TestInBrowser(t *testing.T) {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
	// ensure we have the right tools.
	err := os.MkdirAll(wasmBrowserTestDir, 0755)

	t.Logf("building %s", wasmBrowserTestPackage)
	if err != nil && !os.IsExist(err) {
		t.Fatal(err)
	}

	cmd := exec.Command(
		"go", "build",
		"-o", wasmBrowserTestBin,
		"github.com/agnivade/wasmbrowsertest",
	)
	cmd.Dir = wasmBrowserTestDir
	err = cmd.Run()
	if err != nil {
		t.Fatal(err)
	}
	t.Log("starting server")

49
	// Start a transport which the browser peer will dial.
50
	serverDoneSignal := make(chan struct{})
51
	go func() {
52 53 54
		defer func() {
			close(serverDoneSignal)
		}()
55 56 57 58 59 60
		tpt := New(&tptu.Upgrader{
			Secure: insecure.New("serverPeer"),
			Muxer:  new(mplex.Transport),
		})
		addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/5555/ws")
		if err != nil {
61
			t.Fatal("SERVER:", err)
62 63 64
		}
		listener, err := tpt.Listen(addr)
		if err != nil {
65
			t.Fatal("SERVER:", err)
66 67 68
		}
		conn, err := listener.Accept()
		if err != nil {
69
			t.Fatal("SERVER:", err)
70 71
		}
		defer conn.Close()
72
		stream, err := conn.OpenStream(context.Background())
73
		if err != nil {
74
			t.Fatal("SERVER: could not open stream:", err)
75 76 77 78
		}
		defer stream.Close()
		buf := bufio.NewReader(stream)
		if _, err := stream.Write([]byte("ping\n")); err != nil {
79
			t.Fatal("SERVER:", err)
80 81 82
		}
		msg, err := buf.ReadString('\n')
		if err != nil {
83
			t.Fatal("SERVER: could not read pong message:" + err.Error())
84 85 86
		}
		expected := "pong\n"
		if msg != expected {
87
			t.Fatalf("SERVER: Received wrong message. Expected %q but got %q", expected, msg)
88 89 90
		}
	}()

91 92 93 94 95 96 97 98
	t.Log("starting browser")

	cmd = exec.Command(
		"go", "test", "-v",
		"-exec", filepath.Join(wasmBrowserTestDir, wasmBrowserTestBin),
		"-run", "TestInBrowser",
		".",
	)
99 100 101 102
	cmd.Env = append(os.Environ(), []string{"GOOS=js", "GOARCH=wasm"}...)
	output, err := cmd.CombinedOutput()
	if err != nil {
		formattedOutput := "\t" + strings.Join(strings.Split(string(output), "\n"), "\n\t")
103
		t.Log("BROWSER OUTPUT:\n", formattedOutput)
104 105
		t.Fatal("BROWSER:", err)
	}
106

107
	<-serverDoneSignal
108
}