Skip to content

Commit

Permalink
fix/test: identify anonymous functions and add tests to runtime
Browse files Browse the repository at this point in the history
Signed-off-by: mikeee <[email protected]>
  • Loading branch information
mikeee committed Dec 17, 2023
1 parent 1a7f797 commit 2428183
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
8 changes: 6 additions & 2 deletions workflow/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand All @@ -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
}

Expand Down
51 changes: 45 additions & 6 deletions workflow/runtime_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2428183

Please sign in to comment.