Skip to content

Commit

Permalink
Corenet API: Some more tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <[email protected]>
  • Loading branch information
magik6k committed May 27, 2017
1 parent 672f69d commit 1bb0143
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 10 deletions.
37 changes: 28 additions & 9 deletions test/dependencies/ma-pipe-unidir/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"

ma "gx/ipfs/QmcyqRMCAXVtYPS4DiBrA7sezL9rRGfW8Ctx7cywL4TXJj/go-multiaddr"
manet "gx/ipfs/Qmf1Gq7N45Rpuw7ev47uWgH6dLPtdnvcMRNPkVBwqjLJg2/go-multiaddr-net"
)

const USAGE = "ma-pipe-unidir [-l|--listen] [-h|--help] <send|recv> <multiaddr>\n"
const USAGE = "ma-pipe-unidir [-l|--listen] [--pidFile=path] [-h|--help] <send|recv> <multiaddr>\n"

type Opts struct {
Listen bool
Listen bool
PidFile string
}

func main() {
func app() int {
opts := Opts{}
flag.BoolVar(&opts.Listen, "l", false, "")
flag.BoolVar(&opts.Listen, "listen", false, "")
flag.StringVar(&opts.PidFile, "pidFile", "", "")
flag.Usage = func() {
fmt.Print(USAGE)
}
Expand All @@ -28,34 +32,44 @@ func main() {

if len(args) < 2 { // <mode> <addr>
fmt.Print(USAGE)
return
return 1
}

mode := args[0]
addr := args[1]

if len(opts.PidFile) > 0 {
data := []byte(strconv.Itoa(os.Getpid()))
err := ioutil.WriteFile(opts.PidFile, data, 0644)
if err != nil {
return 1
}

defer os.Remove(opts.PidFile)
}

maddr, err := ma.NewMultiaddr(addr)
if err != nil {
return
return 1
}

var conn manet.Conn

if opts.Listen {
listener, err := manet.Listen(maddr)
if err != nil {
return
return 1
}

conn, err = listener.Accept()
if err != nil {
return
return 1
}
} else {
var err error
conn, err = manet.Dial(maddr)
if err != nil {
return
return 1
}
}

Expand All @@ -68,6 +82,11 @@ func main() {
default:
//TODO: a bit late
fmt.Print(USAGE)
return
return 1
}
return 0
}

func main() {
os.Exit(app())
}
77 changes: 76 additions & 1 deletion test/sharness/t0180-corenet.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ test_expect_success 'Test server to client communications' '
test_expect_success 'Test client to server communications' '
ma-pipe-unidir --listen recv /ip4/127.0.0.1/tcp/10101 > server.out &
SERVER_PID=$!
#sleep 0.5 &&
ipfsi 1 exp corenet dial $PEERID_0 corenet-test /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log &&
ma-pipe-unidir send /ip4/127.0.0.1/tcp/10102 < corenet1.bin
Expand All @@ -57,6 +56,82 @@ test_expect_success 'client to server output looks good' '
test_cmp server.out corenet1.bin
'

test_expect_success "'ipfs corenet ls' succeeds" '
echo "/ip4/127.0.0.1/tcp/10101 /app/corenet-test" > expected &&
ipfsi 0 exp corenet ls > actual
'

test_expect_success "'ipfs corenet ls' output looks good" '
test_cmp expected actual
'

test_expect_success "Cannot re-register app handler" '
(! ipfsi 0 exp corenet listen corenet-test /ip4/127.0.0.1/tcp/10101)
'

test_expect_success "'ipfs corenet streams' output is empty" '
ipfsi 0 exp corenet streams > actual &&
test_must_be_empty actual
'

test_expect_success "Setup: Idle stream" '
ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 &
ipfsi 1 exp corenet dial $PEERID_0 corenet-test /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log &&
ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 &
go-sleep 500ms &&
kill -0 $(cat listener.pid) && kill -0 $(cat client.pid)
'

test_expect_success "'ipfs corenet streams' succeeds" '
echo "2 /app/corenet-test /ip4/127.0.0.1/tcp/10101 $PEERID_1" > expected
ipfsi 0 exp corenet streams > actual
'

test_expect_success "'ipfs corenet streams' output looks good" '
test_cmp expected actual
'

test_expect_success "'ipfs corenet close' closes stream" '
ipfsi 0 exp corenet close 2 &&
ipfsi 0 exp corenet streams > actual &&
[ ! -f listener.pid ] && [ ! -f client.pid ] &&
test_must_be_empty actual
'

test_expect_success "'ipfs corenet close' closes app handler" '
ipfsi 0 exp corenet close corenet-test &&
ipfsi 0 exp corenet ls > actual &&
test_must_be_empty actual
'

test_expect_success "Setup: Idle stream(2)" '
ma-pipe-unidir --listen --pidFile=listener.pid recv /ip4/127.0.0.1/tcp/10101 &
ipfsi 0 exp corenet listen corenet-test2 /ip4/127.0.0.1/tcp/10101 2>&1 > listener-stdouterr.log &&
ipfsi 1 exp corenet dial $PEERID_0 corenet-test2 /ip4/127.0.0.1/tcp/10102 2>&1 > dialer-stdouterr.log &&
ma-pipe-unidir --pidFile=client.pid recv /ip4/127.0.0.1/tcp/10102 &
go-sleep 500ms &&
kill -0 $(cat listener.pid) && kill -0 $(cat client.pid)
'

test_expect_success "'ipfs corenet streams' succeeds(2)" '
echo "3 /app/corenet-test2 /ip4/127.0.0.1/tcp/10101 $PEERID_1" > expected
ipfsi 0 exp corenet streams > actual
test_cmp expected actual
'

test_expect_success "'ipfs corenet close -a' closes streams and app handlers" '
ipfsi 0 exp corenet close -a &&
ipfsi 0 exp corenet streams > actual &&
[ ! -f listener.pid ] && [ ! -f client.pid ] &&
test_must_be_empty actual &&
ipfsi 0 exp corenet ls > actual &&
test_must_be_empty actual
'

test_expect_success 'stop iptb' '
iptb stop
'
Expand Down

0 comments on commit 1bb0143

Please sign in to comment.