diff --git a/component/activation_result_collection_test.go b/component/activation_result_collection_test.go index 283f6b7..4882b3c 100644 --- a/component/activation_result_collection_test.go +++ b/component/activation_result_collection_test.go @@ -46,7 +46,7 @@ func TestActivationResultCollection_Add(t *testing.T) { }, }, { - name: "adding to existing collection", + name: "adding to non-empty collection", collection: NewActivationResultCollection().Add( New("c1").newActivationResultOK(), New("c2").newActivationResultOK(), diff --git a/component/collection_test.go b/component/collection_test.go index 112afab..ff5888e 100644 --- a/component/collection_test.go +++ b/component/collection_test.go @@ -80,7 +80,7 @@ func TestCollection_Add(t *testing.T) { }, }, { - name: "adding to existing collection", + name: "adding to non-empty collection", collection: NewComponentCollection().Add(New("c1"), New("c2")), args: args{ components: []*Component{New("c3"), New("c4")}, diff --git a/component/component.go b/component/component.go index 42446c3..4d7014c 100644 --- a/component/component.go +++ b/component/component.go @@ -33,25 +33,25 @@ func (c *Component) WithDescription(description string) *Component { // WithInputs ads input ports func (c *Component) WithInputs(portNames ...string) *Component { - c.inputs = c.Inputs().Add(port.NewGroup(portNames...)...) + c.inputs = c.Inputs().With(port.NewGroup(portNames...)...) return c } // WithOutputs adds output ports func (c *Component) WithOutputs(portNames ...string) *Component { - c.outputs = c.Outputs().Add(port.NewGroup(portNames...)...) + c.outputs = c.Outputs().With(port.NewGroup(portNames...)...) return c } // WithInputsIndexed creates multiple prefixed ports func (c *Component) WithInputsIndexed(prefix string, startIndex int, endIndex int) *Component { - c.inputs = c.Inputs().AddIndexed(prefix, startIndex, endIndex) + c.inputs = c.Inputs().WithIndexed(prefix, startIndex, endIndex) return c } // WithOutputsIndexed creates multiple prefixed ports func (c *Component) WithOutputsIndexed(prefix string, startIndex int, endIndex int) *Component { - c.outputs = c.Outputs().AddIndexed(prefix, startIndex, endIndex) + c.outputs = c.Outputs().WithIndexed(prefix, startIndex, endIndex) return c } diff --git a/component/component_test.go b/component/component_test.go index bc12241..6600340 100644 --- a/component/component_test.go +++ b/component/component_test.go @@ -46,7 +46,7 @@ func TestNewComponent(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - assert.Equalf(t, tt.want, New(tt.args.name), "New(%v)", tt.args.name) + assert.Equal(t, tt.want, New(tt.args.name)) }) } } @@ -233,10 +233,10 @@ func TestComponent_WithActivationFunc(t *testing.T) { componentAfter := tt.component.WithActivationFunc(tt.args.f) //Compare activation functions by they result and error - 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")...) + testInputs1 := port.NewCollection().With(port.NewGroup("in1", "in2")...) + testInputs2 := port.NewCollection().With(port.NewGroup("in1", "in2")...) + testOutputs1 := port.NewCollection().With(port.NewGroup("out1", "out2")...) + testOutputs2 := port.NewCollection().With(port.NewGroup("out1", "out2")...) err1 := componentAfter.f(testInputs1, testOutputs1) err2 := tt.args.f(testInputs2, testOutputs2) assert.Equal(t, err1, err2) diff --git a/cycle/cycle_test.go b/cycle/cycle_test.go index 21c3a29..88e3f91 100644 --- a/cycle/cycle_test.go +++ b/cycle/cycle_test.go @@ -200,7 +200,7 @@ func TestCycle_WithActivationResults(t *testing.T) { }, }, { - name: "adding to existing collection", + name: "adding to non-empty collection", cycleResult: New().WithActivationResults( component.NewActivationResult("c1"). SetActivated(false). diff --git a/port/collection.go b/port/collection.go index e404bfe..b361027 100644 --- a/port/collection.go +++ b/port/collection.go @@ -59,8 +59,8 @@ func (collection Collection) PutSignals(signals ...*signal.Signal) { } } -// WithSignals adds signals to every port in collection and returns the collection -func (collection Collection) WithSignals(signals ...*signal.Signal) Collection { +// withSignals adds signals to every port in collection and returns the collection +func (collection Collection) withSignals(signals ...*signal.Signal) Collection { collection.PutSignals(signals...) return collection } @@ -79,28 +79,25 @@ func (collection Collection) Flush() { } } -// PipeTo creates pipes from each port in collection -func (collection Collection) PipeTo(toPorts ...*Port) { +// PipeTo creates pipes from each port in collection to given destination ports +func (collection Collection) PipeTo(destPorts ...*Port) { for _, p := range collection { - p.PipeTo(toPorts...) + p.PipeTo(destPorts...) } } -// Add adds ports to collection -func (collection Collection) Add(ports ...*Port) Collection { +// With adds ports to collection and returns it +func (collection Collection) With(ports ...*Port) Collection { for _, port := range ports { - if port == nil { - continue - } collection[port.Name()] = port } return collection } -// AddIndexed creates ports with names like "o1","o2","o3" and so on -func (collection Collection) AddIndexed(prefix string, startIndex int, endIndex int) Collection { - return collection.Add(NewIndexedGroup(prefix, startIndex, endIndex)...) +// WithIndexed creates ports with names like "o1","o2","o3" and so on +func (collection Collection) WithIndexed(prefix string, startIndex int, endIndex int) Collection { + return collection.With(NewIndexedGroup(prefix, startIndex, endIndex)...) } // Signals returns all signals of all ports in the group diff --git a/port/collection_test.go b/port/collection_test.go index a68492b..e33ebb3 100644 --- a/port/collection_test.go +++ b/port/collection_test.go @@ -7,11 +7,9 @@ import ( ) func TestCollection_AllHaveSignal(t *testing.T) { - oneEmptyPorts := NewCollection().Add(NewGroup("p1", "p2", "p3")...).WithSignals(signal.New(123)) + oneEmptyPorts := NewCollection().With(NewGroup("p1", "p2", "p3")...).withSignals(signal.New(123)) oneEmptyPorts.ByName("p2").Clear() - allWithSignalPorts := NewCollection().Add(NewGroup("out1", "out2", "out3")...).WithSignals(signal.New(77)) - tests := []struct { name string ports Collection @@ -19,7 +17,7 @@ func TestCollection_AllHaveSignal(t *testing.T) { }{ { name: "all empty", - ports: NewCollection().Add(NewGroup("p1", "p2")...), + ports: NewCollection().With(NewGroup("p1", "p2")...), want: false, }, { @@ -29,7 +27,7 @@ func TestCollection_AllHaveSignal(t *testing.T) { }, { name: "all set", - ports: allWithSignalPorts, + ports: NewCollection().With(NewGroup("out1", "out2", "out3")...).withSignals(signal.New(77)), want: true, }, } @@ -41,7 +39,7 @@ func TestCollection_AllHaveSignal(t *testing.T) { } func TestCollection_AnyHasSignal(t *testing.T) { - oneEmptyPorts := NewCollection().Add(NewGroup("p1", "p2", "p3")...).WithSignals(signal.New(123)) + oneEmptyPorts := NewCollection().With(NewGroup("p1", "p2", "p3")...).withSignals(signal.New(123)) oneEmptyPorts.ByName("p2").Clear() tests := []struct { @@ -56,7 +54,7 @@ func TestCollection_AnyHasSignal(t *testing.T) { }, { name: "all empty", - ports: NewCollection().Add(NewGroup("p1", "p2", "p3")...), + ports: NewCollection().With(NewGroup("p1", "p2", "p3")...), want: false, }, } @@ -68,8 +66,6 @@ func TestCollection_AnyHasSignal(t *testing.T) { } func TestCollection_ByName(t *testing.T) { - portsWithSignals := NewCollection().Add(NewGroup("p1", "p2")...).WithSignals(signal.New(12)) - type args struct { name string } @@ -81,7 +77,7 @@ func TestCollection_ByName(t *testing.T) { }{ { name: "empty port found", - collection: NewCollection().Add(NewGroup("p1", "p2")...), + collection: NewCollection().With(NewGroup("p1", "p2")...), args: args{ name: "p1", }, @@ -89,7 +85,7 @@ func TestCollection_ByName(t *testing.T) { }, { name: "port with signals found", - collection: portsWithSignals, + collection: NewCollection().With(NewGroup("p1", "p2")...).withSignals(signal.New(12)), args: args{ name: "p2", }, @@ -101,7 +97,7 @@ func TestCollection_ByName(t *testing.T) { }, { name: "port not found", - collection: NewCollection().Add(NewGroup("p1", "p2")...), + collection: NewCollection().With(NewGroup("p1", "p2")...), args: args{ name: "p3", }, @@ -132,7 +128,7 @@ func TestCollection_ByNames(t *testing.T) { }{ { name: "single port found", - ports: NewCollection().Add(NewGroup("p1", "p2")...), + ports: NewCollection().With(NewGroup("p1", "p2")...), args: args{ names: []string{"p1"}, }, @@ -146,7 +142,7 @@ func TestCollection_ByNames(t *testing.T) { }, { name: "multiple ports found", - ports: NewCollection().Add(NewGroup("p1", "p2")...), + ports: NewCollection().With(NewGroup("p1", "p2")...), args: args{ names: []string{"p1", "p2"}, }, @@ -165,7 +161,7 @@ func TestCollection_ByNames(t *testing.T) { }, { name: "single port not found", - ports: NewCollection().Add(NewGroup("p1", "p2")...), + ports: NewCollection().With(NewGroup("p1", "p2")...), args: args{ names: []string{"p7"}, }, @@ -173,7 +169,7 @@ func TestCollection_ByNames(t *testing.T) { }, { name: "some ports not found", - ports: NewCollection().Add(NewGroup("p1", "p2")...), + ports: NewCollection().With(NewGroup("p1", "p2")...), args: args{ names: []string{"p1", "p2", "p3"}, }, @@ -200,14 +196,14 @@ func TestCollection_ByNames(t *testing.T) { func TestCollection_ClearSignal(t *testing.T) { t.Run("happy path", func(t *testing.T) { - ports := NewCollection().Add(NewGroup("p1", "p2", "p3")...).WithSignals(signal.NewGroup(1, 2, 3)...) + ports := NewCollection().With(NewGroup("p1", "p2", "p3")...).withSignals(signal.NewGroup(1, 2, 3)...) assert.True(t, ports.AllHaveSignals()) ports.Clear() assert.False(t, ports.AnyHasSignals()) }) } -func TestCollection_Add(t *testing.T) { +func TestCollection_With(t *testing.T) { type args struct { ports []*Port } @@ -239,8 +235,8 @@ func TestCollection_Add(t *testing.T) { }, }, { - name: "adding to existing collection", - collection: NewCollection().Add(NewGroup("p1", "p2")...), + name: "adding to non-empty collection", + collection: NewCollection().With(NewGroup("p1", "p2")...), args: args{ ports: NewGroup("p3", "p4"), }, @@ -252,10 +248,171 @@ func TestCollection_Add(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.collection = tt.collection.Add(tt.args.ports...) + tt.collection = tt.collection.With(tt.args.ports...) + if tt.assertions != nil { + tt.assertions(t, tt.collection) + } + }) + } +} + +func TestCollection_Flush(t *testing.T) { + tests := []struct { + name string + collection Collection + assertions func(t *testing.T, collection Collection) + }{ + { + name: "empty collection", + collection: NewCollection(), + assertions: func(t *testing.T, collection Collection) { + assert.Len(t, collection, 0) + }, + }, + { + name: "all ports in collection are flushed", + collection: NewCollection().With( + New("src"). + WithSignals(signal.NewGroup(1, 2, 3)...). + withPipes(New("dst1"), New("dst2")), + ), + assertions: func(t *testing.T, collection Collection) { + assert.Len(t, collection, 1) + assert.False(t, collection.ByName("src").HasSignals()) + for _, destPort := range collection.ByName("src").pipes { + assert.Len(t, destPort.Signals(), 3) + assert.Contains(t, destPort.Signals().AllPayloads(), 1) + assert.Contains(t, destPort.Signals().AllPayloads(), 2) + assert.Contains(t, destPort.Signals().AllPayloads(), 3) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.collection.Flush() + if tt.assertions != nil { + tt.assertions(t, tt.collection) + } + }) + } +} + +func TestCollection_PipeTo(t *testing.T) { + type args struct { + destPorts []*Port + } + tests := []struct { + name string + collection Collection + args args + assertions func(t *testing.T, collection Collection) + }{ + { + name: "empty collection", + collection: NewCollection(), + args: args{ + destPorts: NewIndexedGroup("dest_", 1, 3), + }, + assertions: func(t *testing.T, collection Collection) { + assert.Len(t, collection, 0) + }, + }, + { + name: "add pipes to each port in collection", + collection: NewCollection().With(NewIndexedGroup("p", 1, 3)...), + args: args{ + destPorts: NewIndexedGroup("dest", 1, 5), + }, + assertions: func(t *testing.T, collection Collection) { + assert.Len(t, collection, 3) + for _, p := range collection { + assert.True(t, p.HasPipes()) + assert.Len(t, p.pipes, 5) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.collection.PipeTo(tt.args.destPorts...) if tt.assertions != nil { tt.assertions(t, tt.collection) } }) } } + +func TestCollection_WithIndexed(t *testing.T) { + type args struct { + prefix string + startIndex int + endIndex int + } + tests := []struct { + name string + collection Collection + args args + assertions func(t *testing.T, collection Collection) + }{ + { + name: "adding to empty collection", + collection: NewCollection(), + args: args{ + prefix: "p", + startIndex: 1, + endIndex: 3, + }, + assertions: func(t *testing.T, collection Collection) { + assert.Len(t, collection, 3) + }, + }, + { + name: "adding to non-empty collection", + collection: NewCollection().With(NewGroup("p1", "p2", "p3")...), + args: args{ + prefix: "p", + startIndex: 4, + endIndex: 5, + }, + assertions: func(t *testing.T, collection Collection) { + assert.Len(t, collection, 5) + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + collectionAfter := tt.collection.WithIndexed(tt.args.prefix, tt.args.startIndex, tt.args.endIndex) + if tt.assertions != nil { + tt.assertions(t, collectionAfter) + } + }) + } +} + +func TestCollection_Signals(t *testing.T) { + tests := []struct { + name string + collection Collection + want signal.Group + }{ + { + name: "empty collection", + collection: NewCollection(), + want: signal.NewGroup(), + }, + { + name: "non-empty collection", + collection: NewCollection(). + WithIndexed("p", 1, 3). + withSignals(signal.NewGroup(1, 2, 3)...). + withSignals(signal.New("test")), + want: signal.NewGroup(1, 2, 3, "test", 1, 2, 3, "test", 1, 2, 3, "test"), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, tt.collection.Signals()) + }) + } +} diff --git a/port/group.go b/port/group.go index 7caf63b..988e01f 100644 --- a/port/group.go +++ b/port/group.go @@ -14,12 +14,9 @@ func NewGroup(names ...string) Group { return group } -// NewIndexedGroup is useful when you want to create group of ports with same prefix +// NewIndexedGroup is useful to create group of ports with same prefix +// NOTE: endIndex is inclusive, e.g. NewIndexedGroup("p", 0, 0) will create one port with name "p0" func NewIndexedGroup(prefix string, startIndex int, endIndex int) Group { - if prefix == "" { - return nil - } - if startIndex > endIndex { return nil } diff --git a/port/group_test.go b/port/group_test.go new file mode 100644 index 0000000..3fb8a4f --- /dev/null +++ b/port/group_test.go @@ -0,0 +1,137 @@ +package port + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewGroup(t *testing.T) { + type args struct { + names []string + } + tests := []struct { + name string + args args + want Group + }{ + { + name: "empty group", + args: args{ + names: nil, + }, + want: Group{}, + }, + { + name: "non-empty group", + args: args{ + names: []string{"p1", "p2"}, + }, + want: Group{ + New("p1"), + New("p2"), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, NewGroup(tt.args.names...)) + }) + } +} + +func TestNewIndexedGroup(t *testing.T) { + type args struct { + prefix string + startIndex int + endIndex int + } + tests := []struct { + name string + args args + want Group + }{ + { + name: "empty prefix is valid", + args: args{ + prefix: "", + startIndex: 0, + endIndex: 3, + }, + want: NewGroup("0", "1", "2", "3"), + }, + { + name: "with prefix", + args: args{ + prefix: "in_", + startIndex: 4, + endIndex: 5, + }, + want: NewGroup("in_4", "in_5"), + }, + { + name: "with invalid start index", + args: args{ + prefix: "", + startIndex: 999, + endIndex: 5, + }, + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, NewIndexedGroup(tt.args.prefix, tt.args.startIndex, tt.args.endIndex)) + }) + } +} + +func TestGroup_With(t *testing.T) { + type args struct { + ports []*Port + } + tests := []struct { + name string + group Group + args args + assertions func(t *testing.T, group Group) + }{ + { + name: "adding nothing to empty group", + group: NewGroup(), + args: args{ + ports: nil, + }, + assertions: func(t *testing.T, group Group) { + assert.Len(t, group, 0) + }, + }, + { + name: "adding to empty group", + group: NewGroup(), + args: args{ + ports: NewGroup("p1", "p2", "p3"), + }, + assertions: func(t *testing.T, group Group) { + assert.Len(t, group, 3) + }, + }, + { + name: "adding to non-empty group", + group: NewIndexedGroup("p", 1, 3), + args: args{ + ports: NewGroup("p4", "p5", "p6"), + }, + assertions: func(t *testing.T, group Group) { + assert.Len(t, group, 6) + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + groupAfter := tt.group.With(tt.args.ports...) + if tt.assertions != nil { + tt.assertions(t, groupAfter) + } + }) + } +} diff --git a/port/metadata.go b/port/metadata.go deleted file mode 100644 index 319b452..0000000 --- a/port/metadata.go +++ /dev/null @@ -1,20 +0,0 @@ -package port - -// Metadata contains metadata about the port -type Metadata struct { - SignalBufferLen int -} - -// MetadataMap contains port metadata indexed by port name -type MetadataMap map[string]*Metadata - -// GetPortsMetadata returns info about current length of each port in collection -func (collection Collection) GetPortsMetadata() MetadataMap { - res := make(MetadataMap) - for _, p := range collection { - res[p.Name()] = &Metadata{ - SignalBufferLen: len(p.Signals()), - } - } - return res -} diff --git a/port/port.go b/port/port.go index 22a459a..5b26c4e 100644 --- a/port/port.go +++ b/port/port.go @@ -78,15 +78,23 @@ func (p *Port) HasPipes() bool { // PipeTo creates one or multiple pipes to other port(s) // @TODO: hide this method from AF -func (p *Port) PipeTo(toPorts ...*Port) { - for _, toPort := range toPorts { - if toPort == nil { +func (p *Port) PipeTo(destPorts ...*Port) { + for _, destPort := range destPorts { + if destPort == nil { continue } - p.pipes = p.pipes.With(toPort) + p.pipes = p.pipes.With(destPort) } } +// withPipes adds pipes and returns the port +func (p *Port) withPipes(destPorts ...*Port) *Port { + for _, destPort := range destPorts { + p.PipeTo(destPort) + } + return p +} + // ForwardSignals copies all signals from source port to destination port, without clearing the source port func ForwardSignals(source *Port, dest *Port) { dest.PutSignals(source.Signals()...) diff --git a/port/port_test.go b/port/port_test.go index 5fa5cd9..98b12cb 100644 --- a/port/port_test.go +++ b/port/port_test.go @@ -7,8 +7,6 @@ import ( ) func TestPort_HasSignals(t *testing.T) { - portWithSignal := New("portWithSignal").WithSignals(signal.New(123)) - tests := []struct { name string port *Port @@ -21,7 +19,7 @@ func TestPort_HasSignals(t *testing.T) { }, { name: "port has normal signals", - port: portWithSignal, + port: New("p").WithSignals(signal.New(123)), want: true, }, } @@ -33,8 +31,6 @@ func TestPort_HasSignals(t *testing.T) { } func TestPort_Signals(t *testing.T) { - portWithSignal := New("portWithSignal").WithSignals(signal.New(123)) - tests := []struct { name string port *Port @@ -47,7 +43,7 @@ func TestPort_Signals(t *testing.T) { }, { name: "with signal", - port: portWithSignal, + port: New("p").WithSignals(signal.New(123)), want: signal.NewGroup(123), }, } @@ -59,8 +55,6 @@ func TestPort_Signals(t *testing.T) { } func TestPort_Clear(t *testing.T) { - portWithSignal := New("portWithSignal").WithSignals(signal.New(111)) - tests := []struct { name string before *Port @@ -68,8 +62,8 @@ func TestPort_Clear(t *testing.T) { }{ { name: "happy path", - before: portWithSignal, - after: &Port{name: "portWithSignal", pipes: Group{}, signals: signal.Group{}}, + before: New("p").WithSignals(signal.New(111)), + after: &Port{name: "p", pipes: Group{}, signals: signal.Group{}}, }, { name: "cleaning empty port", @@ -131,77 +125,51 @@ func TestPort_PipeTo(t *testing.T) { } func TestPort_PutSignals(t *testing.T) { - portWithSingleSignal := New("portWithSingleSignal").WithSignals(signal.New(11)) - - portWithMultipleSignals := New("portWithMultipleSignals").WithSignals(signal.NewGroup(11, 12)...) - - portWithMultipleSignals2 := New("portWithMultipleSignals2").WithSignals(signal.NewGroup(55, 66)...) - type args struct { signals []*signal.Signal } tests := []struct { - name string - before *Port - after *Port - args args + name string + port *Port + signalsAfter signal.Group + args args }{ { - name: "single signal to empty port", - before: New("emptyPort"), - after: &Port{ - name: "emptyPort", - signals: signal.NewGroup(11), - pipes: Group{}, - }, + name: "single signal to empty port", + port: New("emptyPort"), + signalsAfter: signal.NewGroup(11), args: args{ signals: signal.NewGroup(11), }, }, { - name: "multiple signals to empty port", - before: New("p"), - after: &Port{ - name: "p", - signals: signal.NewGroup(11, 12), - pipes: Group{}, - }, + name: "multiple signals to empty port", + port: New("p"), + signalsAfter: signal.NewGroup(11, 12), args: args{ signals: signal.NewGroup(11, 12), }, }, { - name: "single signal to port with single signal", - before: portWithSingleSignal, - after: &Port{ - name: "portWithSingleSignal", - signals: signal.NewGroup(11, 12), - pipes: Group{}, - }, + name: "single signal to port with single signal", + port: New("p").WithSignals(signal.New(11)), + signalsAfter: signal.NewGroup(11, 12), args: args{ signals: signal.NewGroup(12), }, }, { - name: "single signals to port with multiple signals", - before: portWithMultipleSignals, - after: &Port{ - name: "portWithMultipleSignals", - signals: signal.NewGroup(11, 12, 13), - pipes: Group{}, - }, + name: "single signals to port with multiple signals", + port: New("p").WithSignals(signal.NewGroup(11, 12)...), + signalsAfter: signal.NewGroup(11, 12, 13), args: args{ signals: signal.NewGroup(13), }, }, { - name: "multiple signals to port with multiple signals", - before: portWithMultipleSignals2, - after: &Port{ - name: "portWithMultipleSignals2", - signals: signal.NewGroup(55, 66, 13, 14), //Notice LIFO order - pipes: Group{}, - }, + name: "multiple signals to port with multiple signals", + port: New("p").WithSignals(signal.NewGroup(55, 66)...), + signalsAfter: signal.NewGroup(55, 66, 13, 14), //Notice LIFO order args: args{ signals: signal.NewGroup(13, 14), }, @@ -209,8 +177,8 @@ func TestPort_PutSignals(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.before.PutSignals(tt.args.signals...) - assert.ElementsMatch(t, tt.after.Signals(), tt.before.Signals()) + tt.port.PutSignals(tt.args.signals...) + assert.ElementsMatch(t, tt.signalsAfter, tt.port.Signals()) }) } } @@ -272,3 +240,97 @@ func TestNewPort(t *testing.T) { }) } } + +func TestPort_HasPipes(t *testing.T) { + tests := []struct { + name string + port *Port + want bool + }{ + { + name: "no pipes", + port: New("p"), + want: false, + }, + { + name: "with pipes", + port: New("p1").withPipes(New("p2")), + want: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, tt.port.HasPipes()) + }) + } +} + +func TestPort_Flush(t *testing.T) { + tests := []struct { + name string + srcPort *Port + assertions func(t *testing.T, srcPort *Port) + }{ + { + name: "port with signals and no pipes is not flushed", + srcPort: New("p").WithSignals(signal.NewGroup(1, 2, 3)...), + assertions: func(t *testing.T, srcPort *Port) { + assert.True(t, srcPort.HasSignals()) + assert.Len(t, srcPort.Signals(), 3) + assert.False(t, srcPort.HasPipes()) + }, + }, + { + name: "empty port with pipes is not flushed", + srcPort: New("p").withPipes(New("p1"), New("p2")), + assertions: func(t *testing.T, srcPort *Port) { + assert.False(t, srcPort.HasSignals()) + assert.True(t, srcPort.HasPipes()) + }, + }, + { + name: "flush to empty ports", + srcPort: New("p").WithSignals(signal.NewGroup(1, 2, 3)...). + withPipes( + New("p1"), + New("p2")), + assertions: func(t *testing.T, srcPort *Port) { + assert.False(t, srcPort.HasSignals()) + assert.True(t, srcPort.HasPipes()) + for _, destPort := range srcPort.pipes { + assert.True(t, destPort.HasSignals()) + assert.Len(t, destPort.Signals(), 3) + assert.Contains(t, destPort.Signals().AllPayloads(), 1) + assert.Contains(t, destPort.Signals().AllPayloads(), 2) + assert.Contains(t, destPort.Signals().AllPayloads(), 3) + } + }, + }, + { + name: "flush to non empty ports", + srcPort: New("p").WithSignals(signal.NewGroup(1, 2, 3)...). + withPipes( + New("p1").WithSignals(signal.NewGroup(4, 5, 6)...), + New("p2").WithSignals(signal.NewGroup(7, 8, 9)...)), + assertions: func(t *testing.T, srcPort *Port) { + assert.False(t, srcPort.HasSignals()) + assert.True(t, srcPort.HasPipes()) + for _, destPort := range srcPort.pipes { + assert.True(t, destPort.HasSignals()) + assert.Len(t, destPort.Signals(), 6) + assert.Contains(t, destPort.Signals().AllPayloads(), 1) + assert.Contains(t, destPort.Signals().AllPayloads(), 2) + assert.Contains(t, destPort.Signals().AllPayloads(), 3) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.srcPort.Flush() + if tt.assertions != nil { + tt.assertions(t, tt.srcPort) + } + }) + } +}