diff --git a/workflow/runtime.go b/workflow/runtime.go index 892dc85e..7816c9b8 100644 --- a/workflow/runtime.go +++ b/workflow/runtime.go @@ -32,7 +32,7 @@ type Workflow func(ctx *Context) (any, error) type Activity func(ctx ActivityContext) (any, error) func NewRuntime(host string, port string) (*WorkflowRuntime, error) { - ctx, canc := context.WithTimeout(context.Background(), time.Second*10) + ctx, canc := context.WithTimeout(context.Background(), time.Second*10) // TODO: add timeout option defer canc() address := fmt.Sprintf("%s:%s", host, port) @@ -44,7 +44,7 @@ func NewRuntime(host string, port string) (*WorkflowRuntime, error) { grpc.WithBlock(), // TODO: config ) if err != nil { - return nil, fmt.Errorf("failed to create runtime - grpc connection failed: %v", err) + return &WorkflowRuntime{}, fmt.Errorf("failed to create runtime - grpc connection failed: %v", err) } return &WorkflowRuntime{ @@ -64,6 +64,10 @@ func getDecorator(f interface{}) (string, error) { funcName := callSplit[len(callSplit)-1] + if funcName == "1" { + return "", errors.New("anonymous function name") + } + return funcName, nil } diff --git a/workflow/runtime_test.go b/workflow/runtime_test.go index eae8a86a..2e5f9b91 100644 --- a/workflow/runtime_test.go +++ b/workflow/runtime_test.go @@ -1,28 +1,67 @@ package workflow import ( + "sync" "testing" + "github.com/microsoft/durabletask-go/task" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) +func TestNewRuntime(t *testing.T) { + t.Run("failure to create newruntime without dapr", func(t *testing.T) { + wr, err := NewRuntime("localhost", "50001") + require.Error(t, err) + assert.Equal(t, &WorkflowRuntime{}, wr) + }) +} + func TestWorkflowRuntime(t *testing.T) { + testRuntime := WorkflowRuntime{ + tasks: task.NewTaskRegistry(), + client: nil, + mutex: sync.Mutex{}, + quit: nil, + cancel: nil, + } + // TODO: Mock grpc conn - currently requires dapr to be available - t.Run("test workflow name is correct", func(t *testing.T) { - wr, err := NewRuntime("localhost", "50001") + t.Run("register workflow", func(t *testing.T) { + err := testRuntime.RegisterWorkflow(testWorkflow) require.NoError(t, err) - err = wr.RegisterWorkflow(testOrchestrator) + }) + t.Run("register workflow - anonymous func", func(t *testing.T) { + err := testRuntime.RegisterWorkflow(func(ctx *Context) (any, error) { + return nil, nil + }) + require.Error(t, err) + }) + t.Run("register activity", func(t *testing.T) { + err := testRuntime.RegisterActivity(testActivity) require.NoError(t, err) }) + t.Run("register activity - anonymous func", func(t *testing.T) { + err := testRuntime.RegisterActivity(func(ctx ActivityContext) (any, error) { + return nil, nil + }) + require.Error(t, err) + }) } func TestGetDecorator(t *testing.T) { - name, err := getDecorator(testOrchestrator) + name, err := getDecorator(testWorkflow) require.NoError(t, err) - assert.Equal(t, "testOrchestrator", name) + assert.Equal(t, "testWorkflow", name) +} + +func testWorkflow(ctx *Context) (any, error) { + _ = ctx + return nil, nil } -func testOrchestrator(ctx *Context) (any, error) { +func testActivity(ctx ActivityContext) (any, error) { + _ = ctx return nil, nil }