|
8 | 8 | "database/sql/driver" |
9 | 9 | "encoding/json" |
10 | 10 | "errors" |
| 11 | + "fmt" |
11 | 12 | "os" |
12 | 13 | "strings" |
13 | 14 | "sync" |
@@ -476,39 +477,59 @@ func TestEvictIfSessionFatal(t *testing.T) { |
476 | 477 | // valid tracks the session-dead flag SessionValid() gates on; the opaque |
477 | 478 | // session pointer is orthogonal here (can't construct the incomplete C type), |
478 | 479 | // so assert on k.valid directly. |
479 | | - k := &KernelBackend{valid: true} |
480 | | - |
481 | | - // Non-fatal (e.g. a SQL error) leaves the session valid. |
482 | | - k.evictIfSessionFatal(&KernelError{Code: statusSqlError}) |
483 | | - if !k.valid { |
484 | | - t.Error("a SQL error must not evict the session") |
| 480 | + cases := []struct { |
| 481 | + name string |
| 482 | + err error |
| 483 | + wantValid bool |
| 484 | + }{ |
| 485 | + {"nil", nil, true}, |
| 486 | + {"plain error", errors.New("Session does not exist"), true}, |
| 487 | + {"SQL error", &KernelError{Code: statusSqlError}, true}, |
| 488 | + {"unavailable", &KernelError{Code: statusUnavailable}, false}, |
| 489 | + {"expired session", expiredSessionError(), false}, |
| 490 | + {"wrapped expired session", fmt.Errorf("execute: %w", expiredSessionError()), false}, |
| 491 | + {"invalid session handle", invalidSessionHandleError(), false}, |
| 492 | + {"wrapped invalid session handle", fmt.Errorf("execute: %w", invalidSessionHandleError()), false}, |
| 493 | + {"invalid argument", &KernelError{Code: statusInvalidArgument, Message: "invalid parameter value"}, true}, |
| 494 | + {"missing statement", &KernelError{Code: statusNotFound, Message: "Statement does not exist"}, true}, |
| 495 | + {"missing table", &KernelError{Code: statusNotFound, Message: "Table does not exist"}, true}, |
| 496 | + } |
| 497 | + for _, tc := range cases { |
| 498 | + t.Run(tc.name, func(t *testing.T) { |
| 499 | + k := &KernelBackend{valid: true} |
| 500 | + k.evictIfSessionFatal(tc.err) |
| 501 | + if k.valid != tc.wantValid { |
| 502 | + t.Errorf("valid = %v, want %v", k.valid, tc.wantValid) |
| 503 | + } |
| 504 | + if err := toStatementError(tc.err); err != tc.err || errors.Is(err, driver.ErrBadConn) { |
| 505 | + t.Errorf("statement error = %v, want the original error without ErrBadConn", err) |
| 506 | + } |
| 507 | + }) |
485 | 508 | } |
| 509 | +} |
486 | 510 |
|
487 | | - // A session-fatal error marks the session dead, and the surfaced statement-path |
488 | | - // error is NOT driver.ErrBadConn (so database/sql won't re-run the statement). |
489 | | - fatal := &KernelError{Code: statusUnavailable, Message: "session gone"} |
490 | | - k.evictIfSessionFatal(fatal) |
491 | | - if k.valid { |
492 | | - t.Error("a session-fatal error must evict the session (valid=false)") |
| 511 | +func TestExecuteRejectsDeadSession(t *testing.T) { |
| 512 | + k := &KernelBackend{valid: true} |
| 513 | + k.markSessionDead() |
| 514 | + op, err := k.Execute(context.Background(), backend.ExecRequest{Query: "SELECT 1"}) |
| 515 | + if !errors.Is(err, driver.ErrBadConn) { |
| 516 | + t.Fatalf("Execute error = %v, want ErrBadConn before submission", err) |
493 | 517 | } |
494 | | - if errors.Is(toStatementError(fatal), driver.ErrBadConn) { |
495 | | - t.Error("the statement-path error must not be driver.ErrBadConn (no replay)") |
| 518 | + if op == nil { |
| 519 | + t.Fatal("Execute must return a non-nil Operation per the Backend contract") |
| 520 | + } |
| 521 | + if op.StatementID() != "" { |
| 522 | + t.Error("a rejected request must not have a statement ID") |
| 523 | + } |
| 524 | + if closed, closeErr := op.Close(context.Background()); closed || closeErr != nil { |
| 525 | + t.Errorf("Close = (%v, %v), want (false, nil) for an unsubmitted operation", closed, closeErr) |
496 | 526 | } |
497 | 527 | } |
498 | 528 |
|
499 | | -// Bound parameters are rejected up front by Execute with a clear error, before |
500 | | -// any session/C work — so this runs on a zero-value backend. The returned |
501 | | -// Operation must be non-nil (Backend contract) and its Close must report |
502 | | -// closed=false, since no server statement was ever created (a phantom |
503 | | -// CLOSE_STATEMENT would otherwise be recorded for it). |
504 | | -// When Execute fails before it acquires a statement handle (here: a nil session |
505 | | -// makes new_statement fail), it must still honor the Backend contract — a non-nil, |
506 | | -// handle-less Operation that Closes as a no-op (closed=false, no CLOSE_STATEMENT) |
507 | | -// and reports zero AffectedRows. (A nil-session unit test can't reach the bind |
508 | | -// path: the param mapping is unit-tested hermetically in TestParamBindArg, and |
509 | | -// exercised live end-to-end in TestKernelParamsVsThrift.) |
| 529 | +// Failure before allocating a statement handle must still return an operation |
| 530 | +// whose Close is a no-op, so callers cannot record a phantom close RPC. |
510 | 531 | func TestExecuteHandleLessOpContract(t *testing.T) { |
511 | | - k := &KernelBackend{} // nil session → new_statement fails |
| 532 | + k := &KernelBackend{valid: true} // nil session → new_statement fails |
512 | 533 | op, err := k.Execute(context.Background(), backend.ExecRequest{ |
513 | 534 | Query: "SELECT ?", |
514 | 535 | Params: []backend.Param{{Name: "x", Type: "STRING", Value: strPtr("v")}}, |
@@ -536,7 +557,7 @@ func TestExecuteHandleLessOpContract(t *testing.T) { |
536 | 557 | // refactor that dropped or reordered the check would silently reopen the |
537 | 558 | // silent-no-op data-loss path. Mirrors TestExecuteRejectsParams. |
538 | 559 | func TestExecuteRejectsStaging(t *testing.T) { |
539 | | - k := &KernelBackend{} |
| 560 | + k := &KernelBackend{valid: true} |
540 | 561 | op, err := k.Execute(context.Background(), backend.ExecRequest{ |
541 | 562 | Query: "PUT '/tmp/f' INTO '/Volumes/main/s/e/f.csv'", |
542 | 563 | }) |
|
0 commit comments