redis/pipeline_test.go

80 lines
1.7 KiB
Go
Raw Normal View History

2015-01-15 18:51:22 +03:00
package redis_test
import (
2017-02-18 17:42:34 +03:00
"github.com/go-redis/redis"
2015-01-15 18:51:22 +03:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
2016-12-13 18:28:39 +03:00
var _ = Describe("pipelining", func() {
2015-01-15 18:51:22 +03:00
var client *redis.Client
2016-12-13 18:28:39 +03:00
var pipe *redis.Pipeline
2015-01-15 18:51:22 +03:00
BeforeEach(func() {
client = redis.NewClient(redisOptions())
Expect(client.FlushDb().Err()).NotTo(HaveOccurred())
2015-01-15 18:51:22 +03:00
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
2016-12-13 18:28:39 +03:00
It("supports block style", func() {
2015-01-15 18:51:22 +03:00
var get *redis.StringCmd
2017-05-02 18:00:53 +03:00
cmds, err := client.Pipelined(func(pipe redis.Pipeliner) error {
2015-01-15 18:51:22 +03:00
get = pipe.Get("foo")
return nil
})
Expect(err).To(Equal(redis.Nil))
Expect(cmds).To(HaveLen(1))
Expect(cmds[0]).To(Equal(get))
Expect(get.Err()).To(Equal(redis.Nil))
Expect(get.Val()).To(Equal(""))
})
2016-12-13 18:28:39 +03:00
assertPipeline := func() {
It("returns an error when there are no commands", func() {
_, err := pipe.Exec()
Expect(err).To(MatchError("redis: pipeline is empty"))
})
2015-01-15 18:51:22 +03:00
2016-12-13 18:28:39 +03:00
It("discards queued commands", func() {
pipe.Get("key")
pipe.Discard()
_, err := pipe.Exec()
Expect(err).To(MatchError("redis: pipeline is empty"))
})
2015-01-15 18:51:22 +03:00
2016-12-13 18:28:39 +03:00
It("handles val/err", func() {
err := client.Set("key", "value", 0).Err()
Expect(err).NotTo(HaveOccurred())
2015-01-15 18:51:22 +03:00
2016-12-13 18:28:39 +03:00
get := pipe.Get("key")
cmds, err := pipe.Exec()
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(HaveLen(1))
2015-01-15 18:51:22 +03:00
2016-12-13 18:28:39 +03:00
val, err := get.Result()
Expect(err).NotTo(HaveOccurred())
Expect(val).To(Equal("value"))
})
}
2015-01-15 18:51:22 +03:00
2016-12-13 18:28:39 +03:00
Describe("Pipeline", func() {
BeforeEach(func() {
pipe = client.Pipeline().(*redis.Pipeline)
2016-12-13 18:28:39 +03:00
})
2015-01-15 18:51:22 +03:00
2016-12-13 18:28:39 +03:00
assertPipeline()
2015-01-15 18:51:22 +03:00
})
2016-12-13 18:28:39 +03:00
Describe("TxPipeline", func() {
BeforeEach(func() {
pipe = client.TxPipeline()
})
2015-11-04 15:25:48 +03:00
2016-12-13 18:28:39 +03:00
assertPipeline()
2015-11-04 15:25:48 +03:00
})
2015-01-15 18:51:22 +03:00
})