summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/test/cxx/TestCrashCleanup.cpp
blob: 6a19cc0f6565111a4d16ade36be7abee0c9ecb87 (plain)
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
#include "TestCrashCleanup.h"

#include "base/task.h"
#include "mozilla/CondVar.h"
#include "mozilla/Mutex.h"

#include "IPDLUnitTests.h"  // fail etc.
#include "IPDLUnitTestSubprocess.h"

using mozilla::CondVar;
using mozilla::Mutex;
using mozilla::MutexAutoLock;

namespace mozilla {
namespace _ipdltest {

//-----------------------------------------------------------------------------
// parent

namespace {

// NB: this test does its own shutdown, rather than going through
// QuitParent(), because it's testing degenerate edge cases

void DeleteSubprocess(Mutex* mutex, CondVar* cvar) {
  MutexAutoLock lock(*mutex);

  gSubprocess->Destroy();
  gSubprocess = nullptr;

  cvar->Notify();
}

void DeleteTheWorld() {
  delete static_cast<TestCrashCleanupParent*>(gParentActor);
  gParentActor = nullptr;

  // needs to be synchronous to avoid affecting event ordering on
  // the main thread
  Mutex mutex MOZ_UNANNOTATED("TestCrashCleanup.DeleteTheWorld.mutex");
  CondVar cvar(mutex, "TestCrashCleanup.DeleteTheWorld.cvar");

  MutexAutoLock lock(mutex);

  XRE_GetIOMessageLoop()->PostTask(
      NewRunnableFunction("DeleteSubprocess", DeleteSubprocess, &mutex, &cvar));

  cvar.Wait();
}

void Done() {
  static NS_DEFINE_CID(kAppShellCID, NS_APPSHELL_CID);
  nsCOMPtr<nsIAppShell> appShell(do_GetService(kAppShellCID));
  appShell->Exit();

  passed(__FILE__);
}

}  // namespace

TestCrashCleanupParent::TestCrashCleanupParent() : mCleanedUp(false) {
  MOZ_COUNT_CTOR(TestCrashCleanupParent);
}

TestCrashCleanupParent::~TestCrashCleanupParent() {
  MOZ_COUNT_DTOR(TestCrashCleanupParent);

  if (!mCleanedUp) fail("should have been ActorDestroy()d!");
}

void TestCrashCleanupParent::Main() {
  // NB: has to be enqueued before IO thread's error notification
  MessageLoop::current()->PostTask(
      NewRunnableFunction("DeleteTheWorld", DeleteTheWorld));

  if (CallDIEDIEDIE()) fail("expected an error!");

  Close();

  MessageLoop::current()->PostTask(NewRunnableFunction("Done", Done));
}

//-----------------------------------------------------------------------------
// child

TestCrashCleanupChild::TestCrashCleanupChild() {
  MOZ_COUNT_CTOR(TestCrashCleanupChild);
}

TestCrashCleanupChild::~TestCrashCleanupChild() {
  MOZ_COUNT_DTOR(TestCrashCleanupChild);
}

mozilla::ipc::IPCResult TestCrashCleanupChild::AnswerDIEDIEDIE() {
  _exit(0);
  MOZ_CRASH("unreached");
}

}  // namespace _ipdltest
}  // namespace mozilla