Skip to content

Commit

Permalink
Add test for metrics+unix
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoPolo committed Oct 30, 2024
1 parent 589ff76 commit c3ff407
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
33 changes: 33 additions & 0 deletions p2p/transport/tcp/metrics_unix_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// go:build: unix

package tcp

import (
"testing"

tptu "github.com/libp2p/go-libp2p/p2p/net/upgrader"
"github.com/libp2p/go-libp2p/p2p/transport/tcpreuse"
ttransport "github.com/libp2p/go-libp2p/p2p/transport/testsuite"

"github.com/stretchr/testify/require"
)

func TestTcpTransportCollectsMetricsWithSharedTcpSocket(t *testing.T) {
peerA, ia := makeInsecureMuxer(t)
_, ib := makeInsecureMuxer(t)

sharedTCPSocketA := tcpreuse.NewConnMgr(false, nil, nil)
sharedTCPSocketB := tcpreuse.NewConnMgr(false, nil, nil)

ua, err := tptu.New(ia, muxers, nil, nil, nil)
require.NoError(t, err)
ta, err := NewTCPTransport(ua, nil, sharedTCPSocketA, WithMetrics())
require.NoError(t, err)
ub, err := tptu.New(ib, muxers, nil, nil, nil)
require.NoError(t, err)
tb, err := NewTCPTransport(ub, nil, sharedTCPSocketB, WithMetrics())
require.NoError(t, err)

zero := "/ip4/127.0.0.1/tcp/0"
ttransport.SubtestTransport(t, ta, tb, zero, peerA)
}
26 changes: 26 additions & 0 deletions p2p/transport/tcpreuse/connwithscope.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tcpreuse

import (
"fmt"

"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/p2p/transport/tcpreuse/internal/sampledconn"
manet "github.com/multiformats/go-multiaddr/net"
)

type connWithScope struct {
sampledconn.ManetTCPConnInterface
scope network.ConnManagementScope
}

func (c connWithScope) Scope() network.ConnManagementScope {
return c.scope
}

func manetConnWithScope(c manet.Conn, scope network.ConnManagementScope) (manet.Conn, error) {
if tcpconn, ok := c.(sampledconn.ManetTCPConnInterface); ok {
return &connWithScope{tcpconn, scope}, nil
}

return nil, fmt.Errorf("manet.Conn is not a TCP Conn")
}

0 comments on commit c3ff407

Please sign in to comment.