-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
tx_test.go
279 lines (250 loc) · 7 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package dynamo
import (
"context"
"errors"
"reflect"
"sort"
"sync"
"testing"
"time"
"github.com/aws/smithy-go"
)
func TestTx(t *testing.T) {
if testDB == nil {
t.Skip(offlineSkipMsg)
}
ctx := context.TODO()
date1 := time.Date(1969, 1, 1, 1, 1, 1, 0, time.UTC)
date2 := time.Date(1969, 2, 2, 2, 2, 2, 0, time.UTC)
date3 := time.Date(1969, 3, 3, 3, 3, 3, 0, time.UTC)
widget1 := widget{UserID: 69, Time: date1, Msg: "dog"}
widget2 := widget{UserID: 69, Time: date2, Msg: "cat"}
// basic write & check
table := testDB.Table(testTableWidgets)
tx := testDB.WriteTx()
var cc, ccold ConsumedCapacity
tx.Idempotent(true)
tx.Put(table.Put(widget1))
tx.Put(table.Put(widget2))
tx.Check(table.Check("UserID", 69).Range("Time", date3).IfNotExists())
tx.ConsumedCapacity(&cc)
err := tx.Run(ctx)
if err != nil {
t.Error(err)
}
if cc.Total == 0 {
t.Error("bad consumed capacity:", cc)
}
ccold = cc
err = tx.Run(ctx)
if err != nil {
t.Error(err)
}
if cc.Total <= ccold.Total {
t.Error("bad consumed capacity:", cc.Total, ccold.Total)
}
if cc.Read <= ccold.Read {
t.Error("bad consumed capacity:", cc.Read, ccold.Read)
}
if cc.Write != ccold.Write {
t.Error("bad consumed capacity:", cc.Write, "≠", ccold.Write)
}
// idempotent write with provided idempotency token
tx = testDB.WriteTx()
cc, ccold = ConsumedCapacity{}, ConsumedCapacity{}
token, err := newIdempotencyToken()
if err != nil {
t.Error(err)
}
tx.IdempotentWithToken(token)
tx.Put(table.Put(widget1))
tx.Put(table.Put(widget2))
tx.ConsumedCapacity(&cc)
err = tx.Run(ctx)
if err != nil {
t.Error(err)
}
if cc.Total == 0 {
t.Error("bad consumed capacity:", cc)
}
ccold = cc
err = tx.Run(ctx)
if err != nil {
t.Error(err)
}
if cc.Total <= ccold.Total {
t.Error("bad consumed capacity:", cc.Total, ccold.Total)
}
if cc.Read <= ccold.Read {
t.Error("bad consumed capacity:", cc.Read, ccold.Read)
}
if cc.Write != ccold.Write {
t.Error("bad consumed capacity:", cc.Write, "≠", ccold.Write)
}
// GetOne
getTx := testDB.GetTx()
var record1, record2, record3 widget
var cc2 ConsumedCapacity
getTx.GetOne(table.Get("UserID", 69).Range("Time", Equal, date1), &record1)
getTx.GetOne(table.Get("UserID", 69).Range("Time", Equal, date2), &record2)
getTx.GetOne(table.Get("UserID", 69).Range("Time", Equal, date3), &record3)
getTx.ConsumedCapacity(&cc2)
err = getTx.Run(ctx)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(record1, widget1) {
t.Error("bad results:", record1, "≠", widget1)
}
if !reflect.DeepEqual(record2, widget2) {
t.Error("bad results:", record2, "≠", widget2)
}
if !reflect.DeepEqual(record3, widget{}) {
t.Error("bad results:", record3, "≠", widget{})
}
if cc2.Total == 0 {
t.Error("bad consumed capacity:", cc2)
}
// All
oldCC2 := cc2
var records []widget
err = getTx.All(ctx, &records)
if err != nil {
t.Error(err)
}
if !reflect.DeepEqual(records, []widget{widget1, widget2}) {
t.Error("bad results:", records)
}
if cc2.Total == oldCC2.Total {
t.Error("consumed capacity didn't increase", cc2, oldCC2)
}
// Check & Update
widget2.Msg = "bird"
tx = testDB.WriteTx()
tx.Check(table.Check("UserID", widget1.UserID).Range("Time", widget1.Time).If("Msg = ?", widget1.Msg))
tx.Update(table.Update("UserID", widget2.UserID).Range("Time", widget2.Time).Set("Msg", widget2.Msg))
if err = tx.Run(ctx); err != nil {
t.Error(err)
}
t.Run("UnmarshalItemsFromTxCondCheckFailed", func(t *testing.T) {
tx := testDB.WriteTx()
tx.Put(table.Put(widget{UserID: 69, Time: date1}).If("'BadField' = ?", "should not exist"))
tx.Put(table.Put(widget{UserID: 69, Time: date2}).If("'BadField' = ?", "should not exist"))
tx.IncludeAllItemsInCondCheckFail(true)
err := tx.Run(ctx)
if err == nil {
t.Fatal("expected error")
}
var badItems []widget
match, err := UnmarshalItemsFromTxCondCheckFailed(err, &badItems)
if !match {
t.Error("error doesn't match", err)
}
if err != nil {
t.Error(err)
}
if len(badItems) != 2 {
t.Error("wrong amount of bad items:", len(badItems), badItems)
}
sort.Slice(badItems, func(i, j int) bool {
return badItems[i].Time.Before(badItems[j].Time)
})
if !badItems[0].Time.Equal(date1) || !badItems[1].Time.Equal(date2) {
t.Error("wrong unmarshaled values:", badItems)
}
})
// Delete
tx = testDB.WriteTx()
tx.Delete(table.Delete("UserID", widget1.UserID).Range("Time", widget1.Time).If("Msg = ?", widget1.Msg))
tx.Delete(table.Delete("UserID", widget2.UserID).Range("Time", widget2.Time).If("Msg = ?", widget2.Msg))
if err = tx.Run(ctx); err != nil {
t.Error(err)
}
// zero results
if err = getTx.Run(ctx); err != ErrNotFound {
t.Error("expected ErrNotFound, got:", err)
}
// TransactionCanceledException
tx = testDB.WriteTx()
tx.Put(table.Put(widget{UserID: 69, Time: date1}).If("'Msg' = ?", "should not exist"))
tx.Put(table.Put(widget{UserID: 69, Time: date2}))
tx.Check(table.Check("UserID", 69).Range("Time", date3).IfExists().If("Msg = ?", "don't exist foo"))
err = tx.Run(ctx)
if err == nil {
t.Error("expected error")
} else {
var apiErr smithy.APIError
if errors.As(err, &apiErr) && apiErr.ErrorCode() != "TransactionCanceledException" {
t.Error("unexpected error:", err)
}
}
t.Logf("1: %+v 2: %+v 3: %+v", record1, record2, record3)
t.Logf("All: %+v (len: %d)", records, len(records))
// no input
err = testDB.GetTx().All(ctx, nil)
if err != ErrNoInput {
t.Error("unexpected error", err)
}
err = testDB.WriteTx().Run(ctx)
if err != ErrNoInput {
t.Error("unexpected error", err)
}
}
func TestTxRetry(t *testing.T) {
if testDB == nil {
t.Skip(offlineSkipMsg)
}
ctx := context.TODO()
date1 := time.Date(1999, 1, 1, 1, 1, 1, 0, time.UTC)
widget1 := widget{UserID: 69, Time: date1, Msg: "dog", Count: 0}
table := testDB.Table(testTableWidgets)
if err := table.Put(widget1).Run(ctx); err != nil {
t.Fatal(err)
}
// run 25 transactions against the same item at once
// this should eventually resolve...
const count = 25
var wg sync.WaitGroup
for i := 0; i < count; i++ {
wg.Add(1)
go func() {
defer wg.Done()
tx := testDB.WriteTx()
tx.Update(table.Update("UserID", widget1.UserID).
Range("Time", widget1.Time).
Add("Count", 1))
if err := tx.Run(ctx); err != nil {
// spew.Dump(err)
panic(err)
}
}()
}
// condition check errors shouldn't retry
wg.Add(1)
go func() {
defer wg.Done()
tx := testDB.WriteTx()
tx.Update(table.Update("UserID", widget1.UserID).
Range("Time", widget1.Time).Add("Count", 1).
If("'Count' = ?", -1))
if err := tx.Run(ctx); err != nil && !IsCondCheckFailed(err) {
panic(err)
}
}()
// validation errors shouldn't retry
wg.Add(1)
go func() {
defer wg.Done()
tx := testDB.WriteTx()
tx.Update(table.Update("UserID", "\u0002").Set("Foo", ""))
_ = tx.Run(ctx)
}()
wg.Wait()
var got widget
if err := table.Get("UserID", widget1.UserID).Range("Time", Equal, widget1.Time).One(ctx, &got); err != nil {
t.Fatal(err)
}
if got.Count != count {
t.Error("unexpected count. want:", count, "got:", got.Count)
}
}