123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- // Copyright 2022-2023 EMQ Technologies Co., Ltd.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // 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.
- package node
- import (
- "fmt"
- "reflect"
- "testing"
- "time"
- "github.com/lf-edge/ekuiper/internal/conf"
- "github.com/lf-edge/ekuiper/internal/topo/context"
- "github.com/lf-edge/ekuiper/internal/xsql"
- "github.com/lf-edge/ekuiper/pkg/api"
- "github.com/lf-edge/ekuiper/pkg/ast"
- )
- func TestTuple(t *testing.T) {
- inputs := []*xsql.Tuple{
- {
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 45.6,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 46.6,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 26.6,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 54.3,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 36.6,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 76.6,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 41.2,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 86.6,
- },
- },
- }
- outputs := [][]*xsql.Tuple{
- { // f2 > 40
- {
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 45.6,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 46.6,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 54.3,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 76.6,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 41.2,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 86.6,
- },
- },
- },
- { // f1 == v1
- {
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 45.6,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 36.6,
- },
- }, {
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 76.6,
- },
- },
- },
- { // f1 == v2 && f2 < 40
- {
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 26.6,
- },
- },
- },
- }
- sn, err := NewSwitchNode("test", &SwitchConfig{
- Cases: []ast.Expr{
- &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f2"},
- OP: ast.GT,
- RHS: &ast.NumberLiteral{Val: 40},
- },
- &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f1"},
- OP: ast.EQ,
- RHS: &ast.StringLiteral{Val: "v1"},
- },
- &ast.BinaryExpr{
- LHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f1"},
- OP: ast.EQ,
- RHS: &ast.StringLiteral{Val: "v2"},
- },
- OP: ast.AND,
- RHS: &ast.BinaryExpr{
- LHS: &ast.FieldRef{Name: "f2"},
- OP: ast.LT,
- RHS: &ast.NumberLiteral{Val: 40},
- },
- },
- },
- StopAtFirstMatch: false,
- }, &api.RuleOption{})
- if err != nil {
- t.Fatalf("Failed to create switch node: %v", err)
- }
- contextLogger := conf.Log.WithField("rule", "TestSwitchTuple")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- errCh := make(chan error)
- output1 := make(chan interface{}, 10)
- output2 := make(chan interface{}, 10)
- output3 := make(chan interface{}, 10)
- sn.outputNodes[0].AddOutput(output1, "output1")
- sn.outputNodes[1].AddOutput(output2, "output2")
- sn.outputNodes[2].AddOutput(output3, "output3")
- go sn.Exec(ctx, errCh)
- go func() {
- for i, input := range inputs {
- select {
- case sn.input <- input:
- t.Logf("send input %d", i)
- case <-time.After(time.Second):
- errCh <- fmt.Errorf("Timeout sending input %d", i)
- return
- }
- }
- }()
- actualOuts := make([][]*xsql.Tuple, 3)
- outterFor:
- for {
- select {
- case err := <-errCh:
- t.Fatalf("Error received: %v", err)
- case out1 := <-output1:
- actualOuts[0] = append(actualOuts[0], out1.(*xsql.Tuple))
- case out2 := <-output2:
- actualOuts[1] = append(actualOuts[1], out2.(*xsql.Tuple))
- case out3 := <-output3:
- actualOuts[2] = append(actualOuts[2], out3.(*xsql.Tuple))
- case <-time.After(100 * time.Millisecond):
- break outterFor
- }
- }
- if !reflect.DeepEqual(actualOuts, outputs) {
- t.Errorf("Expected: %v, actual: %v", outputs, actualOuts)
- }
- }
- func TestCollection(t *testing.T) {
- inputs := []*xsql.WindowTuples{
- {
- Content: []xsql.TupleRow{
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 45.6,
- },
- },
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 65.6,
- },
- },
- },
- }, {
- Content: []xsql.TupleRow{
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 46.6,
- },
- },
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 26.6,
- },
- },
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 54.3,
- },
- },
- },
- }, {
- Content: []xsql.TupleRow{
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 36.6,
- },
- },
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 76.6,
- },
- },
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 41.2,
- },
- },
- },
- },
- }
- outputs := [][]*xsql.WindowTuples{
- { // avg(f2) > 50
- {
- Content: []xsql.TupleRow{
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 45.6,
- },
- },
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 65.6,
- },
- },
- },
- }, {
- Content: []xsql.TupleRow{
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 36.6,
- },
- },
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v1",
- "f2": 76.6,
- },
- },
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 41.2,
- },
- },
- },
- },
- },
- { // else
- {
- Content: []xsql.TupleRow{
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 46.6,
- },
- },
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 26.6,
- },
- },
- &xsql.Tuple{
- Message: map[string]interface{}{
- "f1": "v2",
- "f2": 54.3,
- },
- },
- },
- },
- },
- }
- sn, err := NewSwitchNode("test", &SwitchConfig{
- Cases: []ast.Expr{
- &ast.BinaryExpr{
- LHS: &ast.Call{
- Name: "avg",
- FuncId: 0,
- FuncType: ast.FuncTypeAgg,
- Args: []ast.Expr{&ast.FieldRef{Name: "f2"}},
- },
- OP: ast.GT,
- RHS: &ast.NumberLiteral{Val: 50},
- },
- &ast.BinaryExpr{
- LHS: &ast.Call{
- Name: "avg",
- FuncId: 0,
- FuncType: ast.FuncTypeAgg,
- Args: []ast.Expr{&ast.FieldRef{Name: "f2"}},
- },
- OP: ast.LTE,
- RHS: &ast.NumberLiteral{Val: 50},
- },
- },
- StopAtFirstMatch: true,
- }, &api.RuleOption{})
- if err != nil {
- t.Fatalf("Failed to create switch node: %v", err)
- }
- contextLogger := conf.Log.WithField("rule", "TestSwitchWindow")
- ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
- errCh := make(chan error)
- output1 := make(chan interface{}, 10)
- output2 := make(chan interface{}, 10)
- sn.outputNodes[0].AddOutput(output1, "output1")
- sn.outputNodes[1].AddOutput(output2, "output2")
- go sn.Exec(ctx, errCh)
- go func() {
- for i, input := range inputs {
- select {
- case sn.input <- input:
- t.Logf("send input %d", i)
- case <-time.After(time.Second):
- errCh <- fmt.Errorf("Timeout sending input %d", i)
- return
- }
- }
- }()
- actualOuts := make([][]*xsql.WindowTuples, 2)
- outterFor:
- for {
- select {
- case err := <-errCh:
- t.Fatalf("Error received: %v", err)
- case out1 := <-output1:
- actualOuts[0] = append(actualOuts[0], out1.(*xsql.WindowTuples))
- case out2 := <-output2:
- actualOuts[1] = append(actualOuts[1], out2.(*xsql.WindowTuples))
- case <-time.After(100 * time.Millisecond):
- break outterFor
- }
- }
- if !reflect.DeepEqual(actualOuts, outputs) {
- t.Errorf("Expected: %v, actual: %v", outputs, actualOuts)
- }
- }
|