add sharness test for pubsub
License: MIT Signed-off-by: Jan Winkelmann <j-winkelmann@tuhh.de>
Jan Winkelmann committed
Nov 28, 2016 at 20:03 UTC
1b3158f8604276da5c03a754cac41ab60e713356
3 files changed
+68
-18
core/commands/pubsub.go
+5
-15
@@ -122,30 +122,17 @@ To use, the daemon must be run with '--enable-pubsub-experiment'.
122
},
123
Marshalers: cmds.MarshalerMap{
124
cmds.Text: getPsMsgMarshaler(func(m *floodsub.Message) (io.Reader, error) {
125
- if m.Message == nil {
126
- return strings.NewReader(""), nil
127
- }
128
-
125
return bytes.NewReader(m.Data), nil
126
}),
127
"ndpayload": getPsMsgMarshaler(func(m *floodsub.Message) (io.Reader, error) {
132
- if m.Message == nil {
133
- return strings.NewReader("\n"), nil
134
- }
135
-
128
m.Data = append(m.Data, '\n')
129
return bytes.NewReader(m.Data), nil
130
}),
131
"lenpayload": getPsMsgMarshaler(func(m *floodsub.Message) (io.Reader, error) {
132
buf := make([]byte, 8)
133
142
- var data []byte
143
- if m.Message != nil {
144
- data = m.Data
145
- }
146
-
147
- n := binary.PutUvarint(buf, uint64(len(data)))
148
- return io.MultiReader(bytes.NewReader(buf[:n]), bytes.NewReader(data)), nil
134
+ n := binary.PutUvarint(buf, uint64(len(m.Data)))
135
+ return io.MultiReader(bytes.NewReader(buf[:n]), bytes.NewReader(m.Data)), nil
136
}),
137
},
138
Type: floodsub.Message{},
@@ -187,6 +174,9 @@ func getPsMsgMarshaler(f func(m *floodsub.Message) (io.Reader, error)) func(cmds
174
if !ok {
175
return nil, u.ErrCast()
176
}
177
+ if obj.Message == nil {
178
+ return strings.NewReader(""), nil
179
+ }
180
181
return f(obj)
182
}
test/sharness/lib/iptb-lib.sh
+9
-3
@@ -21,9 +21,15 @@ startup_cluster() {
21
num_nodes="$1"
22
bound=$(expr "$num_nodes" - 1)
23
24
- test_expect_success "start up nodes" '
25
- iptb start
26
- '
24
+ if [ "$2" = "--enable-pubsub-experiment" ]; then
25
+ test_expect_success "start up nodes with pubsub enabled" '
26
+ iptb start --args --enable-pubsub-experiment
27
+ '
28
+ else
29
+ test_expect_success "start up nodes" '
30
+ iptb start
31
+ '
32
+ fi
33
34
test_expect_success "connect nodes to eachother" '
35
iptb connect [1-$bound] 0
test/sharness/t0180-pubsub.sh
new
+54
@@ -0,0 +1,54 @@
1
+#!/bin/sh
2
+
3
+test_description="Test dht command"
4
+
5
+. lib/test-lib.sh
6
+
7
+# start iptb + wait for peering
8
+NUM_NODES=5
9
+test_expect_success 'init iptb' '
10
+ iptb init -n $NUM_NODES --bootstrap=none --port=0
11
+'
12
+
13
+startup_cluster $NUM_NODES --enable-pubsub-experiment
14
+
15
+test_expect_success 'peer ids' '
16
+ PEERID_0=$(iptb get id 0) &&
17
+ PEERID_2=$(iptb get id 2)
18
+'
19
+
20
+# ipfs pubsub sub
21
+test_expect_success 'pubsub' '
22
+ echo "testOK" > expected &&
23
+ touch empty &&
24
+ mkfifo wait ||
25
+ test_fsh echo init fail
26
+
27
+ (
28
+ ipfsi 0 pubsub sub --enc=ndpayload testTopic |
29
+ while read line; do
30
+ echo $line > actual &&
31
+ echo > done
32
+ exit
33
+ done
34
+ ) &
35
+
36
+ ipfspid=$!
37
+
38
+ sleep 1
39
+
40
+ # publish something
41
+ ipfsi 1 pubsub pub testTopic "testOK" &> pubErr &&
42
+
43
+ # wait until `echo > wait` executed
44
+ cat wait &&
45
+
46
+ test_cmp pubErr empty &&
47
+ test_cmp expected actual
48
+'
49
+
50
+test_expect_success 'stop iptb' '
51
+ iptb stop
52
+'
53
+
54
+test_done