From 4986d43db7b1f4ad141edb753c6870cd1b417478 Mon Sep 17 00:00:00 2001 From: Jiyong Huang Date: Thu, 28 Mar 2024 16:42:26 +0800 Subject: [PATCH] fix(test): reset clock Signed-off-by: Jiyong Huang --- internal/io/memory/lookupsource_test.go | 12 ++++++++---- internal/topo/node/lookup_node_test.go | 5 ++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/io/memory/lookupsource_test.go b/internal/io/memory/lookupsource_test.go index a9cd173a46..59e08b7d83 100644 --- a/internal/io/memory/lookupsource_test.go +++ b/internal/io/memory/lookupsource_test.go @@ -21,10 +21,12 @@ import ( "time" "github.com/benbjohnson/clock" + "github.com/stretchr/testify/assert" "github.com/lf-edge/ekuiper/internal/conf" "github.com/lf-edge/ekuiper/internal/io/memory/pubsub" "github.com/lf-edge/ekuiper/internal/topo/context" + "github.com/lf-edge/ekuiper/internal/topo/topotest/mockclock" "github.com/lf-edge/ekuiper/pkg/api" ) @@ -117,12 +119,13 @@ func TestLookup(t *testing.T) { } } }() - mc := conf.Clock.(*clock.Mock) + result, _ := ls.Lookup(ctx, []string{}, []string{"ff"}, []interface{}{"value1"}) + mockclock.ResetClock(0) + mc := mockclock.GetMockClock() expected := []api.SourceTuple{ api.NewDefaultSourceTupleWithTime(map[string]interface{}{"ff": "value1", "gg": "value2"}, map[string]interface{}{"topic": "test2"}, mc.Now()), api.NewDefaultSourceTupleWithTime(map[string]interface{}{"ff": "value1", "gg": "value4"}, map[string]interface{}{"topic": "test2"}, mc.Now()), } - result, _ := ls.Lookup(ctx, []string{}, []string{"ff"}, []interface{}{"value1"}) if len(result) != 2 { t.Errorf("expect %v but got %v", expected, result) } else { @@ -130,8 +133,9 @@ func TestLookup(t *testing.T) { result[0], result[1] = result[1], result[0] } } - if !reflect.DeepEqual(result, expected) { - t.Errorf("expect %v but got %v", expected, result) + assert.Equal(t, len(expected), len(result)) + for i, r := range result { + assert.Equal(t, expected[i].Message(), r.Message(), "%d message not equal", i) } err = ls.Close(ctx) if err != nil { diff --git a/internal/topo/node/lookup_node_test.go b/internal/topo/node/lookup_node_test.go index 121512238a..11994f83ab 100644 --- a/internal/topo/node/lookup_node_test.go +++ b/internal/topo/node/lookup_node_test.go @@ -28,6 +28,7 @@ import ( "github.com/lf-edge/ekuiper/internal/plugin" "github.com/lf-edge/ekuiper/internal/topo/context" "github.com/lf-edge/ekuiper/internal/topo/lookup" + "github.com/lf-edge/ekuiper/internal/topo/topotest/mockclock" "github.com/lf-edge/ekuiper/internal/xsql" "github.com/lf-edge/ekuiper/pkg/api" "github.com/lf-edge/ekuiper/pkg/ast" @@ -318,6 +319,7 @@ func TestLookup(t *testing.T) { STRICT_VALIDATION: true, KIND: "lookup", } + mockclock.ResetClock(0) lookup.CreateInstance("mock", "mock", options) contextLogger := conf.Log.WithField("rule", "TestLookup") ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger) @@ -468,7 +470,8 @@ func TestCachedLookup(t *testing.T) { }, } // First run and the set mock result - clock := conf.Clock.(*clock.Mock) + mockclock.ResetClock(0) + clock := mockclock.GetMockClock() select { case err := <-errCh: t.Error(err)