代码拉取完成,页面将自动刷新
package rueidis
import (
"testing"
"time"
)
func TestSubs_Publish(t *testing.T) {
defer ShouldNotLeaked(SetupLeakDetection())
t.Run("without subs", func(t *testing.T) {
s := newSubs()
s.Publish("aa", PubSubMessage{}) // just no block
})
t.Run("with multiple subs", func(t *testing.T) {
s := newSubs()
ch1, cancel1 := s.Subscribe([]string{"a"})
ch2, cancel2 := s.Subscribe([]string{"a"})
ch3, cancel3 := s.Subscribe([]string{"b"})
m1 := PubSubMessage{Pattern: "1", Channel: "2", Message: "3"}
m2 := PubSubMessage{Pattern: "11", Channel: "22", Message: "33"}
go func() {
s.Publish("a", m1)
s.Publish("b", m2)
}()
for m := range ch1 {
if m != m1 {
t.Fatalf("unexpected msg %v", m)
}
cancel1()
}
for m := range ch2 {
if m != m1 {
t.Fatalf("unexpected msg %v", m)
}
cancel2()
}
for m := range ch3 {
if m != m2 {
t.Fatalf("unexpected msg %v", m)
}
cancel3()
}
})
t.Run("drain ch", func(t *testing.T) {
s := newSubs()
ch, cancel := s.Subscribe([]string{"a"})
s.Publish("a", PubSubMessage{})
if len(ch) != 1 {
t.Fatalf("unexpected ch len %v", len(ch))
}
cancel()
for ; len(ch) != 0; time.Sleep(time.Millisecond * 100) {
t.Log("wait ch to be drain")
}
})
}
func TestSubs_Unsubscribe(t *testing.T) {
defer ShouldNotLeaked(SetupLeakDetection())
s := newSubs()
ch, _ := s.Subscribe([]string{"1", "2"})
go func() {
s.Publish("1", PubSubMessage{})
}()
_, ok := <-ch
if !ok {
t.Fatalf("unexpected ch closed")
}
s.Unsubscribe("1")
_, ok = <-ch
if ok {
t.Fatalf("unexpected ch unclosed")
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。