This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgo_incr_client_test.go
98 lines (80 loc) · 1.75 KB
/
mgo_incr_client_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
package main
import (
"labix.org/v2/mgo/bson"
_ "log"
_ "strconv"
"testing"
"time"
)
func TestClientInit(t *testing.T) {
client := &mongodb_behavior{}
props := map[string]string{
"mongodb.run": "counters",
"mongodb.url": "mongodb://localhost:27017",
}
err := client.Init(props)
if err != nil {
t.Error(err)
return
}
defer client.Close()
docid := client.mb.(*mongodb_counters).deadbeef_id
coll := client.s.DB(client.db).C(client.collectionName)
query := coll.FindId(docid)
n, err := query.Count()
if !expectOk(t, err) {
return
}
if !expectInt(t, 1, n) {
return
}
}
func TestClientWork(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
client := &mongodb_behavior{}
props := map[string]string{
"mongodb.run": "counters",
"mongodb.url": "mongodb://localhost:27017",
}
// Connect to mongo
err := client.Init(props)
if err != nil {
t.Error(err)
return
}
// Remember to close
defer client.Close()
// Find the baseline total number
var res struct {
Id bson.ObjectId `bson:"_id"`
Total int64 `bson:"total"`
AccountId string `bson:"account_id"`
}
docid := client.mb.(*mongodb_counters).deadbeef_id
coll := client.s.DB(client.db).C(client.collectionName)
err = coll.FindId(docid).One(&res)
if !expectOk(t, err) {
return
}
orig_total := int(res.Total)
// Do several units of work
var wr WorkResult
for i := 0; i < 20; i += 1 {
wr = client.Work(time.Now())
if wr != WRK_OK {
t.Errorf("expected: WRK_OK, got: %v", wr)
return
}
}
// Check that the total has increased by the same amount
err = coll.FindId(docid).One(&res)
if !expectOk(t, err) {
return
}
next_total := int(res.Total)
if !expectInt(t, orig_total+20, next_total) {
return
}
}