1 // Copyright 2019 The Marl Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
17 
18 #include "marl/scheduler.h"
19 
20 // SchedulerParams holds Scheduler construction parameters for testing.
21 struct SchedulerParams {
22   int numWorkerThreads;
23 
24   friend std::ostream& operator<<(std::ostream& os,
25                                   const SchedulerParams& params) {
26     return os << "SchedulerParams{"
27               << "numWorkerThreads: " << params.numWorkerThreads << "}";
28   }
29 };
30 
31 // WithoutBoundScheduler is a test fixture that does not bind a scheduler.
32 class WithoutBoundScheduler : public testing::Test {
33  public:
SetUp()34   void SetUp() override {
35     allocator = new marl::TrackedAllocator(marl::Allocator::Default);
36   }
37 
TearDown()38   void TearDown() override {
39     auto stats = allocator->stats();
40     ASSERT_EQ(stats.numAllocations(), 0U);
41     ASSERT_EQ(stats.bytesAllocated(), 0U);
42     delete allocator;
43   }
44 
45   marl::TrackedAllocator* allocator = nullptr;
46 };
47 
48 // WithBoundScheduler is a parameterized test fixture that performs tests with
49 // a bound scheduler using a number of different configurations.
50 class WithBoundScheduler : public testing::TestWithParam<SchedulerParams> {
51  public:
SetUp()52   void SetUp() override {
53     allocator = new marl::TrackedAllocator(marl::Allocator::Default);
54 
55     auto& params = GetParam();
56 
57     marl::Scheduler::Config cfg;
58     cfg.setAllocator(allocator);
59     cfg.setWorkerThreadCount(params.numWorkerThreads);
60     cfg.setFiberStackSize(0x10000);
61 
62     auto scheduler = new marl::Scheduler(cfg);
63     scheduler->bind();
64   }
65 
TearDown()66   void TearDown() override {
67     auto scheduler = marl::Scheduler::get();
68     scheduler->unbind();
69     delete scheduler;
70 
71     auto stats = allocator->stats();
72     ASSERT_EQ(stats.numAllocations(), 0U);
73     ASSERT_EQ(stats.bytesAllocated(), 0U);
74     delete allocator;
75   }
76 
77   marl::TrackedAllocator* allocator = nullptr;
78 };
79