File: CompositorWorkerThreadTest.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (244 lines) | stat: -rw-r--r-- 9,517 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "modules/compositorworker/CompositorWorkerThread.h"

#include "bindings/core/v8/ScriptSourceCode.h"
#include "bindings/core/v8/SourceLocation.h"
#include "bindings/core/v8/V8GCController.h"
#include "bindings/core/v8/WorkerOrWorkletScriptController.h"
#include "core/dom/CompositorProxyClient.h"
#include "core/inspector/ConsoleMessage.h"
#include "core/workers/InProcessWorkerObjectProxy.h"
#include "core/workers/ParentFrameTaskRunners.h"
#include "core/workers/WorkerBackingThread.h"
#include "core/workers/WorkerLoaderProxy.h"
#include "core/workers/WorkerOrWorkletGlobalScope.h"
#include "core/workers/WorkerThreadStartupData.h"
#include "platform/CrossThreadFunctional.h"
#include "platform/WaitableEvent.h"
#include "platform/WebThreadSupportingGC.h"
#include "platform/heap/Handle.h"
#include "platform/testing/TestingPlatformSupport.h"
#include "platform/testing/UnitTestHelpers.h"
#include "public/platform/Platform.h"
#include "public/platform/WebAddressSpace.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "wtf/PtrUtil.h"
#include <memory>

namespace blink {
namespace {

// A null InProcessWorkerObjectProxy, supplied when creating
// CompositorWorkerThreads.
class TestCompositorWorkerObjectProxy : public InProcessWorkerObjectProxy {
 public:
  static std::unique_ptr<TestCompositorWorkerObjectProxy> create() {
    return WTF::wrapUnique(new TestCompositorWorkerObjectProxy());
  }

  // (Empty) WorkerReportingProxy implementation:
  virtual void dispatchErrorEvent(const String& errorMessage,
                                  std::unique_ptr<SourceLocation>,
                                  int exceptionId) {}
  void reportConsoleMessage(MessageSource,
                            MessageLevel,
                            const String& message,
                            SourceLocation*) override {}
  void postMessageToPageInspector(const String&) override {}
  void didCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope*) override {}
  void didEvaluateWorkerScript(bool success) override {}
  void didCloseWorkerGlobalScope() override {}
  void willDestroyWorkerGlobalScope() override {}
  void didTerminateWorkerThread() override {}

 private:
  TestCompositorWorkerObjectProxy()
      : InProcessWorkerObjectProxy(nullptr,
                                   ParentFrameTaskRunners::create(nullptr)) {}
};

class TestCompositorProxyClient
    : public GarbageCollected<TestCompositorProxyClient>,
      public CompositorProxyClient {
  USING_GARBAGE_COLLECTED_MIXIN(TestCompositorProxyClient);

 public:
  TestCompositorProxyClient() {}

  void dispose() override {}
  void setGlobalScope(WorkerGlobalScope*) override {}
  void requestAnimationFrame() override {}
  void registerCompositorProxy(CompositorProxy*) override {}
  void unregisterCompositorProxy(CompositorProxy*) override {}
};

class CompositorWorkerTestPlatform : public TestingPlatformSupport {
 public:
  CompositorWorkerTestPlatform()
      : m_thread(WTF::wrapUnique(m_oldPlatform->createThread("Compositor"))) {}

  WebThread* compositorThread() const override { return m_thread.get(); }

  WebCompositorSupport* compositorSupport() override {
    return &m_compositorSupport;
  }

 private:
  std::unique_ptr<WebThread> m_thread;
  TestingCompositorSupport m_compositorSupport;
};

}  // namespace

class CompositorWorkerThreadTest : public ::testing::Test {
 public:
  void SetUp() override {
    CompositorWorkerThread::createSharedBackingThreadForTest();
    m_objectProxy = TestCompositorWorkerObjectProxy::create();
    m_securityOrigin =
        SecurityOrigin::create(KURL(ParsedURLString, "http://fake.url/"));
  }

  void TearDown() override {
    CompositorWorkerThread::clearSharedBackingThread();
  }

  std::unique_ptr<CompositorWorkerThread> createCompositorWorker() {
    std::unique_ptr<CompositorWorkerThread> workerThread =
        CompositorWorkerThread::create(nullptr, *m_objectProxy, 0);
    WorkerClients* clients = WorkerClients::create();
    provideCompositorProxyClientTo(clients, new TestCompositorProxyClient);
    workerThread->start(WorkerThreadStartupData::create(
        KURL(ParsedURLString, "http://fake.url/"), "fake user agent",
        "//fake source code", nullptr, DontPauseWorkerGlobalScopeOnStart,
        nullptr, "", m_securityOrigin.get(), clients, WebAddressSpaceLocal,
        nullptr, nullptr, WorkerV8Settings::Default()));
    return workerThread;
  }

