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

Unified Diff: test/WebRequest.cpp

Issue 29372702: Issue #4826 - Use latch to replace thread-sleeping in tests
Patch Set: Created Jan. 19, 2017, 5:56 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« test/Prefs.cpp ('K') | « test/UpdateCheck.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/WebRequest.cpp
===================================================================
--- a/test/WebRequest.cpp
+++ b/test/WebRequest.cpp
@@ -17,6 +17,7 @@
#include <sstream>
#include "BaseJsTest.h"
+#include "JsLatch.h"
namespace
{
@@ -25,8 +26,6 @@
public:
AdblockPlus::ServerResponse GET(const std::string& url, const AdblockPlus::HeaderList& requestHeaders) const
{
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
-
AdblockPlus::ServerResponse result;
result.status = NS_OK;
result.responseStatus = 123;
@@ -64,9 +63,9 @@
TEST_F(MockWebRequestTest, SuccessfulRequest)
{
- jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(result) {foo = result;} )");
- ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
Eric 2017/01/19 18:38:07 I eliminated this test (and the sleep_for call abo
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
+ JsTestingLatch latch(engine, "latch");
+ jsEngine->Evaluate("_webRequest.GET('http://example.com/', {X: 'Y'}, function(result) {foo = result; latch.Arrive();} )");
+ latch.Wait();
ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->AsInt());
ASSERT_EQ(123, jsEngine->Evaluate("foo.responseStatus")->AsInt());
ASSERT_EQ("http://example.com/\nX\nY", jsEngine->Evaluate("foo.responseText")->AsString());
@@ -78,11 +77,9 @@
{
// This URL should redirect to easylist-downloads.adblockplus.org and we
// should get the actual filter list back.
- jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.org/easylist.txt', {}, function(result) {foo = result;} )");
- do
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
- } while (jsEngine->Evaluate("this.foo")->IsUndefined());
+ JsTestingLatch latch(engine, "latch");
+ jsEngine->Evaluate("_webRequest.GET('https://easylist-downloads.adblockplus.org/easylist.txt', {}, function(result) {foo = result; latch.Arrive();} )");
+ latch.Wait();
ASSERT_EQ("text/plain", jsEngine->Evaluate("foo.responseHeaders['content-type'].substr(0, 10)")->AsString());
ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("foo.status")->AsInt());
ASSERT_EQ(200, jsEngine->Evaluate("foo.responseStatus")->AsInt());
@@ -94,7 +91,7 @@
TEST_F(DefaultWebRequestTest, XMLHttpRequest)
{
AdblockPlus::FilterEngine filterEngine(jsEngine);
-
+ JsTestingLatch latch(engine, "latch");
jsEngine->Evaluate("\
var result;\
var request = new XMLHttpRequest();\
@@ -102,13 +99,10 @@
request.setRequestHeader('X', 'Y');\
request.setRequestHeader('X2', 'Y2');\
request.overrideMimeType('text/plain');\
- request.addEventListener('load', function() {result = request.responseText;}, false);\
- request.addEventListener('error', function() {result = 'error';}, false);\
+ request.addEventListener('load', function() {result = request.responseText; latch.Arrive();}, false);\
+ request.addEventListener('error', function() {result = 'error'; latch.Arrive();}, false);\
request.send(null);");
- do
- {
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
- } while (jsEngine->Evaluate("result")->IsUndefined());
+ latch.Wait();
ASSERT_EQ(AdblockPlus::WebRequest::NS_OK, jsEngine->Evaluate("request.channel.status")->AsInt());
ASSERT_EQ(200, jsEngine->Evaluate("request.status")->AsInt());
ASSERT_EQ("[Adblock Plus ", jsEngine->Evaluate("result.substr(0, 14)")->AsString());
« test/Prefs.cpp ('K') | « test/UpdateCheck.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld