Skip to content

Commit a46596c

Browse files
check that memory allocations return to 0
1 parent 8480508 commit a46596c

File tree

1 file changed

+62
-13
lines changed

1 file changed

+62
-13
lines changed

suites/mux/muxer_suite.go

+62-13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"time"
1919

2020
"github.com/libp2p/go-libp2p-core/network"
21+
"github.com/libp2p/go-libp2p-core/peer"
2122
"github.com/libp2p/go-libp2p-testing/ci"
2223

2324
"github.com/stretchr/testify/require"
@@ -43,6 +44,40 @@ func getFunctionName(i interface{}) string {
4344
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
4445
}
4546

47+
type peerScope struct {
48+
mx sync.Mutex
49+
memory int
50+
}
51+
52+
func (p *peerScope) ReserveMemory(size int, _ uint8) error {
53+
p.mx.Lock()
54+
p.memory += size
55+
p.mx.Unlock()
56+
return nil
57+
}
58+
59+
func (p *peerScope) ReleaseMemory(size int) {
60+
p.mx.Lock()
61+
defer p.mx.Unlock()
62+
if p.memory < size {
63+
panic(fmt.Sprintf("tried to release too much memory: %d (current: %d)", size, p.memory))
64+
}
65+
p.memory -= size
66+
}
67+
68+
// Check checks that we don't have any more reserved memory.
69+
func (p *peerScope) Check(t *testing.T) {
70+
p.mx.Lock()
71+
defer p.mx.Unlock()
72+
require.Zero(t, p.memory, "expected all reserved memory to have been released")
73+
}
74+
75+
func (p *peerScope) Stat() network.ScopeStat { return network.ScopeStat{} }
76+
func (p *peerScope) BeginSpan() (network.ResourceScopeSpan, error) { return nil, nil }
77+
func (p *peerScope) Peer() peer.ID { panic("implement me") }
78+
79+
var _ network.PeerScope = &peerScope{}
80+
4681
type Options struct {
4782
tr network.Multiplexer
4883
connNum int
@@ -141,9 +176,13 @@ func SubtestSimpleWrite(t *testing.T, tr network.Multiplexer) {
141176
defer nc1.Close()
142177

143178
log("wrapping conn")
144-
c1, err := tr.NewConn(nc1, false, nil)
179+
scope := &peerScope{}
180+
c1, err := tr.NewConn(nc1, false, scope)
145181
checkErr(t, err)
146-
defer c1.Close()
182+
defer func() {
183+
c1.Close()
184+
scope.Check(t)
185+
}()
147186

148187
// serve the outgoing conn, because some muxers assume
149188
// that we _always_ call serve. (this is an error?)
@@ -253,7 +292,8 @@ func SubtestStress(t *testing.T, opt Options) {
253292
return
254293
}
255294

256-
c, err := opt.tr.NewConn(nc, false, nil)
295+
scope := &peerScope{}
296+
c, err := opt.tr.NewConn(nc, false, scope)
257297
if err != nil {
258298
t.Fatal(fmt.Errorf("a.AddConn(%s <--> %s): %s", nc.LocalAddr(), nc.RemoteAddr(), err))
259299
return
@@ -282,6 +322,7 @@ func SubtestStress(t *testing.T, opt Options) {
282322
}
283323
wg.Wait()
284324
c.Close()
325+
scope.Check(t)
285326
}
286327

287328
openConnsAndRW := func() {
@@ -375,10 +416,12 @@ func SubtestStreamOpenStress(t *testing.T, tr network.Multiplexer) {
375416
}
376417
}()
377418

378-
muxb, err := tr.NewConn(b, false, nil)
419+
scope := &peerScope{}
420+
muxb, err := tr.NewConn(b, false, scope)
379421
if err != nil {
380422
t.Fatal(err)
381423
}
424+
defer scope.Check(t)
382425

383426
time.Sleep(time.Millisecond * 50)
384427

@@ -428,7 +471,8 @@ func SubtestStreamReset(t *testing.T, tr network.Multiplexer) {
428471
wg.Add(1)
429472
go func() {
430473
defer wg.Done()
431-
muxa, err := tr.NewConn(a, true, nil)
474+
scope := &peerScope{}
475+
muxa, err := tr.NewConn(a, true, scope)
432476
if err != nil {
433477
t.Error(err)
434478
return
@@ -444,18 +488,21 @@ func SubtestStreamReset(t *testing.T, tr network.Multiplexer) {
444488
if err != network.ErrReset {
445489
t.Error("should have been stream reset")
446490
}
447-
448491
s.Close()
492+
scope.Check(t)
449493
}()
450494

451-
muxb, err := tr.NewConn(b, false, nil)
495+
scope := &peerScope{}
496+
muxb, err := tr.NewConn(b, false, scope)
452497
if err != nil {
453498
t.Fatal(err)
454499
}
500+
defer muxb.Close()
455501

456502
str, err := muxb.AcceptStream()
457503
checkErr(t, err)
458504
str.Reset()
505+
scope.Check(t)
459506

460507
wg.Wait()
461508
}
@@ -464,16 +511,18 @@ func SubtestStreamReset(t *testing.T, tr network.Multiplexer) {
464511
func SubtestWriteAfterClose(t *testing.T, tr network.Multiplexer) {
465512
a, b := tcpPipe(t)
466513

467-
muxa, err := tr.NewConn(a, true, nil)
514+
scopea := &peerScope{}
515+
muxa, err := tr.NewConn(a, true, scopea)
468516
checkErr(t, err)
469517

470-
muxb, err := tr.NewConn(b, false, nil)
518+
scopeb := &peerScope{}
519+
muxb, err := tr.NewConn(b, false, scopeb)
471520
checkErr(t, err)
472521

473-
err = muxa.Close()
474-
checkErr(t, err)
475-
err = muxb.Close()
476-
checkErr(t, err)
522+
checkErr(t, muxa.Close())
523+
scopea.Check(t)
524+
checkErr(t, muxb.Close())
525+
scopeb.Check(t)
477526

478527
// make sure the underlying net.Conn was closed
479528
if _, err := a.Write([]byte("foobar")); err == nil || !strings.Contains(err.Error(), "use of closed network connection") {

0 commit comments

Comments
 (0)