Skip to content
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

proxylib: Fix data races in unit tests #17141

Merged
merged 3 commits into from
Aug 19, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions proxylib/npds/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package npds
import (
"context"
"path/filepath"
"sync/atomic"
"testing"
"time"

Expand All @@ -27,8 +28,8 @@ func Test(t *testing.T) {
}

type ClientSuite struct {
acks int
nacks int
acks uint64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are having to make this an atomic variable just to synchronize the goroutine write and reads in the assert call in the test case. Again, nyc, but it looks like the the test case should've synchronized the calls via a channel instead of adding sleep. The callback function in UpsertNetworkPolicy can signal on a channel that the test case blocks on. Anyway I can revisit this in a separate PR.

I don't really understand the sleep duration specified in this block -

// Create version 1 with resource 0.
	s.UpsertNetworkPolicy(c, xdsServer, resources[0])

	time.Sleep(DialDelay * BackOffLimit) ------> there is no back-off?
	c.Assert(s.acks, Equals, 1)
	c.Assert(s.nacks, Equals, 0)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully agreed, I have the same feeling here. But I haven't spend the time to understand it yet.

I'll leave this open for now (as there is no urgency to merge it). If I find time in the next few to clean this up, I will do it in this PR, otherwise I'll label it ready-to-merge (assuming this doesn't need a Jenkins run, since it's unit tests only).

nacks uint64
}

var _ = Suite(&ClientSuite{})
Expand All @@ -53,10 +54,10 @@ func (cs *ClientSuite) UpsertNetworkPolicy(c *C, s *envoy.XDSServer, p *cilium.N
callback := func(err error) {
if err == nil {
log.Debug("ACK Callback called")
cs.acks++
atomic.AddUint64(&cs.acks, 1)
} else {
log.Debug("NACK Callback called")
cs.nacks++
atomic.AddUint64(&cs.nacks, 1)
}
}

Expand Down Expand Up @@ -94,6 +95,6 @@ func (s *ClientSuite) TestRequestAllResources(c *C) {
s.UpsertNetworkPolicy(c, xdsServer, resources[0])

time.Sleep(DialDelay * BackOffLimit)
c.Assert(s.acks, Equals, 1)
c.Assert(s.nacks, Equals, 0)
c.Assert(atomic.LoadUint64(&s.acks), Equals, uint64(1))
c.Assert(atomic.LoadUint64(&s.nacks), Equals, uint64(0))
}