Skip to content

Commit 8f8a2c8

Browse files
authored
Merge pull request #29797 from listx/fix-data-race
fix unit test issues
2 parents ac23f69 + c636b68 commit 8f8a2c8

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

hack/make-rules/go-test/unit.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ fi
6161
# run unit tests with junit output
6262
(
6363
set -x;
64+
umask 0022
6465
mkdir -p "${JUNIT_RESULT_DIR}"
6566
"${REPO_ROOT}/_bin/gotestsum" --junitfile="${JUNIT_RESULT_DIR}/junit-unit.xml" \
66-
-- "./${folder_to_test}"
67+
-- \
68+
-race \
69+
"./${folder_to_test}"
6770
)

prow/cache/cache_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,13 @@ func TestCallbacks(t *testing.T) {
341341
forcedEvictionsCounter := 0
342342
manualEvictionsCounter := 0
343343

344+
counterLock := &sync.Mutex{}
344345
mkCallback := func(counter *int) EventCallback {
345-
callback := func(key interface{}) { (*counter)++ }
346+
callback := func(key interface{}) {
347+
counterLock.Lock()
348+
(*counter)++
349+
counterLock.Unlock()
350+
}
346351
return callback
347352
}
348353

@@ -460,15 +465,16 @@ func TestCallbacks(t *testing.T) {
460465
cacheCallbacks: defaultCallbacks,
461466
lookups: []lookup{
462467
lookup{"(key)1", goodValConstructor},
463-
lookup{"(key)1", goodValConstructor},
464-
lookup{"(key)1", goodValConstructor},
465-
lookup{"(key)1", badValConstructor},
468+
lookup{"(key)2", goodValConstructor},
469+
lookup{"(key)3", goodValConstructor},
466470
lookup{"(key)1", badValConstructor},
471+
lookup{"(key)2", badValConstructor},
472+
lookup{"(key)3", badValConstructor},
467473
},
468474
expected: expected{
469-
lookups: 5,
475+
lookups: 6,
470476
hits: 0,
471-
misses: 5,
477+
misses: 0,
472478
forcedEvictions: 0,
473479
manualEvictions: 0,
474480
// If racyEvictions is true, then we expect some positive number of evictions to occur.

prow/external-plugins/needs-rebase/plugin/plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const (
4343
dependabotUser = "dependabot[bot]"
4444
)
4545

46+
var sleepLock sync.Mutex
4647
var sleep = time.Sleep
4748

4849
type githubClient interface {

prow/external-plugins/needs-rebase/plugin/plugin_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ func TestHandleIssueCommentEvent(t *testing.T) {
162162
return &pr
163163
}
164164

165+
sleepLock.Lock()
165166
oldSleep := sleep
166167
sleep = func(time.Duration) {}
168+
sleepLock.Unlock()
167169
defer func() { sleep = oldSleep }()
168170

169171
testCases := []struct {
@@ -252,8 +254,10 @@ func TestHandleIssueCommentEvent(t *testing.T) {
252254

253255
func TestHandlePullRequestEvent(t *testing.T) {
254256
t.Parallel()
257+
sleepLock.Lock()
255258
oldSleep := sleep
256259
sleep = func(time.Duration) {}
260+
sleepLock.Unlock()
257261
defer func() { sleep = oldSleep }()
258262

259263
testCases := []struct {

prow/github/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ type client struct {
300300
identifier string
301301
gqlc gqlClient
302302
used bool
303+
mutUsed sync.Mutex // protects used
303304
*delegate
304305
}
305306

@@ -907,7 +908,10 @@ func NewFakeClient() Client {
907908
}
908909

909910
func (c *client) log(methodName string, args ...interface{}) (logDuration func()) {
911+
c.mutUsed.Lock()
910912
c.used = true
913+
c.mutUsed.Unlock()
914+
911915
if c.logger == nil {
912916
return func() {}
913917
}

0 commit comments

Comments
 (0)