Skip to content

Commit c3ff407

Browse files
committed
Add test for metrics+unix
1 parent 589ff76 commit c3ff407

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// go:build: unix
2+
3+
package tcp
4+
5+
import (
6+
"testing"
7+
8+
tptu "github.com/libp2p/go-libp2p/p2p/net/upgrader"
9+
"github.com/libp2p/go-libp2p/p2p/transport/tcpreuse"
10+
ttransport "github.com/libp2p/go-libp2p/p2p/transport/testsuite"
11+
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestTcpTransportCollectsMetricsWithSharedTcpSocket(t *testing.T) {
16+
peerA, ia := makeInsecureMuxer(t)
17+
_, ib := makeInsecureMuxer(t)
18+
19+
sharedTCPSocketA := tcpreuse.NewConnMgr(false, nil, nil)
20+
sharedTCPSocketB := tcpreuse.NewConnMgr(false, nil, nil)
21+
22+
ua, err := tptu.New(ia, muxers, nil, nil, nil)
23+
require.NoError(t, err)
24+
ta, err := NewTCPTransport(ua, nil, sharedTCPSocketA, WithMetrics())
25+
require.NoError(t, err)
26+
ub, err := tptu.New(ib, muxers, nil, nil, nil)
27+
require.NoError(t, err)
28+
tb, err := NewTCPTransport(ub, nil, sharedTCPSocketB, WithMetrics())
29+
require.NoError(t, err)
30+
31+
zero := "/ip4/127.0.0.1/tcp/0"
32+
ttransport.SubtestTransport(t, ta, tb, zero, peerA)
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package tcpreuse
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/libp2p/go-libp2p/core/network"
7+
"github.com/libp2p/go-libp2p/p2p/transport/tcpreuse/internal/sampledconn"
8+
manet "github.com/multiformats/go-multiaddr/net"
9+
)
10+
11+
type connWithScope struct {
12+
sampledconn.ManetTCPConnInterface
13+
scope network.ConnManagementScope
14+
}
15+
16+
func (c connWithScope) Scope() network.ConnManagementScope {
17+
return c.scope
18+
}
19+
20+
func manetConnWithScope(c manet.Conn, scope network.ConnManagementScope) (manet.Conn, error) {
21+
if tcpconn, ok := c.(sampledconn.ManetTCPConnInterface); ok {
22+
return &connWithScope{tcpconn, scope}, nil
23+
}
24+
25+
return nil, fmt.Errorf("manet.Conn is not a TCP Conn")
26+
}

0 commit comments

Comments
 (0)