@@ -20,7 +20,9 @@ import (
20
20
"encoding/json"
21
21
"fmt"
22
22
"os"
23
+ "strings"
23
24
"testing"
25
+ "time"
24
26
"unicode/utf8"
25
27
26
28
"github.com/hyperledger/fabric/common/ledger/testutil"
@@ -36,13 +38,14 @@ var username = ""
36
38
var password = ""
37
39
var maxRetries = 3
38
40
var maxRetriesOnStartup = 10
41
+ var requestTimeout = time .Second * 20
39
42
40
43
const updateDocumentConflictError = "conflict"
41
44
const updateDocumentConflictReason = "Document update conflict."
42
45
43
46
func cleanup (database string ) error {
44
47
//create a new connection
45
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
48
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
46
49
if err != nil {
47
50
fmt .Println ("Unexpected error" , err )
48
51
return err
@@ -78,15 +81,15 @@ func TestDBConnectionDef(t *testing.T) {
78
81
ledgertestutil .SetupCoreYAMLConfig ("./../../../../peer" )
79
82
80
83
//create a new connection
81
- _ , err := CreateConnectionDefinition (connectURL , "" , "" , maxRetries , maxRetriesOnStartup )
84
+ _ , err := CreateConnectionDefinition (connectURL , "" , "" , maxRetries , maxRetriesOnStartup , requestTimeout )
82
85
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create database connection definition" ))
83
86
84
87
}
85
88
86
89
func TestDBBadConnectionDef (t * testing.T ) {
87
90
88
91
//create a new connection
89
- _ , err := CreateConnectionDefinition ("^^^localhost:5984" , "" , "" , maxRetries , maxRetriesOnStartup )
92
+ _ , err := CreateConnectionDefinition ("^^^localhost:5984" , "" , "" , maxRetries , maxRetriesOnStartup , requestTimeout )
90
93
testutil .AssertError (t , err , fmt .Sprintf ("Did not receive error when trying to create database connection definition with a bad hostname" ))
91
94
92
95
}
@@ -102,7 +105,7 @@ func TestDBCreateSaveWithoutRevision(t *testing.T) {
102
105
103
106
if err == nil {
104
107
//create a new instance and database object
105
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
108
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
106
109
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
107
110
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
108
111
@@ -128,7 +131,7 @@ func TestDBCreateEnsureFullCommit(t *testing.T) {
128
131
129
132
if err == nil {
130
133
//create a new instance and database object
131
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
134
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
132
135
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
133
136
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
134
137
@@ -153,25 +156,25 @@ func TestDBBadDatabaseName(t *testing.T) {
153
156
if ledgerconfig .IsCouchDBEnabled () {
154
157
155
158
//create a new instance and database object using a valid database name mixed case
156
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
159
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
157
160
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
158
161
_ , dberr := CreateCouchDatabase (* couchInstance , "testDB" )
159
162
testutil .AssertNoError (t , dberr , fmt .Sprintf ("Error when testing a valid database name" ))
160
163
161
164
//create a new instance and database object using a valid database name letters and numbers
162
- couchInstance , err = CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
165
+ couchInstance , err = CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
163
166
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
164
167
_ , dberr = CreateCouchDatabase (* couchInstance , "test132" )
165
168
testutil .AssertNoError (t , dberr , fmt .Sprintf ("Error when testing a valid database name" ))
166
169
167
170
//create a new instance and database object using a valid database name - special characters
168
- couchInstance , err = CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
171
+ couchInstance , err = CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
169
172
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
170
173
_ , dberr = CreateCouchDatabase (* couchInstance , "test1234~!@#$%^&*()[]{}." )
171
174
testutil .AssertNoError (t , dberr , fmt .Sprintf ("Error when testing a valid database name" ))
172
175
173
176
//create a new instance and database object using a invalid database name - too long /*
174
- couchInstance , err = CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
177
+ couchInstance , err = CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
175
178
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
176
179
_ , dberr = CreateCouchDatabase (* couchInstance , "A12345678901234567890123456789012345678901234" +
177
180
"56789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
@@ -192,7 +195,7 @@ func TestDBBadConnection(t *testing.T) {
192
195
if ledgerconfig .IsCouchDBEnabled () {
193
196
194
197
//create a new instance and database object
195
- _ , err := CreateCouchInstance (badConnectURL , username , password , maxRetries , maxRetriesOnStartup )
198
+ _ , err := CreateCouchInstance (badConnectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
196
199
testutil .AssertError (t , err , fmt .Sprintf ("Error should have been thrown for a bad connection" ))
197
200
}
198
201
}
@@ -208,7 +211,7 @@ func TestDBCreateDatabaseAndPersist(t *testing.T) {
208
211
209
212
if err == nil {
210
213
//create a new instance and database object
211
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
214
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
212
215
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
213
216
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
214
217
@@ -286,6 +289,32 @@ func TestDBCreateDatabaseAndPersist(t *testing.T) {
286
289
287
290
}
288
291
292
+ func TestDBRequestTimeout (t * testing.T ) {
293
+
294
+ if ledgerconfig .IsCouchDBEnabled () {
295
+
296
+ database := "testdbrequesttimeout"
297
+ err := cleanup (database )
298
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to cleanup Error: %s" , err ))
299
+ defer cleanup (database )
300
+
301
+ if err == nil {
302
+
303
+ //create an impossibly short timeout
304
+ impossibleTimeout := time .Microsecond * 1
305
+
306
+ //create a new instance and database object with a timeout that will fail
307
+ //Also use a maxRetriesOnStartup=3 to reduce the number of retries
308
+ _ , err := CreateCouchInstance (connectURL , username , password , maxRetries , 3 , impossibleTimeout )
309
+ testutil .AssertError (t , err , fmt .Sprintf ("Error should have been thown while trying to create a couchdb instance with a connection timeout" ))
310
+
311
+ //see if the error message contains the timeout error
312
+ testutil .AssertEquals (t , strings .Count (err .Error (), "Client.Timeout exceeded while awaiting headers" ), 1 )
313
+
314
+ }
315
+ }
316
+ }
317
+
289
318
func TestDBBadJSON (t * testing.T ) {
290
319
291
320
if ledgerconfig .IsCouchDBEnabled () {
@@ -298,7 +327,7 @@ func TestDBBadJSON(t *testing.T) {
298
327
if err == nil {
299
328
300
329
//create a new instance and database object
301
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
330
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
302
331
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
303
332
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
304
333
@@ -334,7 +363,7 @@ func TestPrefixScan(t *testing.T) {
334
363
335
364
if err == nil {
336
365
//create a new instance and database object
337
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
366
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
338
367
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
339
368
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
340
369
@@ -406,7 +435,7 @@ func TestDBSaveAttachment(t *testing.T) {
406
435
attachments = append (attachments , attachment )
407
436
408
437
//create a new instance and database object
409
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
438
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
410
439
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
411
440
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
412
441
@@ -439,7 +468,7 @@ func TestDBDeleteDocument(t *testing.T) {
439
468
440
469
if err == nil {
441
470
//create a new instance and database object
442
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
471
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
443
472
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
444
473
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
445
474
@@ -477,7 +506,7 @@ func TestDBDeleteNonExistingDocument(t *testing.T) {
477
506
478
507
if err == nil {
479
508
//create a new instance and database object
480
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
509
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
481
510
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
482
511
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
483
512
@@ -616,7 +645,7 @@ func TestRichQuery(t *testing.T) {
616
645
617
646
if err == nil {
618
647
//create a new instance and database object --------------------------------------------------------
619
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
648
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
620
649
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
621
650
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
622
651
@@ -830,7 +859,7 @@ func TestBatchBatchOperations(t *testing.T) {
830
859
defer cleanup (database )
831
860
832
861
//create a new instance and database object --------------------------------------------------------
833
- couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup )
862
+ couchInstance , err := CreateCouchInstance (connectURL , username , password , maxRetries , maxRetriesOnStartup , requestTimeout )
834
863
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
835
864
db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
836
865
0 commit comments