mirror of
https://github.com/status-im/go-waku.git
synced 2025-02-26 20:10:44 +00:00
chore: filter v2 tests coverage improvement (#931)
This commit is contained in:
parent
6bd85a1dc9
commit
0b4df80b98
@ -2,6 +2,7 @@ package filter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"github.com/waku-org/go-waku/waku/v2/protocol/filter/pb"
|
||||
"sync"
|
||||
@ -374,3 +375,78 @@ func (s *FilterTestSuite) TestSubscribeFullNode2FullNode() {
|
||||
s.Require().True(hasTestContentTopic)
|
||||
|
||||
}
|
||||
|
||||
func (s *FilterTestSuite) TestIsSubscriptionAlive() {
|
||||
messages := prepareData(2, false, true, false, nil)
|
||||
|
||||
// Subscribe with the first message only
|
||||
s.subDetails = s.subscribe(messages[0].pubSubTopic, messages[0].contentTopic, s.fullNodeHost.ID())
|
||||
|
||||
// IsSubscriptionAlive returns no error for the first message
|
||||
err := s.lightNode.IsSubscriptionAlive(s.ctx, s.subDetails[0])
|
||||
s.Require().NoError(err)
|
||||
|
||||
// Create new host/peer - not related to any node
|
||||
host, err := tests.MakeHost(context.Background(), 54321, rand.Reader)
|
||||
s.Require().NoError(err)
|
||||
|
||||
// Alter the existing peer ID in sub details
|
||||
s.subDetails[0].PeerID = host.ID()
|
||||
|
||||
// IsSubscriptionAlive returns error for the second message, peer ID doesn't match
|
||||
err = s.lightNode.IsSubscriptionAlive(s.ctx, s.subDetails[0])
|
||||
s.Require().Error(err)
|
||||
|
||||
}
|
||||
|
||||
func (s *FilterTestSuite) TestFilterSubscription() {
|
||||
contentFilter := protocol.ContentFilter{PubsubTopic: s.testTopic, ContentTopics: protocol.NewContentTopicSet(s.testContentTopic)}
|
||||
|
||||
// Subscribe
|
||||
s.subDetails = s.subscribe(s.testTopic, s.testContentTopic, s.fullNodeHost.ID())
|
||||
|
||||
// Returns no error and SubscriptionDetails for existing subscription
|
||||
_, err := s.lightNode.FilterSubscription(s.fullNodeHost.ID(), contentFilter)
|
||||
s.Require().NoError(err)
|
||||
|
||||
otherFilter := protocol.ContentFilter{PubsubTopic: "34583495", ContentTopics: protocol.NewContentTopicSet("sjfa402")}
|
||||
|
||||
// Returns error and nil SubscriptionDetails for non existent subscription
|
||||
nonSubscription, err := s.lightNode.FilterSubscription(s.fullNodeHost.ID(), otherFilter)
|
||||
s.Require().Error(err)
|
||||
s.Require().Nil(nonSubscription)
|
||||
|
||||
// Create new host/peer - not related to any node
|
||||
host, err := tests.MakeHost(context.Background(), 54321, rand.Reader)
|
||||
s.Require().NoError(err)
|
||||
|
||||
// Returns error and nil SubscriptionDetails for unrelated host/peer
|
||||
nonSubscription, err = s.lightNode.FilterSubscription(host.ID(), contentFilter)
|
||||
s.Require().Error(err)
|
||||
s.Require().Nil(nonSubscription)
|
||||
|
||||
}
|
||||
|
||||
func (s *FilterTestSuite) TestHandleFilterSubscribeOptions() {
|
||||
contentFilter := protocol.ContentFilter{PubsubTopic: s.testTopic, ContentTopics: protocol.NewContentTopicSet(s.testContentTopic)}
|
||||
|
||||
// Subscribe
|
||||
s.subDetails = s.subscribe(s.testTopic, s.testContentTopic, s.fullNodeHost.ID())
|
||||
|
||||
// With valid peer
|
||||
opts := []FilterSubscribeOption{WithPeer(s.fullNodeHost.ID())}
|
||||
|
||||
// Positive case
|
||||
_, _, err := s.lightNode.handleFilterSubscribeOptions(s.ctx, contentFilter, opts)
|
||||
s.Require().NoError(err)
|
||||
|
||||
addr := s.fullNodeHost.Addrs()[0]
|
||||
|
||||
// Combine mutually exclusive options
|
||||
opts = []FilterSubscribeOption{WithPeer(s.fullNodeHost.ID()), WithPeerAddr(addr)}
|
||||
|
||||
// Should fail on wrong option combination
|
||||
_, _, err = s.lightNode.handleFilterSubscribeOptions(s.ctx, contentFilter, opts)
|
||||
s.Require().Error(err)
|
||||
|
||||
}
|
||||
|
@ -556,6 +556,29 @@ func (s *FilterTestSuite) TestAutoShard() {
|
||||
|
||||
}
|
||||
|
||||
func (s *FilterTestSuite) TestLightNodeIsListening() {
|
||||
|
||||
messages := prepareData(2, true, true, false, nil)
|
||||
|
||||
// Subscribe with the first message only
|
||||
s.subDetails = s.subscribe(messages[0].pubSubTopic, messages[0].contentTopic, s.fullNodeHost.ID())
|
||||
|
||||
// IsListening returns true for the first message
|
||||
listenStatus := s.lightNode.IsListening(messages[0].pubSubTopic, messages[0].contentTopic)
|
||||
s.Require().True(listenStatus)
|
||||
|
||||
// IsListening returns false for the second message
|
||||
listenStatus = s.lightNode.IsListening(messages[1].pubSubTopic, messages[1].contentTopic)
|
||||
s.Require().False(listenStatus)
|
||||
|
||||
// IsListening returns false for combination as well
|
||||
listenStatus = s.lightNode.IsListening(messages[0].pubSubTopic, messages[1].contentTopic)
|
||||
s.Require().False(listenStatus)
|
||||
|
||||
_, err := s.lightNode.UnsubscribeAll(s.ctx)
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
func (s *FilterTestSuite) BeforeTest(suiteName, testName string) {
|
||||
s.log.Info("Executing ", zap.String("testName", testName))
|
||||
}
|
||||
|
@ -2,9 +2,13 @@ package filter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"github.com/libp2p/go-libp2p/core/peerstore"
|
||||
"github.com/waku-org/go-waku/logging"
|
||||
"github.com/waku-org/go-waku/tests"
|
||||
"github.com/waku-org/go-waku/waku/v2/protocol"
|
||||
"go.uber.org/zap"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@ -259,3 +263,41 @@ func (s *FilterTestSuite) TestUnsubscribeAllDiffPubSubContentTopics() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *FilterTestSuite) TestUnsubscribeAllUnrelatedPeer() {
|
||||
|
||||
var messages = prepareData(2, false, true, false, nil)
|
||||
|
||||
// Subscribe with 2 content topics
|
||||
for _, m := range messages {
|
||||
s.subDetails = s.subscribe(m.pubSubTopic, m.contentTopic, s.fullNodeHost.ID())
|
||||
}
|
||||
|
||||
// All messages should be received
|
||||
s.waitForMessages(func() {
|
||||
s.publishMessages(messages)
|
||||
}, s.subDetails, messages)
|
||||
|
||||
// Create new host - not related to any node
|
||||
host, err := tests.MakeHost(context.Background(), 12345, rand.Reader)
|
||||
s.Require().NoError(err)
|
||||
|
||||
s.log.Info("Host ID", logging.HostID("FullNode", s.fullNodeHost.ID()))
|
||||
s.log.Info("Host ID", logging.HostID("LightNode", s.lightNodeHost.ID()))
|
||||
s.log.Info("Host ID", logging.HostID("Unrelated", host.ID()))
|
||||
|
||||
// Unsubscribe all with unrelated peer specification
|
||||
pushResult, err := s.lightNode.UnsubscribeAll(s.ctx, WithPeer(host.ID()))
|
||||
|
||||
for e := range pushResult.errs {
|
||||
s.log.Info("Push Result ", zap.String("error", strconv.Itoa(e)))
|
||||
}
|
||||
|
||||
// All messages should be received because peer ID used was not related to any subscription
|
||||
s.waitForMessages(func() {
|
||||
s.publishMessages(messages)
|
||||
}, s.subDetails, messages)
|
||||
|
||||
// Expect error for unsubscribe from non existing peer
|
||||
s.Require().Error(err)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user