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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #if !defined(JS_LATCH_H)
19 #define JS_LATCH_H
20
21 #include "../src/Latch.h"
22 #include "../src/JsEngineInternal.h"
23
24 /**
25 * A Latch partially exposed as a JS object within an engine.
26 *
27 * This latch class is designed for a common use case in these unit tests:
28 * 1) Evaluate an expression that causes execution of an asynchronous task,
29 * either I/O or web request/
30 * 2) Wait for the asynchronous task to complete.
31 * 3) Continue with the test.
32 * It would be better if our asynchronous tasks did this themselves,
33 * but they're not written that way.
34 *
35 * This class wraps a simple latch object and exposes it as both
36 * a C++ object and a JS object, yet with different interfaces.
37 * The C++ side sees the latch ordinarily (as a reference).
38 * The JS side sees only an \c Arrive() method.
39 * Because JS is single-threaded we don't expose a wait method.
40 *
41 * The life span of a C++ instance and that of a corresponding JS object
42 * are only partially linked together.
43 * Because this is for unit tests, we create the object on the C++ side
44 * and expose it on the JS side.
45 * If the C++ instance is destroyed, the JS object remains valid,
46 * although its \c Arrive() method becomes a no-op.
47 * If the JS instance is destroyed, the C++ object remains valid,
48 * but the handle to the JS object becomes null.
49 */
50 class JsTestingLatch
51 {
52 Latch latch;
53
54 // We need this for the destructor to run.
55 JsEngineInternal* engine;
56
57 V8PersistentNG<v8::Object> jsObject;
58
59 V8PersistentNG<v8::Object> JsObjectInitializer(const std::string& propertyName );
60
61 public:
62 /**
63 * Create a JS latch object as a property with a given name on the global obje ct.
64 *
65 * @param engine
66 * Engine in which to create instance
67 * @param propertyName
68 * Name of property on global object for the instance
69 * @param count
70 * Initial count for the latch
71 */
72 JsTestingLatch(JsEngineInternal* engine, const std::string& propertyName, int count = 1);
73
74 /**
75 * Destructor nullifies the JS object's pointer back to us
76 */
77 ~JsTestingLatch();
78
79 Latch& GetLatch()
80 {
81 return latch;
82 }
83
84 /**
85 * Convenience method. Forwarded from the underlying latch.
86 */
87 void Wait()
88 {
89 latch.Wait();
90 }
91 };
92
93 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld