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

Unified Diff: test/JsLatch.h

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/JsLatch.h
===================================================================
new file mode 100644
--- /dev/null
+++ b/test/JsLatch.h
@@ -0,0 +1,93 @@
+/*
+ * 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/>.
+ */
+
+#if !defined(JS_LATCH_H)
+#define JS_LATCH_H
+
+#include "../src/Latch.h"
+#include "../src/JsEngineInternal.h"
+
+/**
+ * A Latch partially exposed as a JS object within an engine.
+ *
+ * This latch class is designed for a common use case in these unit tests:
+ * 1) Evaluate an expression that causes execution of an asynchronous task,
+ * either I/O or web request/
+ * 2) Wait for the asynchronous task to complete.
+ * 3) Continue with the test.
+ * It would be better if our asynchronous tasks did this themselves,
+ * but they're not written that way.
+ *
+ * This class wraps a simple latch object and exposes it as both
+ * a C++ object and a JS object, yet with different interfaces.
+ * The C++ side sees the latch ordinarily (as a reference).
+ * The JS side sees only an \c Arrive() method.
+ * Because JS is single-threaded we don't expose a wait method.
+ *
+ * The life span of a C++ instance and that of a corresponding JS object
+ * are only partially linked together.
+ * Because this is for unit tests, we create the object on the C++ side
+ * and expose it on the JS side.
+ * If the C++ instance is destroyed, the JS object remains valid,
+ * although its \c Arrive() method becomes a no-op.
+ * If the JS instance is destroyed, the C++ object remains valid,
+ * but the handle to the JS object becomes null.
+ */
+class JsTestingLatch
+{
+ Latch latch;
+
+ // We need this for the destructor to run.
+ JsEngineInternal* engine;
+
+ V8PersistentNG<v8::Object> jsObject;
+
+ V8PersistentNG<v8::Object> JsObjectInitializer(const std::string& propertyName);
+
+public:
+ /**
+ * Create a JS latch object as a property with a given name on the global object.
+ *
+ * @param engine
+ * Engine in which to create instance
+ * @param propertyName
+ * Name of property on global object for the instance
+ * @param count
+ * Initial count for the latch
+ */
+ JsTestingLatch(JsEngineInternal* engine, const std::string& propertyName, int count = 1);
+
+ /**
+ * Destructor nullifies the JS object's pointer back to us
+ */
+ ~JsTestingLatch();
+
+ Latch& GetLatch()
+ {
+ return latch;
+ }
+
+ /**
+ * Convenience method. Forwarded from the underlying latch.
+ */
+ void Wait()
+ {
+ latch.Wait();
+ }
+};
+
+#endif

Powered by Google App Engine
This is Rietveld