forked from whyrusleeping/go-smux-multiplex
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ea94b3
commit 0b33f41
Showing
4 changed files
with
55 additions
and
7 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
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,11 +1,52 @@ | ||
package peerstream_multiplex | ||
|
||
import ( | ||
"errors" | ||
"net" | ||
"testing" | ||
|
||
"github.com/libp2p/go-libp2p-core/network" | ||
test "github.com/libp2p/go-libp2p-testing/suites/mux" | ||
) | ||
|
||
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, | ||
}) | ||
} |