You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kvrocks.apache.org by ti...@apache.org on 2022/10/13 11:52:10 UTC

[incubator-kvrocks] branch unstable updated: move xstream offset tests (#984)

This is an automated email from the ASF dual-hosted git repository.

tison pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/incubator-kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new aee875e  move xstream offset tests (#984)
aee875e is described below

commit aee875e678e3de1d26611523a4ef65d1a79bd854
Author: tison <wa...@gmail.com>
AuthorDate: Thu Oct 13 19:52:04 2022 +0800

    move xstream offset tests (#984)
    
    Signed-off-by: tison <wa...@gmail.com>
---
 tests/gocase/unit/type/stream/stream_test.go | 111 +++++++++++++++++++++++++++
 tests/tcl/tests/unit/type/stream.tcl         |  59 --------------
 2 files changed, 111 insertions(+), 59 deletions(-)

diff --git a/tests/gocase/unit/type/stream/stream_test.go b/tests/gocase/unit/type/stream/stream_test.go
new file mode 100644
index 0000000..996cd96
--- /dev/null
+++ b/tests/gocase/unit/type/stream/stream_test.go
@@ -0,0 +1,111 @@
+/*
+* Licensed to the Apache Software Foundation (ASF) 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
+* 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 stream
+
+import (
+	"context"
+	"fmt"
+	"testing"
+
+	"github.com/apache/incubator-kvrocks/tests/gocase/util"
+	"github.com/go-redis/redis/v9"
+	"github.com/stretchr/testify/require"
+)
+
+func TestStreamOffset(t *testing.T) {
+	srv := util.StartServer(t, map[string]string{})
+	defer srv.Close()
+	ctx := context.Background()
+	rdb := srv.NewClient()
+	defer func() { require.NoError(t, rdb.Close()) }()
+
+	t.Run("XADD advances the entries-added counter and sets the recorded-first-entry-id", func(t *testing.T) {
+		require.NoError(t, rdb.Del(ctx, "x").Err())
+
+		require.NoError(t, rdb.XAdd(ctx, &redis.XAddArgs{
+			Stream: "x",
+			ID:     "1-0",
+			Values: []string{"data", "a"},
+		}).Err())
+		r := rdb.XInfoStreamFull(ctx, "x", 0).Val()
+		require.EqualValues(t, 1, r.EntriesAdded)
+		require.Equal(t, "1-0", r.RecordedFirstEntryID)
+
+		require.NoError(t, rdb.XAdd(ctx, &redis.XAddArgs{
+			Stream: "x",
+			ID:     "2-0",
+			Values: []string{"data", "a"},
+		}).Err())
+		r = rdb.XInfoStreamFull(ctx, "x", 0).Val()
+		require.EqualValues(t, 2, r.EntriesAdded)
+		require.Equal(t, "1-0", r.RecordedFirstEntryID)
+	})
+
+	t.Run("XDEL/TRIM are reflected by recorded first entry", func(t *testing.T) {
+		require.NoError(t, rdb.Del(ctx, "x").Err())
+
+		for i := 0; i < 5; i++ {
+			require.NoError(t, rdb.XAdd(ctx, &redis.XAddArgs{
+				Stream: "x",
+				ID:     fmt.Sprintf("%d-0", i+1),
+				Values: []string{"data", "a"},
+			}).Err())
+		}
+
+		r := rdb.XInfoStreamFull(ctx, "x", 0).Val()
+		require.EqualValues(t, 5, r.EntriesAdded)
+		require.Equal(t, "1-0", r.RecordedFirstEntryID)
+
+		require.NoError(t, rdb.XDel(ctx, "x", "2-0").Err())
+		r = rdb.XInfoStreamFull(ctx, "x", 0).Val()
+		require.Equal(t, "1-0", r.RecordedFirstEntryID)
+
+		require.NoError(t, rdb.XDel(ctx, "x", "1-0").Err())
+		r = rdb.XInfoStreamFull(ctx, "x", 0).Val()
+		require.Equal(t, "3-0", r.RecordedFirstEntryID)
+
+		require.NoError(t, rdb.XTrimMaxLen(ctx, "x", 2).Err())
+		r = rdb.XInfoStreamFull(ctx, "x", 0).Val()
+		require.Equal(t, "4-0", r.RecordedFirstEntryID)
+	})
+
+	t.Run("Maximum XDEL ID behaves correctly", func(t *testing.T) {
+		require.NoError(t, rdb.Del(ctx, "x").Err())
+
+		for i := 0; i < 3; i++ {
+			require.NoError(t, rdb.XAdd(ctx, &redis.XAddArgs{
+				Stream: "x",
+				ID:     fmt.Sprintf("%d-0", i+1),
+				Values: []string{"data", fmt.Sprintf("%c", 'a'+i)},
+			}).Err())
+		}
+
+		r := rdb.XInfoStreamFull(ctx, "x", 0).Val()
+		require.Equal(t, "0-0", r.MaxDeletedEntryID)
+
+		require.NoError(t, rdb.XDel(ctx, "x", "2-0").Err())
+		r = rdb.XInfoStreamFull(ctx, "x", 0).Val()
+		require.Equal(t, "2-0", r.MaxDeletedEntryID)
+
+		require.NoError(t, rdb.XDel(ctx, "x", "1-0").Err())
+		r = rdb.XInfoStreamFull(ctx, "x", 0).Val()
+		require.Equal(t, "2-0", r.MaxDeletedEntryID)
+	})
+}
diff --git a/tests/tcl/tests/unit/type/stream.tcl b/tests/tcl/tests/unit/type/stream.tcl
index ec6db5f..a6c121c 100644
--- a/tests/tcl/tests/unit/type/stream.tcl
+++ b/tests/tcl/tests/unit/type/stream.tcl
@@ -606,62 +606,3 @@ start_server {
         assert {[r xlen mystream] == 55}
     }
 }
-
-start_server {tags {"stream offset"}} {
-    test {XADD advances the entries-added counter and sets the recorded-first-entry-id} {
-        r DEL x
-        r XADD x 1-0 data a
-
-        set reply [r XINFO STREAM x FULL]
-        assert_equal [dict get $reply entries-added] 1
-        assert_equal [dict get $reply recorded-first-entry-id] "1-0"
-
-        r XADD x 2-0 data a
-        set reply [r XINFO STREAM x FULL]
-        assert_equal [dict get $reply entries-added] 2
-        assert_equal [dict get $reply recorded-first-entry-id] "1-0"
-    }
-
-    test {XDEL/TRIM are reflected by recorded first entry} {
-        r DEL x
-        r XADD x 1-0 data a
-        r XADD x 2-0 data a
-        r XADD x 3-0 data a
-        r XADD x 4-0 data a
-        r XADD x 5-0 data a
-
-        set reply [r XINFO STREAM x FULL]
-        assert_equal [dict get $reply entries-added] 5
-        assert_equal [dict get $reply recorded-first-entry-id] "1-0"
-
-        r XDEL x 2-0
-        set reply [r XINFO STREAM x FULL]
-        assert_equal [dict get $reply recorded-first-entry-id] "1-0"
-
-        r XDEL x 1-0
-        set reply [r XINFO STREAM x FULL]
-        assert_equal [dict get $reply recorded-first-entry-id] "3-0"
-
-        r XTRIM x MAXLEN = 2
-        set reply [r XINFO STREAM x FULL]
-        assert_equal [dict get $reply recorded-first-entry-id] "4-0"
-    }
-
-    test {Maxmimum XDEL ID behaves correctly} {
-        r DEL x
-        r XADD x 1-0 data a
-        r XADD x 2-0 data b
-        r XADD x 3-0 data c
-
-        set reply [r XINFO STREAM x FULL]
-        assert_equal [dict get $reply max-deleted-entry-id] "0-0"
-
-        r XDEL x 2-0
-        set reply [r XINFO STREAM x FULL]
-        assert_equal [dict get $reply max-deleted-entry-id] "2-0"
-
-        r XDEL x 1-0
-        set reply [r XINFO STREAM x FULL]
-        assert_equal [dict get $reply max-deleted-entry-id] "2-0"
-    }
-}