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

Function TestContextCancelStmtQuery

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

Source from the content-addressed store, hash-verified

2857}
2858
2859func TestContextCancelStmtQuery(t *testing.T) {
2860 runTestsParallel(t, dsn, func(dbt *DBTest, tbl string) {
2861 dbt.mustExec("CREATE TABLE " + tbl + " (v INTEGER)")
2862 ctx, cancel := context.WithCancel(context.Background())
2863 stmt, err := dbt.db.PrepareContext(ctx, "INSERT INTO "+tbl+" VALUES (SLEEP(1))")
2864 if err != nil {
2865 dbt.Fatalf("unexpected error: %v", err)
2866 }
2867
2868 // Delay execution for just a bit until db.ExecContext has begun.
2869 defer time.AfterFunc(250*time.Millisecond, cancel).Stop()
2870
2871 // This query will be canceled.
2872 startTime := time.Now()
2873 if _, err := stmt.QueryContext(ctx); err != context.Canceled {
2874 dbt.Errorf("expected context.Canceled, got %v", err)
2875 }
2876 if d := time.Since(startTime); d > 500*time.Millisecond {
2877 dbt.Errorf("too long execution time: %s", d)
2878 }
2879
2880 // Wait for the INSERT query has done.
2881 time.Sleep(time.Second)
2882
2883 // Check how many times the query is executed.
2884 var v int
2885 if err := dbt.db.QueryRow("SELECT COUNT(*) FROM " + tbl).Scan(&v); err != nil {
2886 dbt.Fatalf("%s", err.Error())
2887 }
2888 if v != 1 { // TODO: need to kill the query, and v should be 0.
2889 dbt.Skipf("[WARN] expected val to be 1, got %d", v)
2890 }
2891 })
2892}
2893
2894func TestContextCancelBegin(t *testing.T) {
2895 if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {

Callers

nothing calls this directly

Calls 6

runTestsParallelFunction · 0.85
mustExecMethod · 0.80
PrepareContextMethod · 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…