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

Side by Side Diff: test/UpdateCheck.cpp

Issue 29372702: Issue #4826 - Use latch to replace thread-sleeping in tests
Patch Set: Created Jan. 19, 2017, 5:56 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
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 <functional> 18 #include <functional>
19 19
20 #include "BaseJsTest.h" 20 #include "BaseJsTest.h"
21 #include "JsLatch.h"
21 #include "../src/JsEngineTransition.h" 22 #include "../src/JsEngineTransition.h"
22 23
23 namespace 24 namespace
24 { 25 {
25 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr; 26 typedef std::shared_ptr<AdblockPlus::FilterEngine> FilterEnginePtr;
26 27
27 void FindAndReplace(std::string& source, const std::string& find, const std::s tring& replace) 28 void FindAndReplace(std::string& source, const std::string& find, const std::s tring& replace)
28 { 29 {
29 for (size_t pos = 0; (pos = source.find(find), pos) != std::string::npos; po s += replace.size()) 30 for (size_t pos = 0; (pos = source.find(find), pos) != std::string::npos; po s += replace.size())
30 source.replace(pos, find.size(), replace); 31 source.replace(pos, find.size(), replace);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 Destroy(); 94 Destroy();
94 webRequest.reset(); 95 webRequest.reset();
95 } 96 }
96 97
97 void Reset() 98 void Reset()
98 { 99 {
99 Destroy(); 100 Destroy();
100 Construct(); 101 Construct();
101 } 102 }
102 103
103 void ForceUpdateCheck() 104 void ForceUpdateCheckSynchronous()
104 { 105 {
105 filterEngine->ForceUpdateCheck( 106 Latch latch(1);
106 std::bind(&UpdateCheckTest::UpdateCallback, this, std::placeholders::_ 1)); 107 filterEngine->ForceUpdateCheck([&, this](const std::string& error) -> void
108 {
109 UpdateCallback(error);
110 latch.Arrive();
111 });
112 latch.Wait();
107 } 113 }
108 114
109 void EventCallback(AdblockPlus::JsValueList& params) 115 void EventCallback(AdblockPlus::JsValueList& params)
110 { 116 {
111 eventCallbackCalled = true; 117 eventCallbackCalled = true;
112 eventCallbackParams = params; 118 eventCallbackParams = params;
113 } 119 }
114 120
115 void UpdateCallback(const std::string& error) 121 void UpdateCallback(const std::string& error)
116 { 122 {
117 updateCallbackCalled = true; 123 updateCallbackCalled = true;
118 updateError = error; 124 updateError = error;
119 } 125 }
120 }; 126 };
121 } 127 }
122 128
123 TEST_F(UpdateCheckTest, RequestFailure) 129 TEST_F(UpdateCheckTest, RequestFailure)
124 { 130 {
125 webRequest->response.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE; 131 webRequest->response.status = AdblockPlus::WebRequest::NS_ERROR_FAILURE;
126 132
127 appInfo.name = "1"; 133 appInfo.name = "1";
128 appInfo.version = "3"; 134 appInfo.version = "3";
129 appInfo.application = "4"; 135 appInfo.application = "4";
130 appInfo.applicationVersion = "2"; 136 appInfo.applicationVersion = "2";
131 appInfo.developmentBuild = false; 137 appInfo.developmentBuild = false;
132 138
133 Reset(); 139 Reset();
134 ForceUpdateCheck(); 140 ForceUpdateCheckSynchronous();
135
136 std::this_thread::sleep_for(std::chrono::milliseconds(100));
137 141
138 ASSERT_FALSE(eventCallbackCalled); 142 ASSERT_FALSE(eventCallbackCalled);
139 ASSERT_TRUE(updateCallbackCalled); 143 ASSERT_TRUE(updateCallbackCalled);
140 ASSERT_FALSE(updateError.empty()); 144 ASSERT_FALSE(updateError.empty());
141 145
142 std::string expectedUrl(filterEngine->GetPref("update_url_release")->AsString( )); 146 std::string expectedUrl(filterEngine->GetPref("update_url_release")->AsString( ));
143 std::string platform = jsEngine->Evaluate("require('info').platform")->AsStrin g(); 147 std::string platform = jsEngine->Evaluate("require('info').platform")->AsStrin g();
144 std::string platformVersion = jsEngine->Evaluate("require('info').platformVers ion")->AsString(); 148 std::string platformVersion = jsEngine->Evaluate("require('info').platformVers ion")->AsString();
145 149
146 FindAndReplace(expectedUrl, "%NAME%", appInfo.name); 150 FindAndReplace(expectedUrl, "%NAME%", appInfo.name);
(...skipping 14 matching lines...) Expand all
161 webRequest->response.responseStatus = 200; 165 webRequest->response.responseStatus = 200;
162 webRequest->response.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"ht tps://foo.bar/\"}}"; 166 webRequest->response.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"ht tps://foo.bar/\"}}";
163 167
164 appInfo.name = "1"; 168 appInfo.name = "1";
165 appInfo.version = "3"; 169 appInfo.version = "3";
166 appInfo.application = "4"; 170 appInfo.application = "4";
167 appInfo.applicationVersion = "2"; 171 appInfo.applicationVersion = "2";
168 appInfo.developmentBuild = true; 172 appInfo.developmentBuild = true;
169 173
170 Reset(); 174 Reset();
171 ForceUpdateCheck(); 175 ForceUpdateCheckSynchronous();
172
173 std::this_thread::sleep_for(std::chrono::milliseconds(100));
174 176
175 ASSERT_TRUE(eventCallbackCalled); 177 ASSERT_TRUE(eventCallbackCalled);
176 ASSERT_EQ(1u, eventCallbackParams.size()); 178 ASSERT_EQ(1u, eventCallbackParams.size());
177 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0]->AsString()); 179 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0]->AsString());
178 ASSERT_TRUE(updateCallbackCalled); 180 ASSERT_TRUE(updateCallbackCalled);
179 ASSERT_TRUE(updateError.empty()); 181 ASSERT_TRUE(updateError.empty());
180 182
181 std::string expectedUrl(filterEngine->GetPref("update_url_devbuild")->AsString ()); 183 std::string expectedUrl(filterEngine->GetPref("update_url_devbuild")->AsString ());
182 std::string platform = jsEngine->Evaluate("require('info').platform")->AsStrin g(); 184 std::string platform = jsEngine->Evaluate("require('info').platform")->AsStrin g();
183 std::string platformVersion = jsEngine->Evaluate("require('info').platformVers ion")->AsString(); 185 std::string platformVersion = jsEngine->Evaluate("require('info').platformVers ion")->AsString();
(...skipping 16 matching lines...) Expand all
200 webRequest->response.responseStatus = 200; 202 webRequest->response.responseStatus = 200;
201 webRequest->response.responseText = "{\"1/4\": {\"version\":\"3.1\",\"url\":\" https://foo.bar/\"}}"; 203 webRequest->response.responseText = "{\"1/4\": {\"version\":\"3.1\",\"url\":\" https://foo.bar/\"}}";
202 204
203 appInfo.name = "1"; 205 appInfo.name = "1";
204 appInfo.version = "3"; 206 appInfo.version = "3";
205 appInfo.application = "4"; 207 appInfo.application = "4";
206 appInfo.applicationVersion = "2"; 208 appInfo.applicationVersion = "2";
207 appInfo.developmentBuild = true; 209 appInfo.developmentBuild = true;
208 210
209 Reset(); 211 Reset();
210 ForceUpdateCheck(); 212 ForceUpdateCheckSynchronous();
211
212 std::this_thread::sleep_for(std::chrono::milliseconds(100));
213 213
214 ASSERT_TRUE(eventCallbackCalled); 214 ASSERT_TRUE(eventCallbackCalled);
215 ASSERT_EQ(1u, eventCallbackParams.size()); 215 ASSERT_EQ(1u, eventCallbackParams.size());
216 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0]->AsString()); 216 ASSERT_EQ("https://foo.bar/", eventCallbackParams[0]->AsString());
217 ASSERT_TRUE(updateCallbackCalled); 217 ASSERT_TRUE(updateCallbackCalled);
218 ASSERT_TRUE(updateError.empty()); 218 ASSERT_TRUE(updateError.empty());
219 } 219 }
220 220
221 TEST_F(UpdateCheckTest, WrongApplication) 221 TEST_F(UpdateCheckTest, WrongApplication)
222 { 222 {
223 webRequest->response.status = AdblockPlus::WebRequest::NS_OK; 223 webRequest->response.status = AdblockPlus::WebRequest::NS_OK;
224 webRequest->response.responseStatus = 200; 224 webRequest->response.responseStatus = 200;
225 webRequest->response.responseText = "{\"1/3\": {\"version\":\"3.1\",\"url\":\" https://foo.bar/\"}}"; 225 webRequest->response.responseText = "{\"1/3\": {\"version\":\"3.1\",\"url\":\" https://foo.bar/\"}}";
226 226
227 appInfo.name = "1"; 227 appInfo.name = "1";
228 appInfo.version = "3"; 228 appInfo.version = "3";
229 appInfo.application = "4"; 229 appInfo.application = "4";
230 appInfo.applicationVersion = "2"; 230 appInfo.applicationVersion = "2";
231 appInfo.developmentBuild = true; 231 appInfo.developmentBuild = true;
232 232
233 Reset(); 233 Reset();
234 ForceUpdateCheck(); 234 ForceUpdateCheckSynchronous();
235
236 std::this_thread::sleep_for(std::chrono::milliseconds(100));
237 235
238 ASSERT_FALSE(eventCallbackCalled); 236 ASSERT_FALSE(eventCallbackCalled);
239 ASSERT_TRUE(updateCallbackCalled); 237 ASSERT_TRUE(updateCallbackCalled);
240 ASSERT_TRUE(updateError.empty()); 238 ASSERT_TRUE(updateError.empty());
241 } 239 }
242 240
243 TEST_F(UpdateCheckTest, WrongVersion) 241 TEST_F(UpdateCheckTest, WrongVersion)
244 { 242 {
245 webRequest->response.status = AdblockPlus::WebRequest::NS_OK; 243 webRequest->response.status = AdblockPlus::WebRequest::NS_OK;
246 webRequest->response.responseStatus = 200; 244 webRequest->response.responseStatus = 200;
247 webRequest->response.responseText = "{\"1\": {\"version\":\"3\",\"url\":\"http s://foo.bar/\"}}"; 245 webRequest->response.responseText = "{\"1\": {\"version\":\"3\",\"url\":\"http s://foo.bar/\"}}";
248 246
249 appInfo.name = "1"; 247 appInfo.name = "1";
250 appInfo.version = "3"; 248 appInfo.version = "3";
251 appInfo.application = "4"; 249 appInfo.application = "4";
252 appInfo.applicationVersion = "2"; 250 appInfo.applicationVersion = "2";
253 appInfo.developmentBuild = true; 251 appInfo.developmentBuild = true;
254 252
255 Reset(); 253 Reset();
256 ForceUpdateCheck(); 254 ForceUpdateCheckSynchronous();
257
258 std::this_thread::sleep_for(std::chrono::milliseconds(100));
259 255
260 ASSERT_FALSE(eventCallbackCalled); 256 ASSERT_FALSE(eventCallbackCalled);
261 ASSERT_TRUE(updateCallbackCalled); 257 ASSERT_TRUE(updateCallbackCalled);
262 ASSERT_TRUE(updateError.empty()); 258 ASSERT_TRUE(updateError.empty());
263 } 259 }
264 260
265 TEST_F(UpdateCheckTest, WrongURL) 261 TEST_F(UpdateCheckTest, WrongURL)
266 { 262 {
267 webRequest->response.status = AdblockPlus::WebRequest::NS_OK; 263 webRequest->response.status = AdblockPlus::WebRequest::NS_OK;
268 webRequest->response.responseStatus = 200; 264 webRequest->response.responseStatus = 200;
269 webRequest->response.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"ht tp://insecure/\"}}"; 265 webRequest->response.responseText = "{\"1\": {\"version\":\"3.1\",\"url\":\"ht tp://insecure/\"}}";
270 266
271 appInfo.name = "1"; 267 appInfo.name = "1";
272 appInfo.version = "3"; 268 appInfo.version = "3";
273 appInfo.application = "4"; 269 appInfo.application = "4";
274 appInfo.applicationVersion = "2"; 270 appInfo.applicationVersion = "2";
275 appInfo.developmentBuild = true; 271 appInfo.developmentBuild = true;
276 272
277 Reset(); 273 Reset();
278 ForceUpdateCheck(); 274 ForceUpdateCheckSynchronous();
279
280 std::this_thread::sleep_for(std::chrono::milliseconds(100));
281 275
282 ASSERT_FALSE(eventCallbackCalled); 276 ASSERT_FALSE(eventCallbackCalled);
283 ASSERT_TRUE(updateCallbackCalled); 277 ASSERT_TRUE(updateCallbackCalled);
284 ASSERT_FALSE(updateError.empty()); 278 ASSERT_FALSE(updateError.empty());
285 } 279 }
OLDNEW

Powered by Google App Engine
This is Rietveld