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 }