summaryrefslogtreecommitdiff
path: root/src/mongo/transport/service_executor_test.cpp
blob: ca5a2427b2ab5a710fd66626bb2c2473d30a94fa (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
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    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
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    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 Server Side 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/platform/basic.h"

#include "boost/optional.hpp"
#include <algorithm>
#include <asio.hpp>

#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/db/service_context.h"
#include "mongo/db/service_context_test_fixture.h"
#include "mongo/logv2/log.h"
#include "mongo/transport/mock_session.h"
#include "mongo/transport/service_executor_fixed.h"
#include "mongo/transport/service_executor_gen.h"
#include "mongo/transport/service_executor_synchronous.h"
#include "mongo/transport/transport_layer.h"
#include "mongo/transport/transport_layer_mock.h"
#include "mongo/unittest/assert_that.h"
#include "mongo/unittest/barrier.h"
#include "mongo/unittest/death_test.h"
#include "mongo/unittest/matcher.h"
#include "mongo/unittest/thread_assertion_monitor.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/concurrency/notification.h"
#include "mongo/util/concurrency/thread_pool.h"
#include "mongo/util/fail_point.h"
#include "mongo/util/future.h"
#include "mongo/util/scopeguard.h"
#include "mongo/util/synchronized_value.h"

#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kTest


namespace mongo::transport {
namespace {

namespace m = unittest::match;

using unittest::stringify::stringifyForAssert;

constexpr auto kWorkerThreadRunTime = Milliseconds{1000};
// Run time + generous scheduling time slice
constexpr auto kShutdownTime = Milliseconds{kWorkerThreadRunTime.count() + 50};

// Fibonacci generator for slow integer-valued exponential backoff.
template <typename T>
auto fibGenerator() {
    return [seq = std::array{T{0}, T{1}}]() mutable {
        auto r = seq[0];
        seq[0] = seq[1];
        seq[1] = r + seq[0];
        return r;
    };
}

template <typename Pred>
auto pollUntil(const Pred& pred, Milliseconds timeout) {
    auto fib = fibGenerator<Milliseconds>();
    while (true) {
        if (auto r = pred(); r || timeout == Milliseconds{0})
            return r;
        auto zzz = std::min(fib(), timeout);
        timeout -= zzz;
        sleepFor(zzz);
    }
}

template <typename M>
class AtomicWordLoadIs : public m::Matcher {
public:
    explicit AtomicWordLoadIs(M m) : _m{std::move(m)} {}

    std::string describe() const {
        return "AtomicWordLoadIs({})"_format(_m.describe());
    }

    template <typename T>
    m::MatchResult match(const T& x) const {
        auto r = x.load();
        if (auto mr = _m.match(r); !mr)
            return {false,
                    "{} failed {}{}"_format(stringifyForAssert(r), _m.describe(), mr.message())};
        return {};
    }

private:
    M _m;
};

/** Matches a ServiceExecutor or the BSONObj it produces with its `appendStats`. */
template <typename ThreadMatch, typename ClientMatch>
class ExecStatsIs : public m::Matcher {
public:
    ExecStatsIs(std::string execStatsLabel, ThreadMatch tm, ClientMatch cm)
        : _execStatsLabel{std::move(execStatsLabel)}, _tm{std::move(tm)}, _cm{std::move(cm)} {}

    std::string describe() const {
        return "ExecStatsIs({},{})"_format(_tm.describe(), _cm.describe());
    }

    m::MatchResult match(const BSONObj& x) const {
        unittest::stringify::Joiner joiner;
        bool ok = true;
        auto obj = x[_execStatsLabel].Obj();

        auto tIn = obj["threadsRunning"].Int();
        if (auto tmr = _tm.match(tIn); !tmr) {
            joiner("threadsRunning={} failed {}{}"_format(
                stringifyForAssert(tIn), _tm.describe(), tmr.message()));
            ok = false;
        }

        auto cIn = obj["clientsInTotal"].Int();
        if (auto cmr = _cm.match(cIn); !cmr) {
            joiner("clientsInTotal={} failed {}{}"_format(
                stringifyForAssert(cIn), _cm.describe(), cmr.message()));
            ok = false;
        }
        return {ok, std::string{joiner}};
    }

    m::MatchResult match(const ServiceExecutor& exec) const {
        BSONObjBuilder bob;
        exec.appendStats(&bob);
        BSONObj obj = bob.done();
        if (auto mr = match(obj); !mr)
            return {false, "obj={}, message={}"_format(obj.toString(), mr.message())};
        return {};
    }

private:
    std::string _execStatsLabel;
    ThreadMatch _tm;
    ClientMatch _cm;
};

/**
 * Match is re-evaluated repeatedly with an exponential backoff, up to some
 * limit, at which time this enclosing matcher fails.
 */
template <typename M>
class SoonMatches : public m::Matcher {
public:
    explicit SoonMatches(M&& m, Milliseconds timeout = Seconds{5})
        : _m{std::forward<M>(m)}, _timeout{timeout} {}

    std::string describe() const {
        return "SoonMatches({},{})"_format(_m.describe(), _timeout.toString());
    }

    template <typename X>
    m::MatchResult match(const X& x) const {
        auto mr = pollUntil([&] { return _m.match(x); }, _timeout);
        if (mr)
            return mr;
        return {false, "No result matched after {}: {}"_format(_timeout.toString(), mr.message())};
    }

private:
    M _m;
    Milliseconds _timeout;
};

class JoinThread : public stdx::thread {
public:
    using stdx::thread::thread;
    ~JoinThread() {
        if (joinable())
            join();
    }
};

/* This implements the portions of the transport::Reactor based on ASIO, but leaves out
 * the methods not needed by ServiceExecutors.
 *
 * TODO Maybe use AsioTransportLayer's Reactor?
 */
class AsioReactor : public transport::Reactor {
public:
    AsioReactor() : _ioContext() {}

    void run() noexcept final {
        MONGO_UNREACHABLE;
    }

    void runFor(Milliseconds time) noexcept final {
        asio::io_context::work work(_ioContext);

        try {
            _ioContext.run_for(time.toSystemDuration());
        } catch (...) {
            LOGV2_FATAL(50476,
                        "Uncaught exception in reactor: {error}",
                        "Uncaught exception in reactor",
                        "error"_attr = exceptionToStatus());
        }
    }

    void stop() final {
        _ioContext.stop();
    }

    void drain() override final {
        _ioContext.restart();
        while (_ioContext.poll()) {
            LOGV2_DEBUG(22984, 1, "Draining remaining work in reactor.");
        }
        _ioContext.stop();
    }

    std::unique_ptr<ReactorTimer> makeTimer() final {
        MONGO_UNREACHABLE;
    }

    Date_t now() final {
        MONGO_UNREACHABLE;
    }

    void schedule(Task task) final {
        asio::post(_ioContext, [task = std::move(task)] { task(Status::OK()); });
    }

    void dispatch(Task task) final {
        asio::dispatch(_ioContext, [task = std::move(task)] { task(Status::OK()); });
    }

    bool onReactorThread() const final {
        return false;
    }

    operator asio::io_context&() {
        return _ioContext;
    }

private:
    asio::io_context _ioContext;
};

/**
 * ServiceExecutorSynchronous and ServiceExecutorReserved are closely related.
 * This is a common basis for the fixtures that test them.
 */
template <typename Derived>
class ServiceExecutorSynchronousTestBase : public unittest::Test {
public:
    auto execStatsElementMatcher(int threads, int clients) {
        return ExecStatsIs(getStatsLabel(), m::Eq(threads), m::Eq(clients));
    }

    void testCreateDestroy() {
        makeExecutor();
    }

    void testStartStop() {
        auto executor = makeExecutor();
        ASSERT_OK(executor.start());
        ASSERT_OK(executor.shutdown(kShutdownTime));
    }

    void testMakeTaskRunnerFailsBeforeStartup() {
        auto executor = makeExecutor();
        ASSERT_THROWS(executor.makeTaskRunner(), DBException);
    }

    void testMakeTaskRunner() {
        auto executor = makeExecutor();
        ASSERT_OK(executor.start());
        executor.makeTaskRunner();
        ASSERT_OK(executor.shutdown(kShutdownTime));
    }

    void testMakeTaskRunnerMultiple() {
        auto reserved = getReserved();
        auto executor = makeExecutor();
#define LOCAL_CHECK_STATS(threads, clients) \
    ASSERT_THAT(executor, SoonMatches(execStatsElementMatcher(threads, clients)))
        ASSERT_OK(executor.start());
        LOCAL_CHECK_STATS(reserved, 0);
        std::vector<std::unique_ptr<ServiceExecutor::Executor>> runners;
        // Add a few more beyond the reserve.
        for (size_t i = 0; i < reserved + 3; ++i) {
            runners.push_back(executor.makeTaskRunner());
            LOCAL_CHECK_STATS(runners.size() + reserved, runners.size()) << ", i:" << i;
        }
        ASSERT_OK(executor.shutdown(kShutdownTime));
        LOCAL_CHECK_STATS(0, 0);
#undef LOCAL_CHECK_STATS
    }

    void testBasicTaskRuns() {
        auto executor = makeExecutor();
        ASSERT_OK(executor.start());
        PromiseAndFuture<void> pf;
        auto runner = executor.makeTaskRunner();
        runner->schedule([&](Status st) { pf.promise.setFrom(st); });
        ASSERT_DOES_NOT_THROW(pf.future.get());
        ASSERT_OK(executor.shutdown(kShutdownTime));
    }

    void testShutdownTimeout() {
        auto executor = makeExecutor();
        ASSERT_OK(executor.start());
        auto runner = executor.makeTaskRunner();
        PromiseAndFuture<void> taskStarted;
        runner->schedule([&](Status st) {
            taskStarted.promise.setFrom(st);
            sleepFor(Milliseconds{2000});
        });
        taskStarted.future.get();
        ASSERT_THAT(executor.shutdown(Milliseconds{1000}),
                    m::StatusIs(m::Eq(ErrorCodes::ExceededTimeLimit), m::Any()));
    }

    // Should tolerate the failure to spawn all these reserved threads.
    void testManyLeases() {
        auto executor = makeExecutor();
        ASSERT_OK(executor.start());
        for (size_t i = 0; i < 10; ++i) {
            std::vector<std::unique_ptr<ServiceExecutor::Executor>> leases;
            for (size_t j = 0; j < 20; ++j)
                leases.push_back(executor.makeTaskRunner());
        }
    }

    /**
     * Verify that a new connection cannot get a lease on a worker while it
     * is still busy with a task, even if its lease has been destroyed.
     * Destroys lease from within task, as SessionWorkflow does.
     *
     * Exploits the implementation detail that this kind of ServiceExecutor will
     * reuse the most recently released worker first, and that workers are
     * lazily created. So the worker held by `lease` is only worker when
     * `nextLease` is created.
     */
    void testDelayedEnd() {
        auto svcExec = makeExecutor();
        ASSERT_OK(svcExec.start());

        Notification<bool> pass;
        Notification<void> destroyedLease;
        Notification<void> nextLeaseStarted;

        auto lease = svcExec.makeTaskRunner();
        lease->schedule([&](Status) {
            lease = {};
            destroyedLease.set();
            pass.set(pollUntil([&] { return !!nextLeaseStarted; }, Milliseconds{2000}));
        });

        destroyedLease.get();
        auto nextLease = svcExec.makeTaskRunner();
        nextLease->schedule([&](Status) { nextLeaseStarted.set(); });

        ASSERT(pass.get()) << "worker was reused while running a task";
        ASSERT_OK(svcExec.shutdown(Seconds{10}));
    }

    decltype(auto) makeExecutor() {
        return _d().makeExecutor();
    }

    virtual std::string getStatsLabel() const = 0;

    virtual size_t getReserved() const = 0;

    std::unique_ptr<FailPointEnableBlock> makeFailSpawnBlock() {
        return std::make_unique<FailPointEnableBlock>(
            "serviceExecutorSynchronousThreadFailToSpawn");
    }

private:
    decltype(auto) _d() const {
        return static_cast<const Derived&>(*this);
    }
    decltype(auto) _d() {
        return static_cast<Derived&>(*this);
    }
};

class ServiceExecutorSynchronousTest
    : public ServiceExecutorSynchronousTestBase<ServiceExecutorSynchronousTest> {
public:
    ServiceExecutorSynchronous makeExecutor() const {
        return {};
    }
    std::string getStatsLabel() const override {
        return "passthrough";
    }
    size_t getReserved() const override {
        return 0;
    }
};

class ServiceExecutorReservedTest
    : public ServiceExecutorSynchronousTestBase<ServiceExecutorReservedTest> {
public:
    ServiceExecutorReserved makeExecutor() const {
        return {"testReserved", reserved, maxIdleThreads};
    }
    std::string getStatsLabel() const override {
        return "reserved";
    }
    size_t getReserved() const override {
        return reserved;
    }

protected:
    size_t reserved = 5;
    size_t maxIdleThreads = 0;
};

#define SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASE(fixture, case) \
    TEST_F(fixture, case) {                                          \
        test##case ();                                               \
    }

/**
 * Expand this macro to instantiate the test cases for each of the corresponding
 * member functions of the fixture base class. These are tests that
 * ServiceExecutorSynchronous and ServiceExecutorReserved should pass.
 */
#define SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASES(fixture)                              \
    SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASE(fixture, CreateDestroy)                    \
    SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASE(fixture, StartStop)                        \
    SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASE(fixture, BasicTaskRuns)                    \
    SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASE(fixture, MakeTaskRunnerFailsBeforeStartup) \
    SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASE(fixture, MakeTaskRunner)                   \
    SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASE(fixture, MakeTaskRunnerMultiple)           \
    SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASE(fixture, ShutdownTimeout)                  \
    SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASE(fixture, ManyLeases)                       \
    SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASE(fixture, DelayedEnd)                       \
    /**/

SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASES(ServiceExecutorSynchronousTest)
SERVICE_EXECUTOR_SYNCHRONOUS_COMMON_TEST_CASES(ServiceExecutorReservedTest)

#define SERVICE_EXECUTOR_RESERVED_TEST_CHECK_EXEC_STATS(exec, threads, clients) \
    ASSERT_THAT(exec, SoonMatches(execStatsElementMatcher(threads, clients)))

// Create leases until leases exceed the reserve count of threads, and check
// that the executor keeps its thread count above the number of leases by a
// margin of `reserved` as it goes.
TEST_F(ServiceExecutorReservedTest, CreateLeaseBeyondReserve) {
#define LOCAL_CHECK_STATS(threads, clients) \
    SERVICE_EXECUTOR_RESERVED_TEST_CHECK_EXEC_STATS(executor, threads, clients)
    reserved = 5;
    auto executor = makeExecutor();
    ASSERT_OK(executor.start());
    std::vector<std::unique_ptr<ServiceExecutor::Executor>> leases;
    while (leases.size() < reserved + 1) {
        leases.push_back(executor.makeTaskRunner());
        LOCAL_CHECK_STATS(leases.size() + reserved, leases.size());
    }
    while (!leases.empty()) {
        leases.pop_back();
        LOCAL_CHECK_STATS(leases.size() + reserved, leases.size());
    }
    ASSERT_OK(executor.shutdown(kShutdownTime));
    LOCAL_CHECK_STATS(0, 0);
#undef LOCAL_CHECK_STATS
}

TEST_F(ServiceExecutorReservedTest, ImmediateThrowFromNoReserveSpawnFailure) {
    reserved = 0;
    auto executor = makeExecutor();
    ASSERT_OK(executor.start());
    auto failSpawns = makeFailSpawnBlock();
    ASSERT_THAT(executor, SoonMatches(execStatsElementMatcher(reserved, 0)));
    ASSERT_THROWS(executor.makeTaskRunner(), ExceptionFor<ErrorCodes::InternalError>);
    failSpawns = {};
    ASSERT_DOES_NOT_THROW(executor.makeTaskRunner());
}

// The basic point of the "reserved" ServiceExecutor is to allow new connections
// during time periods in which spawns are failing. Verify this fundamental
// requirement of the reserved ServiceExecutor.
TEST_F(ServiceExecutorReservedTest, ReserveMitigatesSpawnFailures) {
    reserved = 5;
    auto executor = makeExecutor();
    ASSERT_OK(executor.start());
    ASSERT_THAT(executor, execStatsElementMatcher(reserved, 0));
    auto failSpawns = makeFailSpawnBlock();
    std::vector<std::unique_ptr<ServiceExecutor::Executor>> leases;
    while (leases.size() < reserved)
        leases.push_back(executor.makeTaskRunner());
    // One worker is in the starting state while it unsuccesfully attempts to spawn.
    // After the failure, we expect it to be removed from the starting bucket.
    ASSERT_THAT(executor, SoonMatches(execStatsElementMatcher(reserved, leases.size())))
        << "Should be sufficient reserve threads for demand during setup";
    ASSERT_THROWS(executor.makeTaskRunner(), ExceptionFor<ErrorCodes::InternalError>)
        << "Should throw when out of reserve threads";
    failSpawns = {};
    ASSERT_DOES_NOT_THROW(executor.makeTaskRunner());
}

// Check that workers are kept alive after their leases expire according to maxIdleThreads.
TEST_F(ServiceExecutorReservedTest, MaxIdleThreads) {
    for (reserved = 0; reserved != 5; ++reserved) {
        for (maxIdleThreads = 0; maxIdleThreads != 5; ++maxIdleThreads) {
            for (size_t leaseCount = 0; leaseCount != reserved + maxIdleThreads; ++leaseCount) {
                auto executor = makeExecutor();
                ASSERT_OK(executor.start());
                ASSERT_THAT(executor, execStatsElementMatcher(reserved, 0));

                std::vector<std::unique_ptr<ServiceExecutor::Executor>> leases;
                while (leases.size() < leaseCount)
                    leases.push_back(executor.makeTaskRunner());
                leases.clear();

                ASSERT_THAT(executor,
                            SoonMatches(execStatsElementMatcher(
                                reserved + std::min(maxIdleThreads, leaseCount), 0)))
                    << ", reserved=" << reserved              //
                    << ", maxIdleThreads=" << maxIdleThreads  //
                    << ", leaseCount=" << leaseCount;
            }
        }
    }
}

class ServiceExecutorFixedTest : public unittest::Test {
public:
    static constexpr size_t kExecutorThreads = 2;

    class Handle {
    public:
        Handle() = default;
        Handle(const Handle&) = delete;
        Handle& operator=(const Handle&) = delete;

        ~Handle() {
            join();
        }

        void join() {
            ASSERT_OK(_executor->shutdown(kShutdownTime));
        }

        void start() {
            ASSERT_OK(_executor->start());
        }

        ServiceExecutorFixed* operator->() const noexcept {
            return &*_executor;
        }

        ServiceExecutorFixed& operator*() const noexcept {
            return *_executor;
        }

    private:
        std::shared_ptr<ServiceExecutorFixed> _executor{std::make_shared<ServiceExecutorFixed>(
            ThreadPool::Limits{kExecutorThreads, kExecutorThreads})};
    };
};

TEST_F(ServiceExecutorFixedTest, MakeTaskRunnerFailsBeforeStartup) {
    Handle handle;
    ASSERT_THROWS(handle->makeTaskRunner(), DBException);
}

TEST_F(ServiceExecutorFixedTest, BasicTaskRuns) {
    Handle handle;
    handle.start();
    auto runner = handle->makeTaskRunner();
    PromiseAndFuture<void> pf;
    runner->schedule([&](Status s) { pf.promise.setFrom(s); });
    ASSERT_DOES_NOT_THROW(pf.future.get());
}

TEST_F(ServiceExecutorFixedTest, ShutdownTimeLimit) {
    unittest::Barrier mayReturn(2);
    Handle handle;
    handle.start();
    auto runner = handle->makeTaskRunner();
    PromiseAndFuture<void> pf;
    runner->schedule([&](Status st) {
        pf.promise.setFrom(st);
        mayReturn.countDownAndWait();
    });
    ASSERT_DOES_NOT_THROW(pf.future.get());
    ASSERT_NOT_OK(handle->shutdown(kShutdownTime));

    // Ensure the service executor is stopped before leaving the test.
    mayReturn.countDownAndWait();
}

TEST_F(ServiceExecutorFixedTest, ScheduleSucceedsBeforeShutdown) {
    boost::optional<FailPointEnableBlock> failpoint("hangBeforeSchedulingServiceExecutorFixedTask");
    PromiseAndFuture<void> pf;
    Handle handle;
    handle.start();
    auto runner = handle->makeTaskRunner();

    // The executor accepts the work, but hasn't used the underlying pool yet.
    JoinThread scheduleClient{[&] { runner->schedule([&](Status s) { pf.promise.setFrom(s); }); }};
    (*failpoint)->waitForTimesEntered(failpoint->initialTimesEntered() + 1);

    // Trigger an immediate shutdown which will not affect the task we have accepted.
    ASSERT_NOT_OK(handle->shutdown(Milliseconds{0}));
    failpoint.reset();

    // Our failpoint has been disabled, so the task can run to completion.
    ASSERT_DOES_NOT_THROW(pf.future.get());

    // Now we can wait for the task to finish and shutdown.
    ASSERT_OK(handle->shutdown(kShutdownTime));
}

TEST_F(ServiceExecutorFixedTest, ScheduleFailsAfterShutdown) {
    Handle handle;
    handle.start();
    auto runner = handle->makeTaskRunner();
    ASSERT_OK(handle->shutdown(kShutdownTime));
    PromiseAndFuture<void> pf;
    runner->schedule([&](Status s) { pf.promise.setFrom(s); });
    ASSERT_THROWS(pf.future.get(), ExceptionFor<ErrorCodes::ServiceExecutorInShutdown>);
}

TEST_F(ServiceExecutorFixedTest, RunTaskAfterWaitingForData) {
    unittest::threadAssertionMonitoredTest([&](auto&& monitor) {
        unittest::Barrier barrier(2);
        auto tl = std::make_unique<TransportLayerMock>();
        auto session = std::dynamic_pointer_cast<MockSession>(tl->createSession());
        invariant(session);

        Handle handle;
        handle.start();
        auto runner = handle->makeTaskRunner();

        const auto signallingThreadId = stdx::this_thread::get_id();

        AtomicWord<bool> ranOnDataAvailable{false};

        runner->runOnDataAvailable(session, [&](Status) {
            ranOnDataAvailable.store(true);
            ASSERT(stdx::this_thread::get_id() != signallingThreadId);
            barrier.countDownAndWait();
        });

        ASSERT(!ranOnDataAvailable.load());

        session->signalAvailableData();

        barrier.countDownAndWait();
        ASSERT(ranOnDataAvailable.load());
    });
}

TEST_F(ServiceExecutorFixedTest, StartAndShutdownAreDeterministic) {
    unittest::threadAssertionMonitoredTest([&](auto&& monitor) {
        Handle handle;

        // Ensure starting the executor results in spawning the specified number of executor
        // threads.
        {
            FailPointEnableBlock failpoint("hangAfterServiceExecutorFixedExecutorThreadsStart");
            handle.start();
            failpoint->waitForTimesEntered(failpoint.initialTimesEntered() + kExecutorThreads);
        }

        // Since destroying ServiceExecutorFixed is blocking, spawn a thread to issue the
        // destruction off of the main execution path.
        stdx::thread shutdownThread;

        // Ensure all executor threads return after receiving the shutdown signal.
        {
            FailPointEnableBlock failpoint(
                "hangBeforeServiceExecutorFixedLastExecutorThreadReturns");
            shutdownThread = monitor.spawn([&] { handle.join(); });
            failpoint->waitForTimesEntered(failpoint.initialTimesEntered() + 1);
        }
        shutdownThread.join();
    });
}

}  // namespace
}  // namespace mongo::transport