Skip to content

Commit

Permalink
Component: better waiting for inputs api
Browse files Browse the repository at this point in the history
  • Loading branch information
hovsep committed Sep 16, 2024
1 parent 257e20c commit 7a9fbda
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 51 deletions.
4 changes: 2 additions & 2 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
27 changes: 24 additions & 3 deletions component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
18 changes: 12 additions & 6 deletions component/errors.go
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 0 additions & 39 deletions component/errors_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion fmesh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}),
Expand Down

0 comments on commit 7a9fbda

Please sign in to comment.