MCPcopy Index your code
hub / github.com/go-sql-driver/mysql / TestContextCancelQuery

Function TestContextCancelQuery

driver_test.go:2737–2779  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

2735}
2736
2737func TestContextCancelQuery(t *testing.T) {
2738 runTestsParallel(t, dsn, func(dbt *DBTest, tbl string) {
2739 dbt.mustExec("CREATE TABLE " + tbl + " (v INTEGER)")
2740 ctx, cancel := context.WithCancel(context.Background())
2741
2742 // Delay execution for just a bit until db.ExecContext has begun.
2743 defer time.AfterFunc(250*time.Millisecond, cancel).Stop()
2744
2745 // This query will be canceled.
2746 startTime := time.Now()
2747 if _, err := dbt.db.QueryContext(ctx, "INSERT INTO "+tbl+" VALUES (SLEEP(1))"); err != context.Canceled {
2748 dbt.Errorf("expected context.Canceled, got %v", err)
2749 }
2750 if d := time.Since(startTime); d > 500*time.Millisecond {
2751 dbt.Errorf("too long execution time: %s", d)
2752 }
2753
2754 // Wait for the INSERT query to be done.
2755 time.Sleep(time.Second)
2756
2757 // Check how many times the query is executed.
2758 var v int
2759 if err := dbt.db.QueryRow("SELECT COUNT(*) FROM " + tbl).Scan(&v); err != nil {
2760 dbt.Fatalf("%s", err.Error())
2761 }
2762 if v != 1 { // TODO: need to kill the query, and v should be 0.
2763 dbt.Skipf("[WARN] expected val to be 1, got %d", v)
2764 }
2765
2766 // Context is already canceled, so error should come before execution.
2767 if _, err := dbt.db.QueryContext(ctx, "INSERT INTO "+tbl+" VALUES (1)"); err != context.Canceled {
2768 dbt.Errorf("expected context.Canceled, got %v", err)
2769 }
2770
2771 // The second insert query will fail, so the table has no changes.
2772 if err := dbt.db.QueryRow("SELECT COUNT(*) FROM " + tbl).Scan(&v); err != nil {
2773 dbt.Fatalf("%s", err.Error())
2774 }
2775 if v != 1 {
2776 dbt.Skipf("[WARN] expected val to be 1, got %d", v)
2777 }
2778 })
2779}
2780
2781func TestContextCancelQueryRow(t *testing.T) {
2782 runTestsParallel(t, dsn, func(dbt *DBTest, tbl string) {

Callers

nothing calls this directly

Calls 5

runTestsParallelFunction · 0.85
mustExecMethod · 0.80
ScanMethod · 0.80
QueryContextMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…