From 4f887ea95044f77bd0499ca6eb79ff72286c1de0 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:26:23 +0000 Subject: [PATCH 1/3] aitools: stop polling tests from waiting on the real poll interval ## Changes This only changes unit tests in `experimental/aitools/cmd`. Production code is untouched. - `query_test.go`: `TestExecuteAndPollWithPolling` and `TestExecuteAndPollFailsDuringPolling` now build their client with `sqlexec.WithPollInterval(time.Millisecond, time.Millisecond)`. They still go through the poll loop, but each poll waits 1ms. - `batch_test.go`, `statement_test.go`: `TestExecuteBatchPreservesInputOrder` and `TestGetStatementResultPolls` now call `t.Parallel()`. These two tests can't take a poll interval: `executeBatch` and `getStatementResult` create their own `sqlexec.Client` from the API interface. Running them in parallel means their 1s waits overlap. ## Why The `sqlexec` client polls with an additive backoff that starts at 1s and grows by 1s per poll. With mocked responses, every test that reached the poll loop slept for real, one test after another: | Test | Polls | Wait before | Wait after | | --- | --- | --- | --- | | `TestExecuteAndPollWithPolling` | 2 | 1s + 2s | ~2ms | | `TestExecuteAndPollFailsDuringPolling` | 1 | 1s | ~1ms | | `TestExecuteBatchPreservesInputOrder` | 1 | 1s | 1s (in parallel) | | `TestGetStatementResultPolls` | 1 | 1s | 1s (in parallel) | That's about 6s of sleeping before (my estimate from the backoff schedule) and about 1s after. The tests check the same things as before. ## Tests ``` go test ./experimental/aitools/cmd -count=1 -run 'TestExecuteAndPoll|TestExecuteBatch|TestGetStatementResult' -v --- PASS: TestExecuteAndPollWithPolling (0.00s) --- PASS: TestExecuteAndPollFailsDuringPolling (0.00s) --- PASS: TestGetStatementResultPolls (1.00s) --- PASS: TestExecuteBatchPreservesInputOrder (1.00s) ok github.com/databricks/cli/experimental/aitools/cmd 1.015s ``` This is a test-only change, so there's no changelog fragment. Co-authored-by: Isaac --- experimental/aitools/cmd/batch_test.go | 2 ++ experimental/aitools/cmd/query_test.go | 8 ++++++-- experimental/aitools/cmd/statement_test.go | 2 ++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/experimental/aitools/cmd/batch_test.go b/experimental/aitools/cmd/batch_test.go index 8288a43da5d..51ed8b82356 100644 --- a/experimental/aitools/cmd/batch_test.go +++ b/experimental/aitools/cmd/batch_test.go @@ -187,6 +187,8 @@ func TestExecuteBatchSetsOnWaitTimeoutContinue(t *testing.T) { } func TestExecuteBatchPreservesInputOrder(t *testing.T) { + // Waits for the engine's real 1s poll interval; run it alongside the other slow test. + t.Parallel() // Index 0 is slow (PENDING then SUCCEEDED on first poll); 1 and 2 are // immediate. Despite the staggered completion, results stay in input order. ctx := cmdio.MockDiscard(t.Context()) diff --git a/experimental/aitools/cmd/query_test.go b/experimental/aitools/cmd/query_test.go index 66e70b238ee..98048343bd3 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" @@ -106,6 +107,9 @@ func TestExecuteAndPollImmediateFailure(t *testing.T) { assert.Contains(t, err.Error(), "syntax error") } +// fastPoll keeps polling tests from waiting the engine's real 1s+ backoff. +var fastPoll = sqlexec.WithPollInterval(time.Millisecond, time.Millisecond) + func TestExecuteAndPollWithPolling(t *testing.T) { ctx := cmdio.MockDiscard(t.Context()) mockAPI := mocksql.NewMockStatementExecutionInterface(t) @@ -129,7 +133,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 +160,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_test.go b/experimental/aitools/cmd/statement_test.go index e6905b3f20d..959e579831f 100644 --- a/experimental/aitools/cmd/statement_test.go +++ b/experimental/aitools/cmd/statement_test.go @@ -71,6 +71,8 @@ func TestSubmitStatementWrapsTransportError(t *testing.T) { } func TestGetStatementResultPolls(t *testing.T) { + // Waits for the engine's real 1s poll interval; run it alongside the other slow test. + t.Parallel() ctx := cmdio.MockDiscard(t.Context()) mockAPI := mocksql.NewMockStatementExecutionInterface(t) From 2a441253ef4d84b1a330d9be9c4cf3e0a9117689 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 19 Nov 2026 19:48:35 +0000 Subject: [PATCH 2/3] aitools: pass poll options through executeBatch and getStatementResult The polling tests for executeBatch and getStatementResult can now use the 1ms poll interval too, so they no longer wait on the real 1s interval or depend on running in parallel. Co-authored-by: Isaac --- experimental/aitools/cmd/batch.go | 6 ++++-- experimental/aitools/cmd/batch_test.go | 4 +--- experimental/aitools/cmd/statement_get.go | 6 ++++-- experimental/aitools/cmd/statement_test.go | 4 +--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/experimental/aitools/cmd/batch.go b/experimental/aitools/cmd/batch.go index cd1890ceb10..019ceb600d0 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) +// +// opts are passed to the sqlexec client. +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 51ed8b82356..9af80b5353a 100644 --- a/experimental/aitools/cmd/batch_test.go +++ b/experimental/aitools/cmd/batch_test.go @@ -187,8 +187,6 @@ func TestExecuteBatchSetsOnWaitTimeoutContinue(t *testing.T) { } func TestExecuteBatchPreservesInputOrder(t *testing.T) { - // Waits for the engine's real 1s poll interval; run it alongside the other slow test. - t.Parallel() // Index 0 is slow (PENDING then SUCCEEDED on first poll); 1 and 2 are // immediate. Despite the staggered completion, results stay in input order. ctx := cmdio.MockDiscard(t.Context()) @@ -217,7 +215,7 @@ func TestExecuteBatchPreservesInputOrder(t *testing.T) { } 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, 8, fastPoll) require.Len(t, results, 3) for i, r := range results { diff --git a/experimental/aitools/cmd/statement_get.go b/experimental/aitools/cmd/statement_get.go index fe011464a0c..59225843123 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) { +// +// opts are passed to the sqlexec client. +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 959e579831f..83f62a74bd2 100644 --- a/experimental/aitools/cmd/statement_test.go +++ b/experimental/aitools/cmd/statement_test.go @@ -71,8 +71,6 @@ func TestSubmitStatementWrapsTransportError(t *testing.T) { } func TestGetStatementResultPolls(t *testing.T) { - // Waits for the engine's real 1s poll interval; run it alongside the other slow test. - t.Parallel() ctx := cmdio.MockDiscard(t.Context()) mockAPI := mocksql.NewMockStatementExecutionInterface(t) @@ -88,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) From 8f477fe7c9ad0de126990e8d7b3072c3d227cce3 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:04:03 +0000 Subject: [PATCH 3/3] aitools: address review nits on the fast-poll tests - Say why executeBatch and getStatementResult take poll options. - Move fastPoll to the top of query_test.go and fix its comment. - Make TestExecuteBatchPreservesInputOrder deterministic: with two slots, the slow statement's poll blocks until the 'fast2' submission starts, so it always completes last. Co-authored-by: Isaac --- experimental/aitools/cmd/batch.go | 2 +- experimental/aitools/cmd/batch_test.go | 37 +++++++++++++++-------- experimental/aitools/cmd/query_test.go | 7 +++-- experimental/aitools/cmd/statement_get.go | 2 +- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/experimental/aitools/cmd/batch.go b/experimental/aitools/cmd/batch.go index 019ceb600d0..e8ebbefe851 100644 --- a/experimental/aitools/cmd/batch.go +++ b/experimental/aitools/cmd/batch.go @@ -59,7 +59,7 @@ type batchResultError struct { // reused across the batch, so callers must ensure each SQL uses only markers // that are covered. // -// opts are passed to the sqlexec client. +// 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...) diff --git a/experimental/aitools/cmd/batch_test.go b/experimental/aitools/cmd/batch_test.go index 9af80b5353a..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, fastPoll) + 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 98048343bd3..4c2a5afcb56 100644 --- a/experimental/aitools/cmd/query_test.go +++ b/experimental/aitools/cmd/query_test.go @@ -22,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 @@ -107,9 +111,6 @@ func TestExecuteAndPollImmediateFailure(t *testing.T) { assert.Contains(t, err.Error(), "syntax error") } -// fastPoll keeps polling tests from waiting the engine's real 1s+ backoff. -var fastPoll = sqlexec.WithPollInterval(time.Millisecond, time.Millisecond) - func TestExecuteAndPollWithPolling(t *testing.T) { ctx := cmdio.MockDiscard(t.Context()) mockAPI := mocksql.NewMockStatementExecutionInterface(t) diff --git a/experimental/aitools/cmd/statement_get.go b/experimental/aitools/cmd/statement_get.go index 59225843123..c671a3ce78f 100644 --- a/experimental/aitools/cmd/statement_get.go +++ b/experimental/aitools/cmd/statement_get.go @@ -59,7 +59,7 @@ invoked the synchronous path.)`, // server-side statement (intentional: 'get' is a poll-only operation; use // 'cancel' to terminate explicitly). // -// opts are passed to the sqlexec client. +// 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, "", opts...)