diff --git a/core/commands/pubsub.go b/core/commands/pubsub.go index 5eb65e2f352..038d688f97f 100644 --- a/core/commands/pubsub.go +++ b/core/commands/pubsub.go @@ -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{}, @@ -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) } diff --git a/test/sharness/lib/iptb-lib.sh b/test/sharness/lib/iptb-lib.sh index 15402dff865..67841e2e449 100644 --- a/test/sharness/lib/iptb-lib.sh +++ b/test/sharness/lib/iptb-lib.sh @@ -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 diff --git a/test/sharness/t0180-pubsub.sh b/test/sharness/t0180-pubsub.sh new file mode 100755 index 00000000000..2ca19a3432b --- /dev/null +++ b/test/sharness/t0180-pubsub.sh @@ -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