Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Oct 8, 2024
1 parent 4e8c890 commit 739ebfe
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 7 deletions.
8 changes: 4 additions & 4 deletions pkg/chart/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (n *ClusterNode) Inbound(name string, prt *port.InPort) {
inPort := port.NewIn()
outPort := port.NewOut()

n.inPorts[node.PortErr] = inPort
n._outPorts[node.PortErr] = outPort
n.inPorts[name] = inPort
n._outPorts[name] = outPort

outPort.Link(prt)

Expand All @@ -57,8 +57,8 @@ func (n *ClusterNode) Outbound(name string, prt *port.OutPort) {
inPort := port.NewIn()
outPort := port.NewOut()

n._inPorts[node.PortErr] = inPort
n.outPorts[node.PortErr] = outPort
n._inPorts[name] = inPort
n.outPorts[name] = outPort

prt.Link(inPort)

Expand Down
105 changes: 105 additions & 0 deletions pkg/chart/cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package chart

import (
"context"
"testing"
"time"

"github.com/go-faker/faker/v4"
"github.com/gofrs/uuid"
"github.com/siyul-park/uniflow/pkg/node"
"github.com/siyul-park/uniflow/pkg/packet"
"github.com/siyul-park/uniflow/pkg/port"
"github.com/siyul-park/uniflow/pkg/process"
"github.com/siyul-park/uniflow/pkg/spec"
"github.com/siyul-park/uniflow/pkg/symbol"
"github.com/siyul-park/uniflow/pkg/types"
"github.com/stretchr/testify/assert"
)

func TestNewClusterNode(t *testing.T) {
n := NewClusterNode(symbol.NewTable())
assert.NotNil(t, n)
assert.NoError(t, n.Close())
}

func TestClusterNode_Inbound(t *testing.T) {
tb := symbol.NewTable()

sb := &symbol.Symbol{
Spec: &spec.Meta{
ID: uuid.Must(uuid.NewV7()),
Kind: faker.Word(),
},
Node: node.NewOneToOneNode(nil),
}
tb.Insert(sb)

n := NewClusterNode(tb)
defer n.Close()

n.Inbound(node.PortIn, sb.In(node.PortIn))
assert.NotNil(t, n.In(node.PortIn))
}

func TestClusterNode_Outbound(t *testing.T) {
tb := symbol.NewTable()

sb := &symbol.Symbol{
Spec: &spec.Meta{
ID: uuid.Must(uuid.NewV7()),
Kind: faker.Word(),
},
Node: node.NewOneToOneNode(nil),
}
tb.Insert(sb)

n := NewClusterNode(tb)
defer n.Close()

n.Outbound(node.PortOut, sb.Out(node.PortOut))
assert.NotNil(t, n.Out(node.PortOut))
}

func NewClusterNode_SendAndReceive(t *testing.T) {
ctx, cancel := context.WithTimeout(context.TODO(), time.Second)
defer cancel()

tb := symbol.NewTable()

sb := &symbol.Symbol{
Spec: &spec.Meta{
ID: uuid.Must(uuid.NewV7()),
Kind: faker.Word(),
},
Node: node.NewOneToOneNode(func(_ *process.Process, inPck *packet.Packet) (*packet.Packet, *packet.Packet) {
return inPck, nil
}),
}
tb.Insert(sb)

n := NewClusterNode(tb)
defer n.Close()

n.Inbound(node.PortIn, sb.In(node.PortIn))
n.Outbound(node.PortOut, sb.Out(node.PortOut))

in := port.NewOut()
in.Link(n.In(node.PortIn))

proc := process.New()
defer proc.Exit(nil)

inWriter := in.Open(proc)

inPayload := types.NewString(faker.UUIDHyphenated())
inPck := packet.New(inPayload)

inWriter.Write(inPck)

select {
case <-inWriter.Receive():
case <-ctx.Done():
assert.Fail(t, ctx.Err().Error())
}
}
10 changes: 8 additions & 2 deletions pkg/chart/linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ func (l *Linker) Load(chrt *Chart) error {
}
return nil, err
}
symbols = append(symbols, &symbol.Symbol{Spec: sp, Node: n})

symbols = append(symbols, &symbol.Symbol{
Spec: sp,
Node: n,
})
}

var loadHooks []symbol.LoadHook
Expand All @@ -80,10 +84,11 @@ func (l *Linker) Load(chrt *Chart) error {
}

n := NewClusterNode(table)

for name, ports := range chrt.GetPorts() {
for _, port := range ports {
for _, sb := range symbols {
if (sb.ID() == port.ID) || (sb.Name() != "" && sb.Name() == port.Name) {
if sb.ID() == port.ID || sb.Name() == port.Name {
if in := sb.In(port.Port); in != nil {
n.Inbound(name, in)
}
Expand All @@ -94,6 +99,7 @@ func (l *Linker) Load(chrt *Chart) error {
}
}
}

return n, nil
})

Expand Down
3 changes: 2 additions & 1 deletion pkg/chart/linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func TestLinker_Load(t *testing.T) {
assert.NoError(t, err)
assert.Contains(t, s.Kinds(), chrt.GetName())

_, err = s.Compile(meta)
n, err := s.Compile(meta)
assert.NoError(t, err)
assert.NotNil(t, n)
}

func TestLinker_Unload(t *testing.T) {
Expand Down

0 comments on commit 739ebfe

Please sign in to comment.