forked from redis/go-redis
-
Notifications
You must be signed in to change notification settings - Fork 7
/
tx_test.go
150 lines (125 loc) · 3.4 KB
/
tx_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//go:build !skiptest
package dicedb_test
import (
"context"
"strconv"
"sync"
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
"github.com/dicedb/dicedb-go"
)
var _ = Describe("Tx", func() {
var client *dicedb.Client
BeforeEach(func() {
client = dicedb.NewClient(redisOptions())
Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred())
})
AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})
It("should Watch", func() {
var incr func(string) error
// Transactionally increments key using GET and SET commands.
incr = func(key string) error {
err := client.Watch(ctx, func(tx *dicedb.Tx) error {
n, err := tx.Get(ctx, key).Int64()
if err != nil && err != dicedb.Nil {
return err
}
_, err = tx.TxPipelined(ctx, func(pipe dicedb.Pipeliner) error {
pipe.Set(ctx, key, strconv.FormatInt(n+1, 10), 0)
return nil
})
return err
}, key)
if err == dicedb.TxFailedErr {
return incr(key)
}
return err
}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer GinkgoRecover()
defer wg.Done()
err := incr("key")
Expect(err).NotTo(HaveOccurred())
}()
}
wg.Wait()
n, err := client.Get(ctx, "key").Int64()
Expect(err).NotTo(HaveOccurred())
Expect(n).To(Equal(int64(100)))
})
It("should discard", Label("NonRedisEnterprise"), func() {
err := client.Watch(ctx, func(tx *dicedb.Tx) error {
cmds, err := tx.TxPipelined(ctx, func(pipe dicedb.Pipeliner) error {
pipe.Set(ctx, "key1", "hello1", 0)
pipe.Discard()
pipe.Set(ctx, "key2", "hello2", 0)
return nil
})
Expect(err).NotTo(HaveOccurred())
Expect(cmds).To(HaveLen(1))
return err
}, "key1", "key2")
Expect(err).NotTo(HaveOccurred())
get := client.Get(ctx, "key1")
Expect(get.Err()).To(Equal(dicedb.Nil))
Expect(get.Val()).To(Equal(""))
get = client.Get(ctx, "key2")
Expect(get.Err()).NotTo(HaveOccurred())
Expect(get.Val()).To(Equal("hello2"))
})
It("returns no error when there are no commands", func() {
err := client.Watch(ctx, func(tx *dicedb.Tx) error {
_, err := tx.TxPipelined(ctx, func(dicedb.Pipeliner) error { return nil })
return err
})
Expect(err).NotTo(HaveOccurred())
v, err := client.Ping(ctx).Result()
Expect(err).NotTo(HaveOccurred())
Expect(v).To(Equal("PONG"))
})
It("should exec bulks", func() {
const N = 20000
err := client.Watch(ctx, func(tx *dicedb.Tx) error {
cmds, err := tx.TxPipelined(ctx, func(pipe dicedb.Pipeliner) error {
for i := 0; i < N; i++ {
pipe.Incr(ctx, "key")
}
return nil
})
Expect(err).NotTo(HaveOccurred())
Expect(len(cmds)).To(Equal(N))
for _, cmd := range cmds {
Expect(cmd.Err()).NotTo(HaveOccurred())
}
return err
})
Expect(err).NotTo(HaveOccurred())
num, err := client.Get(ctx, "key").Int64()
Expect(err).NotTo(HaveOccurred())
Expect(num).To(Equal(int64(N)))
})
It("should recover from bad connection", func() {
// Put bad connection in the pool.
cn, err := client.Pool().Get(context.Background())
Expect(err).NotTo(HaveOccurred())
cn.SetNetConn(&badConn{})
client.Pool().Put(ctx, cn)
do := func() error {
err := client.Watch(ctx, func(tx *dicedb.Tx) error {
_, err := tx.TxPipelined(ctx, func(pipe dicedb.Pipeliner) error {
pipe.Ping(ctx)
return nil
})
return err
})
return err
}
err = do()
Expect(err).NotTo(HaveOccurred())
})
})