This is an automated email from the ASF dual-hosted git repository.

amoghdesai pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 6b759e89a60 Adds Test Parallelization in go-sdk (#51314)
6b759e89a60 is described below

commit 6b759e89a60353779904f1eb9a179d3adeb3140b
Author: Aritra Basu <[email protected]>
AuthorDate: Wed Jun 4 10:56:57 2025 +0530

    Adds Test Parallelization in go-sdk (#51314)
---
 go-sdk/worker/runner.go      |   1 -
 go-sdk/worker/runner_test.go | 128 +++++++++++++++++++++++++------------------
 2 files changed, 74 insertions(+), 55 deletions(-)

diff --git a/go-sdk/worker/runner.go b/go-sdk/worker/runner.go
index f01cde3fdaf..394a220fa9e 100644
--- a/go-sdk/worker/runner.go
+++ b/go-sdk/worker/runner.go
@@ -343,7 +343,6 @@ func (w *worker) ExecuteTaskWorkload(ctx context.Context, 
workload api.ExecuteTa
                        State:   api.TISuccessStatePayloadState(finalState),
                })
        }
-
        err = workloadClient.TaskInstances().UpdateState(
                ctx,
                workload.TI.Id,
diff --git a/go-sdk/worker/runner_test.go b/go-sdk/worker/runner_test.go
index c4ae77d6ec1..b2fa4382b0e 100644
--- a/go-sdk/worker/runner_test.go
+++ b/go-sdk/worker/runner_test.go
@@ -36,30 +36,33 @@ import (
 
 const ExecutionAPIServer = "http://localhost:9999/execution";
 
-var TestWorkload = api.ExecuteTaskWorkload{
-       Token: "",
-       // 
{"context_carrier":{},"dag_id":"tutorial_dag","hostname":null,"id":"0196ab8a-5c97-7d4f-b431-e3f49ce20b7f","map_index":-1,"run_id":"manual__2025-05-07T15:48:39.420678+00:00","task_id":"extract","try_number":5}
-       TI: api.TaskInstance{
-               ContextCarrier: new(map[string]any),
-               DagId:          "tutorial_dag",
-               RunId:          "manual__2025-05-07T15:48:39.420678+00:00",
-               TaskId:         "extract",
-               Id:             
uuid.MustParse("0196ab8a-5c97-7d4f-b431-e3f49ce20b7f"),
-               // MapIndex:       new(int),
-               TryNumber: 1,
-       },
-       BundleInfo: api.BundleInfo{
-               Name:    "example_dags",
-               Version: nil,
-       },
-       // LogPath: new(string),
-}
-
-func init() {
+func newTestWorkLoad(id string, dagId string) api.ExecuteTaskWorkload {
+       if dagId == "" {
+               dagId = "tutorial_dag"
+       }
        idx := -1
-       log := 
"dag_id=tutorial_dag/run_id=manual__2025-05-07T15:48:39.420678+00:00/task_id=extract/attempt=5.log"
-       TestWorkload.TI.MapIndex = &idx
-       TestWorkload.LogPath = &log
+       log := fmt.Sprintf(
+               
"dag_id=%s/run_id=manual__2025-05-07T15:48:39.420678+00:00/task_id=extract/attempt=5.log",
+               dagId,
+       )
+       return api.ExecuteTaskWorkload{
+               Token: "",
+               // 
{"context_carrier":{},"dag_id":"tutorial_dag","hostname":null,"id":"0196ab8a-5c97-7d4f-b431-e3f49ce20b7f","map_index":-1,"run_id":"manual__2025-05-07T15:48:39.420678+00:00","task_id":"extract","try_number":5}
+               TI: api.TaskInstance{
+                       ContextCarrier: new(map[string]any),
+                       DagId:          dagId,
+                       RunId:          
"manual__2025-05-07T15:48:39.420678+00:00",
+                       TaskId:         "extract",
+                       Id:             uuid.MustParse(id),
+                       MapIndex:       &idx,
+                       TryNumber:      1,
+               },
+               BundleInfo: api.BundleInfo{
+                       Name:    "example_dags",
+                       Version: nil,
+               },
+               LogPath: &log,
+       }
 }
 
 type WorkerSuite struct {
@@ -72,17 +75,18 @@ func TestWorkerSuite(t *testing.T) {
        suite.Run(t, &WorkerSuite{})
 }
 
-func (s *WorkerSuite) SetupTest() {
+func (s *WorkerSuite) SetupSuite() {
        s.worker = New(slog.Default())
 
        s.transport = httpmock.NewMockTransport()
        client, err := api.NewClient(ExecutionAPIServer, 
api.WithRoundTripper(s.transport))
        s.Require().NoError(err)
-
+       s.worker.(*worker).heartbeatInterval = 100 * time.Millisecond
        s.worker.(*worker).client = client
 }
 
 func (s *WorkerSuite) TestWithServer() {
+       s.T().Parallel()
        s.worker.(*worker).heartbeatInterval = 100 * time.Millisecond
        iface, err := s.worker.WithServer("http://example.com";)
 
@@ -94,38 +98,41 @@ func (s *WorkerSuite) TestWithServer() {
 
 // ExpectTaskRun sets up  a matcher for the "/task-instances/{id}/run" end 
point and adds a finalize check
 // that it has been called
-func (s *WorkerSuite) ExpectTaskRun() {
+func (s *WorkerSuite) ExpectTaskRun(taskId string) {
        s.transport.RegisterResponder(
                http.MethodPatch,
-               "=~^"+ExecutionAPIServer+"/task-instances/[0-9a-f-]{36}/run",
+               fmt.Sprintf("=~^%s/task-instances/%s/run", ExecutionAPIServer, 
taskId),
                httpmock.NewJsonResponderOrPanic(200, map[string]any{}),
        )
        s.T().Cleanup(func() {
                callCounts := s.transport.GetCallCountInfo()
 
-               s.Equal(callCounts["PATCH 
=~^"+ExecutionAPIServer+"/task-instances/[0-9a-f-]{36}/run"], 1)
+               s.Equal(
+                       callCounts[fmt.Sprintf("PATCH 
=~^%s/task-instances/%s/run", ExecutionAPIServer, taskId)],
+                       1,
+               )
        })
 }
 
-// ExpectTaskState sets up a matcher for the "/task-instances/{id}/state" with 
the given state end point and adds a finalize check
-// that it has been called
-func (s *WorkerSuite) ExpectTaskState(state any) {
+// ExpectTaskState sets up a matcher for the "/task-instances/{id}/state" with 
the given state end point
+func (s *WorkerSuite) ExpectTaskState(taskId string, state any) {
        s.transport.RegisterMatcherResponder(
                http.MethodPatch,
-               "=~^"+ExecutionAPIServer+"/task-instances/[0-9a-f-]{36}/state",
+               fmt.Sprintf("=~^%s/task-instances/%s/state", 
ExecutionAPIServer, taskId),
                s.BodyJSONMatches(fmt.Appendf(nil, `{"state": %q}`, state)),
                httpmock.NewJsonResponderOrPanic(200, map[string]any{}),
        )
-       s.T().Cleanup(func() {
-               callCounts := s.transport.GetCallCountInfo()
+}
 
-               s.Equal(
-                       callCounts["PATCH 
=~^"+ExecutionAPIServer+"/task-instances/[0-9a-f-]{36}/state 
<BodyJSONMatches>"],
-                       1,
-                       "Actual call counts: %#v",
-                       callCounts,
-               )
-       })
+// Validates if task identified by taskId has the "/task-instances/{id}/state" 
api called with the given state
+func (s *WorkerSuite) ValidateTaskState(taskId string, state any) {
+       callCounts := s.transport.GetCallCountInfo()
+       s.Equal(
+               callCounts[fmt.Sprintf("PATCH =~^%s/task-instances/%s/state 
<BodyJSONMatches>", ExecutionAPIServer, taskId)],
+               1,
+               "Actual call counts: %#v",
+               callCounts,
+       )
 }
 
 // BodyContainsJSON creates an httpmock Matcher that will check that the 
http.Request body contains the given
@@ -135,7 +142,8 @@ func (s *WorkerSuite) ExpectTaskState(state any) {
 func (s *WorkerSuite) BodyJSONMatches(expected []byte) httpmock.Matcher {
        matcher := httpmock.NewMatcher("BodyJSONMatches", func(req 
*http.Request) bool {
                b, err := io.ReadAll(req.Body)
-               return err == nil && assertjson.Matches(s.T(), expected, b)
+               match := err == nil && assertjson.Matches(s.T(), expected, b)
+               return match
        })
        return matcher
 }
@@ -143,17 +151,24 @@ func (s *WorkerSuite) BodyJSONMatches(expected []byte) 
httpmock.Matcher {
 // TestTaskNotRegisteredErrors checks that when a task cannot be found we 
report "success" on the Workload but
 // report the task as failed to the Execution API server
 func (s *WorkerSuite) TestTaskNotRegisteredErrors() {
+       s.T().Parallel()
+       id := uuid.New().String()
+       testWorkload := newTestWorkLoad(id, id[:8])
+       s.ExpectTaskState(id, api.TerminalTIStateFailed)
        s.transport.RegisterMatcherResponder(
                http.MethodPatch,
-               "=~^"+ExecutionAPIServer+"/task-instances/[0-9a-f-]{36}/state",
+               fmt.Sprintf("=~^%s/task-instances/%s/state", 
ExecutionAPIServer, id),
                s.BodyJSONMatches([]byte(`{"state": "failed"}`)),
                httpmock.NewJsonResponderOrPanic(200, map[string]any{}),
        )
-       err := s.worker.ExecuteTaskWorkload(context.Background(), TestWorkload)
+       err := s.worker.ExecuteTaskWorkload(context.Background(), testWorkload)
 
-       s.NoError(err, "ExecuteTaskWorkload should not report an error")
-
-       s.Equal(1, s.transport.GetTotalCallCount(), "State was reported to 
server")
+       s.NoError(
+               err,
+               "ExecuteTaskWorkload should not report an error %#v",
+               s.transport.GetCallCountInfo(),
+       )
+       s.ValidateTaskState(id, api.TerminalTIStateFailed)
 }
 
 // TestStartContextErrorTaskDoesntStart checks that if the /run endpoint 
returns an error that task doesn't
@@ -173,21 +188,24 @@ func (s *WorkerSuite) 
TestTaskReturnErrorReportsFailedState() {
 }
 
 func (s *WorkerSuite) TestTaskHeartbeatsWhileRunning() {
-       s.worker.RegisterTaskWithName("tutorial_dag", "extract", func() error {
+       s.T().Parallel()
+       id := uuid.New().String()
+       testWorkload := newTestWorkLoad(id, id[:8])
+       s.worker.RegisterTaskWithName(testWorkload.TI.DagId, 
testWorkload.TI.TaskId, func() error {
                time.Sleep(time.Second)
                return nil
        })
 
-       s.ExpectTaskRun()
-       s.ExpectTaskState(api.TerminalTIStateSuccess)
+       s.ExpectTaskRun(id)
+       s.ExpectTaskState(id, api.TerminalTIStateSuccess)
        s.transport.RegisterResponder(
                http.MethodPut,
-               
"=~^"+ExecutionAPIServer+"/task-instances/[0-9a-f-]{36}/heartbeat",
+               fmt.Sprintf("=~^%s/task-instances/%s/heartbeat", 
ExecutionAPIServer, id),
                httpmock.NewJsonResponderOrPanic(200, map[string]any{}),
        )
 
        s.worker.(*worker).heartbeatInterval = 100 * time.Millisecond
-       err := s.worker.ExecuteTaskWorkload(context.Background(), TestWorkload)
+       err := s.worker.ExecuteTaskWorkload(context.Background(), testWorkload)
        s.NoError(err, "ExecuteTaskWorkload should not report an error")
 
        callCounts := s.transport.GetCallCountInfo()
@@ -196,10 +214,12 @@ func (s *WorkerSuite) TestTaskHeartbeatsWhileRunning() {
        // 1 due to timing imprecision
        s.InDelta(
                10,
-               callCounts["PUT 
=~^"+ExecutionAPIServer+"/task-instances/[0-9a-f-]{36}/heartbeat"],
+               callCounts[fmt.Sprintf("PUT =~^%s/task-instances/%s/heartbeat", 
ExecutionAPIServer, id)],
                1,
-               "Actual call counts: %#v", callCounts,
+               "Actual call counts: %#v",
+               callCounts,
        )
+       s.ValidateTaskState(id, api.TerminalTIStateSuccess)
 }
 
 func (s *WorkerSuite) TestTaskHeatbeatErrorStopsTaskAndLogs() {

Reply via email to