redis/subscribe_test.go

57 lines
1.0 KiB
Go
Raw Permalink Normal View History

package redis_test
import (
"sync/atomic"
"testing"
"time"
"apigo.cc/go/redis"
)
func TestSub(t *testing.T) {
rd := redis.GetRedis("test", nil)
rd.Start()
defer rd.Stop()
var aaa atomic.Value
var bbb atomic.Value
rd.Subscribe("aaa", nil, func(s []byte) {
aaa.Store(string(s))
})
rd.PUBLISH("aaa", "111")
time.Sleep(100 * time.Millisecond)
val := aaa.Load()
if val == nil || val.(string) != "111" {
t.Fatal("Subscribe aaa failed", val)
}
rd.Subscribe("bbb", nil, func(s []byte) {
bbb.Store(string(s))
})
rd.PUBLISH("bbb", "222")
time.Sleep(100 * time.Millisecond)
val = bbb.Load()
if val == nil || val.(string) != "222" {
t.Fatal("Subscribe bbb failed", val)
}
rd.Unsubscribe("aaa")
rd.PUBLISH("aaa", "1111")
rd.PUBLISH("bbb", "2222")
time.Sleep(100 * time.Millisecond)
val = aaa.Load()
if val == nil || val.(string) != "111" {
t.Fatal("Unsubscribe aaa failed: value updated", val)
}
val = bbb.Load()
if val == nil || val.(string) != "2222" {
t.Fatal("Subscribe bbb update failed", val)
}
}