@@ -19,6 +19,7 @@ import (
19
19
"go.mongodb.org/mongo-driver/bson/bsoncodec"
20
20
"go.mongodb.org/mongo-driver/bson/bsonrw"
21
21
"go.mongodb.org/mongo-driver/bson/primitive"
22
+ "go.mongodb.org/mongo-driver/event"
22
23
"go.mongodb.org/mongo-driver/internal"
23
24
"go.mongodb.org/mongo-driver/internal/testutil"
24
25
"go.mongodb.org/mongo-driver/internal/testutil/assert"
@@ -469,15 +470,13 @@ func TestClient(t *testing.T) {
469
470
}
470
471
471
472
func TestClientStress (t * testing.T ) {
472
- // TODO: Enable with GODRIVER-2038.
473
- t .Skip ("TODO: Enable with GODRIVER-2038" )
474
-
475
473
if testing .Short () {
476
474
t .Skip ("skipping integration test in short mode" )
477
475
}
478
476
479
477
mtOpts := mtest .NewOptions ().CreateClient (false )
480
478
mt := mtest .New (t , mtOpts )
479
+ defer mt .Close ()
481
480
482
481
// Test that a Client can recover from a massive traffic spike after the traffic spike is over.
483
482
mt .Run ("Client recovers from traffic spike" , func (mt * mtest.T ) {
@@ -505,6 +504,7 @@ func TestClientStress(t *testing.T) {
505
504
if err != nil {
506
505
errs = append (errs , err )
507
506
}
507
+ time .Sleep (10 * time .Microsecond )
508
508
}
509
509
return errs
510
510
}
@@ -523,24 +523,42 @@ func TestClientStress(t *testing.T) {
523
523
}
524
524
assert .True (mt , maxRTT > 0 , "RTT must be greater than 0" )
525
525
526
- // Run tests with various "maxPoolSize" values, including 1-connection pools and unlimited
527
- // size pools , to test how the client handles traffic spikes using different connection pool
528
- // configurations.
529
- maxPoolSizes := []uint64 {0 , 1 , 10 , 100 }
526
+ // Run tests with various "maxPoolSize" values, including 1-connection pools and the default
527
+ // size of 100 , to test how the client handles traffic spikes using different connection
528
+ // pool configurations.
529
+ maxPoolSizes := []uint64 {1 , 10 , 100 }
530
530
for _ , maxPoolSize := range maxPoolSizes {
531
- maxPoolSizeOpt := mtest .NewOptions ().ClientOptions (options .Client ().SetMaxPoolSize (maxPoolSize ))
531
+ tpm := newTestPoolMonitor ()
532
+ maxPoolSizeOpt := mtest .NewOptions ().ClientOptions (
533
+ options .Client ().
534
+ SetPoolMonitor (tpm .PoolMonitor ).
535
+ SetMaxPoolSize (maxPoolSize ))
532
536
mt .RunOpts (fmt .Sprintf ("maxPoolSize %d" , maxPoolSize ), maxPoolSizeOpt , func (mt * mtest.T ) {
537
+ // Print the count of connection created, connection closed, and pool clear events
538
+ // collected during the test to help with debugging.
539
+ defer func () {
540
+ created := len (tpm .Events (func (e * event.PoolEvent ) bool { return e .Type == event .ConnectionCreated }))
541
+ closed := len (tpm .Events (func (e * event.PoolEvent ) bool { return e .Type == event .ConnectionClosed }))
542
+ poolCleared := len (tpm .Events (func (e * event.PoolEvent ) bool { return e .Type == event .PoolCleared }))
543
+ mt .Logf ("Connections created: %d, connections closed: %d, pool clears: %d" , created , closed , poolCleared )
544
+ }()
545
+
533
546
doc := bson.D {{Key : "_id" , Value : oid }, {Key : "key" , Value : "value" }}
534
547
_ , err := mt .Coll .InsertOne (context .Background (), doc )
535
548
assert .Nil (mt , err , "InsertOne error: %v" , err )
536
549
537
- // Set the timeout to be 10x the maximum observed RTT. Use a minimum 10ms timeout to
550
+ // Set the timeout to be 100x the maximum observed RTT. Use a minimum 100ms timeout to
538
551
// prevent spurious test failures due to extremely low timeouts.
539
- timeout := maxRTT * 10
540
- if timeout < 10 * time .Millisecond {
541
- timeout = 10 * time .Millisecond
552
+ timeout := maxRTT * 100
553
+ minTimeout := 100 * time .Millisecond
554
+ if timeout < minTimeout {
555
+ timeout = minTimeout
542
556
}
543
- t .Logf ("Max RTT %v; using a timeout of %v" , maxRTT , timeout )
557
+ mt .Logf ("Max RTT %v; using a timeout of %v" , maxRTT , timeout )
558
+
559
+ // Warm up the client for 1 second to allow connections to be established. Ignore
560
+ // any errors.
561
+ _ = findOneFor (mt .Coll , timeout , 1 * time .Second )
544
562
545
563
// Simulate normal traffic by running one FindOne loop for 1 second and assert that there
546
564
// are no errors.
@@ -560,11 +578,11 @@ func TestClientStress(t *testing.T) {
560
578
})
561
579
}
562
580
err = g .Wait ()
563
- assert . NotNil ( mt , err , "expected at least one error, got nil" )
581
+ mt . Logf ( "Error from extreme traffic spike (errors are expected): %v" , err )
564
582
565
- // Simulate normal traffic again for 1 second. Ignore any errors to allow any outstanding
583
+ // Simulate normal traffic again for 5 second. Ignore any errors to allow any outstanding
566
584
// connection errors to stop.
567
- _ = findOneFor (mt .Coll , timeout , 1 * time .Second )
585
+ _ = findOneFor (mt .Coll , timeout , 5 * time .Second )
568
586
569
587
// Simulate normal traffic again for 1 second and assert that there are no errors.
570
588
errs = findOneFor (mt .Coll , timeout , 1 * time .Second )
0 commit comments