-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
transport integration tests: add tests for resource manager #2285
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c24be46
Remove duplicate `SetProtocol` call
MarcoPolo 1b12d42
Don't log if transport ErrListenerClosed
MarcoPolo 341a288
Close the conn scope in webtransport dial
MarcoPolo c7e6f7a
Mock resource scope span
MarcoPolo 24fe426
Add transport rcmgr integration test
MarcoPolo 77222e8
PR nits
MarcoPolo 0d5e086
Fix flakiness
MarcoPolo b70e8d7
Threadsafe way of waiting for all streams
MarcoPolo 27f89c0
Expand comment
MarcoPolo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package transport_integration | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
gomock "github.com/golang/mock/gomock" | ||
"github.com/libp2p/go-libp2p/core/host" | ||
"github.com/libp2p/go-libp2p/core/network" | ||
mocknetwork "github.com/libp2p/go-libp2p/core/network/mocks" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/identify" | ||
"github.com/libp2p/go-libp2p/p2p/protocol/ping" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestResourceManagerIsUsed(t *testing.T) { | ||
for _, tc := range transportsToTest { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
for _, testDialer := range []bool{true, false} { | ||
t.Run(tc.Name+fmt.Sprintf(" test_dialer=%v", testDialer), func(t *testing.T) { | ||
|
||
var reservedMemory, releasedMemory atomic.Int32 | ||
defer func() { | ||
require.Equal(t, reservedMemory.Load(), releasedMemory.Load()) | ||
MarcoPolo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
require.NotEqual(t, 0, reservedMemory.Load()) | ||
}() | ||
|
||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
rcmgr := mocknetwork.NewMockResourceManager(ctrl) | ||
rcmgr.EXPECT().Close() | ||
|
||
var listener, dialer host.Host | ||
var expectedPeer peer.ID | ||
var expectedDir network.Direction | ||
var expectedAddr interface{} | ||
if testDialer { | ||
listener = tc.HostGenerator(t, TransportTestCaseOpts{NoRcmgr: true}) | ||
dialer = tc.HostGenerator(t, TransportTestCaseOpts{NoListen: true, ResourceManager: rcmgr}) | ||
expectedPeer = listener.ID() | ||
expectedDir = network.DirOutbound | ||
expectedAddr = listener.Addrs()[0] | ||
} else { | ||
listener = tc.HostGenerator(t, TransportTestCaseOpts{ResourceManager: rcmgr}) | ||
dialer = tc.HostGenerator(t, TransportTestCaseOpts{NoListen: true, NoRcmgr: true}) | ||
expectedPeer = dialer.ID() | ||
expectedDir = network.DirInbound | ||
expectedAddr = gomock.Any() | ||
} | ||
|
||
expectFd := true | ||
if strings.Contains(tc.Name, "QUIC") || strings.Contains(tc.Name, "WebTransport") { | ||
expectFd = false | ||
} | ||
|
||
peerScope := mocknetwork.NewMockPeerScope(ctrl) | ||
peerScope.EXPECT().ReserveMemory(gomock.Any(), gomock.Any()).AnyTimes().Do(func(amount int, pri uint8) { | ||
reservedMemory.Add(int32(amount)) | ||
}) | ||
peerScope.EXPECT().ReleaseMemory(gomock.Any()).AnyTimes().Do(func(amount int) { | ||
releasedMemory.Add(int32(amount)) | ||
}) | ||
peerScope.EXPECT().BeginSpan().AnyTimes().DoAndReturn(func() (network.ResourceScopeSpan, error) { | ||
s := mocknetwork.NewMockResourceScopeSpan(ctrl) | ||
s.EXPECT().BeginSpan().AnyTimes().Return(mocknetwork.NewMockResourceScopeSpan(ctrl), nil) | ||
// No need to track these memory reservations since we assert that Done is called | ||
s.EXPECT().ReserveMemory(gomock.Any(), gomock.Any()) | ||
s.EXPECT().Done() | ||
return s, nil | ||
}) | ||
var calledSetPeer atomic.Bool | ||
|
||
connScope := mocknetwork.NewMockConnManagementScope(ctrl) | ||
connScope.EXPECT().SetPeer(expectedPeer).Do(func(peer.ID) { | ||
calledSetPeer.Store(true) | ||
}) | ||
connScope.EXPECT().PeerScope().AnyTimes().DoAndReturn(func() network.PeerScope { | ||
if calledSetPeer.Load() { | ||
return peerScope | ||
} | ||
return nil | ||
}) | ||
connScope.EXPECT().Done() | ||
|
||
var allStreamsDone sync.WaitGroup | ||
|
||
rcmgr.EXPECT().OpenConnection(expectedDir, expectFd, expectedAddr).Return(connScope, nil) | ||
rcmgr.EXPECT().OpenStream(expectedPeer, gomock.Any()).AnyTimes().DoAndReturn(func(id peer.ID, dir network.Direction) (network.StreamManagementScope, error) { | ||
allStreamsDone.Add(1) | ||
streamScope := mocknetwork.NewMockStreamManagementScope(ctrl) | ||
// No need to track these memory reservations since we assert that Done is called | ||
streamScope.EXPECT().ReserveMemory(gomock.Any(), gomock.Any()).AnyTimes() | ||
streamScope.EXPECT().ReleaseMemory(gomock.Any()).AnyTimes() | ||
streamScope.EXPECT().BeginSpan().AnyTimes().DoAndReturn(func() (network.ResourceScopeSpan, error) { | ||
s := mocknetwork.NewMockResourceScopeSpan(ctrl) | ||
s.EXPECT().BeginSpan().AnyTimes().Return(mocknetwork.NewMockResourceScopeSpan(ctrl), nil) | ||
s.EXPECT().Done() | ||
return s, nil | ||
}) | ||
|
||
streamScope.EXPECT().SetService(gomock.Any()).MaxTimes(1) | ||
streamScope.EXPECT().SetProtocol(gomock.Any()) | ||
|
||
streamScope.EXPECT().Done().Do(func() { | ||
allStreamsDone.Done() | ||
}) | ||
return streamScope, nil | ||
}) | ||
|
||
require.NoError(t, dialer.Connect(context.Background(), peer.AddrInfo{ | ||
ID: listener.ID(), | ||
Addrs: listener.Addrs(), | ||
})) | ||
// Wait for any in progress identifies to finish. | ||
// We shouldn't have to do this, but basic host currently | ||
// always does an identify. | ||
<-dialer.(interface{ IDService() identify.IDService }).IDService().IdentifyWait(dialer.Network().ConnsToPeer(listener.ID())[0]) | ||
<-listener.(interface{ IDService() identify.IDService }).IDService().IdentifyWait(listener.Network().ConnsToPeer(dialer.ID())[0]) | ||
<-ping.Ping(context.Background(), dialer, listener.ID()) | ||
err := dialer.Network().ClosePeer(listener.ID()) | ||
require.NoError(t, err) | ||
|
||
// Wait a bit for any pending .Adds before we call .Wait to avoid a data race. | ||
// This shouldn't be necessary since it should be impossible | ||
// for an OpenStream to happen *after* a ClosePeer, however | ||
// in practice it does and leads to test flakiness. | ||
time.Sleep(10 * time.Millisecond) | ||
allStreamsDone.Wait() | ||
dialer.Close() | ||
listener.Close() | ||
}) | ||
} | ||
}) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ok to capture the loop variable here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so because t.Run blocks on
f
https://pkg.go.dev/[email protected]#T.Run. Would not be safe if we didt.Parallel
in here. But I don't think we would. It would be a bit annoying to havef
be returned from a func to get the closured variables in there. So I'm leaning towards leaving it, unless you prefer to change it.