  // Attempts to run some simple script for |worker|.
  void checkWorkerCanExecuteScript(WorkerThread* worker) {
    std::unique_ptr<WaitableEvent> waitEvent = WTF::makeUnique<WaitableEvent>();
    worker->workerBackingThread().backingThread().postTask(
        BLINK_FROM_HERE,
        crossThreadBind(&CompositorWorkerThreadTest::executeScriptInWorker,
                        crossThreadUnretained(this),
                        crossThreadUnretained(worker),
                        crossThreadUnretained(waitEvent.get())));
    waitEvent->wait();
  }

 private:
  void executeScriptInWorker(WorkerThread* worker, WaitableEvent* waitEvent) {
    WorkerOrWorkletScriptController* scriptController =
        worker->globalScope()->scriptController();
    bool evaluateResult = scriptController->evaluate(
        ScriptSourceCode("var counter = 0; ++counter;"));
    DCHECK(evaluateResult);
    waitEvent->signal();
  }

  RefPtr<SecurityOrigin> m_securityOrigin;
  std::unique_ptr<InProcessWorkerObjectProxy> m_objectProxy;
  ScopedTestingPlatformSupport<CompositorWorkerTestPlatform> m_platform;
};

TEST_F(CompositorWorkerThreadTest, Basic) {
  std::unique_ptr<CompositorWorkerThread> compositorWorker =
      createCompositorWorker();
  checkWorkerCanExecuteScript(compositorWorker.get());
  compositorWorker->terminateAndWait();
}

// Tests that the same WebThread is used for new workers if the WebThread is
// still alive.
TEST_F(CompositorWorkerThreadTest, CreateSecondAndTerminateFirst) {
  // Create the first worker and wait until it is initialized.
  std::unique_ptr<CompositorWorkerThread> firstWorker =
      createCompositorWorker();
  WebThreadSupportingGC* firstThread =
      &firstWorker->workerBackingThread().backingThread();
  checkWorkerCanExecuteScript(firstWorker.get());
  v8::Isolate* firstIsolate = firstWorker->isolate();
  ASSERT_TRUE(firstIsolate);

  // Create the second worker and immediately destroy the first worker.
  std::unique_ptr<CompositorWorkerThread> secondWorker =
      createCompositorWorker();
  // We don't use terminateAndWait here to avoid forcible termination.
  firstWorker->terminate();
  firstWorker->waitForShutdownForTesting();

  // Wait until the second worker is initialized. Verify that the second worker
  // is using the same thread and Isolate as the first worker.
  WebThreadSupportingGC* secondThread =
      &secondWorker->workerBackingThread().backingThread();
  ASSERT_EQ(firstThread, secondThread);

  v8::Isolate* secondIsolate = secondWorker->isolate();
  ASSERT_TRUE(secondIsolate);
  EXPECT_EQ(firstIsolate, secondIsolate);

  // Verify that the worker can still successfully execute script.
  checkWorkerCanExecuteScript(secondWorker.get());

  secondWorker->terminateAndWait();
}

// Tests that a new WebThread is created if all existing workers are terminated
// before a new worker is created.
TEST_F(CompositorWorkerThreadTest, TerminateFirstAndCreateSecond) {
  // Create the first worker, wait until it is initialized, and terminate it.
  std::unique_ptr<CompositorWorkerThread> compositorWorker =
      createCompositorWorker();
  WebThreadSupportingGC* firstThread =
      &compositorWorker->workerBackingThread().backingThread();
  checkWorkerCanExecuteScript(compositorWorker.get());

  // We don't use terminateAndWait here to avoid forcible termination.
  compositorWorker->terminate();
  compositorWorker->waitForShutdownForTesting();

  // Create the second worker. The backing thread is same.
  compositorWorker = createCompositorWorker();
  WebThreadSupportingGC* secondThread =
      &compositorWorker->workerBackingThread().backingThread();
  EXPECT_EQ(firstThread, secondThread);
  checkWorkerCanExecuteScript(compositorWorker.get());

  compositorWorker->terminateAndWait();
}

// Tests that v8::Isolate and WebThread are correctly set-up if a worker is
// created while another is terminating.
TEST_F(CompositorWorkerThreadTest, CreatingSecondDuringTerminationOfFirst) {
  std::unique_ptr<CompositorWorkerThread> firstWorker =
      createCompositorWorker();
  checkWorkerCanExecuteScript(firstWorker.get());
  v8::Isolate* firstIsolate = firstWorker->isolate();
  ASSERT_TRUE(firstIsolate);

  // Request termination of the first worker and create the second worker
  // as soon as possible.
  firstWorker->terminate();
  // We don't wait for its termination.
  // Note: We rely on the assumption that the termination steps don't run
  // on the worker thread so quickly. This could be a source of flakiness.

  std::unique_ptr<CompositorWorkerThread> secondWorker =
      createCompositorWorker();

  v8::Isolate* secondIsolate = secondWorker->isolate();
  ASSERT_TRUE(secondIsolate);
  EXPECT_EQ(firstIsolate, secondIsolate);

  // Verify that the isolate can run some scripts correctly in the second
  // worker.
  checkWorkerCanExecuteScript(secondWorker.get());
  secondWorker->terminateAndWait();
}

}  // namespace blink