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

Unified Diff: test/GlobalJsObject.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
« no previous file with comments | « test/FilterEngine.cpp ('k') | test/JsLatch.h » ('j') | test/Prefs.cpp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/GlobalJsObject.cpp
===================================================================
--- a/test/GlobalJsObject.cpp
+++ b/test/GlobalJsObject.cpp
@@ -16,6 +16,7 @@
*/
#include "BaseJsTest.h"
+#include "JsLatch.h"
namespace
{
@@ -26,17 +27,19 @@
TEST_F(GlobalJsObjectTest, SetTimeout)
{
- jsEngine->Evaluate("setTimeout(function() {foo = 'bar';}, 100)");
- ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
+ JsTestingLatch latch(engine, "latch");
+ jsEngine->Evaluate("setTimeout(function() {foo = 'bar'; latch.Arrive();}, 100)");
+ ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); // RACE
Eric 2017/01/19 18:38:07 The race condition in these timeout tests is mostl
+ latch.Wait();
ASSERT_EQ("bar", jsEngine->Evaluate("this.foo")->AsString());
}
TEST_F(GlobalJsObjectTest, SetTimeoutWithArgs)
{
- jsEngine->Evaluate("setTimeout(function(s) {foo = s;}, 100, 'foobar')");
- ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined());
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
+ JsTestingLatch latch(engine, "latch");
+ jsEngine->Evaluate("setTimeout(function(s) {foo = s; latch.Arrive();}, 100, 'foobar')");
+ ASSERT_TRUE(jsEngine->Evaluate("this.foo")->IsUndefined()); // RACE
+ latch.Wait();
ASSERT_EQ("foobar", jsEngine->Evaluate("this.foo")->AsString());
}
@@ -48,9 +51,10 @@
TEST_F(GlobalJsObjectTest, SetMultipleTimeouts)
{
+ JsTestingLatch latch(engine, "latch", 2); // two arrivals
jsEngine->Evaluate("foo = []");
- jsEngine->Evaluate("setTimeout(function(s) {foo.push('1');}, 100)");
- jsEngine->Evaluate("setTimeout(function(s) {foo.push('2');}, 150)");
- std::this_thread::sleep_for(std::chrono::milliseconds(200));
+ jsEngine->Evaluate("setTimeout(function(s) {foo.push('1'); latch.Arrive();}, 100)");
+ jsEngine->Evaluate("setTimeout(function(s) {foo.push('2'); latch.Arrive();}, 150)");
+ latch.Wait();
ASSERT_EQ("1,2", jsEngine->Evaluate("this.foo")->AsString());
}
« no previous file with comments | « test/FilterEngine.cpp ('k') | test/JsLatch.h » ('j') | test/Prefs.cpp » ('J')

Powered by Google App Engine
This is Rietveld