refactor code structure for gnet

This commit is contained in:
finley
2025-05-25 21:04:49 +08:00
parent 144b642fe7
commit d6bbf0315c
11 changed files with 167 additions and 117 deletions

View File

@@ -0,0 +1,50 @@
package std
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/protocol/asserts"
"testing"
)
func TestPublish(t *testing.T) {
hub := pubsub.MakeHub()
channel := utils.RandString(5)
msg := utils.RandString(5)
conn := connection.NewFakeConn()
pubsub.Subscribe(hub, conn, utils.ToCmdLine(channel))
conn.Clean() // clean subscribe success
pubsub.Publish(hub, utils.ToCmdLine(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.ToCmdLine(channel))
conn.Clean()
pubsub.Publish(hub, utils.ToCmdLine(channel, msg))
data = conn.Bytes()
if len(data) > 0 {
t.Error("expect no msg")
}
// unsubscribe all
pubsub.Subscribe(hub, conn, utils.ToCmdLine(channel))
pubsub.UnSubscribe(hub, conn, utils.ToCmdLine())
conn.Clean()
pubsub.Publish(hub, utils.ToCmdLine(channel, msg))
data = conn.Bytes()
if len(data) > 0 {
t.Error("expect no msg")
}
}