-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add leak checking for vtgate tests #13835
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
Merged
dbussink
merged 17 commits into
vitessio:main
from
planetscale:dbussink/refactor-vtgate-tests
Aug 28, 2023
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
33dd285
Add leak checking for vtgate tests
dbussink 0ad34ce
Remove global primary session
dbussink 47b0e3a
Pass in context for all topos
dbussink f34e71f
vtgate: Remove globals
dbussink 7798273
Fix additional tests and cleanup
dbussink 48b36dc
Plug more leaks
dbussink 478a4df
Cleanup tests
dbussink 864a528
Plug some tabletmanager/vreplication unit test leaks: stats and rowst…
rohit-nayak-ps cfe7a28
Added utility GetLeaks() to log leaks esp. in TestMain() and related …
rohit-nayak-ps be9e89f
Duh, defer stats.Stop()
rohit-nayak-ps b81debc
License
rohit-nayak-ps 649a7a4
Change plain utils.EnsureNoLeaks(0 to defers
rohit-nayak-ps 959cff2
Found testing.tRunner.func1 for TestDeleteShards, for now adding to a…
rohit-nayak-ps d52a492
vttest: use testing.T.Cleanup
vmg fbcaba4
utils: documentat the two new noleak APIs
vmg 488a155
Fix rebased tests
dbussink 27ab83e
Allow additional throttler for leak check
dbussink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -250,3 +250,7 @@ func (lru *LRUCache) checkCapacity() { | |
| lru.evictions++ | ||
| } | ||
| } | ||
|
|
||
| func (lru *LRUCache) Close() { | ||
| lru.Clear() | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| /* | ||
| Copyright 2023 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package utils | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "os/exec" | ||
| "strconv" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "go.uber.org/goleak" | ||
|
|
||
| "vitess.io/vitess/go/vt/log" | ||
| ) | ||
|
|
||
| // LeakCheckContext returns a Context that will be automatically cancelled at the end | ||
| // of this test. If the test has finished successfully, it will be checked for goroutine | ||
| // leaks after context cancellation. | ||
| func LeakCheckContext(t testing.TB) context.Context { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| t.Cleanup(func() { | ||
| cancel() | ||
| EnsureNoLeaks(t) | ||
| }) | ||
| return ctx | ||
| } | ||
|
|
||
| // LeakCheckContextTimeout behaves like LeakCheckContext but the returned Context will | ||
| // be cancelled after `timeout`, or after the test finishes, whichever happens first. | ||
| func LeakCheckContextTimeout(t testing.TB, timeout time.Duration) context.Context { | ||
| ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
| t.Cleanup(func() { | ||
| cancel() | ||
| EnsureNoLeaks(t) | ||
| }) | ||
| return ctx | ||
| } | ||
|
|
||
| // EnsureNoLeaks checks for goroutine and socket leaks and fails the test if any are found. | ||
| func EnsureNoLeaks(t testing.TB) { | ||
| if t.Failed() { | ||
| return | ||
| } | ||
| if err := ensureNoLeaks(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| // GetLeaks checks for goroutine and socket leaks and returns an error if any are found. | ||
| // One use case is in TestMain()s to ensure that all tests are cleaned up. | ||
| func GetLeaks() error { | ||
| return ensureNoLeaks() | ||
| } | ||
|
|
||
| func ensureNoLeaks() error { | ||
| if err := ensureNoGoroutines(); err != nil { | ||
| return err | ||
| } | ||
| if err := ensureNoOpenSockets(); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func ensureNoGoroutines() error { | ||
| var ignored = []goleak.Option{ | ||
| goleak.IgnoreTopFunction("github.com/golang/glog.(*fileSink).flushDaemon"), | ||
| goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"), | ||
| goleak.IgnoreTopFunction("vitess.io/vitess/go/vt/dbconfigs.init.0.func1"), | ||
| goleak.IgnoreTopFunction("vitess.io/vitess/go/vt/vtgate.resetAggregators"), | ||
| goleak.IgnoreTopFunction("vitess.io/vitess/go/vt/vtgate.processQueryInfo"), | ||
| goleak.IgnoreTopFunction("github.com/patrickmn/go-cache.(*janitor).Run"), | ||
| goleak.IgnoreTopFunction("vitess.io/vitess/go/vt/logutil.(*ThrottledLogger).log.func1"), | ||
| goleak.IgnoreTopFunction("vitess.io/vitess/go/vt/vttablet/tabletserver/throttle.initThrottleTicker.func1.1"), | ||
| goleak.IgnoreTopFunction("vitess.io/vitess/go/vt/vttablet/tabletserver/throttle.NewBackgroundClient.initThrottleTicker.func1.1"), | ||
| goleak.IgnoreTopFunction("testing.tRunner.func1"), | ||
| } | ||
|
|
||
| var err error | ||
| for i := 0; i < 5; i++ { | ||
| err = goleak.Find(ignored...) | ||
| if err == nil { | ||
| return nil | ||
| } | ||
| time.Sleep(100 * time.Millisecond) | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func ensureNoOpenSockets() error { | ||
| cmd := exec.Command("lsof", "-a", "-p", strconv.Itoa(os.Getpid()), "-i", "-P", "-V") | ||
| cmd.Stderr = nil | ||
| lsof, err := cmd.Output() | ||
| if err == nil { | ||
| log.Errorf("found open sockets:\n%s", lsof) | ||
| } else { | ||
| if strings.Contains(string(lsof), "no Internet files located") { | ||
| return nil | ||
| } | ||
| log.Errorf("failed to run `lsof`: %v (%q)", err, lsof) | ||
| } | ||
| return err | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.