| Index: test/SchedulerTest.cpp |
| =================================================================== |
| new file mode 100644 |
| --- /dev/null |
| +++ b/test/SchedulerTest.cpp |
| @@ -0,0 +1,106 @@ |
| +/* |
| + * This file is part of Adblock Plus <https://adblockplus.org/>, |
| + * Copyright (C) 2006-2016 Eyeo GmbH |
| + * |
| + * Adblock Plus is free software: you can redistribute it and/or modify |
| + * it under the terms of the GNU General Public License version 3 as |
| + * published by the Free Software Foundation. |
| + * |
| + * Adblock Plus 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 General Public License for more details. |
| + * |
| + * You should have received a copy of the GNU General Public License |
| + * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + */ |
| + |
| +#include "BaseJsTest.h" |
| +#include "../src/Scheduler.h" |
| + |
| +TEST(OperationRunner, Instance) |
| +{ |
| + OperationRunner runner; |
| +} |
| + |
| +TEST(OperationRunner, RunOneThing) |
| +{ |
| + int counter = 0; |
| + { |
| + OperationRunner runner; |
| + runner.Run([&]() {++counter; }); |
| + } |
| + // Runner is out of scope, so all operations must have completed |
| + EXPECT_EQ(1, counter); |
| +} |
| + |
| +TEST(SingleUseWorker, Instance) |
| +{ |
| + SingleUseWorker w; |
| +} |
| + |
| +TEST(SingleUseWorker, RunWithExplicitJoin) |
| +{ |
| + int counter = 0; |
| + SingleUseWorker w; |
| + EXPECT_EQ(0, counter); |
| + w.Run([&]() {++counter; }); |
| + w.Join(); |
| + EXPECT_EQ(1, counter); |
| +} |
| + |
| +TEST(SingleUseWorker, RunWithImplicitJoinInDestructor) |
| +{ |
| + int counter = 0; |
| + { |
| + SingleUseWorker w; |
| + EXPECT_EQ(0, counter); |
| + w.Run([&]() {++counter; }); |
| + } |
| + EXPECT_EQ(1, counter); |
| +} |
| + |
| +TEST(Scheduler, Instance) |
| +{ |
| + SchedulerT<SingleUseWorker> s; |
| +} |
| + |
| +TEST(Scheduler, RunOnce) |
| +{ |
| + int counter = 0; |
| + SchedulerT<SingleUseWorker> s; |
| + EXPECT_EQ(0, counter); |
| + s.Run([&]() {++counter; }); |
| + s.JoinAll(); |
| + EXPECT_EQ(1, counter); |
| +} |
| + |
| +TEST(Scheduler, RunTwice) |
| +{ |
| + int counter = 0; |
| + SchedulerT<SingleUseWorker> s; |
| + s.Run([&]() {++counter; }); |
| + s.JoinAll(); |
| + s.Run([&]() {++counter; }); |
| + s.JoinAll(); |
| + EXPECT_EQ(2, counter); |
| +} |
| + |
| +TEST(Scheduler, RunMany) |
| +{ |
| + std::mutex m; |
| + int counter = 0; |
| + SchedulerT<SingleUseWorker> s; |
| + auto f = [&]() { |
| + std::unique_lock<std::mutex> ul(m); |
| + ++counter; |
| + }; |
| + const int n = 100; |
| + for (int j = 0; j < n; ++j) |
| + { |
| + auto g = f; |
|
sergei
2017/01/20 13:08:14
why do we need g?
Eric
2017/03/30 17:16:59
It's explicitly a copy of f. Perhaps unnecessary,
|
| + s.Run(g); |
| + } |
| + ASSERT_NO_THROW(s.JoinAll()); |
| + EXPECT_EQ(n, counter); |
| +} |