Skip to content

Commit

Permalink
add basic interop test
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Apr 19, 2018
1 parent c72c580 commit 2effb00
Show file tree
Hide file tree
Showing 7 changed files with 425 additions and 0 deletions.
4 changes: 4 additions & 0 deletions interop/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Js/Go interop test

This directory contains a basic js/go interop test. To run it, just run
`./test.sh` in this directory. It depends on `npm` and `go`.
87 changes: 87 additions & 0 deletions interop/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"net"
"sync"

mplex "github.com/libp2p/go-mplex"
)

var jsTestData = "test data from js %d"
var goTestData = "test data from go %d"

func main() {
conn, err := net.Dial("tcp4", "127.0.0.1:9991")
if err != nil {
panic(err)
}
sess := mplex.NewMultiplex(conn, true)
defer sess.Close()

var wg sync.WaitGroup

for i := 0; i < 100; i++ {
wg.Add(1)

go func() {
defer wg.Done()
s, err := sess.NewStream()
if err != nil {
panic(err)
}
readWrite(s)
}()
}
for i := 0; i < 100; i++ {
s, err := sess.Accept()
if err != nil {
panic(err)
}
wg.Add(1)
go func() {
defer wg.Done()
readWrite(s)
}()
}
wg.Wait()
}

func readWrite(s *mplex.Stream) {
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
_, err := fmt.Fprintf(s, goTestData, i)
if err != nil {
panic(err)
}
}
s.Close()
}()
go func() {
defer wg.Done()
for i := 0; i < 100; i++ {
expected := fmt.Sprintf(jsTestData, i)
actual := make([]byte, len(expected))
_, err := io.ReadFull(s, actual)
if err != nil {
panic(err)
}
if expected != string(actual) {
panic("bad bytes")
}
}
buf, err := ioutil.ReadAll(s)
if err != nil {
panic(err)
}
if len(buf) > 0 {
panic("expected EOF")
}
}()
wg.Wait()
}
1 change: 1 addition & 0 deletions interop/js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
250 changes: 250 additions & 0 deletions interop/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions interop/js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "mplex-test",
"version": "1.0.0",
"description": "mplex test program",
"author": "",
"license": "ISC",
"dependencies": {
"interface-connection": "^0.3.2",
"libp2p-mplex": "^0.7.0",
"pull-stream": "^3.6.7",
"stream-to-pull-stream": "^1.7.2",
"tcp": "^1.0.0"
}
}
Loading

0 comments on commit 2effb00

Please sign in to comment.