Skip to content

Commit

Permalink
add sharness test for pubsub
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jan Winkelmann <j-winkelmann@tuhh.de>
  • Loading branch information
keks committed Dec 4, 2016
1 parent 4e8015d commit 1b3158f
Show file tree
Hide file tree
Showing 3 changed files with 68 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
12 changes: 9 additions & 3 deletions test/sharness/lib/iptb-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ startup_cluster() {
num_nodes="$1"
bound=$(expr "$num_nodes" - 1)

test_expect_success "start up nodes" '
iptb start
'
if [ "$2" = "--enable-pubsub-experiment" ]; then
test_expect_success "start up nodes with pubsub enabled" '
iptb start --args --enable-pubsub-experiment
'
else
test_expect_success "start up nodes" '
iptb start
'
fi

test_expect_success "connect nodes to eachother" '
iptb connect [1-$bound] 0
Expand Down
54 changes: 54 additions & 0 deletions test/sharness/t0180-pubsub.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/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
(
ipfsi 0 pubsub sub --enc=ndpayload testTopic |
while read line; do
echo $line > actual &&
echo > done
exit
done
) &
ipfspid=$!
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 1b3158f

Please sign in to comment.