@@ -76,6 +76,9 @@ class WaitForMajorityServiceTest : public ServiceContextTest {
7676 Status waitForWriteConcernStub (OperationContext* opCtx, const repl::OpTime& opTime) {
7777 stdx::unique_lock<stdx::mutex> lk (_mutex);
7878
79+ _waitForMajorityCallCount++;
80+ _callCountChangedCV.notify_one ();
81+
7982 while (!_isTestReady) {
8083 auto status = opCtx->waitForConditionOrInterruptNoAssert (_isTestReadyCV, lk);
8184 if (!status.isOK ()) {
@@ -98,15 +101,22 @@ class WaitForMajorityServiceTest : public ServiceContextTest {
98101 return _lastOpTimeWaited;
99102 }
100103
104+ void waitForMajorityCallCountGreaterThan (int expectedCount) {
105+ stdx::unique_lock lk (_mutex);
106+ _callCountChangedCV.wait (lk, [&] { return _waitForMajorityCallCount > expectedCount; });
107+ }
108+
101109private:
102110 WaitForMajorityService _waitForMajorityService;
103111
104112 stdx::mutex _mutex;
105113 stdx::condition_variable _isTestReadyCV;
106114 stdx::condition_variable _finishWaitingOneOpTimeCV;
115+ stdx::condition_variable _callCountChangedCV;
107116
108117 bool _isTestReady{false };
109118 repl::OpTime _lastOpTimeWaited;
119+ int _waitForMajorityCallCount{0 };
110120};
111121
112122TEST_F (WaitForMajorityServiceTest, WaitOneOpTime) {
@@ -144,8 +154,17 @@ TEST_F(WaitForMajorityServiceTest, WaitWithOpTimeEarlierThanLowestQueued) {
144154 repl::OpTime earlierOpTime (Timestamp (1 , 0 ), 2 );
145155
146156 auto laterFuture = waitService ()->waitUntilMajority (laterOpTime);
157+
158+ // Wait until the background thread picks up the queued opTime.
159+ waitForMajorityCallCountGreaterThan (0 );
160+
161+ // The 2nd request has an earlier time, so it will interrupt 'laterOpTime' and skip the line.
147162 auto earlierFuture = waitService ()->waitUntilMajority (earlierOpTime);
148163
164+ // Wait for background thread to finish transitioning from waiting on laterOpTime to
165+ // earlierOpTime.
166+ waitForMajorityCallCountGreaterThan (1 );
167+
149168 ASSERT_FALSE (laterFuture.isReady ());
150169 ASSERT_FALSE (earlierFuture.isReady ());
151170
0 commit comments