You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@kvrocks.apache.org by GitBox <gi...@apache.org> on 2022/10/13 09:52:44 UTC

[GitHub] [incubator-kvrocks] vmihailenco commented on a diff in pull request #982: Move TCL test unit/pubsub to Go case

vmihailenco commented on code in PR #982:
URL: https://github.com/apache/incubator-kvrocks/pull/982#discussion_r994426236


##########
tests/gocase/unit/pubsub/pubsub_test.go:
##########
@@ -0,0 +1,356 @@
+/*
+ * 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 pubsub
+
+import (
+	"context"
+	"fmt"
+	"testing"
+
+	"github.com/apache/incubator-kvrocks/tests/gocase/util"
+	"github.com/go-redis/redis/v9"
+	"github.com/stretchr/testify/require"
+)
+
+func receiveType[T any](t *testing.T, pubsub *redis.PubSub, typ T) T {
+	msg, err := pubsub.Receive(context.Background())
+	require.NoError(t, err)
+	require.IsType(t, typ, msg)
+	return msg.(T)
+}
+
+func TestPubSub(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("PUBLISH/SUBSCRIBE PING", func(t *testing.T) {
+		pubsub := rdb.Subscribe(ctx, "somechannel")
+		receiveType(t, pubsub, &redis.Subscription{})
+		require.NoError(t, pubsub.Ping(ctx))
+		require.NoError(t, pubsub.Ping(ctx))
+		require.NoError(t, pubsub.Unsubscribe(ctx, "somechannel"))
+		require.Equal(t, "PONG", rdb.Ping(ctx).Val())
+		receiveType(t, pubsub, &redis.Pong{})
+		receiveType(t, pubsub, &redis.Pong{})
+	})
+
+	t.Run("PUBLISH/SUBSCRIBE basics", func(t *testing.T) {
+		// subscribe to two channels
+		pubsub := rdb.Subscribe(ctx, "chan1", "chan2")
+		require.EqualValues(t, 1, receiveType(t, pubsub, &redis.Subscription{}).Count)
+		require.EqualValues(t, 2, receiveType(t, pubsub, &redis.Subscription{}).Count)
+
+		require.EqualValues(t, 1, rdb.Publish(ctx, "chan1", "hello").Val())
+		require.EqualValues(t, 1, rdb.Publish(ctx, "chan2", "world").Val())
+
+		msg := receiveType(t, pubsub, &redis.Message{})
+		require.Equal(t, "chan1", msg.Channel)
+		require.Equal(t, "hello", msg.Payload)
+		msg = receiveType(t, pubsub, &redis.Message{})
+		require.Equal(t, "chan2", msg.Channel)
+		require.Equal(t, "world", msg.Payload)
+
+		// unsubscribe from one of the channels
+		require.NoError(t, pubsub.Unsubscribe(ctx, "chan1"))
+		require.EqualValues(t, 0, rdb.Publish(ctx, "chan1", "hello").Val())
+		require.EqualValues(t, 1, rdb.Publish(ctx, "chan2", "world").Val())
+
+		// unsubscribe from the remaining channel
+		require.NoError(t, pubsub.Unsubscribe(ctx, "chan2"))
+		require.EqualValues(t, 0, rdb.Publish(ctx, "chan1", "hello").Val())
+		require.EqualValues(t, 0, rdb.Publish(ctx, "chan2", "world").Val())
+	})
+
+	t.Run("PUBLISH/SUBSCRIBE with two clients", func(t *testing.T) {
+		c1 := srv.NewClient()
+		defer func() { require.NoError(t, c1.Close()) }()
+		c2 := srv.NewClient()
+		defer func() { require.NoError(t, c2.Close()) }()
+
+		p1 := c1.Subscribe(ctx, "chan1")
+		p2 := c2.Subscribe(ctx, "chan1")
+		require.EqualValues(t, 1, receiveType(t, p1, &redis.Subscription{}).Count)
+		require.EqualValues(t, 1, receiveType(t, p2, &redis.Subscription{}).Count)
+
+		require.EqualValues(t, 2, rdb.Publish(ctx, "chan1", "hello").Val())
+		msg := receiveType(t, p1, &redis.Message{})
+		require.Equal(t, "chan1", msg.Channel)
+		require.Equal(t, "hello", msg.Payload)
+		msg = receiveType(t, p2, &redis.Message{})
+		require.Equal(t, "chan1", msg.Channel)
+		require.Equal(t, "hello", msg.Payload)
+	})
+
+	t.Run("PUBLISH/SUBSCRIBE after UNSUBSCRIBE without arguments", func(t *testing.T) {
+		pubsub := rdb.Subscribe(ctx, "chan1", "chan2", "chan3")
+		require.EqualValues(t, 1, receiveType(t, pubsub, &redis.Subscription{}).Count)
+		require.EqualValues(t, 2, receiveType(t, pubsub, &redis.Subscription{}).Count)
+		require.EqualValues(t, 3, receiveType(t, pubsub, &redis.Subscription{}).Count)
+
+		require.NoError(t, pubsub.Unsubscribe(ctx))

Review Comment:
   @tisonkun looks like you need to add channels here - "chan1", "chan2", "chan3". Not sure why the test is not failing earlier though and you still receive `unsubscribe` events... Perhaps some magic by kvrocks? I wonder if Redis behaves the same.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@kvrocks.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org