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