diff --git a/experimental/aitools/cmd/batch.go b/experimental/aitools/cmd/batch.go index cd1890ceb10..e8ebbefe851 100644 --- a/experimental/aitools/cmd/batch.go +++ b/experimental/aitools/cmd/batch.go @@ -58,8 +58,10 @@ type batchResultError struct { // params, if non-nil, are bound on every statement. The same parameter set is // reused across the batch, so callers must ensure each SQL uses only markers // that are covered. -func executeBatch(ctx context.Context, api sql.StatementExecutionInterface, warehouseID string, sqls []string, params []sql.StatementParameterListItem, concurrency int) []batchResult { - client := sqlexec.New(api, warehouseID) +// +// Tests pass sqlexec.WithPollInterval in opts to skip the real poll backoff. +func executeBatch(ctx context.Context, api sql.StatementExecutionInterface, warehouseID string, sqls []string, params []sql.StatementParameterListItem, concurrency int, opts ...sqlexec.Option) []batchResult { + client := sqlexec.New(api, warehouseID, opts...) pollCtx, pollCancel := context.WithCancel(ctx) defer pollCancel() diff --git a/experimental/aitools/cmd/batch_test.go b/experimental/aitools/cmd/batch_test.go index 8288a43da5d..b4188392462 100644 --- a/experimental/aitools/cmd/batch_test.go +++ b/experimental/aitools/cmd/batch_test.go @@ -187,10 +187,13 @@ func TestExecuteBatchSetsOnWaitTimeoutContinue(t *testing.T) { } func TestExecuteBatchPreservesInputOrder(t *testing.T) { - // Index 0 is slow (PENDING then SUCCEEDED on first poll); 1 and 2 are - // immediate. Despite the staggered completion, results stay in input order. + // Index 0 is slow (PENDING, then SUCCEEDED on first poll); 1 and 2 are + // immediate. With two slots, the 'fast2' submission can only start after + // 'fast1' has finished, and the slow poll blocks until that submission + // starts. So 'slow' always completes last, yet results stay in input order. ctx := cmdio.MockDiscard(t.Context()) mockAPI := mocksql.NewMockStatementExecutionInterface(t) + fast2Submitted := make(chan struct{}) mockAPI.EXPECT().ExecuteStatement(mock.Anything, mock.MatchedBy(func(req sql.ExecuteStatementRequest) bool { return req.Statement == "SELECT 'slow'" @@ -199,23 +202,31 @@ func TestExecuteBatchPreservesInputOrder(t *testing.T) { Status: &sql.StatementStatus{State: sql.StatementStatePending}, }, nil).Once() - mockAPI.EXPECT().GetStatementByStatementId(mock.Anything, "stmt-slow").Return(&sql.StatementResponse{ + mockAPI.EXPECT().GetStatementByStatementId(mock.Anything, "stmt-slow").Run(func(context.Context, string) { + <-fast2Submitted + }).Return(&sql.StatementResponse{ StatementId: "stmt-slow", Status: &sql.StatementStatus{State: sql.StatementStateSucceeded}, }, nil).Once() - for i, sqlStr := range []string{"SELECT 'fast1'", "SELECT 'fast2'"} { - sid := fmt.Sprintf("stmt-fast-%d", i+1) - mockAPI.EXPECT().ExecuteStatement(mock.Anything, mock.MatchedBy(func(req sql.ExecuteStatementRequest) bool { - return req.Statement == sqlStr - })).Return(&sql.StatementResponse{ - StatementId: sid, - Status: &sql.StatementStatus{State: sql.StatementStateSucceeded}, - }, nil).Once() - } + mockAPI.EXPECT().ExecuteStatement(mock.Anything, mock.MatchedBy(func(req sql.ExecuteStatementRequest) bool { + return req.Statement == "SELECT 'fast1'" + })).Return(&sql.StatementResponse{ + StatementId: "stmt-fast-1", + Status: &sql.StatementStatus{State: sql.StatementStateSucceeded}, + }, nil).Once() + + mockAPI.EXPECT().ExecuteStatement(mock.Anything, mock.MatchedBy(func(req sql.ExecuteStatementRequest) bool { + return req.Statement == "SELECT 'fast2'" + })).Run(func(context.Context, sql.ExecuteStatementRequest) { + close(fast2Submitted) + }).Return(&sql.StatementResponse{ + StatementId: "stmt-fast-2", + Status: &sql.StatementStatus{State: sql.StatementStateSucceeded}, + }, nil).Once() sqls := []string{"SELECT 'slow'", "SELECT 'fast1'", "SELECT 'fast2'"} - results := executeBatch(ctx, mockAPI, "wh-1", sqls, nil, 8) + results := executeBatch(ctx, mockAPI, "wh-1", sqls, nil, 2, fastPoll) require.Len(t, results, 3) for i, r := range results { diff --git a/experimental/aitools/cmd/query_test.go b/experimental/aitools/cmd/query_test.go index 66e70b238ee..4c2a5afcb56 100644 --- a/experimental/aitools/cmd/query_test.go +++ b/experimental/aitools/cmd/query_test.go @@ -7,6 +7,7 @@ import ( "path/filepath" "strings" "testing" + "time" "github.com/databricks/cli/cmd/root" "github.com/databricks/cli/experimental/libs/sqlcli" @@ -21,6 +22,10 @@ import ( "github.com/stretchr/testify/require" ) +// fastPoll keeps the package's polling tests from waiting on the engine's +// real 1s+ backoff. +var fastPoll = sqlexec.WithPollInterval(time.Millisecond, time.Millisecond) + func TestCleanSQL(t *testing.T) { tests := []struct { name string @@ -129,7 +134,7 @@ func TestExecuteAndPollWithPolling(t *testing.T) { Result: &sql.ResultData{DataArray: [][]string{{"42"}}}, }, nil).Once() - client := sqlexec.New(mockAPI, "wh-123") + client := sqlexec.New(mockAPI, "wh-123", fastPoll) stmt, err := executeAndPoll(ctx, client, "SELECT 42", nil) require.NoError(t, err) assert.Equal(t, sql.StatementStateSucceeded, stmt.State) @@ -156,7 +161,7 @@ func TestExecuteAndPollFailsDuringPolling(t *testing.T) { }, }, nil).Once() - _, err := executeAndPoll(ctx, sqlexec.New(mockAPI, "wh-123"), "SELECT 1", nil) + _, err := executeAndPoll(ctx, sqlexec.New(mockAPI, "wh-123", fastPoll), "SELECT 1", nil) require.Error(t, err) assert.Contains(t, err.Error(), "RESOURCE_EXHAUSTED") } diff --git a/experimental/aitools/cmd/statement_get.go b/experimental/aitools/cmd/statement_get.go index fe011464a0c..c671a3ce78f 100644 --- a/experimental/aitools/cmd/statement_get.go +++ b/experimental/aitools/cmd/statement_get.go @@ -58,9 +58,11 @@ invoked the synchronous path.)`, // Context cancellation propagates from the poll WITHOUT cancelling the // server-side statement (intentional: 'get' is a poll-only operation; use // 'cancel' to terminate explicitly). -func getStatementResult(ctx context.Context, api sql.StatementExecutionInterface, statementID string) (statementInfo, error) { +// +// Tests pass sqlexec.WithPollInterval in opts to skip the real poll backoff. +func getStatementResult(ctx context.Context, api sql.StatementExecutionInterface, statementID string, opts ...sqlexec.Option) (statementInfo, error) { // Get/Poll don't use the warehouse ID. - client := sqlexec.New(api, "") + client := sqlexec.New(api, "", opts...) // Fetch the current state first so Poll can short-circuit if the statement // is already terminal. diff --git a/experimental/aitools/cmd/statement_test.go b/experimental/aitools/cmd/statement_test.go index e6905b3f20d..83f62a74bd2 100644 --- a/experimental/aitools/cmd/statement_test.go +++ b/experimental/aitools/cmd/statement_test.go @@ -86,7 +86,7 @@ func TestGetStatementResultPolls(t *testing.T) { Result: &sql.ResultData{DataArray: [][]string{{"42"}}}, }, nil).Once() - info, err := getStatementResult(ctx, mockAPI, "stmt-1") + info, err := getStatementResult(ctx, mockAPI, "stmt-1", fastPoll) require.NoError(t, err) assert.Equal(t, "stmt-1", info.StatementID) assert.Equal(t, sql.StatementStateSucceeded, info.State)