|
| 1 | +/* |
| 2 | + * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 | + * Copyright (C) 2006-present eyeo GmbH |
| 4 | + * |
| 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 |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * Adblock Plus is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | + |
| 19 | +#include <thread> |
| 20 | +#include <vector> |
| 21 | +#include <chrono> |
| 22 | +#include <string> |
| 23 | + |
| 24 | +#include "BaseJsTest.h" |
| 25 | + |
| 26 | +using namespace AdblockPlus; |
| 27 | + |
| 28 | +namespace |
| 29 | +{ |
| 30 | + class UpdaterAndFilterEngineCreationTest : public BaseJsTest |
| 31 | + { |
| 32 | + protected: |
| 33 | + |
| 34 | + static const size_t COUNT = 100; |
| 35 | + const std::string PROP_NAME = "patternsbackupinterval"; |
| 36 | + std::vector<std::thread> threadsVector; |
| 37 | + Updater* updaterAddrArray[COUNT]; |
| 38 | + FilterEngine* filterAddrArray[COUNT]; |
| 39 | + DelayedWebRequest::SharedTasks webRequestTasks; |
| 40 | + DelayedTimer::SharedTasks timerTasks; |
| 41 | + |
| 42 | + void SetUp() |
| 43 | + { |
| 44 | + LazyFileSystem* fileSystem; |
| 45 | + ThrowingPlatformCreationParameters platformParams; |
| 46 | + platformParams.logSystem.reset(new LazyLogSystem()); |
| 47 | + platformParams.timer = DelayedTimer::New(timerTasks); |
| 48 | + platformParams.fileSystem.reset(fileSystem = new LazyFileSystem()); |
| 49 | + platformParams.webRequest = DelayedWebRequest::New(webRequestTasks); |
| 50 | + platform.reset(new Platform(std::move(platformParams))); |
| 51 | + threadsVector.reserve(COUNT); |
| 52 | + } |
| 53 | + |
| 54 | + void CallGetUpdaterSimultaneously() |
| 55 | + { |
| 56 | + CallGetSimultaneously(true, false); |
| 57 | + } |
| 58 | + |
| 59 | + void CallGetFilterEngineSimultaneously() |
| 60 | + { |
| 61 | + CallGetSimultaneously(false, true); |
| 62 | + } |
| 63 | + |
| 64 | + void CallGetUpdaterAndGetFilterEngineSimultaneously() |
| 65 | + { |
| 66 | + CallGetSimultaneously(true, true); |
| 67 | + } |
| 68 | + |
| 69 | + private: |
| 70 | + |
| 71 | + void CallGetSimultaneously(bool isUpdater, bool isFilterEngine) |
| 72 | + { |
| 73 | + for (size_t idx = 0; idx < COUNT; ++idx) |
| 74 | + threadsVector.push_back( |
| 75 | + std::thread([this, idx, isUpdater, isFilterEngine]() -> void { |
| 76 | + Updater* updaterAddr = nullptr; |
| 77 | + FilterEngine* filterEngineAddr = nullptr; |
| 78 | + if (isUpdater && isFilterEngine) |
| 79 | + { |
| 80 | + // some randomization in order of calling gets |
| 81 | + if (idx % 2) |
| 82 | + { |
| 83 | + updaterAddr = &(platform->GetUpdater()); |
| 84 | + filterEngineAddr = &(platform->GetFilterEngine()); |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + filterEngineAddr = &(platform->GetFilterEngine()); |
| 89 | + updaterAddr = &(platform->GetUpdater()); |
| 90 | + } |
| 91 | + } |
| 92 | + else if (isUpdater) |
| 93 | + updaterAddr = &(platform->GetUpdater()); |
| 94 | + else if (isFilterEngine) |
| 95 | + filterEngineAddr = &(platform->GetFilterEngine()); |
| 96 | + |
| 97 | + if (updaterAddr != nullptr) |
| 98 | + { |
| 99 | + updaterAddrArray[idx] = updaterAddr; |
| 100 | + } |
| 101 | + if (filterEngineAddr != nullptr) |
| 102 | + { |
| 103 | + filterAddrArray[idx] = filterEngineAddr; |
| 104 | + } |
| 105 | + })); |
| 106 | + |
| 107 | + // join the threads |
| 108 | + for (auto& th: threadsVector) |
| 109 | + if (th.joinable()) |
| 110 | + th.join(); |
| 111 | + } |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +TEST_F(UpdaterAndFilterEngineCreationTest, TestFilterEngineSingleInstance) |
| 116 | +{ |
| 117 | + CallGetFilterEngineSimultaneously(); |
| 118 | + FilterEngine* filterEngineAddr = filterAddrArray[0]; |
| 119 | + for (size_t i = 1; i < COUNT; ++i) |
| 120 | + ASSERT_EQ(filterEngineAddr, filterAddrArray[i]); |
| 121 | +} |
| 122 | + |
| 123 | +TEST_F(UpdaterAndFilterEngineCreationTest, TestUpdaterSingleInstance) |
| 124 | +{ |
| 125 | + CallGetUpdaterSimultaneously(); |
| 126 | + Updater* updaterAddr = updaterAddrArray[0]; |
| 127 | + for (size_t i = 1; i < COUNT; ++i) |
| 128 | + ASSERT_EQ(updaterAddr, updaterAddrArray[i]); |
| 129 | +} |
| 130 | + |
| 131 | +TEST_F(UpdaterAndFilterEngineCreationTest, TestUpdaterAndFilterEngineCreationsDontCollide) |
| 132 | +{ |
| 133 | + CallGetUpdaterAndGetFilterEngineSimultaneously(); |
| 134 | + ASSERT_EQ(filterAddrArray[0], filterAddrArray[COUNT - 1]); |
| 135 | + ASSERT_EQ(updaterAddrArray[0], updaterAddrArray[COUNT - 1]); |
| 136 | +} |
| 137 | + |
| 138 | +TEST_F(UpdaterAndFilterEngineCreationTest, TestUpdaterAndFilterEngineCreationOrder1) |
| 139 | +{ |
| 140 | + Updater& updater = platform->GetUpdater(); |
| 141 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 142 | + FilterEngine& filterEngine = platform->GetFilterEngine(); |
| 143 | + |
| 144 | + int propFromUpdater = updater.GetPref(PROP_NAME).AsInt(); |
| 145 | + int propFromFilterEngine = filterEngine.GetPref(PROP_NAME).AsInt(); |
| 146 | + ASSERT_EQ(propFromUpdater, propFromFilterEngine); |
| 147 | + |
| 148 | + int newPropValue = 8; |
| 149 | + updater.SetPref(PROP_NAME, GetJsEngine().NewValue(newPropValue)); |
| 150 | + propFromUpdater = updater.GetPref(PROP_NAME).AsInt(); |
| 151 | + propFromFilterEngine = filterEngine.GetPref(PROP_NAME).AsInt(); |
| 152 | + ASSERT_EQ(propFromUpdater, newPropValue); |
| 153 | + ASSERT_EQ(propFromFilterEngine, newPropValue); |
| 154 | +} |
| 155 | + |
| 156 | +TEST_F(UpdaterAndFilterEngineCreationTest, TestUpdaterAndFilterEngineCreationOrder2) |
| 157 | +{ |
| 158 | + FilterEngine& filterEngine = platform->GetFilterEngine(); |
| 159 | + std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 160 | + Updater& updater = platform->GetUpdater(); |
| 161 | + |
| 162 | + int propFromFilterEngine = filterEngine.GetPref(PROP_NAME).AsInt(); |
| 163 | + int propFromUpdater = updater.GetPref(PROP_NAME).AsInt(); |
| 164 | + ASSERT_EQ(propFromUpdater, propFromFilterEngine); |
| 165 | + |
| 166 | + int newPropValue = 18; |
| 167 | + filterEngine.SetPref(PROP_NAME, GetJsEngine().NewValue(newPropValue)); |
| 168 | + propFromFilterEngine = filterEngine.GetPref(PROP_NAME).AsInt(); |
| 169 | + propFromUpdater = updater.GetPref(PROP_NAME).AsInt(); |
| 170 | + ASSERT_EQ(propFromUpdater, newPropValue); |
| 171 | + ASSERT_EQ(propFromFilterEngine, newPropValue); |
| 172 | +} |
0 commit comments