From 739ebfe94af145d4c1b06d2dcfb6fdf8dad4d2a5 Mon Sep 17 00:00:00 2001 From: siyul-park Date: Tue, 8 Oct 2024 05:36:29 -0400 Subject: [PATCH] test: add test case --- pkg/chart/cluster.go | 8 +-- pkg/chart/cluster_test.go | 105 ++++++++++++++++++++++++++++++++++++++ pkg/chart/linker.go | 10 +++- pkg/chart/linker_test.go | 3 +- 4 files changed, 119 insertions(+), 7 deletions(-) create mode 100644 pkg/chart/cluster_test.go diff --git a/pkg/chart/cluster.go b/pkg/chart/cluster.go index 51e63826..c81315d3 100644 --- a/pkg/chart/cluster.go +++ b/pkg/chart/cluster.go @@ -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) @@ -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) diff --git a/pkg/chart/cluster_test.go b/pkg/chart/cluster_test.go new file mode 100644 index 00000000..cad7c3fa --- /dev/null +++ b/pkg/chart/cluster_test.go @@ -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()) + } +} diff --git a/pkg/chart/linker.go b/pkg/chart/linker.go index e251d4ab..73f9b098 100644 --- a/pkg/chart/linker.go +++ b/pkg/chart/linker.go @@ -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 @@ -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) } @@ -94,6 +99,7 @@ func (l *Linker) Load(chrt *Chart) error { } } } + return n, nil }) diff --git a/pkg/chart/linker_test.go b/pkg/chart/linker_test.go index 10c9f2a7..54d56438 100644 --- a/pkg/chart/linker_test.go +++ b/pkg/chart/linker_test.go @@ -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) {