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"
2015-01-15 18:51:22 +03:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2021-09-08 16:00:52 +03:00
2023-01-23 09:48:54 +03:00
"github.com/redis/go-redis/v9"
2015-01-15 18:51:22 +03:00
)
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())
2020-03-11 17:26:42 +03:00
Expect(client.FlushDB(ctx).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
2020-03-11 17:26:42 +03:00
cmds, err := client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
get = pipe.Get(ctx, "foo")
2015-01-15 18:51:22 +03:00
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() {
2020-03-11 17:26:42 +03:00
_, err := pipe.Exec(ctx)
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() {
2020-03-11 17:26:42 +03:00
pipe.Get(ctx, "key")
2016-12-13 18:28:39 +03:00
pipe.Discard()
2020-03-11 17:26:42 +03:00
cmds, err := pipe.Exec(ctx)
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() {
2020-03-11 17:26:42 +03:00
err := client.Set(ctx, "key", "value", 0).Err()
2016-12-13 18:28:39 +03:00
Expect(err).NotTo(HaveOccurred())
2015-01-15 18:51:22 +03:00
2020-03-11 17:26:42 +03:00
get := pipe.Get(ctx, "key")
cmds, err := pipe.Exec(ctx)
2016-12-13 18:28:39 +03:00
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() {
2020-03-11 17:26:42 +03:00
pipe.Do(ctx, "ping")
cmds, err := pipe.Exec(ctx)
2019-02-08 14:22:46 +03:00
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(ctx, strconv.Itoa(i)+"_key", strconv.Itoa(i)+"_value", 0)
}
cmds, err := pipe.Exec(ctx)
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
})