summaryrefslogtreecommitdiff
path: root/src/mongo/s/multi_host_query_test.cpp
blob: 5c98bb0519e041235957b1f444d6d79d445d52b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/**
 *    Copyright (C) 2013 MongoDB Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/s/multi_host_query.h"

#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>

#include "mongo/unittest/unittest.h"
#include "mongo/util/concurrency/synchronization.h"

namespace {

    using namespace mongo;
    using boost::scoped_ptr;
    using boost::shared_ptr;
    using std::make_pair;
    using std::map;
    using std::string;
    using std::vector;

    class CallbackCheck {
    public:

        enum LinkMode {
            None, Notify_Other, Wait_For_Other
        };

        CallbackCheck() :
            _status(ErrorCodes::OperationIncomplete, ""), _linkMode(None) {
        }

        void blockUntil(CallbackCheck* other) {

            _otherNotification.reset(new Notification);
            _linkMode = Wait_For_Other;

            other->_otherNotification = _otherNotification;
            other->_linkMode = Notify_Other;
        }

        HostThreadPool::Callback getCallback() {
            return stdx::bind(&CallbackCheck::noteCallback, this);
        }

        HostThreadPool::Callback getHostCallback(const ConnectionString& host) {
            return stdx::bind(&CallbackCheck::noteHostCallback, this, host);
        }

        void noteHostCallback(const ConnectionString& host) {
            _host = host;
            noteCallback();
        }

        void noteCallback() {

            _status = Status::OK();
            _notification.notifyOne();

            if (_linkMode == Wait_For_Other)
                _otherNotification->waitToBeNotified();
            else if (_linkMode == Notify_Other) {
                _otherNotification->notifyOne();
            }
        }

        void waitForCallback() {
            _notification.waitToBeNotified();
        }

        Status getStatus() {
            return _status;
        }

        const ConnectionString& getHost() {
            return _host;
        }

    private:

        Status _status;
        Notification _notification;

        ConnectionString _host;

        shared_ptr<Notification> _otherNotification;
        LinkMode _linkMode;
    };

    TEST(HostThreadPool, Schedule) {

        CallbackCheck cbCheck;

        // NOTE: pool must be initialized *after* the cbCheck that it executes - this avoids a
        // subtle race where the cbCheck structure is disposed before the callback is complete.
        HostThreadPool threadPool(1, true);

        threadPool.schedule(cbCheck.getCallback());

        cbCheck.waitForCallback();
        ASSERT_OK(cbCheck.getStatus());
    }

    TEST(HostThreadPool, ScheduleTwoSerial) {

        CallbackCheck cbCheckA;
        CallbackCheck cbCheckB;

        // NOTE: pool must be initialized *after* the cbCheck that it executes
        HostThreadPool threadPool(1, true);

        threadPool.schedule(cbCheckA.getCallback());
        threadPool.schedule(cbCheckB.getCallback());

        cbCheckB.waitForCallback();
        cbCheckA.waitForCallback();

        ASSERT_OK(cbCheckA.getStatus());
        ASSERT_OK(cbCheckB.getStatus());
    }

    TEST(HostThreadPool, ScheduleTwoParallel) {

        CallbackCheck cbCheckA;
        CallbackCheck cbCheckB;

        // NOTE: pool must be initialized *after* the cbCheck that it executes
        HostThreadPool threadPool(2, true);

        // Don't allow cbCheckA's callback to finish until cbCheckB's callback is processed
        cbCheckA.blockUntil(&cbCheckB);

        threadPool.schedule(cbCheckA.getCallback());
        cbCheckA.waitForCallback();
        ASSERT_OK(cbCheckA.getStatus());
        // We're still blocking the thread processing cbCheckA's callback

        threadPool.schedule(cbCheckB.getCallback());
        cbCheckB.waitForCallback();
        ASSERT_OK(cbCheckB.getStatus());
    }

    TEST(HostThreadPool, ScheduleTwoHosts) {

        CallbackCheck cbCheckA;
        CallbackCheck cbCheckB;

        // NOTE: pool must be initialized *after* the cbCheck that it executes
        HostThreadPools threadPool(1, true);

        // Don't allow cbCheckA's callback to finish until cbCheckB's callback is processed.
        // This means a single thread pool with a single thread would hang.
        cbCheckA.blockUntil(&cbCheckB);

        ConnectionString hostA = ConnectionString::mock(HostAndPort("$hostA:1000"));
        ConnectionString hostB = ConnectionString::mock(HostAndPort("$hostB:1000"));

        threadPool.schedule(hostA, cbCheckA.getHostCallback(hostA));
        cbCheckA.waitForCallback();
        ASSERT_OK(cbCheckA.getStatus());
        ASSERT_EQUALS(cbCheckA.getHost().toString(), hostA.toString());
        // We're still blocking the thread processing cbCheckA's callback

        threadPool.schedule(hostB, cbCheckB.getHostCallback(hostB));
        cbCheckB.waitForCallback();
        ASSERT_OK(cbCheckB.getStatus());
        ASSERT_EQUALS(cbCheckB.getHost().toString(), hostB.toString());
    }

    class MockSystemEnv : public MultiHostQueryOp::SystemEnv {
    private:

        struct MockHostInfo;
        typedef map<ConnectionString, MockHostInfo*> HostInfoMap;

    public:

        MockSystemEnv(HostThreadPools* threadPool) :
            _threadPool(threadPool), _mockTimeMillis(0) {
        }

        virtual ~MockSystemEnv() {
            for (HostInfoMap::iterator it = _mockHostInfo.begin(); it != _mockHostInfo.end();
                ++it) {
                if (_threadPool)
                    _threadPool->waitUntilIdle(it->first);
                delete it->second;
            }
        }

        void setHostThreadPools(HostThreadPools* threadPool) {
            _threadPool = threadPool;
        }

        void addMockHostResultAt(const ConnectionString& host, int timeMillis) {
            newMockHostResultAt(host, timeMillis, Status::OK(), NULL);
        }

        void addMockHostErrorAt(const ConnectionString& host, int timeMillis, Status error) {
            newMockHostResultAt(host, timeMillis, error, NULL);
        }

        void addMockHungHostAt(const ConnectionString& host,
                               int hangTimeMillis,
                               Notification* hangUntilNotify) {
            newMockHostResultAt(host, hangTimeMillis, Status::OK(), hangUntilNotify);
        }

        void addMockTimestepAt(int timeMillis) {

            // Add a mock query to a host we aren't using at the provided time
            ConnectionString host = ConnectionString::mock(HostAndPort("$timestepHost:1000"));
            newMockHostResultAt(host, timeMillis, Status::OK(), NULL);

            // The query won't be scheduled by the multi op, so we need to do so ourselves
            _threadPool->schedule(host,
                                  stdx::bind(&MockSystemEnv::doBlockingQuerySwallowResult,
                                              this,
                                              host,
                                              QuerySpec()));
        }

        Date_t currentTimeMillis() {
            return _mockTimeMillis;
        }

        void doBlockingQuerySwallowResult(const ConnectionString& host,
                                          const QuerySpec& query) {
            StatusWith<DBClientCursor*> result = doBlockingQuery(host, query);
            if (result.isOK())
                delete result.getValue();
        }

        StatusWith<DBClientCursor*> doBlockingQuery(const ConnectionString& host,
                                                    const QuerySpec& query) {

            ASSERT(_mockHostInfo.find(host) != _mockHostInfo.end());

            MockHostInfo& info = *(_mockHostInfo.find(host)->second);

            if (info.prevHostActiveNotify) {
                info.prevHostActiveNotify->waitToBeNotified();
                if (info.waitForPrevHostIdle) {
                    _threadPool->waitUntilIdle(info.prevHost);
                }
            }

            _mockTimeMillis = info.queryTimeMillis;

            if (info.nextHostActiveNotify) {
                info.nextHostActiveNotify->notifyOne();
            }

            if (info.hangUntilNotify) {
                info.hangUntilNotify->waitToBeNotified();
                return StatusWith<DBClientCursor*>(ErrorCodes::InternalError, "");
            }

            if (!info.error.isOK()) {
                return StatusWith<DBClientCursor*>(info.error);
            }

            //
            // Successful mock query
            //

            if (!info.conn) {
                info.conn.reset(new DBClientConnection(false));
                // Need to do a connect failure so that we get an empty MessagingPort on the conn and
                // the host name is set.
                string errMsg;
                ASSERT(!info.conn->connect(HostAndPort(host.toString()), errMsg));
            }
            
            return StatusWith<DBClientCursor*>(new DBClientCursor(info.conn.get(),
                                                                  query.ns(),
                                                                  query.query(),
                                                                  query.ntoreturn(),
                                                                  query.ntoskip(),
                                                                  query.fieldsPtr(),
                                                                  query.options(),
                                                                  0 /* batchSize */));
        }

    private:

        MockHostInfo* newMockHostResultAt(const ConnectionString& host,
                                          int timeMillis,
                                          const Status& error,
                                          Notification* hangUntilNotify) {

            ASSERT(_mockHostInfo.find(host) == _mockHostInfo.end());

            MockHostInfo* info = new MockHostInfo(timeMillis);
            _mockHostInfo.insert(make_pair(host, info));
            info->error = error;
            info->hangUntilNotify = hangUntilNotify;

            linkMockTimes(host, info);
            return info;
        }

        void linkMockTimes(const ConnectionString& host, MockHostInfo* info) {

            //
            // This just basically sets up notifications between the processing of results such that
            // the results are returned in the order defined by the _mockQueryTimes map.
            //
            // Idea is (second host result) waits for (first host result) thread to start and end,
            //         (third host result) waits for (second host result) thread to start and end,
            //         (fourth host result) waits for (third host result) thread to start and end,
            //         ... and so on ...
            //

            ASSERT(_mockQueryTimes.find(info->queryTimeMillis) == _mockQueryTimes.end());

            HostQueryTimes::iterator prev = _mockQueryTimes.insert(make_pair(info->queryTimeMillis,
                                                                             host)).first;

            if (prev != _mockQueryTimes.begin())
                --prev;
            else
                prev = _mockQueryTimes.end();

            HostQueryTimes::iterator next = _mockQueryTimes.upper_bound(info->queryTimeMillis);

            if (prev != _mockQueryTimes.end()) {

                const ConnectionString& prevHost = prev->second;
                MockHostInfo* prevInfo = _mockHostInfo.find(prevHost)->second;

                linkToNext(prevHost, prevInfo, info);
            }

            if (next != _mockQueryTimes.end()) {

                const ConnectionString& nextHost = next->second;
                MockHostInfo* nextInfo = _mockHostInfo.find(nextHost)->second;

                linkToNext(host, info, nextInfo);
            }
        }

        void linkToNext(const ConnectionString& host, MockHostInfo* info, MockHostInfo* nextInfo) {

            nextInfo->prevHost = host;

            nextInfo->prevHostActiveNotify.reset(new Notification());
            info->nextHostActiveNotify = nextInfo->prevHostActiveNotify.get();

            nextInfo->waitForPrevHostIdle = info->hangUntilNotify == NULL;
        }

        // Not owned here, needed to allow ordering of mock queries
        HostThreadPools* _threadPool;

        int _mockTimeMillis;

        typedef map<int, ConnectionString> HostQueryTimes;
        HostQueryTimes _mockQueryTimes;

        struct MockHostInfo {

            MockHostInfo(int queryTimeMillis) :
                nextHostActiveNotify( NULL),
                waitForPrevHostIdle(false),
                queryTimeMillis(queryTimeMillis),
                hangUntilNotify( NULL),
                error(Status::OK()) {
            }

            Notification* nextHostActiveNotify;

            ConnectionString prevHost;
            scoped_ptr<Notification> prevHostActiveNotify;
            bool waitForPrevHostIdle;

            int queryTimeMillis;

            scoped_ptr<DBClientConnection> conn;
            Notification* hangUntilNotify;
            Status error;
        };

        HostInfoMap _mockHostInfo;

    };

    QuerySpec buildSpec(const StringData& ns, const BSONObj& query) {
        return QuerySpec(ns.toString(), query, BSONObj(), 0, 0, 0);
    }

    //
    // Tests for the MultiHostQueryOp
    //

    TEST(MultiHostQueryOp, SingleHost) {

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString host = ConnectionString::mock(HostAndPort("$host:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(host);

        mockSystem.addMockHostResultAt(host, 1000);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 2000);

        ASSERT_OK(result.getStatus());
        ASSERT(NULL != result.getValue());
        ASSERT_EQUALS(result.getValue()->originalHost(), host.toString());
        delete result.getValue();
    }

    TEST(MultiHostQueryOp, SingleHostError) {

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString host = ConnectionString::mock(HostAndPort("$host:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(host);

        Status hostError = Status(ErrorCodes::InternalError, "");
        mockSystem.addMockHostErrorAt(host, 1000, hostError);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 2000);

        ASSERT_EQUALS(result.getStatus().code(), hostError.code());
    }

    TEST(MultiHostQueryOp, SingleHostHang) {

        // Initialize notifier before the thread pool, otherwise we may dispose while threads are
        // active
        Notification unhangNotify;

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString host = ConnectionString::mock(HostAndPort("$host:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(host);

        mockSystem.addMockHungHostAt(host, 4000, &unhangNotify);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 2000);
        // Unhang before checking status, in case it throws
        unhangNotify.notifyOne();

        ASSERT_EQUALS(result.getStatus().code(), ErrorCodes::NetworkTimeout);
    }

    TEST(MultiHostQueryOp, TwoHostResponses) {

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString hostA = ConnectionString::mock(HostAndPort("$hostA:1000"));
        ConnectionString hostB = ConnectionString::mock(HostAndPort("$hostB:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(hostA);
        hosts.push_back(hostB);

        // Make sure we return the first response, from hostB at time 1000
        mockSystem.addMockHostResultAt(hostA, 2000);
        mockSystem.addMockHostResultAt(hostB, 1000);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 3000);

        ASSERT_OK(result.getStatus());
        ASSERT(NULL != result.getValue());
        ASSERT_EQUALS(result.getValue()->originalHost(), hostB.toString());
        delete result.getValue();
    }

    TEST(MultiHostQueryOp, TwoHostsOneErrorResponse) {

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString hostA = ConnectionString::mock(HostAndPort("$hostA:1000"));
        ConnectionString hostB = ConnectionString::mock(HostAndPort("$hostB:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(hostA);
        hosts.push_back(hostB);

        // The first response is a host error, the second is a successful result
        Status hostError = Status(ErrorCodes::InternalError, "");
        mockSystem.addMockHostErrorAt(hostA, 1000, hostError);
        mockSystem.addMockHostResultAt(hostB, 2000);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 3000);

        ASSERT_OK(result.getStatus());
        ASSERT(NULL != result.getValue());
        ASSERT_EQUALS(result.getValue()->originalHost(), hostB.toString());
        delete result.getValue();
    }

    TEST(MultiHostQueryOp, TwoHostsBothErrors) {

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString hostA = ConnectionString::mock(HostAndPort("$hostA:1000"));
        ConnectionString hostB = ConnectionString::mock(HostAndPort("$hostB:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(hostA);
        hosts.push_back(hostB);

        // Both responses are errors
        Status hostError = Status(ErrorCodes::InternalError, "");
        mockSystem.addMockHostErrorAt(hostA, 1000, hostError);
        mockSystem.addMockHostErrorAt(hostB, 2000, hostError);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 3000);

        ASSERT_EQUALS(result.getStatus().code(), ErrorCodes::MultipleErrorsOccurred);
    }

    TEST(MultiHostQueryOp, TwoHostsOneHang) {

        // Initialize notifier before the thread pool
        Notification unhangNotify;

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString hostA = ConnectionString::mock(HostAndPort("$hostA:1000"));
        ConnectionString hostB = ConnectionString::mock(HostAndPort("$hostB:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(hostA);
        hosts.push_back(hostB);

        // One host hangs
        mockSystem.addMockHungHostAt(hostA, 1000, &unhangNotify);
        mockSystem.addMockHostResultAt(hostB, 2000);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 3000);
        // Unhang before checking status, in case it throws
        unhangNotify.notifyOne();

        ASSERT_OK(result.getStatus());
        ASSERT(NULL != result.getValue());
        ASSERT_EQUALS(result.getValue()->originalHost(), hostB.toString());
        delete result.getValue();
    }

    TEST(MultiHostQueryOp, TwoHostsOneHangOneError) {

        // Initialize notifier before the thread pool
        Notification unhangNotify;

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString hostA = ConnectionString::mock(HostAndPort("$hostA:1000"));
        ConnectionString hostB = ConnectionString::mock(HostAndPort("$hostB:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(hostA);
        hosts.push_back(hostB);

        // One host hangs, one host has an error (at the mock timeout point so the query finishes)
        Status hostError = Status(ErrorCodes::InternalError, "");
        mockSystem.addMockHungHostAt(hostA, 1000, &unhangNotify);
        mockSystem.addMockHostErrorAt(hostB, 3000, hostError);
        mockSystem.addMockTimestepAt(4000);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 4000);
        // Unhang before checking status, in case it throws
        unhangNotify.notifyOne();

        ASSERT_EQUALS(result.getStatus().code(), hostError.code());
    }

    TEST(MultiHostQueryOp, ThreeHostsOneHang) {

        // Initialize notifier before the thread pool
        Notification unhangNotify;

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString hostA = ConnectionString::mock(HostAndPort("$hostA:1000"));
        ConnectionString hostB = ConnectionString::mock(HostAndPort("$hostB:1000"));
        ConnectionString hostC = ConnectionString::mock(HostAndPort("$hostC:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(hostA);
        hosts.push_back(hostB);
        hosts.push_back(hostC);

        // One host hangs, last host is fastest with result
        mockSystem.addMockHungHostAt(hostA, 1000, &unhangNotify);
        mockSystem.addMockHostResultAt(hostB, 3000);
        mockSystem.addMockHostResultAt(hostC, 2000);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 4000);
        // Unhang before checking status, in case it throws
        unhangNotify.notifyOne();

        ASSERT_OK(result.getStatus());
        ASSERT(NULL != result.getValue());
        ASSERT_EQUALS(result.getValue()->originalHost(), hostC.toString());
        delete result.getValue();
    }

    TEST(MultiHostQueryOp, ThreeHostsTwoErrors) {

        // Initialize notifier before the thread pool
        Notification unhangNotify;

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString hostA = ConnectionString::mock(HostAndPort("$hostA:1000"));
        ConnectionString hostB = ConnectionString::mock(HostAndPort("$hostB:1000"));
        ConnectionString hostC = ConnectionString::mock(HostAndPort("$hostC:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(hostA);
        hosts.push_back(hostB);
        hosts.push_back(hostC);

        // One host hangs, two hosts have errors (finish at mock timeout point so query ends)
        Status hostError = Status(ErrorCodes::InternalError, "");
        mockSystem.addMockHungHostAt(hostA, 1000, &unhangNotify);
        mockSystem.addMockHostErrorAt(hostB, 4000, hostError);
        mockSystem.addMockHostErrorAt(hostC, 2000, hostError);
        mockSystem.addMockTimestepAt(5000);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 5000);
        // Unhang before checking status, in case it throws
        unhangNotify.notifyOne();

        ASSERT_EQUALS(result.getStatus().code(), ErrorCodes::MultipleErrorsOccurred);
    }

    TEST(MultiHostQueryOp, ThreeHostsOneHangOneError) {

        // Initialize notifier before the thread pool
        Notification unhangNotify;

        HostThreadPools threadPool(1, true);
        MockSystemEnv mockSystem(&threadPool);

        ConnectionString hostA = ConnectionString::mock(HostAndPort("$hostA:1000"));
        ConnectionString hostB = ConnectionString::mock(HostAndPort("$hostB:1000"));
        ConnectionString hostC = ConnectionString::mock(HostAndPort("$hostC:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(hostA);
        hosts.push_back(hostB);
        hosts.push_back(hostC);

        // One host hangs, two hosts have errors (finish at mock timeout point so query ends)
        Status hostError = Status(ErrorCodes::InternalError, "");
        mockSystem.addMockHungHostAt(hostA, 1000, &unhangNotify);
        mockSystem.addMockHostErrorAt(hostB, 2000, hostError);
        mockSystem.addMockHostResultAt(hostC, 3000);

        MultiHostQueryOp queryOp(&mockSystem, &threadPool);

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 4000);
        // Unhang before checking status, in case it throws
        unhangNotify.notifyOne();

        ASSERT_OK(result.getStatus());
        ASSERT(NULL != result.getValue());
        ASSERT_EQUALS(result.getValue()->originalHost(), hostC.toString());
        delete result.getValue();
    }

    TEST(MultiHostQueryOp, TwoHostsOneHangUnscoped) {

        // Initialize notifier before the thread pool
        Notification unhangNotify;

        // Create a thread pool which detaches itself from outstanding work on cleanup
        scoped_ptr<HostThreadPools> threadPool(new HostThreadPools(1, false));
        MockSystemEnv mockSystem(threadPool.get());

        ConnectionString hostA = ConnectionString::mock(HostAndPort("$hostA:1000"));
        ConnectionString hostB = ConnectionString::mock(HostAndPort("$hostB:1000"));
        vector<ConnectionString> hosts;
        hosts.push_back(hostA);
        hosts.push_back(hostB);

        // One host hangs
        mockSystem.addMockHungHostAt(hostA, 1000, &unhangNotify);
        mockSystem.addMockHostResultAt(hostB, 2000);

        MultiHostQueryOp queryOp(&mockSystem, threadPool.get());

        QuerySpec query;
        StatusWith<DBClientCursor*> result = queryOp.queryAny(hosts, query, 3000);

        // Clean up the thread pool
        mockSystem.setHostThreadPools( NULL);
        threadPool.reset();

        // Unhang before checking status, in case it throws
        unhangNotify.notifyOne();

        ASSERT_OK(result.getStatus());
        ASSERT(NULL != result.getValue());
        ASSERT_EQUALS(result.getValue()->originalHost(), hostB.toString());
        delete result.getValue();

        // Make sure we get the next result
        result = queryOp.waitForNextResult(4000);

        ASSERT_EQUALS(result.getStatus().code(), ErrorCodes::InternalError);
    }

} // unnamed namespace