2015-01-15 18:51:22 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2023-04-01 09:44:06 +03:00
|
|
|
"errors"
|
2020-09-17 17:35:34 +03:00
|
|
|
"strconv"
|
|
|
|
|
2023-01-27 18:00:49 +03:00
|
|
|
. "github.com/bsm/ginkgo/v2"
|
|
|
|
. "github.com/bsm/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() {
|
2016-03-16 17:57:24 +03:00
|
|
|
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() {
|
2017-05-30 15:45:36 +03:00
|
|
|
It("returns no errors when there are no commands", func() {
|
2020-03-11 17:26:42 +03:00
|
|
|
_, err := pipe.Exec(ctx)
|
2017-05-30 15:45:36 +03:00
|
|
|
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)
|
2017-05-30 15:45:36 +03:00
|
|
|
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))
|
|
|
|
})
|
2020-09-17 17:35:34 +03:00
|
|
|
|
2024-01-01 23:19:22 +03:00
|
|
|
It("handles large pipelines", Label("NonRedisEnterprise"), func() {
|
2020-09-17 17:35:34 +03:00
|
|
|
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{}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2023-04-01 09:44:06 +03:00
|
|
|
|
|
|
|
It("should Exec, not Do", func() {
|
|
|
|
err := pipe.Do(ctx).Err()
|
|
|
|
Expect(err).To(Equal(errors.New("redis: please enter the command to be executed")))
|
|
|
|
})
|
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() {
|
2017-05-01 18:42:58 +03:00
|
|
|
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() {
|
2017-05-03 16:48:12 +03:00
|
|
|
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
|
|
|
})
|