@@ -386,6 +386,72 @@ namespace {
386386 ASSERT_EQUALS (topocoord.getSyncSourceAddress (),HostAndPort (" h3" ));
387387 }
388388
389+ TEST (TopologyCoordinator, PrepareSyncFromResponse) {
390+ ReplicationExecutor::CallbackHandle cbh;
391+ ReplicationExecutor::CallbackData cbData (NULL ,
392+ cbh,
393+ Status::OK ());
394+ ReplicaSetConfig config;
395+
396+ ASSERT_OK (config.initialize (BSON (" _id" << " rs0" <<
397+ " version" << 1 <<
398+ " members" << BSON_ARRAY (
399+ BSON (" _id" << 10 << " host" << " hself" ) <<
400+ BSON (" _id" << 20 << " host" << " h1" ) <<
401+ BSON (" _id" << 30 << " host" << " h2" ) <<
402+ BSON (" _id" << 40 << " host" << " h3" )))));
403+
404+ OpTime staleOpTime (1 , 1 );
405+ OpTime ourOpTime (staleOpTime.getSecs () + 11 , 1 );
406+
407+
408+ TopologyCoordinatorImpl topocoord ((Seconds (999 )));
409+ Date_t now = 0 ;
410+ topocoord.updateConfig (cbData, config, 0 , now++, OpTime (0 ,0 ));
411+
412+ MemberHeartbeatData hselfInfo (0 );
413+ hselfInfo.setAuthIssue ();
414+ topocoord.updateHeartbeatData (now++, hselfInfo, 10 , OpTime (0 ,0 ));
415+
416+ MemberHeartbeatData h1Info (1 );
417+ h1Info.setDownValues (now, " " );
418+ topocoord.updateHeartbeatData (now++, h1Info, 20 , OpTime (0 ,0 ));
419+
420+ MemberHeartbeatData h2Info (2 );
421+ h2Info.setUpValues (now, MemberState::RS_SECONDARY, OpTime (0 ,0 ), staleOpTime, " " , " " );
422+ topocoord.updateHeartbeatData (now++, h2Info, 30 , OpTime (0 ,0 ));
423+
424+ MemberHeartbeatData h3Info (2 );
425+ h3Info.setUpValues (now, MemberState::RS_SECONDARY, OpTime (0 ,0 ), ourOpTime, " " , " " );
426+ topocoord.updateHeartbeatData (now++, h3Info, 40 , OpTime (0 ,0 ));
427+
428+ Status result = Status::OK ();
429+ BSONObjBuilder response0;
430+ topocoord.prepareSyncFromResponse (cbData, 0 , ourOpTime, &response0, &result);
431+ ASSERT_EQUALS (ErrorCodes::Unauthorized, result);
432+
433+ BSONObjBuilder response1;
434+ topocoord.prepareSyncFromResponse (cbData, 1 , ourOpTime, &response1, &result);
435+ ASSERT_EQUALS (ErrorCodes::HostUnreachable, result);
436+
437+ BSONObjBuilder response2;
438+ topocoord.prepareSyncFromResponse (cbData, 2 , ourOpTime, &response2, &result);
439+ ASSERT_OK (result);
440+ topocoord.chooseNewSyncSource (now++, ourOpTime);
441+ ASSERT_EQUALS (topocoord.getSyncSourceAddress (), HostAndPort (" h2" ));
442+ ASSERT_EQUALS (" requested member \" h2:27017\" is more than 10 seconds behind us" ,
443+ response2.obj ()[" warning" ].String ());
444+
445+ BSONObjBuilder response3;
446+ topocoord.prepareSyncFromResponse (cbData, 3 , ourOpTime, &response3, &result);
447+ ASSERT_OK (result);
448+ topocoord.chooseNewSyncSource (now++, ourOpTime);
449+ ASSERT_EQUALS (topocoord.getSyncSourceAddress (), HostAndPort (" h3" ));
450+ BSONObj response3Obj = response3.obj ();
451+ ASSERT_FALSE (response3Obj.hasField (" warning" ));
452+ ASSERT_EQUALS (HostAndPort (" h2" ).toString (), response3Obj[" prevSyncTarget" ].String ());
453+ }
454+
389455 TEST (TopologyCoordinator, ReplSetGetStatus) {
390456 // This test starts by configuring a TopologyCoordinator as a member of a 4 node replica
391457 // set, with each node in a different state.
0 commit comments