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

Side by Side Diff: test/FileSystemJsObject.cpp

Issue 29372702: Issue #4826 - Use latch to replace thread-sleeping in tests
Patch Set: stray comments Created Jan. 20, 2017, 1:29 p.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 | « test/BaseJsTest.cpp ('k') | test/FilterEngine.cpp » ('j') | no next file with comments »
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 #include <sstream> 18 #include <sstream>
19 #include "BaseJsTest.h" 19 #include "BaseJsTest.h"
20 #include "JsLatch.h"
21 #include "../src/JsEngineTransition.h"
20 22
21 namespace 23 namespace
22 { 24 {
23 class MockFileSystem : public AdblockPlus::FileSystem 25 class MockFileSystem : public AdblockPlus::FileSystem
24 { 26 {
25 public: 27 public:
26 bool success; 28 bool success;
27 std::string contentToRead; 29 std::string contentToRead;
28 std::string lastWrittenPath; 30 std::string lastWrittenPath;
29 std::string lastWrittenContent; 31 std::string lastWrittenContent;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 { 95 {
94 if (!success) 96 if (!success)
95 throw std::runtime_error("Unable to stat " + path); 97 throw std::runtime_error("Unable to stat " + path);
96 return path; 98 return path;
97 } 99 }
98 }; 100 };
99 101
100 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content, 102 void ReadFile(AdblockPlus::JsEnginePtr jsEngine, std::string& content,
101 std::string& error) 103 std::string& error)
102 { 104 {
103 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r})"); 105 auto engine = ToInternal(jsEngine);
104 std::this_thread::sleep_for(std::chrono::milliseconds(50)); 106 JsTestingLatch latch(engine, "latch");
107 jsEngine->Evaluate("_fileSystem.read('', function(r) {result = r; latch.Arri ve();})");
108 latch.Wait();
105 content = jsEngine->Evaluate("result.content")->AsString(); 109 content = jsEngine->Evaluate("result.content")->AsString();
106 error = jsEngine->Evaluate("result.error")->AsString(); 110 error = jsEngine->Evaluate("result.error")->AsString();
107 } 111 }
108 112
109 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr; 113 typedef std::shared_ptr<MockFileSystem> MockFileSystemPtr;
110 114
111 class FileSystemJsObjectTest : public BaseJsTest 115 class FileSystemJsObjectTest : public BaseJsTest
112 { 116 {
113 protected: 117 protected:
114 MockFileSystemPtr mockFileSystem; 118 MockFileSystemPtr mockFileSystem;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 mockFileSystem->success = false; 153 mockFileSystem->success = false;
150 std::string content; 154 std::string content;
151 std::string error; 155 std::string error;
152 ReadFile(jsEngine, content, error); 156 ReadFile(jsEngine, content, error);
153 ASSERT_NE("", error); 157 ASSERT_NE("", error);
154 ASSERT_EQ("", content); 158 ASSERT_EQ("", content);
155 } 159 }
156 160
157 TEST_F(FileSystemJsObjectTest, Write) 161 TEST_F(FileSystemJsObjectTest, Write)
158 { 162 {
159 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ; 163 JsTestingLatch latch(engine, "latch");
160 std::this_thread::sleep_for(std::chrono::milliseconds(50)); 164 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e; la tch.Arrive();})");
165 latch.Wait();
161 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath); 166 ASSERT_EQ("foo", mockFileSystem->lastWrittenPath);
162 ASSERT_EQ("bar", mockFileSystem->lastWrittenContent); 167 ASSERT_EQ("bar", mockFileSystem->lastWrittenContent);
163 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); 168 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString());
164 } 169 }
165 170
166 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments) 171 TEST_F(FileSystemJsObjectTest, WriteIllegalArguments)
167 { 172 {
168 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()")); 173 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write()"));
169 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')")); 174 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.write('', '', '')"));
170 } 175 }
171 176
172 TEST_F(FileSystemJsObjectTest, WriteError) 177 TEST_F(FileSystemJsObjectTest, WriteError)
173 { 178 {
174 mockFileSystem->success = false; 179 mockFileSystem->success = false;
175 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e})") ; 180 JsTestingLatch latch(engine, "latch");
176 std::this_thread::sleep_for(std::chrono::milliseconds(50)); 181 jsEngine->Evaluate("_fileSystem.write('foo', 'bar', function(e) {error = e; la tch.Arrive();})");
182 latch.Wait();
177 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); 183 ASSERT_NE("", jsEngine->Evaluate("error")->AsString());
178 } 184 }
179 185
180 TEST_F(FileSystemJsObjectTest, Move) 186 TEST_F(FileSystemJsObjectTest, Move)
181 { 187 {
182 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 188 JsTestingLatch latch(engine, "latch");
183 std::this_thread::sleep_for(std::chrono::milliseconds(50)); 189 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e; lat ch.Arrive();})");
190 latch.Wait();
184 ASSERT_EQ("foo", mockFileSystem->movedFrom); 191 ASSERT_EQ("foo", mockFileSystem->movedFrom);
185 ASSERT_EQ("bar", mockFileSystem->movedTo); 192 ASSERT_EQ("bar", mockFileSystem->movedTo);
186 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); 193 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString());
187 } 194 }
188 195
189 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments) 196 TEST_F(FileSystemJsObjectTest, MoveIllegalArguments)
190 { 197 {
191 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()")); 198 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move()"));
192 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')")); 199 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.move('', '', '')"));
193 } 200 }
194 201
195 TEST_F(FileSystemJsObjectTest, MoveError) 202 TEST_F(FileSystemJsObjectTest, MoveError)
196 { 203 {
197 mockFileSystem->success = false; 204 mockFileSystem->success = false;
198 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e})"); 205 JsTestingLatch latch(engine, "latch");
199 std::this_thread::sleep_for(std::chrono::milliseconds(50)); 206 jsEngine->Evaluate("_fileSystem.move('foo', 'bar', function(e) {error = e; lat ch.Arrive();})");
207 latch.Wait();
200 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); 208 ASSERT_NE("", jsEngine->Evaluate("error")->AsString());
201 } 209 }
202 210
203 TEST_F(FileSystemJsObjectTest, Remove) 211 TEST_F(FileSystemJsObjectTest, Remove)
204 { 212 {
205 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 213 JsTestingLatch latch(engine, "latch");
206 std::this_thread::sleep_for(std::chrono::milliseconds(50)); 214 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e; latch.Ar rive();})");
215 latch.Wait();
207 ASSERT_EQ("foo", mockFileSystem->removedPath); 216 ASSERT_EQ("foo", mockFileSystem->removedPath);
208 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString()); 217 ASSERT_EQ("", jsEngine->Evaluate("error")->AsString());
209 } 218 }
210 219
211 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments) 220 TEST_F(FileSystemJsObjectTest, RemoveIllegalArguments)
212 { 221 {
213 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()")); 222 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove()"));
214 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')")); 223 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.remove('', '')"));
215 } 224 }
216 225
217 TEST_F(FileSystemJsObjectTest, RemoveError) 226 TEST_F(FileSystemJsObjectTest, RemoveError)
218 { 227 {
219 mockFileSystem->success = false; 228 mockFileSystem->success = false;
220 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e})"); 229 JsTestingLatch latch(engine, "latch");
221 std::this_thread::sleep_for(std::chrono::milliseconds(50)); 230 jsEngine->Evaluate("_fileSystem.remove('foo', function(e) {error = e; latch.Ar rive();})");
231 latch.Wait();
222 ASSERT_NE("", jsEngine->Evaluate("error")->AsString()); 232 ASSERT_NE("", jsEngine->Evaluate("error")->AsString());
223 } 233 }
224 234
225 TEST_F(FileSystemJsObjectTest, Stat) 235 TEST_F(FileSystemJsObjectTest, Stat)
226 { 236 {
227 mockFileSystem->statExists = true; 237 mockFileSystem->statExists = true;
228 mockFileSystem->statIsDirectory= false; 238 mockFileSystem->statIsDirectory= false;
229 mockFileSystem->statIsFile = true; 239 mockFileSystem->statIsFile = true;
230 mockFileSystem->statLastModified = 1337; 240 mockFileSystem->statLastModified = 1337;
231 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 241 JsTestingLatch latch(engine, "latch");
232 std::this_thread::sleep_for(std::chrono::milliseconds(50)); 242 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r; latch.Arr ive();})");
243 latch.Wait();
233 ASSERT_EQ("foo", mockFileSystem->statPath); 244 ASSERT_EQ("foo", mockFileSystem->statPath);
234 ASSERT_EQ("", jsEngine->Evaluate("result.error")->AsString()); 245 ASSERT_EQ("", jsEngine->Evaluate("result.error")->AsString());
235 ASSERT_TRUE(jsEngine->Evaluate("result.exists")->AsBool()); 246 ASSERT_TRUE(jsEngine->Evaluate("result.exists")->AsBool());
236 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory")->AsBool()); 247 ASSERT_FALSE(jsEngine->Evaluate("result.isDirectory")->AsBool());
237 ASSERT_TRUE(jsEngine->Evaluate("result.isFile")->AsBool()); 248 ASSERT_TRUE(jsEngine->Evaluate("result.isFile")->AsBool());
238 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified")->AsInt()); 249 ASSERT_EQ(1337, jsEngine->Evaluate("result.lastModified")->AsInt());
239 } 250 }
240 251
241 TEST_F(FileSystemJsObjectTest, StatIllegalArguments) 252 TEST_F(FileSystemJsObjectTest, StatIllegalArguments)
242 { 253 {
243 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()")); 254 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat()"));
244 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')")); 255 ASSERT_ANY_THROW(jsEngine->Evaluate("_fileSystem.stat('', '')"));
245 } 256 }
246 257
247 TEST_F(FileSystemJsObjectTest, StatError) 258 TEST_F(FileSystemJsObjectTest, StatError)
248 { 259 {
249 mockFileSystem->success = false; 260 mockFileSystem->success = false;
250 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r})"); 261 JsTestingLatch latch(engine, "latch");
251 std::this_thread::sleep_for(std::chrono::milliseconds(50)); 262 jsEngine->Evaluate("_fileSystem.stat('foo', function(r) {result = r; latch.Arr ive();})");
263 latch.Wait();
252 ASSERT_NE("", jsEngine->Evaluate("result.error")->AsString()); 264 ASSERT_NE("", jsEngine->Evaluate("result.error")->AsString());
253 } 265 }
OLDNEW
« no previous file with comments | « test/BaseJsTest.cpp ('k') | test/FilterEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld