From 7a9fbda3500c3c6328240dce2cf5c59e0fddc6df Mon Sep 17 00:00:00 2001 From: hovsep Date: Tue, 17 Sep 2024 02:09:36 +0300 Subject: [PATCH] Component: better waiting for inputs api --- component/component.go | 4 ++-- component/component_test.go | 27 ++++++++++++++++++++++--- component/errors.go | 18 +++++++++++------ component/errors_test.go | 39 ------------------------------------- fmesh_test.go | 2 +- 5 files changed, 39 insertions(+), 51 deletions(-) delete mode 100644 component/errors_test.go diff --git a/component/component.go b/component/component.go index 9523474..16916c9 100644 --- a/component/component.go +++ b/component/component.go @@ -102,10 +102,10 @@ func (c *Component) MaybeActivate() (activationResult *ActivationResult) { //Run the computation err := c.f(c.inputs, c.outputs) - if IsWaitingForInputError(err) { + if errors.Is(err, errWaitingForInputs) { activationResult = c.newActivationCodeWaitingForInput() - if !errors.Is(err, ErrWaitingForInputKeepInputs) { + if !errors.Is(err, errWaitingForInputsKeep) { c.inputs.ClearSignal() } diff --git a/component/component_test.go b/component/component_test.go index 2875d4d..54d0507 100644 --- a/component/component_test.go +++ b/component/component_test.go @@ -420,7 +420,7 @@ func TestComponent_MaybeActivate(t *testing.T) { WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error { if !inputs.ByNames("i1", "i2").AllHaveSignal() { - return ErrWaitingForInputResetInputs + return NewErrWaitForInputs(false) } return nil @@ -432,14 +432,35 @@ func TestComponent_MaybeActivate(t *testing.T) { WithActivationCode(ActivationCodeNoInput), }, { - name: "component is waiting for input", + name: "component is waiting for input, reset inputs", getComponent: func() *Component { c := NewComponent("c1"). WithInputs("i1", "i2"). WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error { if !inputs.ByNames("i1", "i2").AllHaveSignal() { - return ErrWaitingForInputResetInputs + return NewErrWaitForInputs(false) + } + + return nil + }) + //Only one input set + c.Inputs().ByName("i1").PutSignal(signal.New(123)) + return c + }, + wantActivationResult: NewActivationResult("c1"). + SetActivated(false). + WithActivationCode(ActivationCodeWaitingForInput), + }, + { + name: "component is waiting for input, keep inputs", + getComponent: func() *Component { + c := NewComponent("c1"). + WithInputs("i1", "i2"). + WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error { + + if !inputs.ByNames("i1", "i2").AllHaveSignal() { + return NewErrWaitForInputs(true) } return nil diff --git a/component/errors.go b/component/errors.go index 4708a4e..671df5f 100644 --- a/component/errors.go +++ b/component/errors.go @@ -1,13 +1,19 @@ package component -import "errors" +import ( + "errors" + "fmt" +) var ( - //@TODO: provide wrapper methods so exact input can be specified within error - ErrWaitingForInputResetInputs = errors.New("component is waiting for one or more inputs. All inputs will be reset") - ErrWaitingForInputKeepInputs = errors.New("component is waiting for one or more inputs. All inputs will be kept") + errWaitingForInputs = errors.New("component is waiting for some inputs") + errWaitingForInputsKeep = fmt.Errorf("%w: do not clear input ports", errWaitingForInputs) ) -func IsWaitingForInputError(err error) bool { - return errors.Is(err, ErrWaitingForInputResetInputs) || errors.Is(err, ErrWaitingForInputKeepInputs) +// NewErrWaitForInputs returns respective error +func NewErrWaitForInputs(keepInputs bool) error { + if keepInputs { + return errWaitingForInputsKeep + } + return errWaitingForInputs } diff --git a/component/errors_test.go b/component/errors_test.go deleted file mode 100644 index b1a631b..0000000 --- a/component/errors_test.go +++ /dev/null @@ -1,39 +0,0 @@ -package component - -import ( - "errors" - "testing" -) - -func TestIsWaitingForInputError(t *testing.T) { - type args struct { - err error - } - tests := []struct { - name string - args args - want bool - }{ - { - name: "no", - args: args{ - err: errors.New("test error"), - }, - want: false, - }, - { - name: "yes", - args: args{ - err: ErrWaitingForInputKeepInputs, - }, - want: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := IsWaitingForInputError(tt.args.err); got != tt.want { - t.Errorf("IsWaitingForInputError() = %v, want %v", got, tt.want) - } - }) - } -} diff --git a/fmesh_test.go b/fmesh_test.go index 5cfb484..3cb8b93 100644 --- a/fmesh_test.go +++ b/fmesh_test.go @@ -669,7 +669,7 @@ func TestFMesh_runCycle(t *testing.T) { WithOutputs("o1"). WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error { if !inputs.ByNames("i1", "i2").AllHaveSignal() { - return component.ErrWaitingForInputKeepInputs + return component.NewErrWaitForInputs(true) } return nil }),