mirror of
https://github.com/Icinga/icingadb.git
synced 2026-05-28 04:35:54 -04:00
Redis: Add more tests
This commit is contained in:
parent
fed93f199d
commit
ab08af7c3a
2 changed files with 154 additions and 0 deletions
74
redis_pubsub_test.go
Normal file
74
redis_pubsub_test.go
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
package icingadb_connection
|
||||
|
||||
import (
|
||||
"github.com/go-redis/redis"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPubSubWrapper(t *testing.T) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "127.0.0.1:6379",
|
||||
DialTimeout: time.Minute / 2,
|
||||
ReadTimeout: time.Minute,
|
||||
WriteTimeout: time.Minute,
|
||||
})
|
||||
rdbw := NewTestRDBW(rdb)
|
||||
|
||||
if !rdbw.CheckConnection(true) {
|
||||
t.Fatal("This test needs a working Redis connection")
|
||||
}
|
||||
|
||||
ps := rdbw.Subscribe()
|
||||
|
||||
rdbw.CompareAndSetConnected(false)
|
||||
|
||||
var errSubscribe error
|
||||
done1:= make(chan bool)
|
||||
go func () {
|
||||
errSubscribe = ps.Subscribe("testchannel")
|
||||
done1 <- true
|
||||
}()
|
||||
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
rdbw.CheckConnection(true)
|
||||
|
||||
<- done1
|
||||
|
||||
rdbw.CompareAndSetConnected(false)
|
||||
|
||||
var msg *redis.Message
|
||||
var errReceive error
|
||||
done2 := make(chan bool)
|
||||
go func() {
|
||||
msg, errReceive = ps.ReceiveMessage()
|
||||
done2 <- true
|
||||
}()
|
||||
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
rdbw.CheckConnection(true)
|
||||
|
||||
rdbw.Publish("testchannel", "Hello there")
|
||||
|
||||
<- done2
|
||||
|
||||
rdbw.CompareAndSetConnected(false)
|
||||
|
||||
var errClose error
|
||||
done3:= make(chan bool)
|
||||
go func () {
|
||||
errClose = ps.Close()
|
||||
done3 <- true
|
||||
}()
|
||||
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
rdbw.CheckConnection(true)
|
||||
|
||||
<- done3
|
||||
|
||||
assert.NoError(t, errSubscribe)
|
||||
assert.NoError(t, errReceive)
|
||||
assert.NoError(t, errClose)
|
||||
assert.Equal(t, "Hello there", msg.Payload)
|
||||
}
|
||||
|
|
@ -193,3 +193,83 @@ func TestRDBWrapper_XDel(t *testing.T) {
|
|||
assert.Error(t, err)
|
||||
assert.Len(t, streams, 0)
|
||||
}
|
||||
|
||||
func TestRDBWrapper_Publish(t *testing.T) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "127.0.0.1:6379",
|
||||
DialTimeout: time.Minute / 2,
|
||||
ReadTimeout: time.Minute,
|
||||
WriteTimeout: time.Minute,
|
||||
})
|
||||
rdbw := NewTestRDBW(rdb)
|
||||
|
||||
if !rdbw.CheckConnection(true) {
|
||||
t.Fatal("This test needs a working Redis connection")
|
||||
}
|
||||
|
||||
var msg *redis.Message
|
||||
var err error
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
msg, err = rdb.Subscribe("testchannel").ReceiveMessage()
|
||||
done <- true
|
||||
}()
|
||||
|
||||
rdbw.CompareAndSetConnected(false)
|
||||
|
||||
go func () {
|
||||
rdbw.Publish("testchannel", "Hello there")
|
||||
}()
|
||||
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
rdbw.CheckConnection(true)
|
||||
|
||||
<- done
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "Hello there", msg.Payload)
|
||||
}
|
||||
|
||||
func TestRDBWrapper_TxPipelined(t *testing.T) {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "127.0.0.1:6379",
|
||||
DialTimeout: time.Minute / 2,
|
||||
ReadTimeout: time.Minute,
|
||||
WriteTimeout: time.Minute,
|
||||
})
|
||||
rdbw := NewTestRDBW(rdb)
|
||||
|
||||
if !rdbw.CheckConnection(true) {
|
||||
t.Fatal("This test needs a working Redis connection")
|
||||
}
|
||||
|
||||
rdb.Del("firstKey")
|
||||
rdb.Del("secondKey")
|
||||
rdb.HSet("firstKey", "foo", 5)
|
||||
rdb.HSet("secondKey", "bar", 11)
|
||||
|
||||
rdbw.CompareAndSetConnected(false)
|
||||
|
||||
var firstMap *redis.StringStringMapCmd
|
||||
var secondMap *redis.StringStringMapCmd
|
||||
var err error
|
||||
done := make(chan bool)
|
||||
go func() {
|
||||
_, err = rdbw.TxPipelined(func(pipe redis.Pipeliner) error {
|
||||
firstMap = pipe.HGetAll("firstKey")
|
||||
secondMap = pipe.HGetAll("secondKey")
|
||||
return nil
|
||||
})
|
||||
done <- true
|
||||
}()
|
||||
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
rdbw.CheckConnection(true)
|
||||
|
||||
<- done
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, firstMap.Val(), "foo")
|
||||
assert.Contains(t, secondMap.Val(), "bar")
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue