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

[BEAM-14346] Fix incorrect error case index in ret2() #17425

Merged
merged 3 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdks/go/pkg/beam/core/runtime/exec/fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (n *invoker) ret1(pn typex.PaneInfo, ws []typex.Window, ts typex.EventTime,
// ret2 handles processing of a pair of return values.
func (n *invoker) ret2(pn typex.PaneInfo, ws []typex.Window, ts typex.EventTime, r0, r1 interface{}) (*FullValue, error) {
switch {
case n.outErrIdx == 2:
case n.outErrIdx == 1:
if r1 != nil {
return nil, r1.(error)
}
Expand Down
3 changes: 0 additions & 3 deletions sdks/go/pkg/beam/core/runtime/exec/fn_arity.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions sdks/go/pkg/beam/core/runtime/exec/fn_arity.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ func (n *invoker) initCall() {
default:
n.call = func(pn typex.PaneInfo, ws []typex.Window, ts typex.EventTime) (*FullValue, error) {
ret := n.fn.Fn.Call(n.args)
if n.outErrIdx >= 0 && ret[n.outErrIdx] != nil {
return nil, ret[n.outErrIdx].(error)
}

// (5) Return direct output, if any. Input timestamp and windows are implicitly
// propagated.
Expand Down
63 changes: 49 additions & 14 deletions sdks/go/pkg/beam/core/runtime/exec/fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/reflectx"
)

var errGeneric = errors.New("generic error")

// TestInvoke verifies the the various forms of input to Invoke are handled correctly.
func TestInvoke(t *testing.T) {
tests := []struct {
Expand All @@ -40,6 +42,7 @@ func TestInvoke(t *testing.T) {
Expected, Expected2 interface{}
ExpectedTime typex.EventTime
ExpectedContinuation sdf.ProcessContinuation
ExpectedError error
}{
{
// Void function
Expand Down Expand Up @@ -159,6 +162,30 @@ func TestInvoke(t *testing.T) {
Opt: &MainInput{Key: FullValue{Elm: "basketball", Elm2: []typex.T{23}}},
Expected: "basketball", Expected2: 1,
},
{
// ret1() error check
Fn: func(a int) error { return errGeneric },
Opt: &MainInput{Key: FullValue{Elm: 1}},
ExpectedError: errGeneric,
},
{
// ret2() error check
Fn: func(a int) (int, error) { return 0, errGeneric },
Opt: &MainInput{Key: FullValue{Elm: 1}},
ExpectedError: errGeneric,
},
{
// ret3() error check
Fn: func(a int) (typex.EventTime, int, error) { return 0, 0, errGeneric },
Opt: &MainInput{Key: FullValue{Elm: 1}},
ExpectedError: errGeneric,
},
{
// ret4() error check
Fn: func(a int) (typex.EventTime, string, int, error) { return 0, "", 0, errGeneric },
Opt: &MainInput{Key: FullValue{Elm: 1}},
ExpectedError: errGeneric,
},
// TODO(BEAM-11104): Add unit test cases for ProcessContinuations once they are enabled for use.
}

Expand All @@ -181,20 +208,28 @@ func TestInvoke(t *testing.T) {
}

val, err := Invoke(context.Background(), typex.NoFiringPane(), window.SingleGlobalWindow, ts, fn, test.Opt, nil, test.Args...)
if err != nil {
t.Fatalf("Invoke(%v,%v) failed: %v", fn.Fn.Name(), test.Args, err)
}
if val != nil && val.Elm != test.Expected {
t.Errorf("Invoke(%v,%v) = %v, want %v", fn.Fn.Name(), test.Args, val.Elm, test.Expected)
}
if val != nil && val.Elm2 != test.Expected2 {
t.Errorf("Elm2: Invoke(%v,%v) = %v, want %v", fn.Fn.Name(), test.Args, val.Elm2, test.Expected2)
}
if val != nil && val.Timestamp != test.ExpectedTime {
t.Errorf("EventTime: Invoke(%v,%v) = %v, want %v", fn.Fn.Name(), test.Args, val.Timestamp, test.ExpectedTime)
}
if val != nil && val.Continuation != test.ExpectedContinuation {
t.Errorf("EventTime: Invoke(%v,%v) = %v, want %v", fn.Fn.Name(), test.Args, val.Continuation, test.ExpectedContinuation)

switch test.ExpectedError != nil {
case true:
jrmccluskey marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
t.Fatalf("Invoke(%v, %v) succeeded when it should have failed, want %v", fn.Fn.Name(), test.Args, test.ExpectedError)
}
if err != test.ExpectedError {
t.Errorf("Invoke(%v, %v) returned unexpected error, got %v, want %v", fn.Fn.Name(), test.Args, err, test.ExpectedError)
}
default:
if val != nil && val.Elm != test.Expected {
t.Errorf("Invoke(%v,%v) = %v, want %v", fn.Fn.Name(), test.Args, val.Elm, test.Expected)
}
if val != nil && val.Elm2 != test.Expected2 {
t.Errorf("Elm2: Invoke(%v,%v) = %v, want %v", fn.Fn.Name(), test.Args, val.Elm2, test.Expected2)
}
if val != nil && val.Timestamp != test.ExpectedTime {
t.Errorf("EventTime: Invoke(%v,%v) = %v, want %v", fn.Fn.Name(), test.Args, val.Timestamp, test.ExpectedTime)
}
if val != nil && val.Continuation != test.ExpectedContinuation {
t.Errorf("EventTime: Invoke(%v,%v) = %v, want %v", fn.Fn.Name(), test.Args, val.Continuation, test.ExpectedContinuation)
}
}
})
}
Expand Down