mirror of
				https://github.com/HDT3213/godis.git
				synced 2025-10-31 12:06:26 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			51 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| 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/protocol/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.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")
 | |
| 	}
 | |
| }
 | 
