2021-12-27 22:16:31 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 13:42:47 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-12-27 22:16:31 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 13:42:47 +08:00
|
|
|
//
|
2021-12-27 22:16:31 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-04-19 13:42:47 +08:00
|
|
|
|
2020-11-02 16:44:54 +08:00
|
|
|
package flowgraph
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2021-07-15 14:51:55 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-11-02 16:44:54 +08:00
|
|
|
)
|
|
|
|
|
2022-10-10 22:15:22 +08:00
|
|
|
// Flow graph basic example: count `c = pow(a) + 2`
|
2021-07-15 14:51:55 +08:00
|
|
|
// nodeA: receive input value a from input channel
|
|
|
|
// nodeB: count b = pow(a, 2)
|
2022-10-10 22:15:22 +08:00
|
|
|
// nodeD: count c = b + 2
|
2020-11-02 16:44:54 +08:00
|
|
|
|
|
|
|
type nodeA struct {
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode
|
2021-07-15 14:51:55 +08:00
|
|
|
inputChan chan float64
|
|
|
|
a float64
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type nodeB struct {
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode
|
2020-11-02 16:44:54 +08:00
|
|
|
b float64
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeC struct {
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode
|
2021-07-15 14:51:55 +08:00
|
|
|
d float64
|
|
|
|
outputChan chan float64
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
type numMsg struct {
|
2020-11-02 16:44:54 +08:00
|
|
|
num float64
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (m *numMsg) TimeTick() Timestamp {
|
|
|
|
return Timestamp(0)
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeA) Name() string {
|
2020-11-02 16:44:54 +08:00
|
|
|
return "NodeA"
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeA) Operate(in []Msg) []Msg {
|
|
|
|
// ignore `in` because nodeA doesn't have any upstream node.
|
|
|
|
a := <-n.inputChan
|
|
|
|
var res Msg = &numMsg{
|
|
|
|
num: a,
|
|
|
|
}
|
2022-10-10 22:15:22 +08:00
|
|
|
return []Msg{res}
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeB) Name() string {
|
2020-11-02 16:44:54 +08:00
|
|
|
return "NodeB"
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeB) Operate(in []Msg) []Msg {
|
|
|
|
a, ok := in[0].(*numMsg)
|
|
|
|
if !ok {
|
2022-10-10 22:15:22 +08:00
|
|
|
return nil
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-07-15 14:51:55 +08:00
|
|
|
b := math.Pow(a.num, 2)
|
|
|
|
var res Msg = &numMsg{
|
|
|
|
num: b,
|
|
|
|
}
|
|
|
|
return []Msg{res}
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeC) Name() string {
|
2020-11-02 16:44:54 +08:00
|
|
|
return "NodeC"
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
func (n *nodeC) Operate(in []Msg) []Msg {
|
|
|
|
b, ok := in[0].(*numMsg)
|
2020-11-02 16:44:54 +08:00
|
|
|
if !ok {
|
2021-07-15 14:51:55 +08:00
|
|
|
return nil
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2022-10-10 22:15:22 +08:00
|
|
|
c := b.num + 2
|
|
|
|
n.outputChan <- c
|
2021-07-15 14:51:55 +08:00
|
|
|
// return nil because nodeD doesn't have any downstream node.
|
|
|
|
return nil
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2022-10-10 22:15:22 +08:00
|
|
|
func createExampleFlowGraph() (*TimeTickedFlowGraph, chan float64, chan float64, context.CancelFunc, error) {
|
2020-11-18 17:32:52 +08:00
|
|
|
const MaxQueueLength = 1024
|
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
inputChan := make(chan float64, MaxQueueLength)
|
|
|
|
outputChan := make(chan float64, MaxQueueLength)
|
2020-11-12 12:04:12 +08:00
|
|
|
|
2020-11-02 16:44:54 +08:00
|
|
|
fg := NewTimeTickedFlowGraph(ctx)
|
|
|
|
|
|
|
|
var a Node = &nodeA{
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode: BaseNode{
|
2020-11-09 16:27:11 +08:00
|
|
|
maxQueueLength: MaxQueueLength,
|
2020-11-02 16:44:54 +08:00
|
|
|
},
|
2021-07-15 14:51:55 +08:00
|
|
|
inputChan: inputChan,
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
var b Node = &nodeB{
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode: BaseNode{
|
2020-11-09 16:27:11 +08:00
|
|
|
maxQueueLength: MaxQueueLength,
|
2020-11-02 16:44:54 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var c Node = &nodeC{
|
2020-11-02 19:30:12 +08:00
|
|
|
BaseNode: BaseNode{
|
2020-11-09 16:27:11 +08:00
|
|
|
maxQueueLength: MaxQueueLength,
|
2020-11-02 16:44:54 +08:00
|
|
|
},
|
2021-07-15 14:51:55 +08:00
|
|
|
outputChan: outputChan,
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2021-02-25 17:35:36 +08:00
|
|
|
fg.AddNode(a)
|
|
|
|
fg.AddNode(b)
|
|
|
|
fg.AddNode(c)
|
2020-11-02 16:44:54 +08:00
|
|
|
|
|
|
|
var err = fg.SetEdges(a.Name(),
|
2022-10-10 22:15:22 +08:00
|
|
|
[]string{b.Name()},
|
2020-11-02 16:44:54 +08:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-10-10 22:15:22 +08:00
|
|
|
return nil, nil, nil, cancel, err
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = fg.SetEdges(b.Name(),
|
2022-10-10 22:15:22 +08:00
|
|
|
[]string{c.Name()},
|
2020-11-02 16:44:54 +08:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-10-10 22:15:22 +08:00
|
|
|
return nil, nil, nil, cancel, err
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = fg.SetEdges(c.Name(),
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-10-10 22:15:22 +08:00
|
|
|
return nil, nil, nil, cancel, err
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
|
|
|
|
2022-10-10 22:15:22 +08:00
|
|
|
return fg, inputChan, outputChan, cancel, nil
|
2021-07-15 14:51:55 +08:00
|
|
|
}
|
2020-11-02 16:44:54 +08:00
|
|
|
|
2021-09-13 16:34:26 +08:00
|
|
|
func TestTimeTickedFlowGraph_AddNode(t *testing.T) {
|
2021-09-07 09:39:47 +08:00
|
|
|
const MaxQueueLength = 1024
|
|
|
|
inputChan := make(chan float64, MaxQueueLength)
|
|
|
|
|
|
|
|
fg := NewTimeTickedFlowGraph(context.TODO())
|
|
|
|
|
|
|
|
var a Node = &nodeA{
|
|
|
|
BaseNode: BaseNode{
|
|
|
|
maxQueueLength: MaxQueueLength,
|
|
|
|
},
|
|
|
|
inputChan: inputChan,
|
|
|
|
}
|
|
|
|
var b Node = &nodeB{
|
|
|
|
BaseNode: BaseNode{
|
|
|
|
maxQueueLength: MaxQueueLength,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fg.AddNode(a)
|
|
|
|
assert.Equal(t, len(fg.nodeCtx), 1)
|
|
|
|
fg.AddNode(b)
|
|
|
|
assert.Equal(t, len(fg.nodeCtx), 2)
|
|
|
|
}
|
|
|
|
|
2021-09-13 16:34:26 +08:00
|
|
|
func TestTimeTickedFlowGraph_Start(t *testing.T) {
|
2022-10-10 22:15:22 +08:00
|
|
|
fg, inputChan, outputChan, cancel, err := createExampleFlowGraph()
|
|
|
|
assert.NoError(t, err)
|
2021-07-15 14:51:55 +08:00
|
|
|
defer cancel()
|
2021-09-29 20:19:59 +08:00
|
|
|
fg.Start()
|
2020-11-02 16:44:54 +08:00
|
|
|
|
2021-07-15 14:51:55 +08:00
|
|
|
// input
|
|
|
|
go func() {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
a := float64(rand.Int())
|
|
|
|
inputChan <- a
|
|
|
|
|
|
|
|
// output check
|
|
|
|
d := <-outputChan
|
2022-10-10 22:15:22 +08:00
|
|
|
res := math.Pow(a, 2) + 2
|
2021-07-15 14:51:55 +08:00
|
|
|
assert.Equal(t, d, res)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
2020-11-02 16:44:54 +08:00
|
|
|
}
|
2021-09-07 09:39:47 +08:00
|
|
|
|
2021-09-13 16:34:26 +08:00
|
|
|
func TestTimeTickedFlowGraph_Close(t *testing.T) {
|
2022-10-10 22:15:22 +08:00
|
|
|
fg, _, _, cancel, err := createExampleFlowGraph()
|
|
|
|
assert.NoError(t, err)
|
2021-09-07 09:39:47 +08:00
|
|
|
defer cancel()
|
|
|
|
fg.Close()
|
|
|
|
}
|