-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtxnsuite_test.go
166 lines (145 loc) · 4.76 KB
/
txnsuite_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
// Copyright 2017 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package txn_test
import (
"sync/atomic"
"time"
"github.com/juju/mgo/v3"
"github.com/juju/mgo/v3/bson"
mgotesting "github.com/juju/mgo/v3/testing"
"github.com/juju/mgo/v3/txn"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
)
type TxnSuite struct {
testing.IsolationSuite
mgotesting.MgoSuite
db *mgo.Database
txns *mgo.Collection
runner *txn.Runner
}
func (s *TxnSuite) SetUpSuite(c *gc.C) {
s.IsolationSuite.SetUpSuite(c)
s.MgoSuite.SetUpSuite(c)
}
func (s *TxnSuite) TearDownSuite(c *gc.C) {
// Make sure we've removed any Chaos
txn.SetChaos(txn.Chaos{})
s.MgoSuite.TearDownSuite(c)
s.IsolationSuite.TearDownSuite(c)
}
func (s *TxnSuite) SetUpTest(c *gc.C) {
s.IsolationSuite.SetUpTest(c)
s.MgoSuite.SetUpTest(c)
txn.SetChaos(txn.Chaos{})
s.db = s.Session.DB("mgo-test")
s.txns = s.db.C("txns")
s.runner = txn.NewRunner(s.txns)
}
func (s *TxnSuite) TearDownTest(c *gc.C) {
// Make sure we've removed any Chaos
txn.SetChaos(txn.Chaos{})
s.MgoSuite.TearDownTest(c)
s.IsolationSuite.TearDownTest(c)
}
func (s *TxnSuite) runTxn(c *gc.C, ops ...txn.Op) bson.ObjectId {
txnId := bson.NewObjectId()
err := s.runner.Run(ops, txnId, nil)
c.Assert(err, jc.ErrorIsNil)
return txnId
}
var objectIdCounter uint32
// timestampBasedTxnId allows us to create an ObjectId that embeds an exact
// timestamp. This isn't guaranteed unique always like bson.NewObjectId, but is
// good enough for testing.
func timestampBasedTxnId(timestamp time.Time) bson.ObjectId {
baseTxnId := bson.NewObjectIdWithTime(timestamp)
// We increment a counter so all ObjectIds will be distinct.
i := atomic.AddUint32(&objectIdCounter, 1)
asBytes := []byte(baseTxnId)
asBytes[9] = byte(i >> 16)
asBytes[10] = byte(i >> 8)
asBytes[11] = byte(i)
return bson.ObjectId(asBytes)
}
// runTxnWithTimestamp is the same as runTxn but forces the ObjectId of this
// transaction to be at a particular timestamp.
func (s *TxnSuite) runTxnWithTimestamp(c *gc.C, expectedErr error, timestamp time.Time, ops ...txn.Op) bson.ObjectId {
txnId := timestampBasedTxnId(timestamp)
c.Logf("generated txn %v from timestamp %v", txnId, timestamp)
err := s.runner.Run(ops, txnId, nil)
c.Assert(err, gc.Equals, expectedErr)
return txnId
}
func (s *TxnSuite) runFailingTxn(c *gc.C, expectedErr error, ops ...txn.Op) bson.ObjectId {
txnId := bson.NewObjectId()
err := s.runner.Run(ops, txnId, nil)
c.Assert(err, gc.Equals, expectedErr)
return txnId
}
// runInterruptedTxn starts a transaction, but interrupts it just before it gets applied.
func (s *TxnSuite) runInterruptedTxn(c *gc.C, ops ...txn.Op) bson.ObjectId {
txn.SetChaos(txn.Chaos{
KillChance: 1,
Breakpoint: "set-applying",
})
txnId := s.runFailingTxn(c, txn.ErrChaos, ops...)
txn.SetChaos(txn.Chaos{})
return txnId
}
func (s *TxnSuite) assertTxns(c *gc.C, expectedIds ...bson.ObjectId) {
var actualIds []bson.ObjectId
var txnDoc struct {
Id bson.ObjectId `bson:"_id"`
}
iter := s.txns.Find(nil).Select(bson.M{"_id": 1}).Iter()
for iter.Next(&txnDoc) {
actualIds = append(actualIds, txnDoc.Id)
}
c.Assert(actualIds, jc.SameContents, expectedIds)
}
func checkTxnIds(c *gc.C, expectedIds []bson.ObjectId, actualIds []bson.ObjectId) {
expectedHex := make([]string, len(expectedIds))
for i, id := range expectedIds {
expectedHex[i] = id.Hex()
}
actualHex := make([]string, len(actualIds))
for i, id := range actualIds {
actualHex[i] = id.Hex()
}
c.Check(actualHex, jc.SameContents, expectedHex)
}
func (s *TxnSuite) assertDocQueue(c *gc.C, collection string, id interface{}, expectedIds ...bson.ObjectId) {
coll := s.db.C(collection)
var queueDoc struct {
Queue []string `bson:"txn-queue"`
}
err := coll.FindId(id).One(&queueDoc)
c.Assert(err, jc.ErrorIsNil)
txnIdsHex := make([]string, len(queueDoc.Queue))
for i, token := range queueDoc.Queue {
// strip of the _nonce
txnIdsHex[i] = token[:24]
}
expectedHex := make([]string, len(expectedIds))
for i, id := range expectedIds {
expectedHex[i] = id.Hex()
}
c.Check(txnIdsHex, gc.DeepEquals, expectedHex)
}
func (s *TxnSuite) assertStashDocQueue(c *gc.C, collection string, id interface{}, expectedIds ...bson.ObjectId) {
// Assert a pending/removed document that is currently in the stash.
// We identify it by the collection it would have been in.
stashId := bson.D{{"c", collection}, {"id", id}}
s.assertDocQueue(c, "txns.stash", stashId, expectedIds...)
}
func (s *TxnSuite) assertCollCount(c *gc.C, collName string, expectedCount int) {
count := s.getCollCount(c, collName)
c.Assert(count, gc.Equals, expectedCount)
}
func (s *TxnSuite) getCollCount(c *gc.C, collName string) int {
n, err := s.db.C(collName).Count()
c.Assert(err, jc.ErrorIsNil)
return n
}