Skip to content

Commit

Permalink
Retain the setup connection to ensure shared cache does not get busted
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainMuller committed Nov 22, 2024
1 parent a004fcc commit 362dce3
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions _integration-tests/tests/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,38 +20,49 @@ import (

type TestCase struct {
*sql.DB
// untraced is the un-traced database connection, used to set up the test case
// without producing unnecessary spans. It is retained as a [TestCase] field
// and cleaned up using [t.Cleanup] to ensure the shared cache is not lost, as
// it stops existing once the last DB connection using it is closed.
untraced *sql.DB
}

func (tc *TestCase) Setup(t *testing.T) {
const (
dn = "sqlite3"
dsn = "file::memory:?cache=shared"
)

var err error

//orchestrion:ignore
prepareConn, err := sql.Open("sqlite3", "file::memory:?cache=shared")
tc.untraced, err = sql.Open(dn, dsn)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, tc.untraced.Close()) })

func() {
defer prepareConn.Close()
ctx := context.Background()

_, err = prepareConn.ExecContext(context.Background(),
`CREATE TABLE IF NOT EXISTS notes (
_, err = tc.untraced.ExecContext(ctx,
`CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
userid INTEGER,
content STRING,
created STRING
)`)
require.NoError(t, err)
require.NoError(t, err)

_, err = prepareConn.ExecContext(context.Background(),
`INSERT OR REPLACE INTO notes(userid, content, created) VALUES
_, err = tc.untraced.ExecContext(ctx,
`INSERT OR REPLACE INTO notes(userid, content, created) VALUES
(1, 'Hello, John. This is John. You are leaving a note for yourself. You are welcome and thank you.', datetime('now')),
(1, 'Hey, remember to mow the lawn.', datetime('now')),
(2, 'Reminder to submit that report by Thursday.', datetime('now')),
(2, 'Opportunities don''t happen, you create them.', datetime('now')),
(3, 'Pick up cabbage from the store on the way home.', datetime('now')),
(3, 'Review PR #1138', datetime('now')
)`)
require.NoError(t, err)
}()
require.NoError(t, err)

tc.DB, err = sql.Open("sqlite3", "file::memory:?cache=shared")
tc.DB, err = sql.Open(dn, dsn)
require.NoError(t, err)
t.Cleanup(func() {
assert.NoError(t, tc.DB.Close())
Expand Down

0 comments on commit 362dce3

Please sign in to comment.