Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-ws-transport
Commits
2f1f3fa6
Commit
2f1f3fa6
authored
Aug 08, 2019
by
Alex Browne
Committed by
Steven Allen
Sep 30, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make it possible to run browser tests with a single go test command
parent
4616a507
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
25 additions
and
52 deletions
+25
-52
browser_integration_go_test.go
browser_integration_go_test.go
+25
-25
browser_integration_js_test.go
browser_integration_js_test.go
+0
-6
conn_js.go
conn_js.go
+0
-16
websocket_js.go
websocket_js.go
+0
-4
websocket_test.go
websocket_test.go
+0
-1
No files found.
browser_integration_go_test.go
View file @
2f1f3fa6
...
...
@@ -5,7 +5,10 @@ package websocket
import
(
"bufio"
"fmt"
"sync"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/libp2p/go-libp2p-core/sec/insecure"
...
...
@@ -22,59 +25,56 @@ const (
// WebAssembly tests in a headless browser.
func
TestInBrowser
(
t
*
testing
.
T
)
{
// Start a transport which the browser peer will dial.
wg
:=
&
sync
.
WaitGroup
{}
wg
.
Add
(
1
)
serverDoneSignal
:=
make
(
chan
struct
{})
go
func
()
{
defer
wg
.
Done
()
defer
func
()
{
close
(
serverDoneSignal
)
}()
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
{
t
.
Fatal
(
err
)
t
.
Fatal
(
"SERVER:"
,
err
)
}
listener
,
err
:=
tpt
.
Listen
(
addr
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
"SERVER:"
,
err
)
}
fmt
.
Println
(
"server started listener"
)
conn
,
err
:=
listener
.
Accept
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
"SERVER:"
,
err
)
}
defer
conn
.
Close
()
fmt
.
Println
(
"server received conn"
)
stream
,
err
:=
conn
.
OpenStream
()
if
err
!=
nil
{
fmt
.
Println
(
"Could not open stream:"
,
err
.
Error
())
t
.
Fatal
(
err
)
t
.
Fatal
(
"SERVER: could not open stream:"
,
err
)
}
defer
stream
.
Close
()
fmt
.
Println
(
"server opened stream"
)
buf
:=
bufio
.
NewReader
(
stream
)
if
_
,
err
:=
stream
.
Write
([]
byte
(
"ping
\n
"
));
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Fatal
(
"SERVER:"
,
err
)
}
fmt
.
Println
(
"server wrote message"
)
msg
,
err
:=
buf
.
ReadString
(
'\n'
)
if
err
!=
nil
{
t
.
Fatal
(
"could not read pong message:"
+
err
.
Error
())
t
.
Fatal
(
"
SERVER:
could not read pong message:"
+
err
.
Error
())
}
fmt
.
Println
(
"server received message"
)
expected
:=
"pong
\n
"
if
msg
!=
expected
{
t
.
Fatalf
(
"Received wrong message. Expected %q but got %q"
,
expected
,
msg
)
t
.
Fatalf
(
"
SERVER:
Received wrong message. Expected %q but got %q"
,
expected
,
msg
)
}
}()
// fmt.Println("Starting headless browser tests...")
// cmd := exec.Command("go", "test", "-exec", `"$GOPATH/bin/wasmbrowsertest"`, "-run", "TestInBrowser", ".")
// cmd.Env = append(os.Environ(), []string{"GOOS=js", "GOARCH=wasm"}...)
// if output, err := cmd.CombinedOutput(); err != nil {
// t.Log(string(output))
// t.Fatal(err)
// }
testExecPath
:=
filepath
.
Join
(
os
.
Getenv
(
"GOPATH"
),
"bin"
,
"wasmbrowsertest"
)
cmd
:=
exec
.
Command
(
"go"
,
"test"
,
"-exec"
,
testExecPath
,
"-run"
,
"TestInBrowser"
,
"."
,
"-v"
)
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
"
)
fmt
.
Println
(
"BROWSER OUTPUT:
\n
"
,
formattedOutput
)
t
.
Fatal
(
"BROWSER:"
,
err
)
}
wg
.
Wait
()
<-
serverDoneSignal
}
browser_integration_js_test.go
View file @
2f1f3fa6
...
...
@@ -5,7 +5,6 @@ package websocket
import
(
"bufio"
"context"
"fmt"
"testing"
"time"
...
...
@@ -24,19 +23,16 @@ func TestInBrowser(t *testing.T) {
if
err
!=
nil
{
t
.
Fatal
(
"could not parse multiaddress:"
+
err
.
Error
())
}
fmt
.
Println
(
"parsed addr"
)
conn
,
err
:=
tpt
.
Dial
(
context
.
Background
(),
addr
,
"serverPeer"
)
if
err
!=
nil
{
t
.
Fatal
(
"could not dial server:"
+
err
.
Error
())
}
fmt
.
Println
(
"dialed server"
)
defer
conn
.
Close
()
stream
,
err
:=
conn
.
AcceptStream
()
if
err
!=
nil
{
t
.
Fatal
(
"could not accept stream:"
+
err
.
Error
())
}
fmt
.
Println
(
"accepted stream"
)
defer
stream
.
Close
()
buf
:=
bufio
.
NewReader
(
stream
)
...
...
@@ -44,7 +40,6 @@ func TestInBrowser(t *testing.T) {
if
err
!=
nil
{
t
.
Fatal
(
"could not read ping message:"
+
err
.
Error
())
}
fmt
.
Println
(
"read ping"
)
expected
:=
"ping
\n
"
if
msg
!=
expected
{
t
.
Fatalf
(
"Received wrong message. Expected %q but got %q"
,
expected
,
msg
)
...
...
@@ -54,7 +49,6 @@ func TestInBrowser(t *testing.T) {
if
err
!=
nil
{
t
.
Fatal
(
"could not write pong message:"
+
err
.
Error
())
}
fmt
.
Println
(
"wrote pong"
)
// TODO(albrow): This hack is necessary in order to give the reader time to
// finish. We should find some way to remove it.
...
...
conn_js.go
View file @
2f1f3fa6
...
...
@@ -3,7 +3,6 @@ package websocket
import
(
"bytes"
"errors"
"fmt"
"io"
"net"
"strings"
...
...
@@ -40,11 +39,8 @@ func (c *Conn) Read(b []byte) (int, error) {
}
if
c
.
currData
==
nil
{
fmt
.
Println
(
"Waiting for incoming data"
)
select
{
case
data
:=
<-
c
.
incomingData
:
fmt
.
Println
(
"Received new incoming data"
)
fmt
.
Println
(
string
(
data
))
c
.
currData
=
bytes
.
NewBuffer
(
data
)
case
<-
c
.
closeSignal
:
return
0
,
io
.
EOF
...
...
@@ -62,19 +58,13 @@ func (c *Conn) Read(b []byte) (int, error) {
func
(
c
*
Conn
)
checkOpen
()
error
{
state
:=
c
.
Get
(
"readyState"
)
.
Int
()
switch
state
{
case
webSocketStateOpen
:
fmt
.
Println
(
"readyState is open"
)
case
webSocketStateClosed
,
webSocketStateClosing
:
fmt
.
Println
(
"readyState is closed or closing"
)
return
errIsClosed
default
:
fmt
.
Printf
(
"readyState is %d
\n
"
,
state
)
}
return
nil
}
func
(
c
*
Conn
)
Write
(
b
[]
byte
)
(
n
int
,
err
error
)
{
fmt
.
Println
(
"Write was called"
)
// c.mut.Lock()
// defer c.mut.Unlock()
if
err
:=
c
.
checkOpen
();
err
!=
nil
{
...
...
@@ -153,11 +143,7 @@ func (c *Conn) setUpHandlers() {
// should check binaryType and then decode accordingly.
blob
:=
args
[
0
]
.
Get
(
"data"
)
text
:=
readBlob
(
blob
)
fmt
.
Println
(
"onmessage was triggered"
)
fmt
.
Println
(
"received:"
,
text
)
fmt
.
Println
(
"Sending to incomingData"
)
c
.
incomingData
<-
[]
byte
(
text
)
fmt
.
Println
(
"Sent to incomingData"
)
}()
return
nil
})
...
...
@@ -165,7 +151,6 @@ func (c *Conn) setUpHandlers() {
c
.
Call
(
"addEventListener"
,
"message"
,
messageHandler
)
closeHandler
:=
js
.
FuncOf
(
func
(
this
js
.
Value
,
args
[]
js
.
Value
)
interface
{}
{
fmt
.
Println
(
"onclose was triggered"
)
close
(
c
.
closeSignal
)
return
nil
})
...
...
@@ -176,7 +161,6 @@ func (c *Conn) setUpHandlers() {
func
(
c
*
Conn
)
waitForOpen
()
error
{
openSignal
:=
make
(
chan
struct
{})
handler
:=
js
.
FuncOf
(
func
(
this
js
.
Value
,
args
[]
js
.
Value
)
interface
{}
{
fmt
.
Println
(
"onopen was triggered"
)
close
(
openSignal
)
return
nil
})
...
...
websocket_js.go
View file @
2f1f3fa6
...
...
@@ -5,7 +5,6 @@ package websocket
import
(
"context"
"errors"
"fmt"
"syscall/js"
"github.com/libp2p/go-libp2p-core/transport"
...
...
@@ -18,11 +17,9 @@ func (t *WebsocketTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (ma
if
err
!=
nil
{
return
nil
,
err
}
fmt
.
Println
(
"wsurl:"
,
wsurl
)
rawConn
:=
js
.
Global
()
.
Get
(
"WebSocket"
)
.
New
(
wsurl
)
rawConn
.
Call
(
"addEventListener"
,
"error"
,
js
.
FuncOf
(
func
(
this
js
.
Value
,
args
[]
js
.
Value
)
interface
{}
{
fmt
.
Println
(
"onerror was triggered"
)
js
.
Global
()
.
Get
(
"console"
)
.
Call
(
"log"
,
args
[
0
])
return
nil
}))
...
...
@@ -34,7 +31,6 @@ func (t *WebsocketTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (ma
return
nil
,
err
}
fmt
.
Println
(
"Returning from maDial"
)
return
mnc
,
nil
}
...
...
websocket_test.go
View file @
2f1f3fa6
...
...
@@ -11,7 +11,6 @@ import (
"testing/iotest"
"github.com/libp2p/go-libp2p-core/sec/insecure"
mplex
"github.com/libp2p/go-libp2p-mplex"
ttransport
"github.com/libp2p/go-libp2p-testing/suites/transport"
tptu
"github.com/libp2p/go-libp2p-transport-upgrader"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment