-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
circuitv2: relaysvc metrics track client disconnects
- Loading branch information
Showing
5 changed files
with
182 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//go:build nocover | ||
|
||
package relay | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/test" | ||
pbv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/pb" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/util" | ||
) | ||
|
||
func TestNoCoverNoAlloc(t *testing.T) { | ||
statuses := []pbv2.Status{ | ||
pbv2.Status_OK, | ||
pbv2.Status_NO_RESERVATION, | ||
pbv2.Status_RESOURCE_LIMIT_EXCEEDED, | ||
pbv2.Status_PERMISSION_DENIED, | ||
} | ||
peerIds := []peer.ID{peer.ID("p1"), peer.ID("p2"), peer.ID("p3")} | ||
peers := []*pbv2.Peer{ | ||
util.PeerInfoToPeerV2(peer.AddrInfo{ID: test.RandPeerIDFatal(t)}), | ||
util.PeerInfoToPeerV2(peer.AddrInfo{ID: test.RandPeerIDFatal(t)}), | ||
util.PeerInfoToPeerV2(peer.AddrInfo{ID: test.RandPeerIDFatal(t)}), | ||
} | ||
mt := NewMetricsTracer() | ||
tests := map[string]func(){ | ||
"RelayStatus": func() { mt.RelayStatus(rand.Intn(2) == 1) }, | ||
"ConnectionOpened": func() { mt.ConnectionOpened() }, | ||
"ConnectionClosed": func() { mt.ConnectionClosed(time.Duration(rand.Intn(10)) * time.Second) }, | ||
"ConnectionRequestHandled": func() { mt.ConnectionRequestHandled(peers[rand.Intn(len(peers))], statuses[rand.Intn(len(statuses))]) }, | ||
"ReservationAllowed": func() { mt.ReservationAllowed(peerIds[rand.Intn(len(peerIds))], rand.Intn(2) == 1) }, | ||
"ReservationExpired": func() { mt.ReservationExpired(rand.Intn(10)) }, | ||
"ReservationRequestHandled": func() { mt.ReservationRequestHandled(statuses[rand.Intn(len(statuses))]) }, | ||
"BytesTransferred": func() { mt.BytesTransferred(rand.Intn(1000)) }, | ||
"PeerDisconnected": func() { mt.PeerDisconnected(peerIds[rand.Intn(len(peerIds))], time.Time{}) }, | ||
"GC": func() { mt.GC() }, | ||
} | ||
for method, f := range tests { | ||
allocs := testing.AllocsPerRun(1000, f) | ||
if allocs > 0 { | ||
t.Fatalf("Alloc Test: %s, got: %0.2f, expected: 0 allocs", method, allocs) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,71 @@ | ||
//go:build nocover | ||
|
||
package relay | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
pbv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/pb" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/test" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/pb" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/util" | ||
ma "github.com/multiformats/go-multiaddr" | ||
"github.com/prometheus/client_golang/prometheus" | ||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
||
func TestNoCoverNoAlloc(t *testing.T) { | ||
statuses := []pbv2.Status{ | ||
pbv2.Status_OK, | ||
pbv2.Status_NO_RESERVATION, | ||
pbv2.Status_RESOURCE_LIMIT_EXCEEDED, | ||
pbv2.Status_PERMISSION_DENIED, | ||
func getCounterValue(counter prometheus.Counter) (int, error) { | ||
m := &dto.Metric{} | ||
if err := counter.Write(m); err != nil { | ||
return 0, err | ||
} | ||
return int(*m.Counter.Value), nil | ||
} | ||
|
||
func getRandAddrInfo(t *testing.T) peer.AddrInfo { | ||
t.Helper() | ||
pid1 := test.RandPeerIDFatal(t) | ||
addrs1 := []ma.Multiaddr{ma.StringCast("/ip4/127.0.0.1/udp/1/quic")} | ||
peer1 := peer.AddrInfo{ID: pid1, Addrs: addrs1} | ||
return peer1 | ||
} | ||
|
||
func TestClientDisconnectionReason(t *testing.T) { | ||
reg := prometheus.NewRegistry() | ||
|
||
peer1 := getRandAddrInfo(t) | ||
|
||
mt := NewMetricsTracer(WithRegisterer(reg)) | ||
|
||
// Trigger Disconnected Event | ||
expiry := time.Now().Add(10 * time.Second) | ||
mt.PeerDisconnected(peer1.ID, expiry) | ||
|
||
// This should increment "client disconnected" | ||
mt.ConnectionRequestHandled(util.PeerInfoToPeerV2(peer1), pb.Status_NO_RESERVATION) | ||
clientDisconnect, err := getCounterValue(connectionRejectionsTotal.WithLabelValues("client disconnected")) | ||
if err != nil { | ||
t.Errorf("Unexpected error %s", err) | ||
} | ||
noReservation, err := getCounterValue(connectionRejectionsTotal.WithLabelValues("no reservation")) | ||
if err != nil { | ||
t.Errorf("Unexpected err %s", err) | ||
} | ||
if clientDisconnect != 1 || noReservation != 0 { | ||
t.Errorf("Invalid count values: expected clientDisconnect: 1, noReservation: 0, got: clientDisconnect: %d, noReservation: %d ", clientDisconnect, noReservation) | ||
} | ||
|
||
peer2 := getRandAddrInfo(t) | ||
// This should increment "no reservation" | ||
mt.ConnectionRequestHandled(util.PeerInfoToPeerV2(peer2), pb.Status_NO_RESERVATION) | ||
clientDisconnect, err = getCounterValue(connectionRejectionsTotal.WithLabelValues("client disconnected")) | ||
if err != nil { | ||
t.Errorf("Unexpected error %s", err) | ||
} | ||
mt := NewMetricsTracer() | ||
tests := map[string]func(){ | ||
"RelayStatus": func() { mt.RelayStatus(rand.Intn(2) == 1) }, | ||
"ConnectionOpened": func() { mt.ConnectionOpened() }, | ||
"ConnectionClosed": func() { mt.ConnectionClosed(time.Duration(rand.Intn(10)) * time.Second) }, | ||
"ConnectionRequestHandled": func() { mt.ConnectionRequestHandled(statuses[rand.Intn(len(statuses))]) }, | ||
"ReservationAllowed": func() { mt.ReservationAllowed(rand.Intn(2) == 1) }, | ||
"ReservationClosed": func() { mt.ReservationClosed(rand.Intn(10)) }, | ||
"ReservationRequestHandled": func() { mt.ReservationRequestHandled(statuses[rand.Intn(len(statuses))]) }, | ||
"BytesTransferred": func() { mt.BytesTransferred(rand.Intn(1000)) }, | ||
noReservation, err = getCounterValue(connectionRejectionsTotal.WithLabelValues("no reservation")) | ||
if err != nil { | ||
t.Errorf("Unexpected err %s", err) | ||
} | ||
for method, f := range tests { | ||
allocs := testing.AllocsPerRun(1000, f) | ||
if allocs > 0 { | ||
t.Fatalf("Alloc Test: %s, got: %0.2f, expected: 0 allocs", method, allocs) | ||
} | ||
if clientDisconnect != 1 || noReservation != 1 { | ||
t.Errorf("Invalid count values: expected clientDisconnect: 1, noReservation: 0, got: clientDisconnect: %d, noReservation: %d ", clientDisconnect, noReservation) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters