2015-01-15 18:51:22 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2016-12-03 18:30:13 +03:00
|
|
|
"gopkg.in/redis.v5"
|
|
|
|
|
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() {
|
2016-03-16 17:57:24 +03:00
|
|
|
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
|
|
|
|
cmds, err := client.Pipelined(func(pipe *redis.Pipeline) error {
|
|
|
|
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()
|
|
|
|
})
|
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
|
|
|
})
|