common/pkg/redis/syb_test.go

34 lines
590 B
Go
Raw Permalink Normal View History

2024-08-29 20:38:48 +08:00
package redis
import (
"context"
"testing"
)
func TestInitDefaultRedis(t *testing.T) {
rdb, err := InitDefaultRedis()
if err != nil {
t.Fatal(err)
}
chanel := "/provider/id/cfg"
ctx, cancel := context.WithCancel(context.Background())
pubsub := rdb.Subscribe(ctx, chanel)
c := make(chan struct{})
// 使用完毕,记得关闭
defer pubsub.Close()
go func() {
ch := pubsub.Channel()
for msg := range ch {
t.Log(msg.Channel, msg.Payload)
c <- struct{}{}
}
}()
err = rdb.Publish(ctx, chanel, "payload").Err()
if err != nil {
t.Fatal(err)
}
cancel()
<-c
}