Skip to content

Commit

Permalink
Merge pull request #3432 from keks/feat/pubsub-sharness
Browse files Browse the repository at this point in the history
add sharness test for pubsub
  • Loading branch information
whyrusleeping authored Dec 5, 2016
2 parents a1d2d47 + 993e781 commit 0c413de
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
20 changes: 5 additions & 15 deletions core/commands/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,17 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
},
Marshalers: cmds.MarshalerMap{
cmds.Text: getPsMsgMarshaler(func(m *floodsub.Message) (io.Reader, error) {
if m.Message == nil {
return strings.NewReader(""), nil
}

return bytes.NewReader(m.Data), nil
}),
"ndpayload": getPsMsgMarshaler(func(m *floodsub.Message) (io.Reader, error) {
if m.Message == nil {
return strings.NewReader("\n"), nil
}

m.Data = append(m.Data, '\n')
return bytes.NewReader(m.Data), nil
}),
"lenpayload": getPsMsgMarshaler(func(m *floodsub.Message) (io.Reader, error) {
buf := make([]byte, 8)

var data []byte
if m.Message != nil {
data = m.Data
}

n := binary.PutUvarint(buf, uint64(len(data)))
return io.MultiReader(bytes.NewReader(buf[:n]), bytes.NewReader(data)), nil
n := binary.PutUvarint(buf, uint64(len(m.Data)))
return io.MultiReader(bytes.NewReader(buf[:n]), bytes.NewReader(m.Data)), nil
}),
},
Type: floodsub.Message{},
Expand Down Expand Up @@ -187,6 +174,9 @@ func getPsMsgMarshaler(f func(m *floodsub.Message) (io.Reader, error)) func(cmds
if !ok {
return nil, u.ErrCast()
}
if obj.Message == nil {
return strings.NewReader(""), nil
}

return f(obj)
}
Expand Down
14 changes: 11 additions & 3 deletions test/sharness/lib/iptb-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ check_has_connection() {

startup_cluster() {
num_nodes="$1"
shift
other_args="$@"
bound=$(expr "$num_nodes" - 1)

test_expect_success "start up nodes" '
iptb start
'
if test -n "$other_args"; then
test_expect_success "start up nodes with additional args" '
iptb start --args $other_args
'
else
test_expect_success "start up nodes" '
iptb start
'
fi

test_expect_success "connect nodes to eachother" '
iptb connect [1-$bound] 0
Expand Down
53 changes: 53 additions & 0 deletions test/sharness/t0180-pubsub.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh

test_description="Test dht command"

. lib/test-lib.sh

# start iptb + wait for peering
NUM_NODES=5
test_expect_success 'init iptb' '
iptb init -n $NUM_NODES --bootstrap=none --port=0
'

startup_cluster $NUM_NODES --enable-pubsub-experiment

test_expect_success 'peer ids' '
PEERID_0=$(iptb get id 0) &&
PEERID_2=$(iptb get id 2)
'

# ipfs pubsub sub
test_expect_success 'pubsub' '
echo "testOK" > expected &&
touch empty &&
mkfifo wait ||
test_fsh echo init fail
# ipfs pubsub sub is long-running so we need to start it in the background and
# wait put its output somewhere where we can access it
(
ipfsi 0 pubsub sub --enc=ndpayload testTopic | if read line; then
echo $line > actual &&
echo > wait
fi
) &
# wait until ipfs pubsub sub is ready to do work
sleep 1 &&
# publish something
ipfsi 1 pubsub pub testTopic "testOK" &> pubErr &&
# wait until `echo > wait` executed
cat wait &&
test_cmp pubErr empty &&
test_cmp expected actual
'

test_expect_success 'stop iptb' '
iptb stop
'

test_done

0 comments on commit 0c413de

Please sign in to comment.