github.com/hdt3213/godis@v1.2.9/cluster/pubsub_test.go (about)

     1  package cluster
     2  
     3  import (
     4  	"github.com/hdt3213/godis/lib/utils"
     5  	"github.com/hdt3213/godis/redis/connection"
     6  	"github.com/hdt3213/godis/redis/parser"
     7  	"github.com/hdt3213/godis/redis/protocol/asserts"
     8  	"testing"
     9  )
    10  
    11  func TestPublish(t *testing.T) {
    12  	channel := utils.RandString(5)
    13  	msg := utils.RandString(5)
    14  	conn := connection.NewFakeConn()
    15  	Subscribe(testNodeA, conn, utils.ToCmdLine("SUBSCRIBE", channel))
    16  	conn.Clean() // clean subscribe success
    17  	Publish(testNodeA, conn, utils.ToCmdLine("PUBLISH", channel, msg))
    18  	data := conn.Bytes()
    19  	ret, err := parser.ParseOne(data)
    20  	if err != nil {
    21  		t.Error(err)
    22  		return
    23  	}
    24  	asserts.AssertMultiBulkReply(t, ret, []string{
    25  		"message",
    26  		channel,
    27  		msg,
    28  	})
    29  
    30  	// unsubscribe
    31  	UnSubscribe(testNodeA, conn, utils.ToCmdLine("UNSUBSCRIBE", channel))
    32  	conn.Clean()
    33  	Publish(testNodeA, conn, utils.ToCmdLine("PUBLISH", channel, msg))
    34  	data = conn.Bytes()
    35  	if len(data) > 0 {
    36  		t.Error("expect no msg")
    37  	}
    38  
    39  	// unsubscribe all
    40  	Subscribe(testNodeA, conn, utils.ToCmdLine("SUBSCRIBE", channel))
    41  	UnSubscribe(testNodeA, conn, utils.ToCmdLine("UNSUBSCRIBE"))
    42  	conn.Clean()
    43  	Publish(testNodeA, conn, utils.ToCmdLine("PUBLISH", channel, msg))
    44  	data = conn.Bytes()
    45  	if len(data) > 0 {
    46  		t.Error("expect no msg")
    47  	}
    48  }