@@ -458,6 +458,119 @@ test_server_description_hello_type_error (void)
458
458
mongoc_server_description_cleanup (& sd );
459
459
}
460
460
461
+ static void
462
+ test_copy (const char * hello_json )
463
+ {
464
+ mongoc_server_description_t sd , * sd_copy ;
465
+ mongoc_server_description_init (& sd , "host:1234" , 1 );
466
+ bson_error_t empty_error = {0 };
467
+ mongoc_server_description_handle_hello (& sd , tmp_bson (hello_json ), 0 , & empty_error );
468
+ sd_copy = mongoc_server_description_new_copy (& sd );
469
+
470
+ // Check server descriptions compare equal by "Server Description Equality" rules. Not all fields are considered.
471
+ ASSERT (_mongoc_server_description_equal (& sd , sd_copy ));
472
+
473
+ // Check all fields:
474
+ ASSERT_CMPUINT32 (sd .id , = = , sd_copy -> id );
475
+ ASSERT_CMPSTR (sd .host .host_and_port , sd_copy -> host .host_and_port );
476
+ ASSERT_CMPINT64 (sd .round_trip_time_msec , = = , sd_copy -> round_trip_time_msec );
477
+ ASSERT_CMPINT64 (sd .last_update_time_usec , = = , sd_copy -> last_update_time_usec );
478
+ ASSERT_EQUAL_BSON (& sd .last_hello_response , & sd_copy -> last_hello_response );
479
+ ASSERT_CMPINT ((int ) sd .has_hello_response , = = , (int ) sd_copy -> has_hello_response );
480
+ ASSERT_CMPINT ((int ) sd .hello_ok , = = , (int ) sd_copy -> hello_ok );
481
+ ASSERT_CMPSTR (sd .connection_address , sd_copy -> connection_address );
482
+ ASSERT_CMPSTR (sd .me , sd_copy -> me );
483
+ ASSERT_CMPINT ((int ) sd .opened , = = , (int ) sd_copy -> opened );
484
+ ASSERT_CMPSTR (sd .set_name , sd_copy -> set_name );
485
+ ASSERT_MEMCMP (& sd .error , & sd_copy -> error , (int ) sizeof (bson_error_t ));
486
+ ASSERT_CMPINT ((int ) sd .type , = = , (int ) sd_copy -> type );
487
+ ASSERT_CMPINT32 (sd .min_wire_version , = = , sd_copy -> min_wire_version );
488
+ ASSERT_CMPINT32 (sd .max_wire_version , = = , sd_copy -> max_wire_version );
489
+ ASSERT_CMPINT32 (sd .max_msg_size , = = , sd_copy -> max_msg_size );
490
+ ASSERT_CMPINT32 (sd .max_bson_obj_size , = = , sd_copy -> max_bson_obj_size );
491
+ ASSERT_CMPINT32 (sd .max_write_batch_size , = = , sd_copy -> max_write_batch_size );
492
+ ASSERT_CMPINT64 (sd .session_timeout_minutes , = = , sd_copy -> session_timeout_minutes );
493
+ ASSERT_EQUAL_BSON (& sd .hosts , & sd_copy -> hosts );
494
+ ASSERT_EQUAL_BSON (& sd .passives , & sd_copy -> passives );
495
+ ASSERT_EQUAL_BSON (& sd .arbiters , & sd_copy -> arbiters );
496
+ ASSERT_EQUAL_BSON (& sd .tags , & sd_copy -> tags );
497
+ ASSERT_CMPSTR (sd .current_primary , sd_copy -> current_primary );
498
+ ASSERT_CMPINT64 (sd .set_version , = = , sd_copy -> set_version );
499
+ ASSERT_MEMCMP (& sd .election_id , & sd_copy -> election_id , (int ) sizeof (bson_oid_t ));
500
+ ASSERT_CMPINT64 (sd .last_write_date_ms , = = , sd_copy -> last_write_date_ms );
501
+ ASSERT_EQUAL_BSON (& sd .compressors , & sd_copy -> compressors );
502
+ ASSERT_EQUAL_BSON (& sd .topology_version , & sd_copy -> topology_version );
503
+ ASSERT_CMPUINT32 (sd .generation , = = , sd_copy -> generation );
504
+ ASSERT (sd_copy -> _generation_map_ != NULL ); // Do not compare entries. Just ensure non-NULL.
505
+ ASSERT_MEMCMP (& sd .service_id , & sd_copy -> service_id , (int ) sizeof (bson_oid_t ));
506
+ ASSERT_CMPINT64 (sd .server_connection_id , = = , sd_copy -> server_connection_id );
507
+
508
+ mongoc_server_description_cleanup (& sd );
509
+ mongoc_server_description_destroy (sd_copy );
510
+ }
511
+
512
+ static void
513
+ test_server_description_copy (void )
514
+ {
515
+ const char * hello_mongod = BSON_STR ({
516
+ "topologyVersion" : {"processId" : {"$oid" : "6792ef87965dee8797402adb" }, "counter" : 6 },
517
+ "hosts" : ["localhost:27017" ],
518
+ "setName" : "rs0" ,
519
+ "setVersion" : 1 ,
520
+ "isWritablePrimary" : true,
521
+ "secondary" : false,
522
+ "primary" : "localhost:27017" ,
523
+ "me" : "localhost:27017" ,
524
+ "electionId" : {"$oid" : "7fffffff0000000000000016" },
525
+ "lastWrite" : {
526
+ "opTime" : {"ts" : {"$timestamp" : {"t" : 1737682844 , "i" : 1 }}, "t" : 22 },
527
+ "lastWriteDate" : {"$date" : "2025-01-24T01:40:44Z" },
528
+ "majorityOpTime" : {"ts" : {"$timestamp" : {"t" : 1737682844 , "i" : 1 }}, "t" : 22 },
529
+ "majorityWriteDate" : {"$date" : "2025-01-24T01:40:44Z" }
530
+ },
531
+ "maxBsonObjectSize" : 16777216 ,
532
+ "maxMessageSizeBytes" : 48000000 ,
533
+ "maxWriteBatchSize" : 100000 ,
534
+ "localTime" : {"$date" : "2025-01-24T01:40:51.968Z" },
535
+ "logicalSessionTimeoutMinutes" : 30 ,
536
+ "connectionId" : 13 ,
537
+ "minWireVersion" : 0 ,
538
+ "maxWireVersion" : 25 ,
539
+ "readOnly" : false,
540
+ "ok" : 1.0 ,
541
+ "$clusterTime" : {
542
+ "clusterTime" : {"$timestamp" : {"t" : 1737682844 , "i" : 1 }},
543
+ "signature" :
544
+ {"hash" : {"$binary" : {"base64" : "AAAAAAAAAAAAAAAAAAAAAAAAAAA = ", " subType " : " 00 "}}, " keyId " : 0}
545
+ },
546
+ " operationTime " : {" $timestamp " : {" t " : 1737682844, " i " : 1}}
547
+ });
548
+
549
+ const char * hello_mongos = BSON_STR ({
550
+ "isWritablePrimary" : true,
551
+ "msg" : "isdbgrid ",
552
+ " topologyVersion " : {" processId " : {" $oid " : " 6791 af1181771f367602ec40 "}, " counter " : 0},
553
+ " maxBsonObjectSize " : 16777216,
554
+ " maxMessageSizeBytes " : 48000000,
555
+ " maxWriteBatchSize " : 100000,
556
+ " localTime " : {" $date " : " 2025 -01 - 24 T01 :24 :57.217 Z "},
557
+ " logicalSessionTimeoutMinutes " : 30,
558
+ " connectionId " : 3310,
559
+ " maxWireVersion " : 25,
560
+ " minWireVersion " : 0,
561
+ " ok " : 1.0,
562
+ " $clusterTime " : {
563
+ " clusterTime " : {" $timestamp " : {" t " : 1737681896, " i " : 1}},
564
+ " signature " :
565
+ {" hash " : {" $binary " : {" base64 " : " AAAAAAAAAAAAAAAAAAAAAAAAAAA = ", " subType " : " 00 "}}, " keyId " : 0}
566
+ },
567
+ " operationTime " : {" $timestamp " : {" t " : 1737681896, " i " : 1}}
568
+ });
569
+
570
+ test_copy (hello_mongod );
571
+ test_copy (hello_mongos );
572
+ }
573
+
461
574
void
462
575
test_server_description_install (TestSuite * suite )
463
576
{
@@ -470,4 +583,5 @@ test_server_description_install (TestSuite *suite)
470
583
TestSuite_Add (suite , "/server_description/legacy_hello_ok" , test_server_description_legacy_hello_ok );
471
584
TestSuite_Add (suite , "/server_description/connection_id" , test_server_description_connection_id );
472
585
TestSuite_Add (suite , "/server_description/hello_type_error" , test_server_description_hello_type_error );
586
+ TestSuite_Add (suite , "/server_description/copy" , test_server_description_copy );
473
587
}
0 commit comments