-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathoracle_test.go
283 lines (259 loc) · 7.74 KB
/
oracle_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
280
281
282
283
// Copyright 2017 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package txn_test
import (
"time"
"github.com/juju/mgo/v3"
"github.com/juju/mgo/v3/bson"
"github.com/juju/mgo/v3/txn"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
jujutxn "github.com/juju/txn/v3"
)
// OracleSuite will be run against all oracle implementations.
type OracleSuite struct {
TxnSuite
OracleFunc func(*mgo.Collection, time.Time, int) (jujutxn.Oracle, func(), error)
}
func (s *OracleSuite) txnToToken(c *gc.C, id bson.ObjectId) string {
var noncer struct {
Nonce string `bson:"n"`
}
err := s.txns.FindId(id).Select(bson.M{"n": 1}).One(&noncer)
c.Assert(err, jc.ErrorIsNil)
return id.Hex() + "_" + noncer.Nonce
}
func (s *OracleSuite) TestKnownAndUnknownTxns(c *gc.C) {
completedTxnId := s.runTxn(c, txn.Op{
C: "coll",
Id: 0,
Insert: bson.M{},
})
pendingTxnId := s.runInterruptedTxn(c, txn.Op{
C: "coll",
Id: 0,
Update: bson.M{},
})
oracle, cleanup, err := s.OracleFunc(s.txns, time.Time{}, 0)
defer cleanup()
c.Assert(oracle, gc.NotNil)
c.Assert(err, jc.ErrorIsNil)
// One is the real one, one is a flusher that raced and failed
completedToken1 := s.txnToToken(c, completedTxnId)
completedToken2 := completedTxnId.Hex() + "_56780123"
pendingToken := s.txnToToken(c, pendingTxnId)
unknownToken := "0123456789abcdef78901234_deadbeef"
tokens := []string{completedToken1, completedToken2, pendingToken, unknownToken}
completed, err := oracle.CompletedTokens(tokens)
c.Assert(err, jc.ErrorIsNil)
c.Check(completed, jc.DeepEquals, map[string]bool{
completedToken1: true,
completedToken2: true,
})
}
func (s *OracleSuite) TestRemovedTxns(c *gc.C) {
txnId1 := s.runTxn(c, txn.Op{
C: "coll",
Id: 0,
Insert: bson.M{},
})
txnId2 := s.runTxn(c, txn.Op{
C: "coll",
Id: 1,
Insert: bson.M{},
})
oracle, cleanup, err := s.OracleFunc(s.txns, time.Time{}, 0)
defer cleanup()
c.Assert(oracle, gc.NotNil)
c.Assert(err, jc.ErrorIsNil)
token1 := s.txnToToken(c, txnId1)
token2 := s.txnToToken(c, txnId2)
completed, err := oracle.CompletedTokens([]string{token1, token2})
c.Assert(err, jc.ErrorIsNil)
c.Check(completed, jc.DeepEquals, map[string]bool{
token1: true,
token2: true,
})
count, err := oracle.RemoveTxns([]bson.ObjectId{txnId1})
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 1)
completed, err = oracle.CompletedTokens([]string{token1, token2})
c.Assert(err, jc.ErrorIsNil)
c.Check(completed, jc.DeepEquals, map[string]bool{
token2: true,
})
}
func (s *OracleSuite) TestIterTxns(c *gc.C) {
txnId1 := s.runTxn(c, txn.Op{
C: "coll",
Id: 0,
Insert: bson.M{},
})
txnId2 := s.runTxn(c, txn.Op{
C: "coll",
Id: 1,
Insert: bson.M{},
})
txnId3 := s.runTxn(c, txn.Op{
C: "coll",
Id: 2,
Insert: bson.M{},
})
oracle, cleanup, err := s.OracleFunc(s.txns, time.Time{}, 0)
defer cleanup()
c.Assert(oracle, gc.NotNil)
c.Assert(err, jc.ErrorIsNil)
c.Check(oracle.Count(), gc.Equals, 3)
count, err := oracle.RemoveTxns([]bson.ObjectId{txnId2})
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 1)
c.Check(oracle.Count(), gc.Equals, 2)
// Doesn't hurt to remove one already removed
count, err = oracle.RemoveTxns([]bson.ObjectId{txnId2})
c.Assert(err, jc.ErrorIsNil)
c.Check(count, gc.Equals, 0)
c.Check(oracle.Count(), gc.Equals, 2)
all := make([]bson.ObjectId, 0)
iter, err := oracle.IterTxns()
c.Assert(err, jc.ErrorIsNil)
var txnId bson.ObjectId
for txnId, err = iter.Next(); err == nil; txnId, err = iter.Next() {
all = append(all, txnId)
}
c.Assert(err, gc.Equals, jujutxn.EOF)
// Do we care about the order here?
c.Check(all, jc.DeepEquals, []bson.ObjectId{txnId1, txnId3})
}
func (s *OracleSuite) TestDoesntSeeNewTransactions(c *gc.C) {
baseTime, err := time.Parse("2006-01-02 15:04:05", "2017-01-01 12:00:00")
c.Assert(err, jc.ErrorIsNil)
txnId1 := s.runTxnWithTimestamp(c, nil, baseTime.Add(-time.Second), txn.Op{
C: "coll",
Id: 1,
Insert: bson.M{},
})
s.runTxnWithTimestamp(c, nil, baseTime, txn.Op{
C: "coll",
Id: 2,
Insert: bson.M{},
})
s.runTxnWithTimestamp(c, nil, baseTime.Add(time.Second), txn.Op{
C: "coll",
Id: 3,
Insert: bson.M{},
})
oracle, cleanup, err := s.OracleFunc(s.txns, baseTime, 0)
defer cleanup()
c.Assert(oracle, gc.NotNil)
c.Assert(err, jc.ErrorIsNil)
all := make([]bson.ObjectId, 0)
iter, err := oracle.IterTxns()
c.Assert(err, jc.ErrorIsNil)
var txnId bson.ObjectId
for txnId, err = iter.Next(); err == nil; txnId, err = iter.Next() {
all = append(all, txnId)
}
c.Assert(err, gc.Equals, jujutxn.EOF)
// Objects that are exactly 'baseTime' or newer are omitted
checkTxnIds(c, []bson.ObjectId{txnId1}, all)
}
func (s *OracleSuite) countFoundTxns(c *gc.C, maxTxns int) int {
oracle, cleanup, err := s.OracleFunc(s.txns, time.Time{}, maxTxns)
defer cleanup()
c.Assert(oracle, gc.NotNil)
c.Assert(err, jc.ErrorIsNil)
return oracle.Count()
}
func (s *OracleSuite) TestLimitsTxns(c *gc.C) {
const txnCount = 50
txnIds := make([]bson.ObjectId, txnCount)
for i := 0; i < txnCount; i++ {
txnIds[i] = s.runTxn(c, txn.Op{
C: "coll",
Id: i,
Insert: bson.M{},
})
}
// We should only find 10 of the 50 txns.
c.Check(s.countFoundTxns(c, 10), gc.Equals, 10)
// using a limit of '0' means all txns
c.Check(s.countFoundTxns(c, 0), gc.Equals, 50)
}
func (s *OracleSuite) TestNoThresholdSeesAllTransactions(c *gc.C) {
baseTime := time.Now()
txnId1 := s.runTxnWithTimestamp(c, nil, baseTime.Add(-30*time.Second), txn.Op{
C: "coll",
Id: 1,
Insert: bson.M{},
})
txnId2 := s.runTxnWithTimestamp(c, nil, baseTime, txn.Op{
C: "coll",
Id: 2,
Insert: bson.M{},
})
txnId3 := s.runTxnWithTimestamp(c, nil, baseTime.Add(30*time.Second), txn.Op{
C: "coll",
Id: 3,
Insert: bson.M{},
})
oracle, cleanup, err := s.OracleFunc(s.txns, time.Time{}, 0)
defer cleanup()
c.Assert(oracle, gc.NotNil)
c.Assert(err, jc.ErrorIsNil)
all := make([]bson.ObjectId, 0)
iter, err := oracle.IterTxns()
c.Assert(err, jc.ErrorIsNil)
var txnId bson.ObjectId
for txnId, err = iter.Next(); err == nil; txnId, err = iter.Next() {
all = append(all, txnId)
}
c.Assert(err, gc.Equals, jujutxn.EOF)
// Objects that are exactly 'baseTime' or newer are omitted
checkTxnIds(c, []bson.ObjectId{txnId1, txnId2, txnId3}, all)
}
func dbOracleFunc(c *mgo.Collection, thresholdTime time.Time, maxTxns int) (jujutxn.Oracle, func(), error) {
return jujutxn.NewDBOracle(c, thresholdTime, maxTxns)
}
// DBOracleSuite causes the test suite to run against the DBOracle implementation
type DBOracleSuite struct {
OracleSuite
}
var _ = gc.Suite(&DBOracleSuite{
OracleSuite: OracleSuite{
OracleFunc: dbOracleFunc,
},
})
func (s *DBOracleSuite) TestConfirmOutSupported(c *gc.C) {
tmpname := "coll.temp"
coll := s.db.C("coll")
pipe := coll.Pipe([]bson.M{{"$match": bson.M{}}, {"$out": tmpname}})
err := pipe.All(&bson.D{})
if jujutxn.CheckMongoSupportsOut(s.db) {
c.Assert(err, jc.ErrorIsNil)
} else {
c.Check(err, gc.ErrorMatches, ".*Unrecognized pipeline stage name: '\\$out'")
}
s.db.C(tmpname).DropCollection()
}
type DBCompatOracleSuite struct {
OracleSuite
}
func dbNoOutOracleFunc(c *mgo.Collection, thresholdTime time.Time, maxTxns int) (jujutxn.Oracle, func(), error) {
return jujutxn.NewDBOracleNoOut(c, thresholdTime, maxTxns)
}
var _ = gc.Suite(&DBCompatOracleSuite{
OracleSuite: OracleSuite{
OracleFunc: dbNoOutOracleFunc,
},
})
func memOracleFunc(c *mgo.Collection, thresholdTime time.Time, maxTxns int) (jujutxn.Oracle, func(), error) {
return jujutxn.NewMemOracle(c, thresholdTime, maxTxns)
}
type MemOracleSuite struct {
OracleSuite
}
var _ = gc.Suite(&MemOracleSuite{
OracleSuite: OracleSuite{
OracleFunc: memOracleFunc,
},
})