Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if function type is nil before attempting to register the function #325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion statefun-sdk-go/v3/pkg/statefun/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
"net/http"
"sync"

"github.com/apache/flink-statefun/statefun-sdk-go/v3/pkg/statefun/internal/protocol"
"google.golang.org/protobuf/proto"

"github.com/apache/flink-statefun/statefun-sdk-go/v3/pkg/statefun/internal/protocol"
)

// StatefulFunctions is a registry for multiple StatefulFunction's. A RequestReplyHandler
Expand Down Expand Up @@ -72,6 +73,10 @@ type handler struct {
}

func (h *handler) WithSpec(spec StatefulFunctionSpec) error {
if spec.FunctionType == nil {
return fmt.Errorf("function type is required")
}

log.Printf("registering Stateful Function %v\n", spec.FunctionType)
if _, exists := h.module[spec.FunctionType]; exists {
err := fmt.Errorf("failed to register Stateful Function %s, there is already a spec registered under that type", spec.FunctionType)
Expand Down
20 changes: 19 additions & 1 deletion statefun-sdk-go/v3/pkg/statefun/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"testing"

"github.com/apache/flink-statefun/statefun-sdk-go/v3/pkg/statefun/internal/protocol"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/proto"

"github.com/apache/flink-statefun/statefun-sdk-go/v3/pkg/statefun/internal/protocol"
)

// helper to create a protocol Address from an Address
Expand Down Expand Up @@ -87,3 +88,20 @@ func TestStatefunHandler_WithCaller_ContextCallerIsCorrect(t *testing.T) {
err := invokeStatefulFunction(context.Background(), &target, &caller, nil, StatefulFunctionPointer(statefulFunction))
assert.Nil(t, err)
}

func TestStatefulFunctionsBuilder_FunctionTypeRequired(t *testing.T) {
caller := Address{FunctionType: TypeNameFrom("namespace/function2"), Id: "2"}

statefulFunction := func(ctx Context, message Message) error {
assert.Equal(t, caller.String(), ctx.Caller().String())
return nil
}

builder := StatefulFunctionsBuilder()
err := builder.WithSpec(StatefulFunctionSpec{
Function: StatefulFunctionPointer(statefulFunction),
})

assert.Error(t, err)
assert.Contains(t, err.Error(), "function type is required")
}