redis/pipeline_test.go

105 lines
2.3 KiB
Go
Raw Permalink Normal View History

2015-01-15 18:51:22 +03:00
package redis_test
import (
"strconv"
2019-08-08 14:29:44 +03:00
"github.com/go-redis/redis/v7"
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())
2017-06-17 12:53:16 +03:00
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 no errors when there are no commands", func() {
2016-12-13 18:28:39 +03:00
_, err := pipe.Exec()
Expect(err).NotTo(HaveOccurred())
2016-12-13 18:28:39 +03:00
})
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()
cmds, err := pipe.Exec()
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(BeNil())
2016-12-13 18:28:39 +03:00
})
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"))
})
2019-02-08 14:22:46 +03:00
It("supports custom command", func() {
pipe.Do("ping")
cmds, err := pipe.Exec()
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(HaveLen(1))
})
It("handles large pipelines", func() {
for callCount := 1; callCount < 16; callCount++ {
for i := 1; i <= callCount; i++ {
pipe.SetNX(strconv.Itoa(i)+"_key", strconv.Itoa(i)+"_value", 0)
}
cmds, err := pipe.Exec()
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(HaveLen(callCount))
for _, cmd := range cmds {
Expect(cmd).To(BeAssignableToTypeOf(&redis.BoolCmd{}))
}
}
})
2016-12-13 18:28:39 +03:00
}
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().(*redis.Pipeline)
2016-12-13 18:28:39 +03:00
})
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
})