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