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

Unified Diff: test/LatchTest.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
Index: test/LatchTest.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/test/LatchTest.cpp
@@ -0,0 +1,91 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <gtest/gtest.h>
+#include "BaseJsTest.h"
+
+#include "JsLatch.h"
+#include "../src/JsEngineTransition.h"
+
+TEST(Latch, InstanceZero)
+{
+ Latch x(0);
+ ASSERT_TRUE(x.TryWait());
+}
+
+TEST(Latch, InstanceOne)
+{
+ Latch x(1);
+ ASSERT_FALSE(x.TryWait());
+}
+
+/*
+ * Verify that Wait() does not block when the latch is initialized to zero in the same thread.
+ */
+TEST(Latch, InitialZeroSameThread)
+{
+ ASSERT_NO_THROW(
+ {
+ Latch x(0);
+ x.Wait();
+ });
+}
+
+/*
+ * Verify that Wait() does not block when the latch becomes zero in the same thread.
+ */
+TEST(Latch, BecomesZeroSameThread)
+{
+ ASSERT_NO_THROW(
+ {
+ Latch x(1);
+ x.Arrive();
+ ASSERT_TRUE(x.TryWait());
+ x.Wait();
+ });
+}
+
+namespace
+{
+ struct JsTestingLatchTest
+ : public BaseJsTest
+ {
+ JsEngineInternal* engine;
+
+ void SetUp()
+ {
+ BaseJsTest::SetUp();
+ engine = ToInternal(jsEngine);
+ }
+ };
+}
+
+TEST_F(JsTestingLatchTest, Instance)
+{
+ JsTestingLatch x(engine, "foo");
+}
+
+TEST_F(JsTestingLatchTest, InstanceInIsolate)
+{
+ JsTestingLatch x(engine, "foo");
+ auto y = engine->Evaluate("foo");
+ ASSERT_FALSE(y->IsUndefined());
+ y = engine->Evaluate("foo.Arrive()");
+ ASSERT_TRUE(x.GetLatch().TryWait());
+ x.Wait();
+}
+

Powered by Google App Engine
This is Rietveld