Skip to content
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 test race condition; remove verbose global #457

Merged
merged 9 commits into from
Jan 28, 2023
Merged

fix test race condition; remove verbose global #457

merged 9 commits into from
Jan 28, 2023

Conversation

mfridman
Copy link
Collaborator

@mfridman mfridman commented Jan 28, 2023

I noticed a race condition in the tests, having to do with setting verbose globally in sqlparser.

https://github.com/pressly/goose/actions/runs/4032205783/jobs/6931938495

This PR updates the internal/sqlparser. Changes the stateMachine to a well-defined struct which we pass around as a pointer. The caller sets the direction (well-defined) and the verbose bool, no more global state in sqlparser.

For convenience, one can set env DEBUG_TEST=true in tests to see those verbose parsing statements.

DEBUG_TEST=true go test -race -count=1 -v -timeout=10m ./internal/sqlparser/...

@@ -11,6 +11,20 @@ import (
"sync"
)

type Direction string
Copy link
Collaborator Author

@mfridman mfridman Jan 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ParseSQLMigration took 2 booleans with a signature like (r io.Reader, direction, verbose bool).

It's waaaay too easy for the caller to mix up the booleans, so instead, the second param is now a well-defined type to avoid any footguns.

@@ -23,15 +37,37 @@ const (
gooseStatementEndDown // 6
)

type stateMachine parserState
type stateMachine struct {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to attach the verbose bool to the struct and hang a print helper.

We pass around the stateMachine struct as a pointer.

@@ -73,6 +74,11 @@ func TestMain(m *testing.M) {
migrationsDir = filepath.Join("testdata", *dialect, "migrations")
seedDir = filepath.Join("testdata", *dialect, "seed")

if err := goose.SetDialect(*dialect); err != nil {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dialect is set once within the e2e tests, we can do this here in TestMain.

@mfridman mfridman merged commit b4af752 into master Jan 28, 2023
@mfridman mfridman deleted the fix-test branch January 28, 2023 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant