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

Delta Between Two Patch Sets: test/BaseJsTest.h

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Left Patch Set: Updated implementation. Created June 2, 2017, 3:49 p.m.
Right Patch Set: Rebase on master. Last changes. Created July 7, 2017, 1:36 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/Utils.cpp ('k') | test/BaseJsTest.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #ifndef MOCKS_H 18 #ifndef MOCKS_H
19 #define MOCKS_H 19 #define MOCKS_H
20 20
21 #include <thread> 21 #include <thread>
22 22
23 #include <AdblockPlus.h> 23 #include <AdblockPlus.h>
24 #include <gtest/gtest.h> 24 #include <gtest/gtest.h>
25 #include "../src/Thread.h" 25 #include "../src/Thread.h"
26 #include "../src/Utils.h"
27
28 class Sync
29 {
30 public:
31 Sync()
32 : set(false)
33 {
34 }
35 void Wait(int sec = 20)
36 {
37 std::unique_lock<std::mutex> lock(mutex);
38 while (!set)
39 cv.wait_for(lock, std::chrono::seconds(sec));
40 }
41 void Set(const std::string& error_ = std::string())
42 {
43 {
44 std::unique_lock<std::mutex> lock(mutex);
45 set = true;
46 error = error_;
47 }
48 cv.notify_all();
49 }
50 std::string GetError()
51 {
52 std::unique_lock<std::mutex> lock(mutex);
53 return error;
54 }
55 private:
56 std::mutex mutex;
57 std::condition_variable cv;
58 bool set;
59 std::string error;
60 };
61 26
62 // Strictly speaking in each test there should be a special implementation of 27 // Strictly speaking in each test there should be a special implementation of
63 // an interface, which is merely referenced by a wrapper and the latter should 28 // an interface, which is merely referenced by a wrapper and the latter should
64 // be injected into JsEngine or what ever. However the everthing a test often 29 // be injected into JsEngine or what ever. However the everthing a test often
65 // actually needs is the access to pending tasks. Therefore instantiation of 30 // actually needs is the access to pending tasks. Therefore instantiation of
66 // implemenation of an interface, creation of shared tasks and sharing them 31 // implemenation of an interface, creation of shared tasks and sharing them
67 // with tasks in test is located in this class. 32 // with tasks in test is located in this class.
68 // 33 //
69 // Task is passed as an additional template parameter instead of using traits 34 // Task is passed as an additional template parameter instead of using traits
70 // (CRTP does not work with types in derived class) merely to simplify the code 35 // (CRTP does not work with types in derived class) merely to simplify the code
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 void operator()(LogLevel logLevel, const std::string& message, 100 void operator()(LogLevel logLevel, const std::string& message,
136 const std::string& source) 101 const std::string& source)
137 { 102 {
138 throw std::runtime_error("Unexpected error: " + message); 103 throw std::runtime_error("Unexpected error: " + message);
139 } 104 }
140 }; 105 };
141 106
142 class ThrowingFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus:: FileSystem 107 class ThrowingFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus:: FileSystem
143 { 108 {
144 public: 109 public:
145 std::shared_ptr<std::istream> Read(const std::string& path) const 110 IOBuffer Read(const std::string& path) const
146 { 111 {
147 throw std::runtime_error("Not implemented"); 112 throw std::runtime_error("Not implemented");
148 } 113 }
149 void Read(const std::string& path, 114 void Read(const std::string& path,
150 const ReadCallback& callback) const 115 const ReadCallback& callback) const
151 { 116 {
152 throw std::runtime_error("Not implemented"); 117 throw std::runtime_error("Not implemented");
153 } 118 }
154 119
155 void Write(const std::string& path, std::istream& content) 120 void Write(const std::string& path, const IOBuffer& content)
156 { 121 {
157 throw std::runtime_error("Not implemented"); 122 throw std::runtime_error("Not implemented");
158 } 123 }
159 void Write(const std::string& path, std::shared_ptr<std::istream> data, 124 void Write(const std::string& path, const IOBuffer& data,
160 const Callback& callback) 125 const Callback& callback)
161 { 126 {
162 throw std::runtime_error("Not implemented"); 127 throw std::runtime_error("Not implemented");
163 } 128 }
164 129
165 void Move(const std::string& fromPath, const std::string& toPath) 130 void Move(const std::string& fromPath, const std::string& toPath)
166 { 131 {
167 throw std::runtime_error("Not implemented"); 132 throw std::runtime_error("Not implemented");
168 } 133 }
169 void Move(const std::string& fromPath, const std::string& toPath, 134 void Move(const std::string& fromPath, const std::string& toPath,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 public: 167 public:
203 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback&) override 168 void GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders , const GetCallback&) override
204 { 169 {
205 throw std::runtime_error("Unexpected GET: " + url); 170 throw std::runtime_error("Unexpected GET: " + url);
206 } 171 }
207 }; 172 };
208 173
209 class LazyFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus::File System 174 class LazyFileSystem : public AdblockPlus::IFileSystem, public AdblockPlus::File System
210 { 175 {
211 public: 176 public:
212 std::shared_ptr<std::istream> Read(const std::string& path) const 177 IOBuffer Read(const std::string& path) const
213 { 178 {
214 std::string dummyData(""); 179 std::string dummyData("");
215 if (path == "patterns.ini") 180 if (path == "patterns.ini")
216 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~"; 181 dummyData = "# Adblock Plus preferences\n[Subscription]\nurl=~fl~";
217 else if (path == "prefs.json") 182 else if (path == "prefs.json")
218 dummyData = "{}"; 183 dummyData = "{}";
219 return std::shared_ptr<std::istream>(new std::istringstream(dummyData)); 184 return IOBuffer(dummyData.cbegin(), dummyData.cend());
220 } 185 }
221 186
222 void Read(const std::string& path, const ReadCallback& callback) const 187 void Read(const std::string& path, const ReadCallback& callback) const
223 { 188 {
224 std::thread([this, path, callback] 189 std::thread([this, path, callback]
225 { 190 {
226 auto result = Read(path); 191 auto data = Read(path);
227 std::string content = AdblockPlus::Utils::Slurp(*result); 192 callback(std::move(data), "");
228 callback(std::move(content), ""); 193 }).detach();
229 }).detach(); 194 }
230 } 195
231 196 void Write(const std::string& path, const IOBuffer& content)
232 void Write(const std::string& path, std::istream& content) 197 {
233 { 198 }
234 } 199
235 200 void Write(const std::string& path, const IOBuffer& data,
236 void Write(const std::string& path, std::shared_ptr<std::istream> data,
237 const Callback& callback) 201 const Callback& callback)
238 { 202 {
239 std::thread([this, path, data, callback] 203 std::thread([this, path, data, callback]
240 { 204 {
241 Write(path, *data); 205 Write(path, data);
242 callback(""); 206 callback("");
243 }).detach(); 207 }).detach();
244 } 208 }
245 209
246 void Move(const std::string& fromPath, const std::string& toPath) 210 void Move(const std::string& fromPath, const std::string& toPath)
247 { 211 {
248 } 212 }
249 213
250 void Move(const std::string& fromPath, const std::string& toPath, 214 void Move(const std::string& fromPath, const std::string& toPath,
251 const Callback& callback) 215 const Callback& callback)
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 protected: 311 protected:
348 AdblockPlus::JsEnginePtr jsEngine; 312 AdblockPlus::JsEnginePtr jsEngine;
349 313
350 virtual void SetUp() 314 virtual void SetUp()
351 { 315 {
352 jsEngine = CreateJsEngine(); 316 jsEngine = CreateJsEngine();
353 } 317 }
354 }; 318 };
355 319
356 #endif 320 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld