forked from mongo-go/testdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testdb.go
196 lines (170 loc) · 6.15 KB
/
testdb.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
// Package testdb provides the ability to easily create MongoDB databases/
// collections within tests.
package testdb
import (
"context"
"fmt"
"math/rand"
"os"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
// ENV_VAR_TEST_MONGO_URL is an environment variable that, if set, can
// override the MongoDB url used in a TestDB. The OverrideWithEnvVars
// method must be called for it to take effect.
ENV_VAR_TEST_MONGO_URL = "TEST_MONGO_URL"
// ENV_VAR_TEST_MONGO_DB is an environment variable that, if set, can
// override the MongoDB database used in a TestDB. The OverrideWithEnvVars
// method must be called for it to take effect.
ENV_VAR_TEST_MONGO_DB = "TEST_MONGO_DB"
)
// NoIndexes can be passed to CreateRandomCollection to create a collection
// without indexes.
var NoIndexes []mongo.IndexModel
func init() { rand.Seed(time.Now().UnixNano()) }
// A TestDB represents a MongoDB database used for running tests against.
type TestDB struct {
url string
db string
timeout time.Duration
// --
client *mongo.Client
}
// NewTestDB creates a new TestDB with the provided url, database name, and
// timeout. It doesn't actually connect to MongoDB; the Connect method must be
// called to do that.
func NewTestDB(url, db string, timeout time.Duration) *TestDB {
return &TestDB{
url: url,
db: db,
timeout: timeout,
}
}
// OverrideWithEnvVars overrides the url and database in a TestDB if certain
// environment variables are set. This makes it easy for multiple people to
// run tests that require a MongoDB instance even if they have it running at
// different urls or if they want to use different databases.
//
// This method will only do anything if Connect hasn't already been called on
// the TestDB.
func (t *TestDB) OverrideWithEnvVars() {
if t.client != nil {
return
}
if urlOverride := os.Getenv(ENV_VAR_TEST_MONGO_URL); urlOverride != "" {
t.url = urlOverride
}
if dbOverride := os.Getenv(ENV_VAR_TEST_MONGO_DB); dbOverride != "" {
t.db = dbOverride
}
}
// Connect initializes a connection to the TestDB. It will return an error if
// it cannot connect to MongoDB.
func (t *TestDB) Connect() error {
// SetServerSelectionTimeout is different and more important than SetConnectTimeout.
// Internally, the mongo driver is polling and updating the topology,
// i.e. the list of replicas/nodes in the cluster. SetServerSelectionTimeout
// applies to selecting a node from the topology, which should be nearly
// instantaneous when the cluster is ok _and_ when it's down. When a node
// is down, it's reflected in the topology, so there's no need to wait for
// another server because we only use one server: the master replica.
// The 500ms below is really how long the driver will wait for the master
// replica to come back online.
//
// SetConnectTimeout is what is seems: timeout when a connection is actually
// made. This guards against slows networks, or the case when the mongo driver
// thinks the master is online but really it's not.
opts := options.Client().
ApplyURI(t.url).
SetConnectTimeout(t.timeout).
SetServerSelectionTimeout(time.Duration(500 * time.Millisecond))
client, err := mongo.NewClient(opts)
if err != nil {
return err
}
// mongo.Connect() does not actually connect:
// The Client.Connect method starts background goroutines to monitor the
// state of the deployment and does not do any I/O in the main goroutine to
// prevent the main goroutine from blocking. Therefore, it will not error if
// the deployment is down.
// https://pkg.go.dev/go.mongodb.org/mongo-driver/mongo?tab=doc#Connect
// The caller must call client.Ping() to actually connect. Consequently,
// we don't need a context here. As long as there's not a bug in the mongo
// driver, this won't block.
if err := client.Connect(context.Background()); err != nil {
return err
}
t.client = client
return nil
}
// CreateRandomCollection creates a collection with the details of info, and
// ensures it has the provided indexes. The name of the collection will be
// random, following the format of "test_" + 8 random characters. The
// DropCollection method should always be called to clean up collections
// created by this method.
//
// TestDB only supports creating random collections due to the fact that tests
// run concurrently. If multiple tests used the same collection, they would
// probably stomp on each other.
func (t *TestDB) CreateRandomCollection(indexes []mongo.IndexModel) (*mongo.Collection, error) {
if t.client == nil {
return nil, fmt.Errorf("must call Connect first")
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
collection := "test_" + randSeq(8)
coll := t.client.Database(t.db).Collection(collection)
if len(indexes) > 0 {
indexView := coll.Indexes()
opts := options.CreateIndexes().SetMaxTime(2 * time.Second)
if _, err := indexView.CreateMany(ctx, indexes, opts); err != nil {
coll.Drop(ctx)
return nil, err
}
}
return coll, nil
}
// Close terminates the TestDB's connection to MongoDB.
func (t *TestDB) Close() {
t.client.Disconnect(context.Background())
}
const dupeKeyCode = 11000
// IsDupeKeyError returns true if the error is a Mongo duplicate key error.
func IsDupeKeyError(err error) bool {
// mongo.WriteException{
// WriteConcernError:(*mongo.WriteConcernError)(nil),
// WriteErrors:mongo.WriteErrors{
// mongo.WriteError{
// Index:0,
// Code:11000,
// Message:"E11000 duplicate key error collection: coll.nodes index: x_1 dup key: { : 6 }"
// }
// }
// }
if _, ok := err.(mongo.WriteException); ok {
we := err.(mongo.WriteException)
for _, e := range we.WriteErrors {
if e.Code == dupeKeyCode {
return true
}
}
}
if _, ok := err.(mongo.CommandError); ok {
ce := err.(mongo.CommandError)
if ce.Code == dupeKeyCode {
return true
}
}
return false
}
// ------------------------------------------------------------------------- //
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}