redis/tx_test.go

149 lines
3.3 KiB
Go
Raw Permalink Normal View History

2015-01-15 18:51:22 +03:00
package redis_test
import (
2019-07-04 11:18:06 +03:00
"context"
2015-12-16 17:11:52 +03:00
"strconv"
"sync"
2015-01-15 18:51:22 +03:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
2021-09-08 16:00:52 +03:00
2022-06-04 17:39:21 +03:00
"github.com/go-redis/redis/v9"
2015-01-15 18:51:22 +03:00
)
var _ = Describe("Tx", func() {
2015-01-15 18:51:22 +03:00
var client *redis.Client
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())
})
2015-12-16 17:11:52 +03:00
It("should Watch", func() {
var incr func(string) error
2015-01-15 18:51:22 +03:00
2015-12-16 17:11:52 +03:00
// Transactionally increments key using GET and SET commands.
incr = func(key string) error {
2020-03-11 17:26:42 +03:00
err := client.Watch(ctx, func(tx *redis.Tx) error {
n, err := tx.Get(ctx, key).Int64()
2016-05-02 15:54:15 +03:00
if err != nil && err != redis.Nil {
return err
}
2020-03-11 17:26:42 +03:00
_, err = tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Set(ctx, key, strconv.FormatInt(n+1, 10), 0)
2016-05-02 15:54:15 +03:00
return nil
})
2015-12-16 17:11:52 +03:00
return err
2016-05-02 15:54:15 +03:00
}, key)
2015-12-16 17:11:52 +03:00
if err == redis.TxFailedErr {
return incr(key)
}
return err
}
2015-01-15 18:51:22 +03:00
2015-12-16 17:11:52 +03:00
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer GinkgoRecover()
2015-12-16 17:11:52 +03:00
defer wg.Done()
err := incr("key")
Expect(err).NotTo(HaveOccurred())
}()
}
wg.Wait()
2020-03-11 17:26:42 +03:00
n, err := client.Get(ctx, "key").Int64()
2015-12-16 17:11:52 +03:00
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(100)))
2015-01-15 18:51:22 +03:00
})
It("should discard", func() {
2020-03-11 17:26:42 +03:00
err := client.Watch(ctx, func(tx *redis.Tx) error {
cmds, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Set(ctx, "key1", "hello1", 0)
pipe.Discard()
2020-03-11 17:26:42 +03:00
pipe.Set(ctx, "key2", "hello2", 0)
2016-05-02 15:54:15 +03:00
return nil
})
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(HaveLen(1))
return err
}, "key1", "key2")
2015-01-15 18:51:22 +03:00
Expect(err).NotTo(HaveOccurred())
2020-03-11 17:26:42 +03:00
get := client.Get(ctx, "key1")
2015-01-15 18:51:22 +03:00
Expect(get.Err()).To(Equal(redis.Nil))
Expect(get.Val()).To(Equal(""))
2020-03-11 17:26:42 +03:00
get = client.Get(ctx, "key2")
2015-01-15 18:51:22 +03:00
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello2"))
})
It("returns no error when there are no commands", func() {
2020-03-11 17:26:42 +03:00
err := client.Watch(ctx, func(tx *redis.Tx) error {
_, err := tx.TxPipelined(ctx, func(redis.Pipeliner) error { return nil })
2016-05-02 15:54:15 +03:00
return err
})
Expect(err).NotTo(HaveOccurred())
2015-01-15 18:51:22 +03:00
2020-03-11 17:26:42 +03:00
v, err := client.Ping(ctx).Result()
2015-01-15 18:51:22 +03:00
Expect(err).NotTo(HaveOccurred())
2016-05-02 15:54:15 +03:00
Expect(v).To(Equal("PONG"))
2015-01-15 18:51:22 +03:00
})
It("should exec bulks", func() {
2016-05-02 15:54:15 +03:00
const N = 20000
2015-01-15 18:51:22 +03:00
2020-03-11 17:26:42 +03:00
err := client.Watch(ctx, func(tx *redis.Tx) error {
cmds, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
2016-05-02 15:54:15 +03:00
for i := 0; i < N; i++ {
2020-03-11 17:26:42 +03:00
pipe.Incr(ctx, "key")
2016-05-02 15:54:15 +03:00
}
return nil
})
Expect(err).NotTo(HaveOccurred())
Expect(len(cmds)).To(Equal(N))
for _, cmd := range cmds {
Expect(cmd.Err()).NotTo(HaveOccurred())
2015-01-15 18:51:22 +03:00
}
2016-05-02 15:54:15 +03:00
return err
2015-01-15 18:51:22 +03:00
})
Expect(err).NotTo(HaveOccurred())
2020-03-11 17:26:42 +03:00
num, err := client.Get(ctx, "key").Int64()
2016-05-02 15:54:15 +03:00
Expect(err).NotTo(HaveOccurred())
Expect(num).To(Equal(int64(N)))
2015-01-15 18:51:22 +03:00
})
It("should recover from bad connection", func() {
// Put bad connection in the pool.
2019-07-04 11:18:06 +03:00
cn, err := client.Pool().Get(context.Background())
Expect(err).NotTo(HaveOccurred())
cn.SetNetConn(&badConn{})
2020-08-15 15:36:02 +03:00
client.Pool().Put(ctx, cn)
2016-05-02 15:54:15 +03:00
do := func() error {
2020-03-11 17:26:42 +03:00
err := client.Watch(ctx, func(tx *redis.Tx) error {
_, err := tx.TxPipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.Ping(ctx)
return nil
2016-05-02 15:54:15 +03:00
})
return err
})
return err
}
2016-05-02 15:54:15 +03:00
err = do()
Expect(err).NotTo(HaveOccurred())
})
2015-01-15 18:51:22 +03:00
})