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

Unified Diff: test/JsLatch.cpp

Issue 29372702: Issue #4826 - Use latch to replace thread-sleeping in tests
Patch Set: stray comments Created Jan. 20, 2017, 1:29 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/JsLatch.h ('k') | test/LatchTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/JsLatch.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/test/JsLatch.cpp
@@ -0,0 +1,74 @@
+/*
+ * 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 "JsLatch.h"
+
+namespace
+{
+ /**
+ * Implementation of JS function "Arrive()" on latch objects.
+ */
+ v8::Handle<v8::Value> ArriveCallback(const v8::Arguments& arguments)
+ {
+ auto self = static_cast<JsTestingLatch *>(
+ v8::Local<v8::External>::Cast(arguments.Holder()->GetInternalField(0))->Value());
+ if (self)
+ {
+ self->GetLatch().Arrive();
+ }
+ return v8::Undefined();
+ }
+}
+
+/**
+ * \par Implementation Notes
+ * Internal field 0 of the JS object contains a copy of \c this.
+ */
+V8PersistentNG<v8::Object> JsTestingLatch::JsObjectInitializer(const std::string& propertyName)
+{
+ /*
+ * N.B. Member order ensures that \c engine has been initialized before this function runs.
+ */
+ V8ExecutionScope context(engine);
+ // Object template
+ auto jsObjectTemplate = v8::ObjectTemplate::New();
+ jsObjectTemplate->SetInternalFieldCount(1);
+ jsObjectTemplate->Set(engine->ToV8String("Arrive"), v8::FunctionTemplate::New(ArriveCallback));
+ // Object
+ auto localJsObject = jsObjectTemplate->NewInstance();
+ localJsObject->SetInternalField(0, v8::External::New(this));
+ // Property on global
+ engine->GetGlobalObject()->Set(engine->ToV8String(propertyName), localJsObject);
+ // Persistent
+ return V8PersistentNG<v8::Object>(engine->GetIsolate(), localJsObject);
+}
+
+JsTestingLatch::JsTestingLatch(JsEngineInternal* engine, const std::string& propertyName, int count)
+ : engine(engine), latch(count), jsObject(JsObjectInitializer(propertyName))
+{}
+
+JsTestingLatch::~JsTestingLatch()
+{
+ V8ExecutionScope context(engine);
+ auto obj = jsObject.Get(engine->GetIsolate());
+ // ensure object still exists
+ if (obj->IsObject())
+ {
+ obj->SetInternalField(0, v8::External::New(nullptr));
+ }
+ // Rather than changing the "Arrive" property, we handle a null pointer in the callback for it.
+}
« no previous file with comments | « test/JsLatch.h ('k') | test/LatchTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld