Skip to content

Commit

Permalink
Merge pull request cockroachdb#20635 from mjibson/job-lock
Browse files Browse the repository at this point in the history
sql/jobs: fix races in TestRegistryResumeExpiredLease
  • Loading branch information
maddyblue authored Dec 13, 2017
2 parents 949695e + 3ac7f3c commit 48f354e
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions pkg/sql/jobs/registry_external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ func TestRegistryResumeExpiredLease(t *testing.T) {
resumeCounts := make(map[int64]int)
// done prevents jobs from finishing.
done := make(chan struct{})
defer close(done)
// resumeCalled does a locked, blocking send when a job is started/resumed. A
// receive on it will block until a job is running.
resumeCalled := make(chan struct{})
Expand All @@ -128,11 +127,11 @@ func TestRegistryResumeExpiredLease(t *testing.T) {
hookCallCount++
lock.Unlock()
return jobs.FakeResumer{OnResume: func(job *jobs.Job) error {
lock.Lock()
select {
case resumeCalled <- struct{}{}:
case <-done:
}
lock.Lock()
resumeCounts[*job.ID()]++
lock.Unlock()
<-done
Expand Down Expand Up @@ -175,31 +174,35 @@ func TestRegistryResumeExpiredLease(t *testing.T) {
return nil
})

lock.Lock()
if e, a := 2, resumeCounts[jobMap[1]]; e != a {
t.Fatalf("expected resumeCount to be %d, but got %d", e, a)
}
lock.Unlock()
testutils.SucceedsSoon(t, func() error {
lock.Lock()
defer lock.Unlock()
if e, a := 2, resumeCounts[jobMap[1]]; e != a {
return errors.Errorf("expected resumeCount to be %d, but got %d", e, a)
}
return nil
})

nodeLiveness.FakeIncrementEpoch(3)
<-resumeCalled
close(done)
testutils.SucceedsSoon(t, func() error {
lock.Lock()
if e, a := 2, resumeCounts[jobMap[3]]; e != a {
return errors.Errorf("expected resumeCount to be %d, but got %d", e, a)
defer lock.Unlock()
if e, a := 2, resumeCounts[jobMap[3]]; e > a {
return errors.Errorf("expected resumeCount to be > %d, but got %d", e, a)
}
if e, a := 1, resumeCounts[jobMap[2]]; e != a {
return errors.Errorf("expected resumeCount to be %d, but got %d", e, a)
if e, a := 1, resumeCounts[jobMap[2]]; e > a {
return errors.Errorf("expected resumeCount to be > %d, but got %d", e, a)
}
count := 0
for _, ct := range resumeCounts {
count += ct
}

if e, a := 5, count; e != a {
return errors.Errorf("expected total jobs to be %d, but got %d", e, a)
if e, a := 5, count; e > a {
return errors.Errorf("expected total jobs to be > %d, but got %d", e, a)
}
lock.Unlock()
return nil
})
}
Expand Down

0 comments on commit 48f354e

Please sign in to comment.