Skip to content

Commit

Permalink
[CALCITE-5324] Cancel context in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
F21 committed Oct 9, 2022
1 parent 50ab73a commit b0a42d1
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 35 deletions.
39 changes: 24 additions & 15 deletions driver_go18_hsqldb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ func TestHSQLDBContext(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {

// Create and seed table
dbt.mustExecContext(getContext(), "CREATE TABLE "+dbt.tableName+" (id BIGINT PRIMARY KEY, val VARCHAR(1))")
ctx, cancel := getContext()
defer cancel()

dbt.mustExecContext(getContext(), "INSERT INTO "+dbt.tableName+" VALUES (1,'A')")
dbt.mustExecContext(ctx, "CREATE TABLE "+dbt.tableName+" (id BIGINT PRIMARY KEY, val VARCHAR(1))")

dbt.mustExecContext(getContext(), "INSERT INTO "+dbt.tableName+" VALUES (2,'B')")
dbt.mustExecContext(ctx, "INSERT INTO "+dbt.tableName+" VALUES (1,'A')")

rows := dbt.mustQueryContext(getContext(), "SELECT COUNT(*) FROM "+dbt.tableName)
dbt.mustExecContext(ctx, "INSERT INTO "+dbt.tableName+" VALUES (2,'B')")

rows := dbt.mustQueryContext(ctx, "SELECT COUNT(*) FROM "+dbt.tableName)
defer rows.Close()

for rows.Next() {
Expand All @@ -59,25 +62,25 @@ func TestHSQLDBContext(t *testing.T) {
}

// Test transactions and prepared statements
_, err := dbt.db.BeginTx(getContext(), &sql.TxOptions{Isolation: sql.LevelReadUncommitted, ReadOnly: true})
_, err := dbt.db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadUncommitted, ReadOnly: true})

if err == nil {
t.Error("Expected an error while creating a read only transaction, but no error was returned")
}

tx, err := dbt.db.BeginTx(getContext(), &sql.TxOptions{Isolation: sql.LevelReadCommitted})
tx, err := dbt.db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})

if err != nil {
t.Errorf("Unexpected error while creating transaction: %s", err)
}

stmt, err := tx.PrepareContext(getContext(), "INSERT INTO "+dbt.tableName+" VALUES(?,?)")
stmt, err := tx.PrepareContext(ctx, "INSERT INTO "+dbt.tableName+" VALUES(?,?)")

if err != nil {
t.Errorf("Unexpected error while preparing statement: %s", err)
}

res, err := stmt.ExecContext(getContext(), 3, "C")
res, err := stmt.ExecContext(ctx, 3, "C")

if err != nil {
t.Errorf("Unexpected error while executing statement: %s", err)
Expand All @@ -99,13 +102,13 @@ func TestHSQLDBContext(t *testing.T) {
t.Errorf("Error committing transaction: %s", err)
}

stmt2, err := dbt.db.PrepareContext(getContext(), "SELECT * FROM "+dbt.tableName+" WHERE id = ?")
stmt2, err := dbt.db.PrepareContext(ctx, "SELECT * FROM "+dbt.tableName+" WHERE id = ?")

if err != nil {
t.Errorf("Error preparing statement: %s", err)
}

row := stmt2.QueryRowContext(getContext(), 3)
row := stmt2.QueryRowContext(ctx, 3)

if err != nil {
t.Errorf("Error querying for row: %s", err)
Expand Down Expand Up @@ -138,13 +141,16 @@ func TestHSQLDBMultipleResultSets(t *testing.T) {

runTests(t, dsn, func(dbt *DBTest) {
// Create and seed table
dbt.mustExecContext(getContext(), "CREATE TABLE "+dbt.tableName+" (id BIGINT PRIMARY KEY, val VARCHAR(1))")
ctx, cancel := getContext()
defer cancel()

dbt.mustExecContext(ctx, "CREATE TABLE "+dbt.tableName+" (id BIGINT PRIMARY KEY, val VARCHAR(1))")

dbt.mustExecContext(getContext(), "INSERT INTO "+dbt.tableName+" VALUES (1,'A')")
dbt.mustExecContext(ctx, "INSERT INTO "+dbt.tableName+" VALUES (1,'A')")

dbt.mustExecContext(getContext(), "INSERT INTO "+dbt.tableName+" VALUES (2,'B')")
dbt.mustExecContext(ctx, "INSERT INTO "+dbt.tableName+" VALUES (2,'B')")

rows, err := dbt.db.QueryContext(getContext(), "SELECT * FROM "+dbt.tableName+" WHERE id = 1")
rows, err := dbt.db.QueryContext(ctx, "SELECT * FROM "+dbt.tableName+" WHERE id = 1")

if err != nil {
t.Errorf("Unexpected error while executing query: %s", err)
Expand Down Expand Up @@ -203,7 +209,10 @@ func TestHSQLDBColumnTypes(t *testing.T) {
)`)

// Select
rows, err := dbt.db.QueryContext(getContext(), "SELECT * FROM "+dbt.tableName)
ctx, cancel := getContext()
defer cancel()

rows, err := dbt.db.QueryContext(ctx, "SELECT * FROM "+dbt.tableName)

if err != nil {
t.Errorf("Unexpected error while selecting from table: %s", err)
Expand Down
39 changes: 24 additions & 15 deletions driver_go18_phoenix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ func TestPhoenixContext(t *testing.T) {
runTests(t, dsn, func(dbt *DBTest) {

// Create and seed table
dbt.mustExecContext(getContext(), "CREATE TABLE "+dbt.tableName+" (id BIGINT PRIMARY KEY, val VARCHAR) TRANSACTIONAL=false")
ctx, cancel := getContext()
defer cancel()

dbt.mustExecContext(getContext(), "UPSERT INTO "+dbt.tableName+" VALUES (1,'A')")
dbt.mustExecContext(ctx, "CREATE TABLE "+dbt.tableName+" (id BIGINT PRIMARY KEY, val VARCHAR) TRANSACTIONAL=false")

dbt.mustExecContext(getContext(), "UPSERT INTO "+dbt.tableName+" VALUES (2,'B')")
dbt.mustExecContext(ctx, "UPSERT INTO "+dbt.tableName+" VALUES (1,'A')")

rows := dbt.mustQueryContext(getContext(), "SELECT COUNT(*) FROM "+dbt.tableName)
dbt.mustExecContext(ctx, "UPSERT INTO "+dbt.tableName+" VALUES (2,'B')")

rows := dbt.mustQueryContext(ctx, "SELECT COUNT(*) FROM "+dbt.tableName)
defer rows.Close()

for rows.Next() {
Expand All @@ -59,25 +62,25 @@ func TestPhoenixContext(t *testing.T) {
}

// Test transactions and prepared statements
_, err := dbt.db.BeginTx(getContext(), &sql.TxOptions{Isolation: sql.LevelReadUncommitted, ReadOnly: true})
_, err := dbt.db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadUncommitted, ReadOnly: true})

if err == nil {
t.Error("Expected an error while creating a read only transaction, but no error was returned")
}

tx, err := dbt.db.BeginTx(getContext(), &sql.TxOptions{Isolation: sql.LevelReadCommitted})
tx, err := dbt.db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})

if err != nil {
t.Errorf("Unexpected error while creating transaction: %s", err)
}

stmt, err := tx.PrepareContext(getContext(), "UPSERT INTO "+dbt.tableName+" VALUES(?,?)")
stmt, err := tx.PrepareContext(ctx, "UPSERT INTO "+dbt.tableName+" VALUES(?,?)")

if err != nil {
t.Errorf("Unexpected error while preparing statement: %s", err)
}

res, err := stmt.ExecContext(getContext(), 3, "C")
res, err := stmt.ExecContext(ctx, 3, "C")

if err != nil {
t.Errorf("Unexpected error while executing statement: %s", err)
Expand All @@ -99,13 +102,13 @@ func TestPhoenixContext(t *testing.T) {
t.Errorf("Error committing transaction: %s", err)
}

stmt2, err := dbt.db.PrepareContext(getContext(), "SELECT * FROM "+dbt.tableName+" WHERE id = ?")
stmt2, err := dbt.db.PrepareContext(ctx, "SELECT * FROM "+dbt.tableName+" WHERE id = ?")

if err != nil {
t.Errorf("Error preparing statement: %s", err)
}

row := stmt2.QueryRowContext(getContext(), 3)
row := stmt2.QueryRowContext(ctx, 3)

if err != nil {
t.Errorf("Error querying for row: %s", err)
Expand Down Expand Up @@ -138,13 +141,16 @@ func TestPhoenixMultipleResultSets(t *testing.T) {

runTests(t, dsn, func(dbt *DBTest) {
// Create and seed table
dbt.mustExecContext(getContext(), "CREATE TABLE "+dbt.tableName+" (id BIGINT PRIMARY KEY, val VARCHAR) TRANSACTIONAL=false")
ctx, cancel := getContext()
defer cancel()

dbt.mustExecContext(ctx, "CREATE TABLE "+dbt.tableName+" (id BIGINT PRIMARY KEY, val VARCHAR) TRANSACTIONAL=false")

dbt.mustExecContext(getContext(), "UPSERT INTO "+dbt.tableName+" VALUES (1,'A')")
dbt.mustExecContext(ctx, "UPSERT INTO "+dbt.tableName+" VALUES (1,'A')")

dbt.mustExecContext(getContext(), "UPSERT INTO "+dbt.tableName+" VALUES (2,'B')")
dbt.mustExecContext(ctx, "UPSERT INTO "+dbt.tableName+" VALUES (2,'B')")

rows, err := dbt.db.QueryContext(getContext(), "SELECT * FROM "+dbt.tableName+" WHERE id = 1")
rows, err := dbt.db.QueryContext(ctx, "SELECT * FROM "+dbt.tableName+" WHERE id = 1")

if err != nil {
t.Errorf("Unexpected error while executing query: %s", err)
Expand Down Expand Up @@ -213,7 +219,10 @@ func TestPhoenixColumnTypes(t *testing.T) {
) TRANSACTIONAL=false`)

// Select
rows, err := dbt.db.QueryContext(getContext(), "SELECT * FROM "+dbt.tableName)
ctx, cancel := getContext()
defer cancel()

rows, err := dbt.db.QueryContext(ctx, "SELECT * FROM "+dbt.tableName)

if err != nil {
t.Errorf("Unexpected error while selecting from table: %s", err)
Expand Down
6 changes: 2 additions & 4 deletions driver_go18_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ func (dbt *DBTest) mustQueryContext(ctx context.Context, query string, args ...i
return rows
}

func getContext() context.Context {
ctx, _ := context.WithTimeout(context.Background(), 4*time.Minute)

return ctx
func getContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), 4*time.Minute)
}

func TestPing(t *testing.T) {
Expand Down
4 changes: 3 additions & 1 deletion driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func init() {
dsn = serverAddr

// Wait for the avatica server to be ready
ctx, _ := context.WithTimeout(context.Background(), 5*time.Minute)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()

ticker := time.NewTicker(2 * time.Second)

for {
Expand Down

0 comments on commit b0a42d1

Please sign in to comment.