Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov committed Feb 8, 2021
1 parent 20ddc09 commit 1d74a75
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 40 deletions.
45 changes: 27 additions & 18 deletions pkg/registry/common/memory/ns_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,33 +121,32 @@ func TestNetworkServiceRegistryServer_DataRace(t *testing.T) {

s := memory.NewNetworkServiceRegistryServer()

go func() {
for ctx.Err() == nil {
ns, err := s.Register(ctx, &registry.NetworkService{Name: "ns-1"})
require.NoError(t, err)

_, err = s.Unregister(ctx, ns)
require.NoError(t, err)
}
}()
_, err := s.Register(ctx, &registry.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, &registry.NetworkServiceQuery{
NetworkService: &registry.NetworkService{Name: "ns-1"},
NetworkService: &registry.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)
Expand All @@ -156,8 +155,14 @@ func TestNetworkServiceRegistryServer_DataRace(t *testing.T) {
}
}()
}
wgStart.Wait()

wg.Wait()
for i := 0; i < 100; i++ {
_, err := s.Register(ctx, &registry.NetworkService{Name: fmt.Sprintf("ns-%d", i)})
require.NoError(t, err)
}

wgEnd.Wait()
}

func TestNetworkServiceRegistryServer_SlowReceiver(t *testing.T) {
Expand All @@ -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, &registry.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) {
Expand All @@ -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)

Expand Down
56 changes: 34 additions & 22 deletions pkg/registry/common/memory/nse_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,32 @@ func TestNetworkServiceEndpointRegistryServer_DataRace(t *testing.T) {

s := memory.NewNetworkServiceEndpointRegistryServer()

go func() {
for ctx.Err() == nil {
nse, err := s.Register(ctx, &registry.NetworkServiceEndpoint{Name: "nse-1"})
require.NoError(t, err)

_, err = s.Unregister(ctx, nse)
require.NoError(t, err)
}
}()
_, err := s.Register(ctx, &registry.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, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.NetworkServiceEndpoint{Name: "nse-1"},
stream, err := c.Find(findCtx, &registry.NetworkServiceEndpointQuery{
NetworkServiceEndpoint: &registry.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)
Expand All @@ -223,8 +225,14 @@ func TestNetworkServiceEndpointRegistryServer_DataRace(t *testing.T) {
}
}()
}
wgStart.Wait()

wg.Wait()
for i := 0; i < 100; i++ {
_, err := s.Register(ctx, &registry.NetworkServiceEndpoint{Name: fmt.Sprintf("nse-%d", i)})
require.NoError(t, err)
}

wgEnd.Wait()
}

func TestNetworkServiceEndpointRegistryServer_SlowReceiver(t *testing.T) {
Expand All @@ -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, &registry.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) {
Expand All @@ -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)

Expand Down Expand Up @@ -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, &registry.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)

Expand Down

0 comments on commit 1d74a75

Please sign in to comment.