Skip to content

Commit 1ae9556

Browse files
committed
Reworked last commit applying review comments
1 parent 511c949 commit 1ae9556

1 file changed

Lines changed: 30 additions & 44 deletions

File tree

test/UpdaterAndFilterEngineCreation.cpp

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
19-
#include <thread>
20-
#include <vector>
21-
#include <chrono>
2218
#include <string>
2319
#include <memory>
20+
#include <array>
21+
22+
#include <AdblockPlus/AsyncExecutor.h>
2423

2524
#include "BaseJsTest.h"
2625

@@ -34,9 +33,8 @@ namespace
3433

3534
static const size_t COUNT = 100;
3635
const std::string PROP_NAME = "patternsbackupinterval";
37-
std::vector<std::thread> threadsVector;
38-
Updater* updaterAddrArray[COUNT];
39-
FilterEngine* filterAddrArray[COUNT];
36+
std::array<Updater*, COUNT> updaterAddrArray;
37+
std::array<FilterEngine*, COUNT> filterAddrArray;
4038
DelayedWebRequest::SharedTasks webRequestTasks;
4139
DelayedTimer::SharedTasks timerTasks;
4240

@@ -49,9 +47,8 @@ namespace
4947
platformParams.fileSystem.reset(fileSystem = new LazyFileSystem());
5048
platformParams.webRequest = DelayedWebRequest::New(webRequestTasks);
5149
platform.reset(new Platform(std::move(platformParams)));
52-
threadsVector.reserve(COUNT);
53-
std::uninitialized_fill(updaterAddrArray, updaterAddrArray + COUNT, nullptr);
54-
std::uninitialized_fill(filterAddrArray, filterAddrArray + COUNT, nullptr);
50+
std::uninitialized_fill(updaterAddrArray.begin(), updaterAddrArray.end(), nullptr);
51+
std::uninitialized_fill(filterAddrArray.begin(), filterAddrArray.end(), nullptr);
5552
}
5653

5754
void CallGetUpdaterSimultaneously()
@@ -73,41 +70,28 @@ namespace
7370

7471
void CallGetSimultaneously(bool isUpdater, bool isFilterEngine)
7572
{
73+
AsyncExecutor asyncExecutor;
7674
for (size_t idx = 0; idx < COUNT; ++idx)
77-
threadsVector.push_back(
78-
std::thread([this, idx, isUpdater, isFilterEngine]() -> void {
79-
Updater* updaterAddr = nullptr;
80-
FilterEngine* filterEngineAddr = nullptr;
81-
if (isUpdater && isFilterEngine)
75+
asyncExecutor.Dispatch(([this, idx, isUpdater, isFilterEngine]() -> void {
76+
if (isUpdater && isFilterEngine)
77+
{
78+
// some randomization in order of calling gets
79+
if (idx % 2)
80+
{
81+
updaterAddrArray[idx] = &(platform->GetUpdater());
82+
filterAddrArray[idx] = &(platform->GetFilterEngine());
83+
}
84+
else
8285
{
83-
// some randomization in order of calling gets
84-
if (idx % 2)
85-
{
86-
updaterAddr = &(platform->GetUpdater());
87-
filterEngineAddr = &(platform->GetFilterEngine());
88-
}
89-
else
90-
{
91-
filterEngineAddr = &(platform->GetFilterEngine());
92-
updaterAddr = &(platform->GetUpdater());
93-
}
86+
filterAddrArray[idx] = &(platform->GetFilterEngine());
87+
updaterAddrArray[idx] = &(platform->GetUpdater());
9488
}
95-
else if (isUpdater)
96-
updaterAddr = &(platform->GetUpdater());
97-
else if (isFilterEngine)
98-
filterEngineAddr = &(platform->GetFilterEngine());
99-
100-
if (updaterAddr != nullptr)
101-
updaterAddrArray[idx] = updaterAddr;
102-
103-
if (filterEngineAddr != nullptr)
104-
filterAddrArray[idx] = filterEngineAddr;
105-
}));
106-
107-
// join the threads
108-
for (auto& th: threadsVector)
109-
if (th.joinable())
110-
th.join();
89+
}
90+
else if (isUpdater)
91+
updaterAddrArray[idx] = &(platform->GetUpdater());
92+
else if (isFilterEngine)
93+
filterAddrArray[idx] = &(platform->GetFilterEngine());
94+
}));
11195
}
11296
};
11397
}
@@ -116,6 +100,7 @@ TEST_F(UpdaterAndFilterEngineCreationTest, TestFilterEngineSingleInstance)
116100
{
117101
CallGetFilterEngineSimultaneously();
118102
FilterEngine* filterEngineAddr = filterAddrArray[0];
103+
ASSERT_NE(filterEngineAddr, nullptr);
119104
for (size_t i = 1; i < COUNT; ++i)
120105
ASSERT_EQ(filterEngineAddr, filterAddrArray[i]);
121106
}
@@ -124,21 +109,23 @@ TEST_F(UpdaterAndFilterEngineCreationTest, TestUpdaterSingleInstance)
124109
{
125110
CallGetUpdaterSimultaneously();
126111
Updater* updaterAddr = updaterAddrArray[0];
112+
ASSERT_NE(updaterAddr, nullptr);
127113
for (size_t i = 1; i < COUNT; ++i)
128114
ASSERT_EQ(updaterAddr, updaterAddrArray[i]);
129115
}
130116

131117
TEST_F(UpdaterAndFilterEngineCreationTest, TestUpdaterAndFilterEngineCreationsDontCollide)
132118
{
133119
CallGetUpdaterAndGetFilterEngineSimultaneously();
120+
ASSERT_NE(filterAddrArray[0], nullptr);
134121
ASSERT_EQ(filterAddrArray[0], filterAddrArray[COUNT - 1]);
122+
ASSERT_NE(updaterAddrArray[0], nullptr);
135123
ASSERT_EQ(updaterAddrArray[0], updaterAddrArray[COUNT - 1]);
136124
}
137125

138126
TEST_F(UpdaterAndFilterEngineCreationTest, TestUpdaterAndFilterEngineCreationOrder1)
139127
{
140128
Updater& updater = platform->GetUpdater();
141-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
142129
FilterEngine& filterEngine = platform->GetFilterEngine();
143130

144131
int propFromUpdater = updater.GetPref(PROP_NAME).AsInt();
@@ -156,7 +143,6 @@ TEST_F(UpdaterAndFilterEngineCreationTest, TestUpdaterAndFilterEngineCreationOrd
156143
TEST_F(UpdaterAndFilterEngineCreationTest, TestUpdaterAndFilterEngineCreationOrder2)
157144
{
158145
FilterEngine& filterEngine = platform->GetFilterEngine();
159-
std::this_thread::sleep_for(std::chrono::milliseconds(100));
160146
Updater& updater = platform->GetUpdater();
161147

162148
int propFromFilterEngine = filterEngine.GetPref(PROP_NAME).AsInt();

0 commit comments

Comments
 (0)