Skip to content

Commit

Permalink
Rework TestNSMGR_ShouldCleanAllClientAndEndpointGoroutines to allow i…
Browse files Browse the repository at this point in the history
…nit() goroutines

Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov committed Jan 27, 2021
1 parent a171b80 commit dd77036
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions pkg/networkservice/chains/nsmgr/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,21 +468,45 @@ func TestNSMGR_ShouldCleanAllClientAndEndpointGoroutines(t *testing.T) {
Build()
defer domain.Cleanup()

// We have lazy initialization in some chain elements in both networkservice, registry chains. So registering an
// endpoint and requesting it from client can result in new endless NSMgr goroutines.

testNSEAndClient(ctx, t, domain, &registry.NetworkServiceEndpoint{
Name: "endpoint-init",
NetworkServiceNames: []string{"service-init"},
})

// At this moment all possible endless NSMgr goroutines have been started. So we expect all newly created goroutines
// to be canceled no later than some of these events:
// 1. GRPC request context cancel
// 2. NSC connection close
// 3. NSE unregister

defer goleak.VerifyNone(t, goleak.IgnoreCurrent())

testNSEAndClient(ctx, t, domain, &registry.NetworkServiceEndpoint{
Name: "endpoint-final",
NetworkServiceNames: []string{"service-final"},
})
}

func testNSEAndClient(
ctx context.Context,
t *testing.T,
domain *sandbox.Domain,
nseReg *registry.NetworkServiceEndpoint,
) {
nseCtx, nseCancel := context.WithCancel(ctx)
defer nseCancel()

nseReg := &registry.NetworkServiceEndpoint{
Name: "final-endpoint",
NetworkServiceNames: []string{"my-service"},
}
_, err := sandbox.NewEndpoint(nseCtx, nseReg, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr)
require.NoError(t, err)

clientCtx, clientCancel := context.WithCancel(ctx)
defer clientCancel()

nsc := sandbox.NewClient(clientCtx, sandbox.GenerateTestToken, domain.Nodes[0].NSMgr.URL)

defer goleak.VerifyNone(t, goleak.IgnoreCurrent())

conn, err := func() (*networkservice.Connection, error) {
requestCtx, requestCancel := context.WithCancel(ctx)
defer requestCancel()
Expand All @@ -492,7 +516,7 @@ func TestNSMGR_ShouldCleanAllClientAndEndpointGoroutines(t *testing.T) {
{Cls: cls.LOCAL, Type: kernelmech.MECHANISM},
},
Connection: &networkservice.Connection{
NetworkService: "my-service",
NetworkService: nseReg.NetworkServiceNames[0],
},
})
}()
Expand All @@ -505,9 +529,6 @@ func TestNSMGR_ShouldCleanAllClientAndEndpointGoroutines(t *testing.T) {
return nsc.Close(closeCtx, conn)
}()
require.NoError(t, err)

clientCancel()
nseCancel()
}

type passThroughClient struct {
Expand Down

0 comments on commit dd77036

Please sign in to comment.