-
Notifications
You must be signed in to change notification settings - Fork 38.6k
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
fix staticcheck failures: apiserver/pkg/{storage,util} #95683
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -650,6 +650,7 @@ func TestCacherNoLeakWithMultipleWatchers(t *testing.T) { | |||||||
stopCh := make(chan struct{}) | ||||||||
time.AfterFunc(3*time.Second, func() { close(stopCh) }) | ||||||||
|
||||||||
errorCh := make(chan error, 1) | ||||||||
wg := &sync.WaitGroup{} | ||||||||
|
||||||||
wg.Add(1) | ||||||||
|
@@ -663,7 +664,8 @@ func TestCacherNoLeakWithMultipleWatchers(t *testing.T) { | |||||||
ctx, _ := context.WithTimeout(context.Background(), 3*time.Second) | ||||||||
w, err := cacher.Watch(ctx, "pods/ns", storage.ListOptions{ResourceVersion: "0", Predicate: pred}) | ||||||||
if err != nil { | ||||||||
t.Fatalf("Failed to create watch: %v", err) | ||||||||
errorCh <- fmt.Errorf("Failed to create watch: %v", err) | ||||||||
return | ||||||||
} | ||||||||
w.Stop() | ||||||||
} | ||||||||
|
@@ -682,9 +684,15 @@ func TestCacherNoLeakWithMultipleWatchers(t *testing.T) { | |||||||
} | ||||||||
} | ||||||||
}() | ||||||||
go func() { | ||||||||
// wait for adding/removing watchers to end | ||||||||
wg.Wait() | ||||||||
close(errorCh) | ||||||||
}() | ||||||||
|
||||||||
// wait for adding/removing watchers to end | ||||||||
wg.Wait() | ||||||||
if err = <-errorCh; err != nil { | ||||||||
t.Fatal(err) | ||||||||
} | ||||||||
|
||||||||
// wait out the expiration period and pop expired watchers | ||||||||
time.Sleep(2 * time.Second) | ||||||||
|
@@ -720,6 +728,7 @@ func testCacherSendBookmarkEvents(t *testing.T, allowWatchBookmarks, expectedBoo | |||||||
t.Fatalf("Failed to create watch: %v", err) | ||||||||
} | ||||||||
|
||||||||
errorCh := make(chan error, 1) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defer close this channel? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't think we'll need to close this since it's only used in a |
||||||||
resourceVersion := uint64(1000) | ||||||||
go func() { | ||||||||
deadline := time.Now().Add(time.Second) | ||||||||
|
@@ -731,7 +740,7 @@ func testCacherSendBookmarkEvents(t *testing.T, allowWatchBookmarks, expectedBoo | |||||||
ResourceVersion: fmt.Sprintf("%v", resourceVersion+uint64(i)), | ||||||||
}}) | ||||||||
if err != nil { | ||||||||
t.Fatalf("failed to add a pod: %v", err) | ||||||||
errorCh <- fmt.Errorf("failed to add a pod: %v", err) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Upon getting a non-nil error, we should return here? (as apposed to keep trying in the loop) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, will add it.
Suggested change
|
||||||||
} | ||||||||
time.Sleep(100 * time.Millisecond) | ||||||||
} | ||||||||
|
@@ -765,6 +774,10 @@ func testCacherSendBookmarkEvents(t *testing.T, allowWatchBookmarks, expectedBoo | |||||||
t.Fatal("Unexpected timeout to receive a bookmark event") | ||||||||
} | ||||||||
return | ||||||||
case err = <-errorCh: // Error from goroutine | ||||||||
if err != nil { | ||||||||
t.Fatal(err) | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
@@ -924,7 +937,6 @@ func TestDispatchingBookmarkEventsWithConcurrentStop(t *testing.T) { | |||||||
|
||||||||
select { | ||||||||
case <-done: | ||||||||
break | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this is removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because this is an ineffective break,
|
||||||||
case <-time.After(time.Second): | ||||||||
t.Fatal("receive result timeout") | ||||||||
} | ||||||||
|
@@ -974,28 +986,45 @@ func TestBookmarksOnResourceVersionUpdates(t *testing.T) { | |||||||
expectedRV := 2000 | ||||||||
|
||||||||
wg := sync.WaitGroup{} | ||||||||
done := make(chan struct{}) | ||||||||
errorCh := make(chan error, 1) | ||||||||
wg.Add(1) | ||||||||
go func() { | ||||||||
defer wg.Done() | ||||||||
for { | ||||||||
event, ok := <-w.ResultChan() | ||||||||
if !ok { | ||||||||
t.Fatalf("Unexpected closed channel") | ||||||||
} | ||||||||
rv, err := cacher.versioner.ObjectResourceVersion(event.Object) | ||||||||
if err != nil { | ||||||||
t.Errorf("failed to parse resource version from %#v: %v", event.Object, err) | ||||||||
} | ||||||||
if event.Type == watch.Bookmark && rv == uint64(expectedRV) { | ||||||||
select { | ||||||||
case <-done: | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I do not follow why we need the done channel to exit the function? Aren't the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My bad 😄, this is actually not needed here (I had in mind this is multiple goroutines, but it's only one) |
||||||||
return | ||||||||
default: | ||||||||
event, ok := <-w.ResultChan() | ||||||||
if !ok { | ||||||||
errorCh <- fmt.Errorf("Unexpected closed channel") | ||||||||
return | ||||||||
} | ||||||||
rv, err := cacher.versioner.ObjectResourceVersion(event.Object) | ||||||||
if err != nil { | ||||||||
errorCh <- fmt.Errorf("failed to parse resource version from %#v: %v", event.Object, err) | ||||||||
return | ||||||||
} | ||||||||
if event.Type == watch.Bookmark && rv == uint64(expectedRV) { | ||||||||
return | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
}() | ||||||||
|
||||||||
// Simulate progress notify event. | ||||||||
cacher.watchCache.UpdateResourceVersion(strconv.Itoa(expectedRV)) | ||||||||
|
||||||||
wg.Wait() | ||||||||
go func() { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, not sure why a new go routine is needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, to avoid blocking and allow receive on |
||||||||
wg.Wait() | ||||||||
close(errorCh) | ||||||||
}() | ||||||||
|
||||||||
if err = <-errorCh; err != nil { | ||||||||
close(done) | ||||||||
t.Fatal(err) | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
type fakeTimeBudget struct{} | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,8 @@ func TestCachingObjectRaces(t *testing.T) { | |
|
||
numWorkers := 1000 | ||
wg := &sync.WaitGroup{} | ||
errorCh := make(chan error, numWorkers) | ||
|
||
wg.Add(numWorkers) | ||
|
||
for i := 0; i < numWorkers; i++ { | ||
|
@@ -141,20 +143,34 @@ func TestCachingObjectRaces(t *testing.T) { | |
for _, encoder := range encoders { | ||
buffer.Reset() | ||
if err := object.CacheEncode(encoder.identifier, encoder.encode, buffer); err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
errorCh <- fmt.Errorf("unexpected error: %v", err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the staticcheck complaining these "Errorf" statements? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, the staticcheck is complaining about the |
||
return | ||
} | ||
if callsNumber := atomic.LoadInt32(&encoder.callsNumber); callsNumber != 1 { | ||
t.Errorf("unexpected number of serializations: %d", callsNumber) | ||
errorCh <- fmt.Errorf("unexpected number of serializations: %d", callsNumber) | ||
return | ||
} | ||
} | ||
accessor, err := meta.Accessor(object.GetObject()) | ||
if err != nil { | ||
t.Fatalf("failed to get accessor: %v", err) | ||
errorCh <- fmt.Errorf("failed to get accessor: %v", err) | ||
return | ||
} | ||
if selfLink := accessor.GetSelfLink(); selfLink != "selfLink" { | ||
t.Errorf("unexpected selfLink: %s", selfLink) | ||
errorCh <- fmt.Errorf("unexpected selfLink: %s", selfLink) | ||
return | ||
} | ||
}() | ||
} | ||
wg.Wait() | ||
|
||
go func() { | ||
wg.Wait() | ||
close(errorCh) | ||
}() | ||
|
||
for err := range errorCh { | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I do not follow why we need a new go routine here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is so we don't block and allow receive on errorCh on line 693
if err = <- errorCh; err != nil {...}