Skip to content

Commit

Permalink
Refactor: use port as a buffer of signals
Browse files Browse the repository at this point in the history
closes #30
  • Loading branch information
hovsep committed Sep 17, 2024
1 parent 3917558 commit 9c6fd04
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 537 deletions.
16 changes: 8 additions & 8 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Component struct {
func NewComponent(name string) *Component {
return &Component{
name: name,
inputs: port.NewPortsCollection(),
outputs: port.NewPortsCollection(),
inputs: port.NewCollection(),
outputs: port.NewCollection(),
}
}

Expand All @@ -34,13 +34,13 @@ func (c *Component) WithDescription(description string) *Component {

// WithInputs ads input ports
func (c *Component) WithInputs(portNames ...string) *Component {
c.inputs = c.inputs.Add(port.NewPortGroup(portNames...)...)
c.inputs = c.inputs.Add(port.NewGroup(portNames...)...)
return c
}

// WithOutputs adds output ports
func (c *Component) WithOutputs(portNames ...string) *Component {
c.outputs = c.outputs.Add(port.NewPortGroup(portNames...)...)
c.outputs = c.outputs.Add(port.NewGroup(portNames...)...)
return c
}

Expand Down Expand Up @@ -80,7 +80,7 @@ func (c *Component) MaybeActivate() (activationResult *ActivationResult) {
defer func() {
if r := recover(); r != nil {
//Clear inputs and exit
c.inputs.ClearSignal()
c.inputs.ClearSignals()
activationResult = c.newActivationCodePanicked(fmt.Errorf("panicked with: %v", r))
}
}()
Expand All @@ -92,7 +92,7 @@ func (c *Component) MaybeActivate() (activationResult *ActivationResult) {
return
}

if !c.inputs.AnyHasSignal() {
if !c.inputs.AnyHasSignals() {
//No inputs set, stop here
activationResult = c.newActivationCodeNoInput()

Expand All @@ -106,14 +106,14 @@ func (c *Component) MaybeActivate() (activationResult *ActivationResult) {
activationResult = c.newActivationCodeWaitingForInput()

if !errors.Is(err, errWaitingForInputsKeep) {
c.inputs.ClearSignal()
c.inputs.ClearSignals()
}

return
}

//Clear inputs
c.inputs.ClearSignal()
c.inputs.ClearSignals()

if err != nil {
activationResult = c.newActivationCodeReturnedError(err)
Expand Down
68 changes: 34 additions & 34 deletions component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ func TestComponent_Description(t *testing.T) {
}

func TestComponent_FlushOutputs(t *testing.T) {
sink := port.NewPort("sink")
sink := port.New("sink")

componentWithAllOutputsSet := NewComponent("c1").WithOutputs("o1", "o2")
componentWithAllOutputsSet.Outputs().ByName("o1").PutSignal(signal.New(777))
componentWithAllOutputsSet.Outputs().ByName("o1").PutSignal(signal.New(888))
componentWithAllOutputsSet.Outputs().ByName("o1").PutSignals(signal.New(777))
componentWithAllOutputsSet.Outputs().ByName("o1").PutSignals(signal.New(888))
componentWithAllOutputsSet.Outputs().ByName("o1").PipeTo(sink)
componentWithAllOutputsSet.Outputs().ByName("o2").PipeTo(sink)

Expand All @@ -133,18 +133,18 @@ func TestComponent_FlushOutputs(t *testing.T) {
component: NewComponent("c1").WithOutputs("o1", "o2"),
destPort: nil,
assertions: func(t *testing.T, componentAfterFlush *Component, destPort *port.Port) {
assert.False(t, componentAfterFlush.Outputs().AnyHasSignal())
assert.False(t, componentAfterFlush.Outputs().AnyHasSignals())
},
},
{
name: "happy path",
component: componentWithAllOutputsSet,
destPort: sink,
assertions: func(t *testing.T, componentAfterFlush *Component, destPort *port.Port) {
assert.Contains(t, destPort.Signal().Payloads(), 777)
assert.Contains(t, destPort.Signal().Payloads(), 888)
assert.Len(t, destPort.Signal().Payloads(), 2)
assert.False(t, componentAfterFlush.Outputs().AnyHasSignal())
assert.Contains(t, destPort.Signals().AllPayloads(), 777)
assert.Contains(t, destPort.Signals().AllPayloads(), 888)
assert.Len(t, destPort.Signals().AllPayloads(), 2)
assert.False(t, componentAfterFlush.Outputs().AnyHasSignals())
},
},
}
Expand All @@ -171,8 +171,8 @@ func TestComponent_Inputs(t *testing.T) {
name: "with inputs",
component: NewComponent("c1").WithInputs("i1", "i2"),
want: port.Collection{
"i1": port.NewPort("i1"),
"i2": port.NewPort("i2"),
"i1": port.New("i1"),
"i2": port.New("i2"),
},
},
}
Expand Down Expand Up @@ -200,8 +200,8 @@ func TestComponent_Outputs(t *testing.T) {
name: "with outputs",
component: NewComponent("c1").WithOutputs("o1", "o2"),
want: port.Collection{
"o1": port.NewPort("o1"),
"o2": port.NewPort("o2"),
"o1": port.New("o1"),
"o2": port.New("o2"),
},
},
}
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestComponent_WithActivationFunc(t *testing.T) {
component: NewComponent("c1"),
args: args{
f: func(inputs port.Collection, outputs port.Collection) error {
outputs.ByName("out1").PutSignal(signal.New(23))
outputs.ByName("out1").PutSignals(signal.New(23))
return nil
},
},
Expand All @@ -240,10 +240,10 @@ func TestComponent_WithActivationFunc(t *testing.T) {
componentAfter := tt.component.WithActivationFunc(tt.args.f)

//Compare activation functions by they result and error
testInputs1 := port.NewPortsCollection().Add(port.NewPortGroup("in1", "in2")...)
testInputs2 := port.NewPortsCollection().Add(port.NewPortGroup("in1", "in2")...)
testOutputs1 := port.NewPortsCollection().Add(port.NewPortGroup("out1", "out2")...)
testOutputs2 := port.NewPortsCollection().Add(port.NewPortGroup("out1", "out2")...)
testInputs1 := port.NewCollection().Add(port.NewGroup("in1", "in2")...)
testInputs2 := port.NewCollection().Add(port.NewGroup("in1", "in2")...)
testOutputs1 := port.NewCollection().Add(port.NewGroup("out1", "out2")...)
testOutputs2 := port.NewCollection().Add(port.NewGroup("out1", "out2")...)
err1 := componentAfter.f(testInputs1, testOutputs1)
err2 := tt.args.f(testInputs2, testOutputs2)
assert.Equal(t, err1, err2)
Expand Down Expand Up @@ -306,8 +306,8 @@ func TestComponent_WithInputs(t *testing.T) {
name: "c1",
description: "",
inputs: port.Collection{
"p1": port.NewPort("p1"),
"p2": port.NewPort("p2"),
"p1": port.New("p1"),
"p2": port.New("p2"),
},
outputs: port.Collection{},
f: nil,
Expand Down Expand Up @@ -358,8 +358,8 @@ func TestComponent_WithOutputs(t *testing.T) {
description: "",
inputs: port.Collection{},
outputs: port.Collection{
"p1": port.NewPort("p1"),
"p2": port.NewPort("p2"),
"p1": port.New("p1"),
"p2": port.New("p2"),
},
f: nil,
},
Expand Down Expand Up @@ -405,7 +405,7 @@ func TestComponent_MaybeActivate(t *testing.T) {
name: "component with inputs set, but no activation func",
getComponent: func() *Component {
c := NewComponent("c1").WithInputs("i1")
c.Inputs().ByName("i1").PutSignal(signal.New(123))
c.Inputs().ByName("i1").PutSignals(signal.New(123))
return c
},
wantActivationResult: NewActivationResult("c1").
Expand All @@ -419,7 +419,7 @@ func TestComponent_MaybeActivate(t *testing.T) {
WithInputs("i1", "i2").
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {

if !inputs.ByNames("i1", "i2").AllHaveSignal() {
if !inputs.ByNames("i1", "i2").AllHaveSignals() {
return NewErrWaitForInputs(false)
}

Expand All @@ -438,14 +438,14 @@ func TestComponent_MaybeActivate(t *testing.T) {
WithInputs("i1", "i2").
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {

if !inputs.ByNames("i1", "i2").AllHaveSignal() {
if !inputs.ByNames("i1", "i2").AllHaveSignals() {
return NewErrWaitForInputs(false)
}

return nil
})
//Only one input set
c.Inputs().ByName("i1").PutSignal(signal.New(123))
c.Inputs().ByName("i1").PutSignals(signal.New(123))
return c
},
wantActivationResult: NewActivationResult("c1").
Expand All @@ -459,14 +459,14 @@ func TestComponent_MaybeActivate(t *testing.T) {
WithInputs("i1", "i2").
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {

if !inputs.ByNames("i1", "i2").AllHaveSignal() {
if !inputs.ByNames("i1", "i2").AllHaveSignals() {
return NewErrWaitForInputs(true)
}

return nil
})
//Only one input set
c.Inputs().ByName("i1").PutSignal(signal.New(123))
c.Inputs().ByName("i1").PutSignals(signal.New(123))
return c
},
wantActivationResult: NewActivationResult("c1").
Expand All @@ -482,7 +482,7 @@ func TestComponent_MaybeActivate(t *testing.T) {
return errors.New("test error")
})
//Only one input set
c.Inputs().ByName("i1").PutSignal(signal.New(123))
c.Inputs().ByName("i1").PutSignals(signal.New(123))
return c
},
wantActivationResult: NewActivationResult("c1").
Expand All @@ -497,11 +497,11 @@ func TestComponent_MaybeActivate(t *testing.T) {
WithInputs("i1").
WithOutputs("o1").
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {
port.ForwardSignal(inputs.ByName("i1"), outputs.ByName("o1"))
port.ForwardSignals(inputs.ByName("i1"), outputs.ByName("o1"))
return nil
})
//Only one input set
c.Inputs().ByName("i1").PutSignal(signal.New(123))
c.Inputs().ByName("i1").PutSignals(signal.New(123))
return c
},
wantActivationResult: NewActivationResult("c1").
Expand All @@ -515,12 +515,12 @@ func TestComponent_MaybeActivate(t *testing.T) {
WithInputs("i1").
WithOutputs("o1").
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {
port.ForwardSignal(inputs.ByName("i1"), outputs.ByName("o1"))
port.ForwardSignals(inputs.ByName("i1"), outputs.ByName("o1"))
panic(errors.New("oh shrimps"))
return nil
})
//Only one input set
c.Inputs().ByName("i1").PutSignal(signal.New(123))
c.Inputs().ByName("i1").PutSignals(signal.New(123))
return c
},
wantActivationResult: NewActivationResult("c1").
Expand All @@ -535,12 +535,12 @@ func TestComponent_MaybeActivate(t *testing.T) {
WithInputs("i1").
WithOutputs("o1").
WithActivationFunc(func(inputs port.Collection, outputs port.Collection) error {
port.ForwardSignal(inputs.ByName("i1"), outputs.ByName("o1"))
port.ForwardSignals(inputs.ByName("i1"), outputs.ByName("o1"))
panic("oh shrimps")
return nil
})
//Only one input set
c.Inputs().ByName("i1").PutSignal(signal.New(123))
c.Inputs().ByName("i1").PutSignals(signal.New(123))
return c
},
wantActivationResult: NewActivationResult("c1").
Expand Down
Loading

0 comments on commit 9c6fd04

Please sign in to comment.