mattn / go-sqlite3

sqlite3 driver for go using database/sql

Home Page:http://mattn.github.io/go-sqlite3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Race conditions during unit tests despite workarounds

stevarino opened this issue · comments

Just to check my prerequisites, I've already read through #1205 and the faq so I'm pretty sure I'm holding it right. :-D

Currently using mattn/go-sqlite3 v1.14.22 on go1.22.1 windows/amd64. Running on Windows 10 x64. GCC is gcc version 10.3.0 (tdm64-1).

Code

My unit tests call the following function at the beginning:

func getTestDatabase(t *testing.T, ctx context.Context) (*application.App, *Data) {
	r, err := application.New(ctx, application.AppOptions{
		Testing:    true,
		// the settings in question:
		SqlitePath: ":memory:"',
		SqliteOptions: map[string]string{
			"cache": "shared",
			"mode":  "rwc",
			// "vfs": "memdb",
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	db, err := New(ctx, r)
	if err != nil {
		t.Fatal(err)
	}
	return r, db
}

func Test_GetUser_NotFound(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	_, d := getTestDatabase(t, ctx)
	ur, err := d.GetUser("bob")
	if ur != nil || err != nil {
		t.Fatalf("GetUser() got (%v, %v), want (nil, nil)", ur, err)
	}
}

Test output:

...
2024/04/27 13:50:11 Opening database: file::memory:?cache=shared&mode=rwc
2024/04/27 13:50:11 Opening database: file::memory:?cache=shared&mode=rwc
--- FAIL: Test_GetUser_NotFound (0.00s)
    user_test.go:17: GetOrCreateUser() got (&{1 bob    {     } map[v9C8:true] map[] map[] map[] []}, <nil>), want (nil, nil)
...

The errors are very inconsistent: rerunning the test framework (go test ./... -count=5), tests will often pass or fail pretty randomly.

Some connection strings I have tried:

  • file::memory:?cache=shared&mode=rwc
  • file:/fake?vfs=memdb
  • file::memory:?cache=shared
  • file:/fake?mode=memory&vfs=memdb

What does seem to work:

Any of the above memory options, but setting the filename to a unique string for the given test. Notice below that I'm not even using cache=shared:

var (
	testID = 0
)

func getTestDatabase(t *testing.T, ctx context.Context) (*application.App, *Data) {
	app, err := application.New(ctx, application.AppOptions{
		Testing:    true,
		SqlitePath: fmt.Sprintf("/test%d", testID),
		SqliteOptions: map[string]string{
			// "cache": "shared",
			"mode": "rwc",
			"vfs":  "memdb",
		},
	})
	if err != nil {
		t.Fatal(err)
	}
	// Database options are consumed here.
	data, err := New(ctx, app)
	if err != nil {
		t.Fatal(err)
	}
	testID++
	return app, data
}

Posting this here to continue the discussion and hopefully help anyone else who gets caught with this error.

(Thank you for the awesome library, btw! Greatly enjoying learning go with it.)

I'm guessing you have another test case that inserts a user into a database returned by getTestDatabase, but neglects to defer db.Close() (or use t.Cleanup). Consequently, you may observe such side effects. See for reference https://www.sqlite.org/draft/inmemorydb.html

The database is automatically deleted and memory is reclaimed when the last connection to the database closes.

So I looked into it some more and I think what was happening was the database was being closed, but this was being handled in a go-routine listener thread. The race condition on my end was for the go-routine to notice and shut down the database connections before the test framework moved on to the next test.

I corrected this by making the defer'd shutdown function block until all the parts are properly shutdown. So I guess I was holding it wrong.

Unsure if this case should be documented so leaving it open for now, but if you don't feel that's necessary feel free to close. Thanks!