2015-01-15 18:51:22 +03:00
|
|
|
package redis_test
|
|
|
|
|
|
|
|
import (
|
2015-12-16 17:11:52 +03:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis"
|
|
|
|
|
2015-01-15 18:51:22 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2016-04-09 11:23:58 +03:00
|
|
|
var _ = Describe("Tx", func() {
|
2015-01-15 18:51:22 +03:00
|
|
|
var client *redis.Client
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2016-03-16 17:57:24 +03:00
|
|
|
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())
|
|
|
|
})
|
|
|
|
|
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 {
|
2016-05-02 15:54:15 +03:00
|
|
|
err := client.Watch(func(tx *redis.Tx) error {
|
|
|
|
n, err := tx.Get(key).Int64()
|
|
|
|
if err != nil && err != redis.Nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-02 18:00:53 +03:00
|
|
|
_, err = tx.Pipelined(func(pipe redis.Pipeliner) error {
|
2016-10-13 14:36:15 +03:00
|
|
|
pipe.Set(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() {
|
2016-03-16 17:57:24 +03:00
|
|
|
defer GinkgoRecover()
|
2015-12-16 17:11:52 +03:00
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
err := incr("key")
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
n, err := client.Get("key").Int64()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(n).To(Equal(int64(100)))
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should discard", func() {
|
2016-05-02 15:54:15 +03:00
|
|
|
err := client.Watch(func(tx *redis.Tx) error {
|
2017-05-02 18:00:53 +03:00
|
|
|
cmds, err := tx.Pipelined(func(pipe redis.Pipeliner) error {
|
2016-10-13 14:36:15 +03:00
|
|
|
pipe.Set("key1", "hello1", 0)
|
|
|
|
pipe.Discard()
|
|
|
|
pipe.Set("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())
|
|
|
|
|
|
|
|
get := client.Get("key1")
|
|
|
|
Expect(get.Err()).To(Equal(redis.Nil))
|
|
|
|
Expect(get.Val()).To(Equal(""))
|
|
|
|
|
|
|
|
get = client.Get("key2")
|
|
|
|
Expect(get.Err()).NotTo(HaveOccurred())
|
|
|
|
Expect(get.Val()).To(Equal("hello2"))
|
|
|
|
})
|
|
|
|
|
2017-05-30 15:45:36 +03:00
|
|
|
It("returns no error when there are no commands", func() {
|
2016-05-02 15:54:15 +03:00
|
|
|
err := client.Watch(func(tx *redis.Tx) error {
|
2017-05-02 18:00:53 +03:00
|
|
|
_, err := tx.Pipelined(func(redis.Pipeliner) error { return nil })
|
2016-05-02 15:54:15 +03:00
|
|
|
return err
|
|
|
|
})
|
2017-05-30 15:45:36 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
2015-01-15 18:51:22 +03:00
|
|
|
|
2016-05-02 15:54:15 +03:00
|
|
|
v, err := client.Ping().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
|
|
|
|
2016-05-02 15:54:15 +03:00
|
|
|
err := client.Watch(func(tx *redis.Tx) error {
|
2017-05-02 18:00:53 +03:00
|
|
|
cmds, err := tx.Pipelined(func(pipe redis.Pipeliner) error {
|
2016-05-02 15:54:15 +03:00
|
|
|
for i := 0; i < N; i++ {
|
2016-10-13 14:36:15 +03:00
|
|
|
pipe.Incr("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())
|
|
|
|
|
2016-05-02 15:54:15 +03:00
|
|
|
num, err := client.Get("key").Int64()
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(num).To(Equal(int64(N)))
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|
|
|
|
|
2015-11-14 15:44:16 +03:00
|
|
|
It("should recover from bad connection", func() {
|
|
|
|
// Put bad connection in the pool.
|
2016-09-29 15:07:04 +03:00
|
|
|
cn, _, err := client.Pool().Get()
|
2015-11-14 15:44:16 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2017-02-08 12:24:09 +03:00
|
|
|
cn.SetNetConn(&badConn{})
|
2015-11-14 15:44:16 +03:00
|
|
|
err = client.Pool().Put(cn)
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
2016-05-02 15:54:15 +03:00
|
|
|
do := func() error {
|
|
|
|
err := client.Watch(func(tx *redis.Tx) error {
|
2017-05-02 18:00:53 +03:00
|
|
|
_, err := tx.Pipelined(func(pipe redis.Pipeliner) error {
|
2016-10-13 14:36:15 +03:00
|
|
|
pipe.Ping()
|
2016-05-02 15:54:15 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
2015-11-14 15:44:16 +03:00
|
|
|
|
2016-05-02 15:54:15 +03:00
|
|
|
err = do()
|
2015-11-14 15:44:16 +03:00
|
|
|
Expect(err).To(MatchError("bad connection"))
|
|
|
|
|
2016-05-02 15:54:15 +03:00
|
|
|
err = do()
|
2015-11-14 15:44:16 +03:00
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
})
|
2015-01-15 18:51:22 +03:00
|
|
|
})
|