Skip to content

Commit

Permalink
fix peer test failure
Browse files Browse the repository at this point in the history
  • Loading branch information
chaitanyaprem committed Oct 3, 2023
1 parent 1f80dde commit 330cf92
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 11 additions & 1 deletion waku/v2/peermanager/peer_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package peermanager
import (
"context"
"errors"
"fmt"
"math/rand"
"sync"
"time"
Expand Down Expand Up @@ -537,11 +538,16 @@ type pingResult struct {
// to maintain the RTT as part of peer-scoring and just select based on that.
func (pm *PeerManager) SelectPeerWithLowestRTT(criteria PeerSelectionCriteria) (peer.ID, error) {
var peers peer.IDSlice
var err error
if criteria.Ctx == nil {
criteria.Ctx = context.Background()
}

peers, err := pm.FilterPeersByProto(criteria.SpecificPeers, criteria.Proto)
if criteria.PubsubTopic != "" {
peers = pm.host.Peerstore().(wps.WakuPeerstore).PeersByPubSubTopic(criteria.PubsubTopic, criteria.SpecificPeers...)
}

peers, err = pm.FilterPeersByProto(peers, criteria.Proto)
if err != nil {
return "", err
}
Expand All @@ -563,6 +569,8 @@ func (pm *PeerManager) SelectPeerWithLowestRTT(criteria PeerSelectionCriteria) (
p: p,
rtt: result.RTT,
}
} else {
fmt.Println("Error in Ping", result)
}
}(p)
}
Expand All @@ -575,6 +583,7 @@ func (pm *PeerManager) SelectPeerWithLowestRTT(criteria PeerSelectionCriteria) (
case <-waitCh:
var min *pingResult
for p := range pingCh {
fmt.Println("ping result", p)
if min == nil {
min = &p
} else {
Expand All @@ -584,6 +593,7 @@ func (pm *PeerManager) SelectPeerWithLowestRTT(criteria PeerSelectionCriteria) (
}
}
if min == nil {
pm.logger.Info("Could not find min")
return "", ErrNoPeersAvailable
}

Expand Down
15 changes: 11 additions & 4 deletions waku/v2/peermanager/peer_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/rand"
"fmt"
"strings"
"testing"
"time"

Expand All @@ -20,16 +21,22 @@ import (

func getAddr(h host.Host) multiaddr.Multiaddr {
id, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", h.ID().Pretty()))
return h.Network().ListenAddresses()[0].Encapsulate(id)
var selectedAddr multiaddr.Multiaddr
//For now skipping circuit relay addresses as libp2p seems to be returning empty p2p-circuit addresses.
for _, addr := range h.Network().ListenAddresses() {
if strings.Contains(addr.String(), "p2p-circuit") {
continue
}
selectedAddr = addr
}
return selectedAddr.Encapsulate(id)
}

func initTest(t *testing.T) (context.Context, *PeerManager, func()) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
// hosts
h1, err := tests.MakeHost(ctx, 0, rand.Reader)
require.NoError(t, err)
defer h1.Close()

// host 1 is used by peer manager
pm := NewPeerManager(10, 20, utils.Logger())
Expand Down

0 comments on commit 330cf92

Please sign in to comment.