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

ash 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 f1e3ddfdb09 Go SDK: add tests to the task function reflection code 
(#54676)
f1e3ddfdb09 is described below

commit f1e3ddfdb09aac14b3eb7c74ceb88e1d79fd39b4
Author: Ash Berlin-Taylor <[email protected]>
AuthorDate: Tue Aug 19 18:27:23 2025 +0100

    Go SDK: add tests to the task function reflection code (#54676)
    
    This is an important part of the GoSDK as it is what lets us call the user's
    task function with the right arguments. In future this will be extended to 
let
    us pass arguments from XCom etc too!
    
    A small PR as part of a larger work to make it easier to review.
---
 go-sdk/worker/task_test.go | 99 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 99 insertions(+)

diff --git a/go-sdk/worker/task_test.go b/go-sdk/worker/task_test.go
new file mode 100644
index 00000000000..9768f11b614
--- /dev/null
+++ b/go-sdk/worker/task_test.go
@@ -0,0 +1,99 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package worker
+
+import (
+       "context"
+       "log/slog"
+       "testing"
+
+       "github.com/stretchr/testify/suite"
+
+       "github.com/apache/airflow/go-sdk/pkg/logging"
+)
+
+type TaskSuite struct {
+       suite.Suite
+}
+
+func TestTaskSuite(t *testing.T) {
+       suite.Run(t, &TaskSuite{})
+}
+
+func (s *TaskSuite) TestReturnValidation() {
+       cases := map[string]struct {
+               fn          any
+               errContains string
+       }{
+               "no-ret-values": {
+                       func() {},
+                       `func\d+ has 0 return values, must be`,
+               },
+               "too-many-ret-values": {
+                       func() (a, b, c int) { return },
+                       `func\d+ has 3 return values, must be`,
+               },
+               "invalid-ret": {
+                       func() (c chan int) { return },
+                       `func\d+ last return value to return error but found 
chan`,
+               },
+       }
+
+       for name, tt := range cases {
+               s.Run(name, func() {
+                       _, err := NewTaskFunction(tt.fn)
+                       if s.Assert().Error(err) {
+                               s.Assert().Regexp(tt.errContains, err.Error())
+                       }
+               })
+       }
+}
+
+func (s *TaskSuite) TestArgumentBinding() {
+       cases := map[string]struct {
+               fn any
+       }{
+               "no-args": {
+                       func() error { return nil },
+               },
+               "context": {
+                       func(ctx context.Context) error {
+                               s.Assert().Equal("def", ctx.Value("abc"))
+                               return nil
+                       },
+               },
+               "context-and-logger": {
+                       func(ctx context.Context, logger *slog.Logger) error {
+                               s.Assert().Equal("def", ctx.Value("abc"))
+                               s.Assert().NotNil(logger)
+                               return nil
+                       },
+               },
+       }
+
+       for name, tt := range cases {
+               s.Run(name, func() {
+                       task, err := NewTaskFunction(tt.fn)
+                       s.Require().NoError(err)
+
+                       ctx := context.WithValue(context.Background(), "abc", 
"def")
+                       logger := slog.New(logging.NewTeeLogger())
+                       task.Execute(ctx, logger)
+               })
+       }
+}

Reply via email to