@@ -12,6 +12,7 @@ import (
12
12
13
13
"go.mongodb.org/mongo-driver/bson"
14
14
"go.mongodb.org/mongo-driver/bson/primitive"
15
+ "go.mongodb.org/mongo-driver/event"
15
16
"go.mongodb.org/mongo-driver/internal/testutil/assert"
16
17
"go.mongodb.org/mongo-driver/mongo"
17
18
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
@@ -515,6 +516,92 @@ func TestChangeStream_ReplicaSet(t *testing.T) {
515
516
tryNextGetmoreError (mt , cs )
516
517
})
517
518
})
519
+
520
+ customDeploymentClientOpts := options .Client ().
521
+ SetPoolMonitor (poolMonitor ).
522
+ SetWriteConcern (mtest .MajorityWc ).
523
+ SetReadConcern (mtest .MajorityRc ).
524
+ SetRetryReads (false )
525
+ customDeploymentOpts := mtest .NewOptions ().
526
+ Topologies (mtest .ReplicaSet ). // Avoid complexity of sharded fail points.
527
+ MinServerVersion ("4.0" ). // 4.0 is needed to use replica set fail points.
528
+ ClientOptions (customDeploymentClientOpts ).
529
+ CreateClient (false )
530
+ mt .RunOpts ("custom deployment" , customDeploymentOpts , func (mt * mtest.T ) {
531
+ // Tests for the changeStreamDeployment type. These are written as integration tests for ChangeStream rather
532
+ // than unit/integration tests for changeStreamDeployment to ensure that the deployment is correctly wired
533
+ // by ChangeStream when executing an aggregate.
534
+
535
+ mt .Run ("errors are processed for SDAM on initial aggregate" , func (mt * mtest.T ) {
536
+ clearPoolChan ()
537
+ mt .SetFailPoint (mtest.FailPoint {
538
+ ConfigureFailPoint : "failCommand" ,
539
+ Mode : mtest.FailPointMode {
540
+ Times : 1 ,
541
+ },
542
+ Data : mtest.FailPointData {
543
+ FailCommands : []string {"aggregate" },
544
+ CloseConnection : true ,
545
+ },
546
+ })
547
+
548
+ _ , err := mt .Coll .Watch (mtest .Background , mongo.Pipeline {})
549
+ assert .NotNil (mt , err , "expected Watch error, got nil" )
550
+ assert .True (mt , isPoolCleared (), "expected pool to be cleared after non-timeout network error but was not" )
551
+ })
552
+ mt .Run ("errors are processed for SDAM on getMore" , func (mt * mtest.T ) {
553
+ clearPoolChan ()
554
+ mt .SetFailPoint (mtest.FailPoint {
555
+ ConfigureFailPoint : "failCommand" ,
556
+ Mode : mtest.FailPointMode {
557
+ Times : 1 ,
558
+ },
559
+ Data : mtest.FailPointData {
560
+ FailCommands : []string {"getMore" },
561
+ CloseConnection : true ,
562
+ },
563
+ })
564
+
565
+ cs , err := mt .Coll .Watch (mtest .Background , mongo.Pipeline {})
566
+ assert .Nil (mt , err , "Watch error: %v" , err )
567
+ defer closeStream (cs )
568
+
569
+ _ , err = mt .Coll .InsertOne (mtest .Background , bson.D {{"x" , 1 }})
570
+ assert .Nil (mt , err , "InsertOne error: %v" , err )
571
+
572
+ assert .True (mt , cs .Next (mtest .Background ), "expected Next to return true, got false (iteration error %v)" ,
573
+ cs .Err ())
574
+ assert .True (mt , isPoolCleared (), "expected pool to be cleared after non-timeout network error but was not" )
575
+ })
576
+ retryAggClientOpts := options .Client ().SetRetryReads (true ).SetPoolMonitor (poolMonitor )
577
+ retryAggMtOpts := mtest .NewOptions ().ClientOptions (retryAggClientOpts )
578
+ mt .RunOpts ("errors are processed for SDAM on retried aggregate" , retryAggMtOpts , func (mt * mtest.T ) {
579
+ clearPoolChan ()
580
+
581
+ mt .SetFailPoint (mtest.FailPoint {
582
+ ConfigureFailPoint : "failCommand" ,
583
+ Mode : mtest.FailPointMode {
584
+ Times : 2 ,
585
+ },
586
+ Data : mtest.FailPointData {
587
+ FailCommands : []string {"aggregate" },
588
+ CloseConnection : true ,
589
+ },
590
+ })
591
+
592
+ _ , err := mt .Coll .Watch (mtest .Background , mongo.Pipeline {})
593
+ assert .NotNil (mt , err , "expected Watch error, got nil" )
594
+
595
+ var numClearedEvents int
596
+ for len (poolChan ) > 0 {
597
+ curr := <- poolChan
598
+ if curr .Type == event .PoolCleared {
599
+ numClearedEvents ++
600
+ }
601
+ }
602
+ assert .Equal (mt , 2 , numClearedEvents , "expected two PoolCleared events, got %d" , numClearedEvents )
603
+ })
604
+ })
518
605
}
519
606
520
607
func closeStream (cs * mongo.ChangeStream ) {
0 commit comments