Skip to content

Commit

Permalink
fix: TestWatchConnectionStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Jul 14, 2024
1 parent 8e0e351 commit ae0bbcb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
11 changes: 10 additions & 1 deletion internal/transport/waku.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/pkg/errors"
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
Expand Down Expand Up @@ -38,6 +39,7 @@ type Node struct {
lightMode bool
statusSubscribers []ConnectionStatusSubscription
connectionStatus ConnectionStatus
connectedPeers map[peer.ID]struct{}
}

func NewNode(ctx context.Context, logger *zap.Logger) *Node {
Expand Down Expand Up @@ -113,6 +115,7 @@ func (n *Node) Initialize() error {
n.waku = wakuNode
n.peerConnection = peerConnection
n.logger = n.logger.Named("waku")
n.connectedPeers = make(map[peer.ID]struct{})

return nil
}
Expand Down Expand Up @@ -357,7 +360,13 @@ func (n *Node) watchConnectionStatus() {
return
}
n.logger.Debug("peer connection", zap.Any("status", status))
count := n.waku.PeerCount()
if status.Connected {
n.connectedPeers[status.PeerID] = struct{}{}
} else {
delete(n.connectedPeers, status.PeerID)
}
//count := n.waku.PeerCount()
count := len(n.connectedPeers)
n.notifyConnectionStatus(ConnectionStatus{
IsOnline: count > 0,
HasHistory: false,
Expand Down
32 changes: 10 additions & 22 deletions internal/transport/waku_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/brianvoe/gofakeit/v6"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/stretchr/testify/suite"
"github.com/waku-org/go-waku/waku/v2/node"
wakuenr "github.com/waku-org/go-waku/waku/v2/protocol/enr"
Expand Down Expand Up @@ -94,35 +95,22 @@ func (s *WakuSuite) TestWatchConnectionStatus() {

sub := s.node.SubscribeToConnectionStatus()

finished := make(chan struct{})

go func() {
s.node.watchConnectionStatus()
close(finished)
}()

sent := node.ConnStatus{}
err = gofakeit.Struct(&sent)
s.Require().NoError(err)
sent := node.PeerConnection{
PeerID: peer.ID(gofakeit.UUID()),
Connected: true,
}

s.node.wakuConnectionStatus <- sent
s.node.peerConnection <- sent

select {
case received := <-sub:
s.Require().Equal(sent.IsOnline, received.IsOnline)
s.Require().Equal(sent.HasHistory, received.HasHistory)
s.Require().Equal(len(sent.Peers), received.PeersCount)
s.Require().True(received.IsOnline)
s.Require().False(received.HasHistory)
s.Require().Equal(1, received.PeersCount)
s.Require().True(reflect.DeepEqual(received, s.node.ConnectionStatus()))
case <-time.After(500 * time.Millisecond):
s.Require().Fail("timeout waiting for connection status")
}

close(s.node.wakuConnectionStatus)

select {
case <-finished:
break
case <-time.After(500 * time.Millisecond):
s.Require().Fail("timeout waiting for connection status watch finish")
}
close(s.node.peerConnection)
}

0 comments on commit ae0bbcb

Please sign in to comment.