Fix race in test

This commit is contained in:
Bora Tunca 2017-11-12 11:47:30 -05:00
parent 3cc01a3088
commit 493fd6bd1f
1 changed files with 14 additions and 10 deletions

View File

@ -500,7 +500,7 @@ func TestTargetSetThrottlesTheSyncCalls(t *testing.T) {
case <-finalize: case <-finalize:
for name, tp := range targetProviders { for name, tp := range targetProviders {
runCallCount := *tp.(mockTargetProvider).callCount runCallCount := tp.(mockTargetProvider).callCount()
if runCallCount != 1 { if runCallCount != 1 {
t.Errorf("%d. %q: TargetProvider Run should be called once for each target provider. For %q was called %d times", i, testCase.title, name, runCallCount) t.Errorf("%d. %q: TargetProvider Run should be called once for each target provider. For %q was called %d times", i, testCase.title, name, runCallCount)
} }
@ -992,8 +992,8 @@ func TestTargetSetRunsSameTargetProviderMultipleTimes(t *testing.T) {
t.Error("Test timed out after 20 seconds. All targets should be sent within the timeout") t.Error("Test timed out after 20 seconds. All targets should be sent within the timeout")
case <-finalize: case <-finalize:
if *tp.callCount != 2 { if tp.callCount() != 2 {
t.Errorf("Was expecting 2 calls, received %d", tp.callCount) t.Errorf("Was expecting 2 calls, received %d", tp.callCount())
} }
} }
} }
@ -1014,24 +1014,24 @@ type update struct {
} }
type mockTargetProvider struct { type mockTargetProvider struct {
callCount *uint32 _callCount *int32
updates []update updates []update
up chan<- []*config.TargetGroup up chan<- []*config.TargetGroup
} }
func newMockTargetProvider(updates []update) mockTargetProvider { func newMockTargetProvider(updates []update) mockTargetProvider {
var callCount uint32 var callCount int32
tp := mockTargetProvider{ tp := mockTargetProvider{
callCount: &callCount, _callCount: &callCount,
updates: updates, updates: updates,
} }
return tp return tp
} }
func (tp mockTargetProvider) Run(ctx context.Context, up chan<- []*config.TargetGroup) { func (tp mockTargetProvider) Run(ctx context.Context, up chan<- []*config.TargetGroup) {
atomic.AddUint32(tp.callCount, 1) atomic.AddInt32(tp._callCount, 1)
tp.up = up tp.up = up
tp.sendUpdates() tp.sendUpdates()
} }
@ -1049,3 +1049,7 @@ func (tp mockTargetProvider) sendUpdates() {
tp.up <- tgs tp.up <- tgs
} }
} }
func (tp mockTargetProvider) callCount() int {
return int(atomic.LoadInt32(tp._callCount))
}