ipc/ipdl/test/cxx/TestCrashCleanup.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 #include "TestCrashCleanup.h"
     3 #include "mozilla/CondVar.h"
     4 #include "mozilla/Mutex.h"
     6 #include "IPDLUnitTests.h"      // fail etc.
     7 #include "IPDLUnitTestSubprocess.h"
     9 using mozilla::CondVar;
    10 using mozilla::Mutex;
    11 using mozilla::MutexAutoLock;
    13 namespace mozilla {
    14 namespace _ipdltest {
    16 //-----------------------------------------------------------------------------
    17 // parent
    19 namespace {
    21 // NB: this test does its own shutdown, rather than going through
    22 // QuitParent(), because it's testing degenerate edge cases
    24 void DeleteSubprocess(Mutex* mutex, CondVar* cvar)
    25 {
    26     MutexAutoLock lock(*mutex);
    28     delete gSubprocess;
    29     gSubprocess = nullptr;
    31     cvar->Notify();
    32 }
    34 void DeleteTheWorld()
    35 {
    36     delete static_cast<TestCrashCleanupParent*>(gParentActor);
    37     gParentActor = nullptr;
    39     // needs to be synchronous to avoid affecting event ordering on
    40     // the main thread
    41     Mutex mutex("TestCrashCleanup.DeleteTheWorld.mutex");
    42     CondVar cvar(mutex, "TestCrashCleanup.DeleteTheWorld.cvar");
    44     MutexAutoLock lock(mutex);
    46     XRE_GetIOMessageLoop()->PostTask(
    47       FROM_HERE,
    48       NewRunnableFunction(DeleteSubprocess, &mutex, &cvar));
    50     cvar.Wait();
    51 }
    53 void Done()
    54 {
    55   static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
    56   nsCOMPtr<nsIAppShell> appShell (do_GetService(kAppShellCID));
    57   appShell->Exit();
    59   passed(__FILE__);
    60 }
    62 } // namespace <anon>
    64 TestCrashCleanupParent::TestCrashCleanupParent() : mCleanedUp(false)
    65 {
    66     MOZ_COUNT_CTOR(TestCrashCleanupParent);
    67 }
    69 TestCrashCleanupParent::~TestCrashCleanupParent()
    70 {
    71     MOZ_COUNT_DTOR(TestCrashCleanupParent);
    73     if (!mCleanedUp)
    74         fail("should have been ActorDestroy()d!");
    75 }
    77 void
    78 TestCrashCleanupParent::Main()
    79 {
    80     // NB: has to be enqueued before IO thread's error notification
    81     MessageLoop::current()->PostTask(
    82         FROM_HERE, NewRunnableFunction(DeleteTheWorld));
    84     if (CallDIEDIEDIE())
    85         fail("expected an error!");
    87     Close();
    89     MessageLoop::current()->PostTask(FROM_HERE, NewRunnableFunction(Done));
    90 }
    93 //-----------------------------------------------------------------------------
    94 // child
    96 TestCrashCleanupChild::TestCrashCleanupChild()
    97 {
    98     MOZ_COUNT_CTOR(TestCrashCleanupChild);
    99 }
   101 TestCrashCleanupChild::~TestCrashCleanupChild()
   102 {
   103     MOZ_COUNT_DTOR(TestCrashCleanupChild);
   104 }
   106 bool
   107 TestCrashCleanupChild::AnswerDIEDIEDIE()
   108 {
   109     _exit(0);
   110     NS_RUNTIMEABORT("unreached");
   111     return false;
   112 }
   115 } // namespace _ipdltest
   116 } // namespace mozilla

mercurial