From 1d74a755eb3810dbde65fd20938fccf1273cc709 Mon Sep 17 00:00:00 2001 From: Vladimir Popov Date: Fri, 5 Feb 2021 11:45:09 +0700 Subject: [PATCH] Fix tests Signed-off-by: Vladimir Popov --- pkg/registry/common/memory/ns_server_test.go | 45 +++++++++------ pkg/registry/common/memory/nse_server_test.go | 56 +++++++++++-------- 2 files changed, 61 insertions(+), 40 deletions(-) diff --git a/pkg/registry/common/memory/ns_server_test.go b/pkg/registry/common/memory/ns_server_test.go index 77f1d4d11..be40783c5 100644 --- a/pkg/registry/common/memory/ns_server_test.go +++ b/pkg/registry/common/memory/ns_server_test.go @@ -121,33 +121,32 @@ func TestNetworkServiceRegistryServer_DataRace(t *testing.T) { s := memory.NewNetworkServiceRegistryServer() - go func() { - for ctx.Err() == nil { - ns, err := s.Register(ctx, ®istry.NetworkService{Name: "ns-1"}) - require.NoError(t, err) - - _, err = s.Unregister(ctx, ns) - require.NoError(t, err) - } - }() + _, err := s.Register(ctx, ®istry.NetworkService{Name: "ns"}) + require.NoError(t, err) c := adapters.NetworkServiceServerToClient(s) - wg := new(sync.WaitGroup) + var wgStart, wgEnd sync.WaitGroup for i := 0; i < 10; i++ { - wg.Add(1) + wgStart.Add(1) + wgEnd.Add(1) go func() { - defer wg.Done() + defer wgEnd.Done() findCtx, findCancel := context.WithTimeout(ctx, time.Second) defer findCancel() stream, err := c.Find(findCtx, ®istry.NetworkServiceQuery{ - NetworkService: ®istry.NetworkService{Name: "ns-1"}, + NetworkService: ®istry.NetworkService{Name: "ns"}, Watch: true, }) assert.NoError(t, err) + _, err = stream.Recv() + assert.NoError(t, err) + + wgStart.Done() + for j := 0; j < 100; j++ { ns, err := stream.Recv() assert.NoError(t, err) @@ -156,8 +155,14 @@ func TestNetworkServiceRegistryServer_DataRace(t *testing.T) { } }() } + wgStart.Wait() - wg.Wait() + for i := 0; i < 100; i++ { + _, err := s.Register(ctx, ®istry.NetworkService{Name: fmt.Sprintf("ns-%d", i)}) + require.NoError(t, err) + } + + wgEnd.Wait() } func TestNetworkServiceRegistryServer_SlowReceiver(t *testing.T) { @@ -178,17 +183,21 @@ func TestNetworkServiceRegistryServer_SlowReceiver(t *testing.T) { }) require.NoError(t, err) - for i := 0; i < 200; i++ { + for i := 0; i < 1000; i++ { _, err = s.Register(ctx, ®istry.NetworkService{Name: fmt.Sprintf("ns-%d", i)}) require.NoError(t, err) } - defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + ignoreCurrent := goleak.IgnoreCurrent() _, err = stream.Recv() require.NoError(t, err) findCancel() + + require.Eventually(t, func() bool { + return goleak.Find(ignoreCurrent) == nil + }, 100*time.Millisecond, time.Millisecond) } func TestNetworkServiceRegistryServer_ShouldReceiveAllRegisters(t *testing.T) { @@ -201,8 +210,8 @@ func TestNetworkServiceRegistryServer_ShouldReceiveAllRegisters(t *testing.T) { c := adapters.NetworkServiceServerToClient(s) - wg := new(sync.WaitGroup) - for i := 0; i < 200; i++ { + var wg sync.WaitGroup + for i := 0; i < 300; i++ { wg.Add(1) name := fmt.Sprintf("ns-%d", i) diff --git a/pkg/registry/common/memory/nse_server_test.go b/pkg/registry/common/memory/nse_server_test.go index e187e0be8..2ea66d8bf 100644 --- a/pkg/registry/common/memory/nse_server_test.go +++ b/pkg/registry/common/memory/nse_server_test.go @@ -191,30 +191,32 @@ func TestNetworkServiceEndpointRegistryServer_DataRace(t *testing.T) { s := memory.NewNetworkServiceEndpointRegistryServer() - go func() { - for ctx.Err() == nil { - nse, err := s.Register(ctx, ®istry.NetworkServiceEndpoint{Name: "nse-1"}) - require.NoError(t, err) - - _, err = s.Unregister(ctx, nse) - require.NoError(t, err) - } - }() + _, err := s.Register(ctx, ®istry.NetworkServiceEndpoint{Name: "nse"}) + require.NoError(t, err) c := adapters.NetworkServiceEndpointServerToClient(s) - wg := new(sync.WaitGroup) + var wgStart, wgEnd sync.WaitGroup for i := 0; i < 10; i++ { - wg.Add(1) + wgStart.Add(1) + wgEnd.Add(1) go func() { - defer wg.Done() + defer wgEnd.Done() + + findCtx, findCancel := context.WithTimeout(ctx, time.Second) + defer findCancel() - stream, err := c.Find(ctx, ®istry.NetworkServiceEndpointQuery{ - NetworkServiceEndpoint: ®istry.NetworkServiceEndpoint{Name: "nse-1"}, + stream, err := c.Find(findCtx, ®istry.NetworkServiceEndpointQuery{ + NetworkServiceEndpoint: ®istry.NetworkServiceEndpoint{Name: "nse"}, Watch: true, }) assert.NoError(t, err) + _, err = stream.Recv() + assert.NoError(t, err) + + wgStart.Done() + for j := 0; j < 100; j++ { nse, err := stream.Recv() assert.NoError(t, err) @@ -223,8 +225,14 @@ func TestNetworkServiceEndpointRegistryServer_DataRace(t *testing.T) { } }() } + wgStart.Wait() - wg.Wait() + for i := 0; i < 100; i++ { + _, err := s.Register(ctx, ®istry.NetworkServiceEndpoint{Name: fmt.Sprintf("nse-%d", i)}) + require.NoError(t, err) + } + + wgEnd.Wait() } func TestNetworkServiceEndpointRegistryServer_SlowReceiver(t *testing.T) { @@ -245,17 +253,21 @@ func TestNetworkServiceEndpointRegistryServer_SlowReceiver(t *testing.T) { }) require.NoError(t, err) - for i := 0; i < 200; i++ { + for i := 0; i < 1000; i++ { _, err = s.Register(ctx, ®istry.NetworkServiceEndpoint{Name: fmt.Sprintf("nse-%d", i)}) require.NoError(t, err) } - defer goleak.VerifyNone(t, goleak.IgnoreCurrent()) + ignoreCurrent := goleak.IgnoreCurrent() _, err = stream.Recv() require.NoError(t, err) findCancel() + + require.Eventually(t, func() bool { + return goleak.Find(ignoreCurrent) == nil + }, 100*time.Millisecond, time.Millisecond) } func TestNetworkServiceEndpointRegistryServer_ShouldReceiveAllRegisters(t *testing.T) { @@ -268,8 +280,8 @@ func TestNetworkServiceEndpointRegistryServer_ShouldReceiveAllRegisters(t *testi c := adapters.NetworkServiceEndpointServerToClient(s) - wg := new(sync.WaitGroup) - for i := 0; i < 200; i++ { + var wg sync.WaitGroup + for i := 0; i < 300; i++ { wg.Add(1) name := fmt.Sprintf("nse-%d", i) @@ -305,15 +317,15 @@ func TestNetworkServiceEndpointRegistryServer_ShouldReceiveAllUnregisters(t *tes s := memory.NewNetworkServiceEndpointRegistryServer() - for i := 0; i < 200; i++ { + for i := 0; i < 300; i++ { _, err := s.Register(ctx, ®istry.NetworkServiceEndpoint{Name: fmt.Sprintf("nse-%d", i)}) require.NoError(t, err) } c := adapters.NetworkServiceEndpointServerToClient(s) - wg := new(sync.WaitGroup) - for i := 0; i < 200; i++ { + var wg sync.WaitGroup + for i := 0; i < 300; i++ { wg.Add(1) name := fmt.Sprintf("nse-%d", i)