OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 { | 53 { |
54 int counter = 0; | 54 int counter = 0; |
55 { | 55 { |
56 SingleUseWorker w; | 56 SingleUseWorker w; |
57 EXPECT_EQ(0, counter); | 57 EXPECT_EQ(0, counter); |
58 w.Run([&]() {++counter; }); | 58 w.Run([&]() {++counter; }); |
59 } | 59 } |
60 EXPECT_EQ(1, counter); | 60 EXPECT_EQ(1, counter); |
61 } | 61 } |
62 | 62 |
| 63 struct RudimentaryTask |
| 64 : public TaskFunctionInterface |
| 65 { |
| 66 std::function<void()> f; |
| 67 RudimentaryTask(std::function<void()> f) |
| 68 : f(f) |
| 69 {} |
| 70 virtual void operator()() override |
| 71 { |
| 72 f(); |
| 73 } |
| 74 virtual void Interrupt() override |
| 75 {} |
| 76 }; |
| 77 |
| 78 std::shared_ptr<RudimentaryTask> MakeRudimentaryTask(std::function<void()> f) |
| 79 { |
| 80 return std::make_shared<RudimentaryTask>(f); |
| 81 } |
| 82 |
| 83 struct NullUserData {}; |
| 84 |
63 TEST(Scheduler, Instance) | 85 TEST(Scheduler, Instance) |
64 { | 86 { |
65 SchedulerT<SingleUseWorker> s; | 87 SchedulerT<SingleUseWorker, NullUserData> s; |
66 } | 88 } |
| 89 std::shared_ptr<NullUserData> MakeNullUserData() |
| 90 { |
| 91 return std::shared_ptr<NullUserData>(); |
| 92 } |
| 93 |
67 | 94 |
68 TEST(Scheduler, RunOnce) | 95 TEST(Scheduler, RunOnce) |
69 { | 96 { |
70 int counter = 0; | 97 int counter = 0; |
71 SchedulerT<SingleUseWorker> s; | 98 SchedulerT<SingleUseWorker, NullUserData> s; |
72 EXPECT_EQ(0, counter); | 99 EXPECT_EQ(0, counter); |
73 s.Run([&]() {++counter; }); | 100 s.Run(MakeRudimentaryTask([&]() {++counter; }), MakeNullUserData()); |
74 s.JoinAll(); | 101 s.JoinAll(); |
75 EXPECT_EQ(1, counter); | 102 EXPECT_EQ(1, counter); |
76 } | 103 } |
77 | 104 |
78 TEST(Scheduler, RunTwice) | 105 TEST(Scheduler, RunTwice) |
79 { | 106 { |
80 int counter = 0; | 107 int counter = 0; |
81 SchedulerT<SingleUseWorker> s; | 108 SchedulerT<SingleUseWorker, NullUserData> s; |
82 s.Run([&]() {++counter; }); | 109 s.Run(MakeRudimentaryTask([&]() {++counter; }), MakeNullUserData()); |
83 s.JoinAll(); | 110 s.JoinAll(); |
84 s.Run([&]() {++counter; }); | 111 s.Run(MakeRudimentaryTask([&]() {++counter; }), MakeNullUserData()); |
85 s.JoinAll(); | 112 s.JoinAll(); |
86 EXPECT_EQ(2, counter); | 113 EXPECT_EQ(2, counter); |
87 } | 114 } |
88 | 115 |
89 TEST(Scheduler, RunMany) | 116 TEST(Scheduler, RunMany) |
90 { | 117 { |
91 std::mutex m; | 118 std::mutex m; |
92 int counter = 0; | 119 int counter = 0; |
93 SchedulerT<SingleUseWorker> s; | 120 SchedulerT<SingleUseWorker, NullUserData> s; |
94 auto f = [&]() { | 121 auto f = [&]() { |
95 std::unique_lock<std::mutex> ul(m); | 122 std::unique_lock<std::mutex> ul(m); |
96 ++counter; | 123 ++counter; |
97 }; | 124 }; |
98 const int n = 100; | 125 const int n = 100; |
99 for (int j = 0; j < n; ++j) | 126 for (int j = 0; j < n; ++j) |
100 { | 127 { |
101 auto g = f; | 128 auto g = f; |
102 s.Run(g); | 129 s.Run(MakeRudimentaryTask(g), MakeNullUserData()); |
103 } | 130 } |
104 ASSERT_NO_THROW(s.JoinAll()); | 131 ASSERT_NO_THROW(s.JoinAll()); |
105 EXPECT_EQ(n, counter); | 132 EXPECT_EQ(n, counter); |
106 } | 133 } |
OLD | NEW |