Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p-pubsub
Commits
cbb7bfc1
Commit
cbb7bfc1
authored
Apr 02, 2021
by
vyzo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add tests for gs features and custom protocols
parent
0e387d79
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
119 additions
and
0 deletions
+119
-0
gossipsub_feat_test.go
gossipsub_feat_test.go
+119
-0
No files found.
gossipsub_feat_test.go
0 → 100644
View file @
cbb7bfc1
package
pubsub
import
(
"bytes"
"context"
"fmt"
"math/rand"
"testing"
"time"
"github.com/libp2p/go-libp2p-core/protocol"
)
func
TestDefaultGossipSubFeatures
(
t
*
testing
.
T
)
{
if
GossipSubDefaultFeatures
(
GossipSubFeatureMesh
,
FloodSubID
)
{
t
.
Fatal
(
"floodsub should not support Mesh"
)
}
if
!
GossipSubDefaultFeatures
(
GossipSubFeatureMesh
,
GossipSubID_v10
)
{
t
.
Fatal
(
"gossipsub-v1.0 should support Mesh"
)
}
if
!
GossipSubDefaultFeatures
(
GossipSubFeatureMesh
,
GossipSubID_v11
)
{
t
.
Fatal
(
"gossipsub-v1.1 should support Mesh"
)
}
if
GossipSubDefaultFeatures
(
GossipSubFeaturePX
,
FloodSubID
)
{
t
.
Fatal
(
"floodsub should not support PX"
)
}
if
GossipSubDefaultFeatures
(
GossipSubFeaturePX
,
GossipSubID_v10
)
{
t
.
Fatal
(
"gossipsub-v1.0 should not support PX"
)
}
if
!
GossipSubDefaultFeatures
(
GossipSubFeatureMesh
,
GossipSubID_v11
)
{
t
.
Fatal
(
"gossipsub-v1.1 should support PX"
)
}
}
func
TestGossipSubCustomProtocols
(
t
*
testing
.
T
)
{
customsub
:=
protocol
.
ID
(
"customsub/1.0.0"
)
protos
:=
[]
protocol
.
ID
{
customsub
,
FloodSubID
}
features
:=
func
(
feat
GossipSubFeature
,
proto
protocol
.
ID
)
bool
{
if
proto
==
customsub
{
return
true
}
return
false
}
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
Background
())
defer
cancel
()
hosts
:=
getNetHosts
(
t
,
ctx
,
3
)
gsubs
:=
getGossipsubs
(
ctx
,
hosts
[
:
2
],
WithGossipSubProtocols
(
protos
,
features
))
fsub
:=
getPubsub
(
ctx
,
hosts
[
2
])
psubs
:=
append
(
gsubs
,
fsub
)
connectAll
(
t
,
hosts
)
topic
:=
"test"
var
subs
[]
*
Subscription
for
_
,
ps
:=
range
psubs
{
subch
,
err
:=
ps
.
Subscribe
(
topic
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
subs
=
append
(
subs
,
subch
)
}
// wait for heartbeats to build mesh
time
.
Sleep
(
time
.
Second
*
2
)
// check the meshes of the gsubs, the gossipsub meshes should include each other but not the
// floddsub peer
gsubs
[
0
]
.
eval
<-
func
()
{
gs
:=
gsubs
[
0
]
.
rt
.
(
*
GossipSubRouter
)
_
,
ok
:=
gs
.
mesh
[
topic
][
hosts
[
1
]
.
ID
()]
if
!
ok
{
t
.
Fatal
(
"expected gs0 to have gs1 in its mesh"
)
}
_
,
ok
=
gs
.
mesh
[
topic
][
hosts
[
2
]
.
ID
()]
if
ok
{
t
.
Fatal
(
"expected gs0 to not have fs in its mesh"
)
}
}
gsubs
[
1
]
.
eval
<-
func
()
{
gs
:=
gsubs
[
1
]
.
rt
.
(
*
GossipSubRouter
)
_
,
ok
:=
gs
.
mesh
[
topic
][
hosts
[
0
]
.
ID
()]
if
!
ok
{
t
.
Fatal
(
"expected gs1 to have gs0 in its mesh"
)
}
_
,
ok
=
gs
.
mesh
[
topic
][
hosts
[
2
]
.
ID
()]
if
ok
{
t
.
Fatal
(
"expected gs1 to not have fs in its mesh"
)
}
}
// send some messages
for
i
:=
0
;
i
<
10
;
i
++
{
msg
:=
[]
byte
(
fmt
.
Sprintf
(
"%d it's not quite a floooooood %d"
,
i
,
i
))
owner
:=
rand
.
Intn
(
len
(
psubs
))
psubs
[
owner
]
.
Publish
(
topic
,
msg
)
for
_
,
sub
:=
range
subs
{
got
,
err
:=
sub
.
Next
(
ctx
)
if
err
!=
nil
{
t
.
Fatal
(
sub
.
err
)
}
if
!
bytes
.
Equal
(
msg
,
got
.
Data
)
{
t
.
Fatal
(
"got wrong message!"
)
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment