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
/
mongodb_writes_test.go
167 lines (134 loc) · 2.88 KB
/
mongodb_writes_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
package main
import (
_ "labix.org/v2/mgo/bson"
_ "log"
"strconv"
"testing"
"time"
)
func TestMongoDbWritesProperties(t *testing.T) {
mode := &mongodb_writes{}
props := make(map[string]string)
err := mode.parseProperties(props)
if !expectOk(t, err) {
return
}
if !expectInt(t, MONGO_DEFAULT_DOCUMENT_LENGTH, mode.doc_length) {
return
}
props["mongodb.doc_length"] = "NAN"
err = mode.parseProperties(props)
if err == nil {
t.Error("expected an error when mongodb.doc_length is not a number")
return
}
props["mongodb.doc_length"] = strconv.Itoa(MONGO_DEFAULT_DOCUMENT_LENGTH - 1)
err = mode.parseProperties(props)
if err == nil {
t.Error("expected an error when mongodb.doc_length < 64")
return
}
props["mongodb.doc_length"] = "64"
err = mode.parseProperties(props)
if err != nil {
t.Error(err)
return
}
if !expectInt(t, MONGO_DEFAULT_DOCUMENT_LENGTH, mode.doc_length) {
return
}
props["mongodb.doc_length"] = "1024"
err = mode.parseProperties(props)
if err != nil {
t.Error(err)
return
}
if !expectInt(t, 1024, mode.doc_length) {
return
}
}
func TestMongoDbWritesInit(t *testing.T) {
b := &mongodb_behavior{}
props := map[string]string{
"mongodb.run": "writes",
"mongodb.url": "mongodb://localhost:27017",
}
err := b.Init(props)
if err != nil {
t.Error(err)
return
}
defer b.Close()
mode, ok := b.mb.(*mongodb_writes)
if !expectBool(t, true, ok) {
return
}
const content = "I wrote some Go!"
if !expectString(t, content, mode.doc_data) {
return
}
}
func TestMongoDbWritesInit1024(t *testing.T) {
b := &mongodb_behavior{}
props := map[string]string{
"mongodb.run": "writes",
"mongodb.url": "mongodb://localhost:27017",
"mongodb.doc_length": "1024",
}
err := b.Init(props)
if err != nil {
t.Error(err)
return
}
defer b.Close()
mode, ok := b.mb.(*mongodb_writes)
if !expectBool(t, true, ok) {
return
}
if !expectInt(t, 1024, mode.doc_length) {
return
}
if !expectInt(t, 1024, len(mode.doc_data)) {
return
}
}
func TestMongoDbWritesWork(t *testing.T) {
b := &mongodb_behavior{}
props := map[string]string{
"mongodb.run": "writes",
"mongodb.url": "mongodb://localhost:27017",
}
// Connect to mongo
err := b.Init(props)
if err != nil {
t.Error(err)
return
}
// Remember to close
defer b.Close()
mode, ok := b.mb.(*mongodb_writes)
if !expectBool(t, true, ok) {
return
}
// Find the baseline total number of documents in this collection
baseline, err := mode.collection().Count()
if !expectOk(t, err) {
return
}
// Do several units of work
for i := 0; i < 20; i += 1 {
wr := b.Work(time.Now())
if wr != WRK_OK {
t.Errorf("expected: WRK_OK, got: %v", wr)
return
}
}
// Check that the number of documents has increased by the same amount
v, err := mode.collection().Count()
if !expectOk(t, err) {
return
}
if !expectInt(t, baseline+20, v) {
return
}
}