Bug 1138620 - Part 2: Test case for NS_NewUnmonitoredThread(). r=khuey

This commit is contained in:
Cervantes Yu 2015-04-21 18:23:48 +08:00
parent 471994fb56
commit 04a88e9a92
3 changed files with 167 additions and 0 deletions

View File

@ -92,6 +92,47 @@ private:
bool mEnabled;
bool mShutdown;
nsRefPtr<ContentParent> mPreallocatedAppProcess;
#if defined(MOZ_NUWA_PROCESS) && defined(ENABLE_TESTS)
// For testing NS_NewUnmonitoredThread().
void CreateUnmonitoredThread();
void DestroyUnmonitoredThread();
class UnmonitoredThreadRunnable : public nsRunnable
{
public:
UnmonitoredThreadRunnable()
: mMonitor("UnmonitoredThreadRunnable")
, mEnabled(true)
{ }
NS_IMETHODIMP Run() override
{
MonitorAutoLock mon(mMonitor);
while (mEnabled) {
mMonitor.Wait();
}
return NS_OK;
}
void Disable()
{
MonitorAutoLock mon(mMonitor);
mEnabled = false;
mMonitor.NotifyAll();
}
private:
~UnmonitoredThreadRunnable() { }
Monitor mMonitor;
bool mEnabled;
};
nsCOMPtr<nsIThread> mUnmonitoredThread;
nsRefPtr<UnmonitoredThreadRunnable> mUnmonitoredThreadRunnable;
#endif
};
/* static */ StaticRefPtr<PreallocatedProcessManagerImpl>
@ -155,6 +196,40 @@ PreallocatedProcessManagerImpl::Observe(nsISupports* aSubject,
return NS_OK;
}
#if defined(MOZ_NUWA_PROCESS) && defined(ENABLE_TESTS)
void
PreallocatedProcessManagerImpl::CreateUnmonitoredThread()
{
if (Preferences::GetBool("dom.ipc.newUnmonitoredThread.testMode")) {
// Create an unmonitored thread and dispatch a blocking runnable in test
// case startup.
nsresult rv = NS_NewUnmonitoredThread(getter_AddRefs(mUnmonitoredThread),
nullptr);
NS_ENSURE_SUCCESS_VOID(rv);
mUnmonitoredThreadRunnable = new UnmonitoredThreadRunnable();
mUnmonitoredThread->Dispatch(mUnmonitoredThreadRunnable,
NS_DISPATCH_NORMAL);
}
}
void
PreallocatedProcessManagerImpl::DestroyUnmonitoredThread()
{
// Cleanup after the test case finishes.
if (mUnmonitoredThreadRunnable) {
mUnmonitoredThreadRunnable->Disable();
}
if (mUnmonitoredThread) {
mUnmonitoredThread->Shutdown();
}
mUnmonitoredThreadRunnable = nullptr;
mUnmonitoredThread = nullptr;
}
#endif
void
PreallocatedProcessManagerImpl::RereadPrefs()
{
@ -180,6 +255,11 @@ PreallocatedProcessManagerImpl::Enable()
mEnabled = true;
#ifdef MOZ_NUWA_PROCESS
#ifdef ENABLE_TESTS
// For testing New_UnmonitoredThread().
CreateUnmonitoredThread();
#endif
ScheduleDelayedNuwaFork();
#else
AllocateAfterDelay();
@ -372,6 +452,11 @@ PreallocatedProcessManagerImpl::Disable()
mEnabled = false;
#ifdef MOZ_NUWA_PROCESS
#ifdef ENABLE_TESTS
// Shut down the test-only unmonitored thread.
DestroyUnmonitoredThread();
#endif
// Cancel pending fork.
if (mPreallocateAppProcessTask) {
mPreallocateAppProcessTask->Cancel();

View File

@ -17,6 +17,8 @@ skip-if = buildapp == 'b2g' || buildapp == 'mulet'
skip-if = toolkit != 'gonk'
[test_NuwaProcessDeadlock.html]
skip-if = toolkit != 'gonk'
[test_NewUnmonitoredThread.html]
skip-if = toolkit != 'gonk'
[test_child_docshell.html]
skip-if = toolkit == 'cocoa' # disabled due to hangs, see changeset 6852e7c47edf
[test_CrashService_crash.html]

View File

@ -0,0 +1,80 @@
<!DOCTYPE HTML>
<html>
<!--
Test if Nuwa process created successfully.
-->
<head>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="setup()">
<script type="application/javascript;version=1.7">
"use strict";
function runTest()
{
info("Launch the Nuwa process");
let cpmm = SpecialPowers.Cc["@mozilla.org/childprocessmessagemanager;1"]
.getService(SpecialPowers.Ci.nsISyncMessageSender);
let seenNuwaReady = false;
let msgHandler = {
receiveMessage: function receiveMessage(msg) {
msg = SpecialPowers.wrap(msg);
if (msg.name == 'TEST-ONLY:nuwa-ready') {
ok(true, "Got nuwa-ready");
is(seenNuwaReady, false, "Already received nuwa ready");
seenNuwaReady = true;
} else if (msg.name == 'TEST-ONLY:nuwa-add-new-process') {
ok(true, "Got nuwa-add-new-process");
is(seenNuwaReady, true, "Receive nuwa-add-new-process before nuwa-ready");
shutdown();
}
}
};
function shutdown() {
info("Shut down the test case");
cpmm.removeMessageListener("TEST-ONLY:nuwa-ready", msgHandler);
cpmm.removeMessageListener("TEST-ONLY:nuwa-add-new-process", msgHandler);
SimpleTest.finish();
}
cpmm.addMessageListener("TEST-ONLY:nuwa-ready", msgHandler);
cpmm.addMessageListener("TEST-ONLY:nuwa-add-new-process", msgHandler);
// Setting this pref to true should cause us to prelaunch a process.
SpecialPowers.setBoolPref('dom.ipc.processPrelaunch.enabled', true);
}
function setup2()
{
info("Enable the Nuwa process to test the unmonitored thread");
SpecialPowers.pushPrefEnv({
'set': [
['dom.ipc.processPrelaunch.enabled', false],
['dom.ipc.preallocatedProcessManager.testMode', true]
]
}, runTest);
}
function setup()
{
info("Create an unmonitored thread.");
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({
'set': [
// For testing NS_NewUnmonitoredThread()
['dom.ipc.newUnmonitoredThread.testMode', true],
]
}, setup2);
}
</script>
</body>
</html>