Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/BaseJsTest.h

Issue 29499583: Issue 4938 - fix race conditions related to LazyFileSystem (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: Created July 27, 2017, 8:53 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/BaseJsTest.cpp » ('j') | test/FilterEngine.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 142
143 class ThrowingWebRequest : public AdblockPlus::IWebRequest 143 class ThrowingWebRequest : public AdblockPlus::IWebRequest
144 { 144 {
145 public: 145 public:
146 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback&) override 146 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback&) override
147 { 147 {
148 throw std::runtime_error("Unexpected GET: " + url); 148 throw std::runtime_error("Unexpected GET: " + url);
149 } 149 }
150 }; 150 };
151 151
152 class LazyFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus::File System 152 class LazyFileSystem : public AdblockPlus::IFileSystem
153 { 153 {
154 public: 154 public:
155 IOBuffer Read(const std::string& path) const 155 typedef std::function<void()> Task;
156 typedef std::function<void(const Task& task)> Scheduler;
157 static void ExecuteImmediately(const Task& task)
156 { 158 {
157 std::string dummyData(""); 159 if (task)
158 if (path == "patterns.ini") 160 task();
159 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~";
160 else if (path == "prefs.json")
161 dummyData = "{}";
162 return IOBuffer(dummyData.cbegin(), dummyData.cend());
163 } 161 }
164 162 explicit LazyFileSystem(const Scheduler& scheduler = LazyFileSystem::ExecuteIm mediately)
165 void Read(const std::string& path, const ReadCallback& callback) const 163 : scheduler(scheduler)
166 {
167 std::thread([this, path, callback]
168 {
169 auto data = Read(path);
170 callback(std::move(data), "");
171 }).detach();
172 }
173
174 void Write(const std::string& path, const IOBuffer& content)
175 { 164 {
176 } 165 }
177 166
178 void Write(const std::string& path, const IOBuffer& data, 167 void Read(const std::string& path, const ReadCallback& callback) const overrid e
179 const Callback& callback)
180 { 168 {
181 std::thread([this, path, data, callback] 169 scheduler([path, callback]
182 { 170 {
183 Write(path, data); 171 if (path == "patterns.ini")
184 callback(""); 172 {
185 }).detach(); 173 std::string dummyData = "# Adblock Plus preferences\n[Subscription]\nurl =~fl~";
174 callback(IOBuffer(dummyData.cbegin(), dummyData.cend()), "");
175 }
176 else if (path == "prefs.json")
177 {
178 std::string dummyData = "{}";
179 callback(IOBuffer(dummyData.cbegin(), dummyData.cend()), "");
180 }
181 });
186 } 182 }
187 183
188 void Move(const std::string& fromPath, const std::string& toPath) 184 void Write(const std::string& path, const IOBuffer& data,
185 const Callback& callback) override
189 { 186 {
190 } 187 }
191 188
189
192 void Move(const std::string& fromPath, const std::string& toPath, 190 void Move(const std::string& fromPath, const std::string& toPath,
193 const Callback& callback) 191 const Callback& callback) override
194 {
195 std::thread([this, fromPath, toPath, callback]
196 {
197 Move(fromPath, toPath);
198 callback("");
199 }).detach();
200 }
201
202 void Remove(const std::string& path)
203 { 192 {
204 } 193 }
205 194
206 void Remove(const std::string& path, const Callback& callback) 195 void Remove(const std::string& path, const Callback& callback) override
207 { 196 {
208 std::thread([this, path, callback]
209 {
210 Remove(path);
211 callback("");
212 }).detach();
213 } 197 }
214 198
215 StatResult Stat(const std::string& path) const 199 void Stat(const std::string& path, const StatCallback& callback) const overrid e
216 { 200 {
217 StatResult result; 201 scheduler([path, callback]
218 if (path == "patterns.ini")
219 { 202 {
220 result.exists = true; 203 StatResult result;
221 result.isFile = true; 204 if (path == "patterns.ini")
222 } 205 {
223 return result; 206 result.exists = true;
207 result.isFile = true;
208 }
209 callback(result, "");
210 });
224 } 211 }
225 212
226 void Stat(const std::string& path, const StatCallback& callback) const 213 std::string Resolve(const std::string& path) const override
227 {
228 std::thread([this, path, callback]
229 {
230 callback(Stat(path), "");
231 }).detach();
232 }
233
234 std::string Resolve(const std::string& path) const
235 { 214 {
236 return path; 215 return path;
237 } 216 }
217 public:
218 Scheduler scheduler;
238 }; 219 };
239 220
221 AdblockPlus::FilterEnginePtr CreateFilterEngine(LazyFileSystem& fileSystem,
222 const AdblockPlus::JsEnginePtr& jsEngine,
223 const AdblockPlus::FilterEngine::CreationParameters& creationParams = AdblockP lus::FilterEngine::CreationParameters());
224
240 class NoopWebRequest : public AdblockPlus::IWebRequest 225 class NoopWebRequest : public AdblockPlus::IWebRequest
241 { 226 {
242 public: 227 public:
243 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback& callback) override 228 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback& callback) override
244 { 229 {
245 } 230 }
246 }; 231 };
247 232
248 struct DelayedWebRequestTask 233 struct DelayedWebRequestTask
249 { 234 {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 protected: 274 protected:
290 AdblockPlus::JsEnginePtr jsEngine; 275 AdblockPlus::JsEnginePtr jsEngine;
291 276
292 virtual void SetUp() 277 virtual void SetUp()
293 { 278 {
294 jsEngine = CreateJsEngine(); 279 jsEngine = CreateJsEngine();
295 } 280 }
296 }; 281 };
297 282
298 #endif 283 #endif
OLDNEW
« no previous file with comments | « no previous file | test/BaseJsTest.cpp » ('j') | test/FilterEngine.cpp » ('J')

Powered by Google App Engine
This is Rietveld