(t *testing.T)
| 2689 | } |
| 2690 | |
| 2691 | func TestContextCancelExec(t *testing.T) { |
| 2692 | runTestsParallel(t, dsn, func(dbt *DBTest, tbl string) { |
| 2693 | dbt.mustExec("CREATE TABLE " + tbl + " (v INTEGER)") |
| 2694 | ctx, cancel := context.WithCancel(context.Background()) |
| 2695 | |
| 2696 | // Delay execution for just a bit until db.ExecContext has begun. |
| 2697 | defer time.AfterFunc(250*time.Millisecond, cancel).Stop() |
| 2698 | |
| 2699 | // This query will be canceled. |
| 2700 | startTime := time.Now() |
| 2701 | if _, err := dbt.db.ExecContext(ctx, "INSERT INTO "+tbl+" VALUES (SLEEP(1))"); err != context.Canceled { |
| 2702 | dbt.Errorf("expected context.Canceled, got %v", err) |
| 2703 | } |
| 2704 | if d := time.Since(startTime); d > 500*time.Millisecond { |
| 2705 | dbt.Errorf("too long execution time: %s", d) |
| 2706 | } |
| 2707 | |
| 2708 | // Wait for the INSERT query to be done. |
| 2709 | time.Sleep(time.Second) |
| 2710 | |
| 2711 | // Check how many times the query is executed. |
| 2712 | var v int |
| 2713 | if err := dbt.db.QueryRow("SELECT COUNT(*) FROM " + tbl).Scan(&v); err != nil { |
| 2714 | dbt.Fatalf("%s", err.Error()) |
| 2715 | } |
| 2716 | if v != 1 { // TODO: need to kill the query, and v should be 0. |
| 2717 | dbt.Skipf("[WARN] expected val to be 1, got %d", v) |
| 2718 | } |
| 2719 | |
| 2720 | // Context is already canceled, so error should come before execution. |
| 2721 | if _, err := dbt.db.ExecContext(ctx, "INSERT INTO "+tbl+" VALUES (1)"); err == nil { |
| 2722 | dbt.Error("expected error") |
| 2723 | } else if err.Error() != "context canceled" { |
| 2724 | dbt.Fatalf("unexpected error: %s", err) |
| 2725 | } |
| 2726 | |
| 2727 | // The second insert query will fail, so the table has no changes. |
| 2728 | if err := dbt.db.QueryRow("SELECT COUNT(*) FROM " + tbl).Scan(&v); err != nil { |
| 2729 | dbt.Fatalf("%s", err.Error()) |
| 2730 | } |
| 2731 | if v != 1 { |
| 2732 | dbt.Skipf("[WARN] expected val to be 1, got %d", v) |
| 2733 | } |
| 2734 | }) |
| 2735 | } |
| 2736 | |
| 2737 | func TestContextCancelQuery(t *testing.T) { |
| 2738 | runTestsParallel(t, dsn, func(dbt *DBTest, tbl string) { |
nothing calls this directly
no test coverage detected