forked from r3b/usergrid-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusergrid_test.go
More file actions
310 lines (305 loc) · 9.27 KB
/
usergrid_test.go
File metadata and controls
310 lines (305 loc) · 9.27 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package usergrid
import (
"encoding/json"
"strconv"
"testing"
"time"
)
func TestPost(t *testing.T){
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
data := map[string]string{"name":"go_dog", "status":"good dog"}
err:= client.Post("dogs", nil, data, JSONResponseHandler(&objmap))
if(err!=nil){
t.Logf("TestPost failed: %s\n", err)
t.Fail()
}
if(t.Failed()){
str,_:=json.MarshalIndent(&objmap,""," ")
t.Logf("RESPONSE: %s", str)
}
}
func TestPostDuplicate(t *testing.T){
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
data := map[string]string{"name":"go_dog", "status":"good dog"}
err:= client.Post("dogs", nil, data, JSONResponseHandler(&objmap))
if(err==nil){
t.Logf("TestPost failed. Should have received a duplicate entity error.\n")
t.Fail()
}
if(t.Failed()){
str,_:=json.MarshalIndent(&objmap,""," ")
t.Logf("RESPONSE: %s", str)
}
}
func TestGet(t *testing.T){
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
err:= client.Get("dogs/go_dog", nil, JSONResponseHandler(&objmap))
if(err!=nil){
t.Logf("TestGet failed: %s\n", err)
t.Fail()
}else{
omap:=objmap.(map[string]interface{})
if (omap["entities"]==nil) {
t.Logf("No entities returned")
t.Fail()
}else if(len(omap["entities"].([]interface{}))!=1){
t.Logf("Incorrect number of entities")
t.Fail()
}
}
if(t.Failed()){
str,_:=json.MarshalIndent(&objmap,""," ")
t.Logf("RESPONSE: %s", str)
}
}
func TestGetBadEntity(t *testing.T){
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
err:= client.Get("dogs/go_dogizzle", nil, JSONResponseHandler(&objmap))
if(err==nil){
t.Logf("TestGet failed. dog should not exist: %s\n", err)
t.Fail()
}
if(t.Failed()){
str,_:=json.MarshalIndent(&objmap,""," ")
t.Logf("RESPONSE: %s", str)
}
}
func TestPut(t *testing.T){
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
data := map[string]string{"status":"flat out on the highway and drying in the sun"}
err:= client.Put("dogs/go_dog", nil, data, JSONResponseHandler(&objmap))
if(err!=nil){
t.Logf("TestPut failed: %s\n", err)
t.Fail()
}
if(t.Failed()){
str,_:=json.MarshalIndent(&objmap,""," ")
t.Logf("RESPONSE: %s", str)
}
}
func TestDelete(t *testing.T){
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
err:= client.Delete("dogs/go_dog", nil, JSONResponseHandler(&objmap))
if(err!=nil){
t.Logf("Test failed: %s\n", err)
t.Fail()
}
if(t.Failed()){
str,_:=json.MarshalIndent(&objmap,""," ")
t.Logf("RESPONSE: %s", str)
}
}
func TestDelete2(t *testing.T){
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
err:= client.Get("dogs/go_dog", nil, JSONResponseHandler(&objmap))
if(err==nil){
t.Logf("Test failed. Dog should be deleted: %s\n", err)
t.Fail()
}
if(t.Failed()){
str,_:=json.MarshalIndent(&objmap,""," ")
t.Logf("RESPONSE: %s", str)
}
}
func TestDelete3(t *testing.T){
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
err:= client.Delete("dogs/go_dog", nil, JSONResponseHandler(&objmap))
if(err==nil){
t.Logf("Test failed. Should return service_resource_not_found error\n")
t.Fail()
}
if(t.Failed()){
str,_:=json.MarshalIndent(&objmap,""," ")
t.Logf("RESPONSE: %s", str)
}
}
func BenchmarkPost(b *testing.B) {
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
data := map[string]string{"index":"0", "description":"golang benchmark"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
data["index"]=strconv.Itoa(i);
err:= client.Post("benchmark", nil, data, JSONResponseHandler(&objmap))
if(err!=nil){
b.Logf("BenchmarkPost failed: %s\n", err)
b.Fail()
}
if(b.Failed()){
str,_:=json.MarshalIndent(objmap,""," ")
b.Logf("RESPONSE: %s", str)
}
}
}
func BenchmarkRawRequests(b *testing.B) {
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
params := map[string]string{"limit":strconv.Itoa(10)}
err:= client.Get("benchmark", params, JSONResponseHandler(&objmap))
if(err!=nil){
b.Logf("Test failed: %s\n", err)
b.Fail()
}
omap:=objmap.(map[string]interface{})
if (omap["entities"]==nil || len(omap["entities"].([]interface{}))==0) {
b.Logf("Test failed: no entities\n")
b.Fail()
}
entities:=omap["entities"].([]interface{})
requests:=0
responses:=0
errors:=0
// var objmap interface{}
var entity map[string]interface{}
responseChan := make(chan []byte)
go func(){
for {
select {
case v := <-responseChan:
if err := json.Unmarshal(v, &objmap); err == nil{
if err:=CheckForError(&objmap); err != nil {
errors++
}
}else{
errors++
}
responses++
requests--
case <-time.After(time.Second * 10):
return
}
}
}()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if(len(entities)==0){
b.Logf("Test failed: we ran out of entities\n")
b.Fail()
continue
}
entity = entities[i % len(entities)].(map[string]interface{})
for ;requests>=MAX_CONCURRENT_REQUESTS;{
// if we outpace GOMAXPROCS, we'll run out of threads
time.Sleep(60 * time.Millisecond)
}
client.Request("GET", "http://api.usergrid.com/yourorgname/sandbox/benchmark/"+entity["uuid"].(string), nil, nil, responseChan)
requests++
}
for ;requests>0;{
// wait for the last few responses
time.Sleep(60 * time.Millisecond)
}
}
func BenchmarkGet(b *testing.B) {
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
params := map[string]string{"limit":strconv.Itoa(500)}
err:= client.Get("benchmark", params, JSONResponseHandler(&objmap))
if(err!=nil){
b.Logf("Test failed: %s\n", err)
b.Fail()
}
omap:=objmap.(map[string]interface{})
if (omap["entities"]==nil || len(omap["entities"].([]interface{}))==0) {
b.Logf("Test failed: no entities to delete\n")
b.Fail()
}
entities:=omap["entities"].([]interface{})
var entity map[string]interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if(len(entities)==0){
b.Logf("Test failed: we ran out of entities\n")
b.Fail()
continue
}
entity, entities = entities[len(entities)-1].(map[string]interface{}), entities[:len(entities)-1]
err:= client.Get("benchmark/"+entity["uuid"].(string), nil, NOOPResponseHandler(&objmap))
if(err!=nil){
b.Logf("BenchmarkGet failed: %s\n", err)
b.Fail()
}
}
if(b.Failed()){
str,_:=json.MarshalIndent(objmap,""," ")
b.Logf("RESPONSE: %s", str)
}
}
func BenchmarkPut(b *testing.B) {
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
params := map[string]string{"limit":strconv.Itoa(500)}
err:= client.Get("benchmark", params, JSONResponseHandler(&objmap))
if(err!=nil){
b.Logf("Test failed: %s\n", err)
b.Fail()
}
omap:=objmap.(map[string]interface{})
if (omap["entities"]==nil || len(omap["entities"].([]interface{}))==0) {
b.Logf("Test failed: no entities to delete\n")
b.Fail()
}
entities:=omap["entities"].([]interface{})
var entity map[string]interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if(len(entities)==0){
b.Logf("Test failed: we ran out of entities\n")
b.Fail()
continue
}
entity, entities = entities[len(entities)-1].(map[string]interface{}), entities[:len(entities)-1]
err:= client.Put("benchmark/"+entity["uuid"].(string), nil, map[string]string{"updated":"true"}, NOOPResponseHandler(&objmap))
if(err!=nil){
b.Logf("BenchmarkPut failed: %s\n", err)
b.Fail()
}
}
if(b.Failed()){
str,_:=json.MarshalIndent(objmap,""," ")
b.Logf("RESPONSE: %s", str)
}
}
func BenchmarkDelete(b *testing.B) {
var objmap interface{}
client := Client {Organization:"yourorgname",Application:"sandbox",Uri:"https://api.usergrid.com"}
params := map[string]string{"limit":strconv.Itoa(500)}
err:= client.Get("benchmark", params, JSONResponseHandler(&objmap))
if(err!=nil){
b.Logf("Test failed: %s\n", err)
b.Fail()
}
omap:=objmap.(map[string]interface{})
if (omap["entities"]==nil || len(omap["entities"].([]interface{}))==0) {
b.Logf("Test failed: no entities to delete\n")
b.Fail()
}
entities:=omap["entities"].([]interface{})
var entity map[string]interface{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if(len(entities)==0){
b.Logf("Test failed: we ran out of entities\n")
b.Fail()
continue
}
entity, entities = entities[len(entities)-1].(map[string]interface{}), entities[:len(entities)-1]
err:= client.Delete("benchmark/"+entity["uuid"].(string), nil, NOOPResponseHandler(&objmap))
if(err!=nil){
b.Logf("BenchmarkDelete failed: %s\n", err)
b.Fail()
}
}
if(b.Failed()){
str,_:=json.MarshalIndent(objmap,""," ")
b.Logf("RESPONSE: %s", str)
}
}