forked from whyrusleeping/go-smux-multiplex
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtransport_test.go
52 lines (43 loc) · 1.01 KB
/
transport_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package mplex
import (
"errors"
"net"
"testing"
"github.com/libp2p/go-libp2p/core/network"
test "github.com/libp2p/go-libp2p/p2p/muxer/testsuite"
)
func TestDefaultTransport(t *testing.T) {
test.SubtestAll(t, DefaultTransport)
}
type memoryScope struct {
network.PeerScope
limit int
reserved int
}
func (m *memoryScope) ReserveMemory(size int, prio uint8) error {
if m.reserved+size > m.limit {
return errors.New("too much")
}
m.reserved += size
return nil
}
func (m *memoryScope) ReleaseMemory(size int) {
m.reserved -= size
if m.reserved < 0 {
panic("too much memory released")
}
}
type memoryLimitedTransport struct {
Transport
}
func (t *memoryLimitedTransport) NewConn(nc net.Conn, isServer bool, scope network.PeerScope) (network.MuxedConn, error) {
return t.Transport.NewConn(nc, isServer, &memoryScope{
limit: 3 * 1 << 20,
PeerScope: scope,
})
}
func TestDefaultTransportWithMemoryLimit(t *testing.T) {
test.SubtestAll(t, &memoryLimitedTransport{
Transport: *DefaultTransport,
})
}