-
Notifications
You must be signed in to change notification settings - Fork 53
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
test: Improve change detector performance #1433
Merged
AndrewSisley
merged 8 commits into
sourcenetwork:develop
from
AndrewSisley:1186-change-detector
May 1, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ca04c4f
Refactor out temp test dir
AndrewSisley f2ade88
Change defra temp dir
AndrewSisley f8fae70
Namespace change detector temp dir
AndrewSisley f91e31b
Flip code namespace
AndrewSisley a2ae7a1
Refactor out getting of test package path
AndrewSisley 2f98115
Disable change detector for explain
AndrewSisley 34ddd9d
Do change-detector setup once per package
AndrewSisley 06d445c
Add breaking change file
AndrewSisley 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 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,5 @@ | ||
# Modify change detector setup flow | ||
|
||
This is not a breaking (production code) change. | ||
|
||
This does however change the way the change detector sets up the target branch in a way that is incompatible with the current develop, this file is required to allow the CI to pass (including comparing develop vs master). |
This file contains 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 |
---|---|---|
|
@@ -14,16 +14,18 @@ import ( | |
"context" | ||
"fmt" | ||
"io/fs" | ||
"math/rand" | ||
"os" | ||
"os/exec" | ||
"path" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/sourcenetwork/defradb/client" | ||
"time" | ||
) | ||
|
||
var skip bool | ||
|
||
func IsDetectingDbChanges() bool { | ||
return DetectDbChanges | ||
} | ||
|
@@ -33,6 +35,10 @@ func DetectDbChangesPreTestChecks( | |
t *testing.T, | ||
collectionNames []string, | ||
) bool { | ||
if skip { | ||
t.SkipNow() | ||
} | ||
|
||
if previousTestCaseTestName == t.Name() { | ||
// The database format changer currently only supports running the first test | ||
// case, if a second case is detected we return early | ||
|
@@ -64,10 +70,21 @@ func detectDbChangesInit(repository string, targetBranch string) { | |
return | ||
} | ||
|
||
tempDir := os.TempDir() | ||
defraTempDir := path.Join(os.TempDir(), "defradb") | ||
changeDetectorTempDir := path.Join(defraTempDir, "tests", "changeDetector") | ||
|
||
latestTargetCommitHash := getLatestCommit(repository, targetBranch) | ||
detectDbChangesCodeDir = path.Join(tempDir, "defra", latestTargetCommitHash, "code") | ||
detectDbChangesCodeDir = path.Join(changeDetectorTempDir, "code", latestTargetCommitHash) | ||
rand.Seed(time.Now().Unix()) | ||
randNumber := rand.Int() | ||
dbsDir := path.Join(changeDetectorTempDir, "dbs", fmt.Sprint(randNumber)) | ||
|
||
testPackagePath, isIntegrationTest := getTestPackagePath() | ||
if !isIntegrationTest { | ||
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. question: When would that not be an integration test? 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. The bench suite uses this, and there might be other references too. |
||
skip = true | ||
return | ||
} | ||
rootDatabaseDir = path.Join(dbsDir, strings.ReplaceAll(testPackagePath, "/", "_")) | ||
|
||
_, err := os.Stat(detectDbChangesCodeDir) | ||
// Warning - there is a race condition here, where if running multiple packages in | ||
|
@@ -110,22 +127,12 @@ func detectDbChangesInit(repository string, targetBranch string) { | |
} | ||
|
||
areDatabaseFormatChangesDocumented = checkIfDatabaseFormatChangesAreDocumented() | ||
} | ||
|
||
func SetupDatabaseUsingTargetBranch( | ||
ctx context.Context, | ||
t *testing.T, | ||
collectionNames []string, | ||
) client.DB { | ||
currentTestPackage, err := os.Getwd() | ||
if err != nil { | ||
panic(err) | ||
if areDatabaseFormatChangesDocumented { | ||
// Dont bother doing anything if the changes are documented | ||
return | ||
} | ||
|
||
targetTestPackage := detectDbChangesCodeDir + "/tests/integration/" + strings.Split( | ||
currentTestPackage, | ||
"/tests/integration/", | ||
)[1] | ||
targetTestPackage := detectDbChangesCodeDir + "/tests/integration/" + testPackagePath | ||
|
||
// If we are checking for database changes, and we are not seting up the database, | ||
// then we must be in the main test process, and need to create a new process | ||
|
@@ -135,55 +142,41 @@ func SetupDatabaseUsingTargetBranch( | |
"go", | ||
"test", | ||
"./...", | ||
"--run", | ||
fmt.Sprintf("^%s$", t.Name()), | ||
"-v", | ||
) | ||
|
||
path := t.TempDir() | ||
|
||
goTestCmd.Dir = targetTestPackage | ||
goTestCmd.Env = os.Environ() | ||
goTestCmd.Env = append( | ||
goTestCmd.Env, | ||
setupOnlyEnvName+"=true", | ||
fileBadgerPathEnvName+"="+path, | ||
rootDBFilePathEnvName+"="+rootDatabaseDir, | ||
) | ||
out, err := goTestCmd.Output() | ||
|
||
if err != nil { | ||
// If file is not found - this must be a new test and | ||
// doesn't exist in the target branch, so we pass it | ||
// because the child process tries to run the test, but | ||
// if it doesnt find it, the parent test should pass (not panic). | ||
if strings.Contains(err.Error(), ": no such file or directory") { | ||
t.SkipNow() | ||
} else { | ||
// Only log the output if there is an error different from above, | ||
// logging child test runs confuses the go test runner making it | ||
// think there are no tests in the parent run (it will still | ||
// run everything though)! | ||
log.ErrorE(ctx, string(out), err) | ||
panic(err) | ||
} | ||
log.ErrorE(context.TODO(), string(out), err) | ||
panic(err) | ||
} | ||
} | ||
|
||
refreshedDb, err := newBadgerFileDB(ctx, t, path) | ||
// getTestPackagePath returns the path to the package currently under test, relative | ||
// to `./tests/integration/`. Will return an empty string and false if the tests | ||
// are not within that directory. | ||
func getTestPackagePath() (string, bool) { | ||
currentTestPackage, err := os.Getwd() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
_, err = refreshedDb.GetCollectionByName(ctx, collectionNames[0]) | ||
if err != nil { | ||
if err.Error() == "datastore: key not found" { | ||
// If collection is not found - this must be a new test and | ||
// doesn't exist in the target branch, so we pass it | ||
t.SkipNow() | ||
} else { | ||
panic(err) | ||
} | ||
splitPath := strings.Split( | ||
currentTestPackage, | ||
"/tests/integration/", | ||
) | ||
|
||
if len(splitPath) != 2 { | ||
return "", false | ||
} | ||
return refreshedDb | ||
return splitPath[1], true | ||
} | ||
|
||
func checkIfDatabaseFormatChangesAreDocumented() bool { | ||
|
This file contains 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 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
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.
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.
nitpick: