This is an automated email from the ASF dual-hosted git repository. francischuang pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/calcite-avatica-go.git
commit b0a42d115264c1d3df6c6ece39b2b3e9f460a757 Author: Francis Chuang <[email protected]> AuthorDate: Mon Oct 10 09:30:29 2022 +1100 [CALCITE-5324] Cancel context in tests --- driver_go18_hsqldb_test.go | 39 ++++++++++++++++++++++++--------------- driver_go18_phoenix_test.go | 39 ++++++++++++++++++++++++--------------- driver_go18_test.go | 6 ++---- driver_test.go | 4 +++- 4 files changed, 53 insertions(+), 35 deletions(-) diff --git a/driver_go18_hsqldb_test.go b/driver_go18_hsqldb_test.go index 17b2fff..b1f518d 100644 --- a/driver_go18_hsqldb_test.go +++ b/driver_go18_hsqldb_test.go @@ -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() { @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/driver_go18_phoenix_test.go b/driver_go18_phoenix_test.go index c119b99..9b143b2 100644 --- a/driver_go18_phoenix_test.go +++ b/driver_go18_phoenix_test.go @@ -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() { @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/driver_go18_test.go b/driver_go18_test.go index f1414c1..701505c 100644 --- a/driver_go18_test.go +++ b/driver_go18_test.go @@ -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) { diff --git a/driver_test.go b/driver_test.go index 39f648a..808ebfb 100644 --- a/driver_test.go +++ b/driver_test.go @@ -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 {
