mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 08:46:56 +08:00
add unittests and bug fix
This commit is contained in:
50
redis/server/pubsub_test.go
Normal file
50
redis/server/pubsub_test.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/hdt3213/godis/lib/utils"
|
||||
"github.com/hdt3213/godis/pubsub"
|
||||
"github.com/hdt3213/godis/redis/connection"
|
||||
"github.com/hdt3213/godis/redis/parser"
|
||||
"github.com/hdt3213/godis/redis/reply/asserts"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPublish(t *testing.T) {
|
||||
hub := pubsub.MakeHub()
|
||||
channel := utils.RandString(5)
|
||||
msg := utils.RandString(5)
|
||||
conn := &connection.FakeConn{}
|
||||
pubsub.Subscribe(hub, conn, utils.ToBytesList(channel))
|
||||
conn.Clean() // clean subscribe success
|
||||
pubsub.Publish(hub, utils.ToBytesList(channel, msg))
|
||||
data := conn.Bytes()
|
||||
ret, err := parser.ParseOne(data)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
asserts.AssertMultiBulkReply(t, ret, []string{
|
||||
"message",
|
||||
channel,
|
||||
msg,
|
||||
})
|
||||
|
||||
// unsubscribe
|
||||
pubsub.UnSubscribe(hub, conn, utils.ToBytesList(channel))
|
||||
conn.Clean()
|
||||
pubsub.Publish(hub, utils.ToBytesList(channel, msg))
|
||||
data = conn.Bytes()
|
||||
if len(data) > 0 {
|
||||
t.Error("expect no msg")
|
||||
}
|
||||
|
||||
// unsubscribe all
|
||||
pubsub.Subscribe(hub, conn, utils.ToBytesList(channel))
|
||||
pubsub.UnSubscribe(hub, conn, utils.ToBytesList())
|
||||
conn.Clean()
|
||||
pubsub.Publish(hub, utils.ToBytesList(channel, msg))
|
||||
data = conn.Bytes()
|
||||
if len(data) > 0 {
|
||||
t.Error("expect no msg")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user