summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/test/cxx/TestOpens.cpp
blob: cb1ed00b59b2d7d258b316f0b9d0dbd58fc6735a (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
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
245
246
247
248
249
250
251
#include "base/task.h"
#include "base/thread.h"

#include "TestOpens.h"

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

using namespace mozilla::ipc;

using base::ProcessHandle;
using base::Thread;

namespace mozilla {
// NB: this is generally bad style, but I am lazy.
using namespace _ipdltest;
using namespace _ipdltest2;

static MessageLoop* gMainThread;

static void
AssertNotMainThread()
{
    if (!gMainThread)
        fail("gMainThread is not initialized");
    if (MessageLoop::current() == gMainThread)
        fail("unexpectedly called on the main thread");
}

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

// Thread on which TestOpensOpenedParent runs
static Thread* gParentThread;

void
TestOpensParent::Main()
{
    if (!SendStart())
        fail("sending Start");
}

static void
OpenParent(TestOpensOpenedParent* aParent,
           Transport* aTransport, base::ProcessId aOtherPid)
{
    AssertNotMainThread();

    // Open the actor on the off-main thread to park it there.
    // Messages will be delivered to this thread's message loop
    // instead of the main thread's.
    if (!aParent->Open(aTransport, aOtherPid,
                       XRE_GetIOMessageLoop(), ipc::ParentSide))
        fail("opening Parent");
}

PTestOpensOpenedParent*
TestOpensParent::AllocPTestOpensOpenedParent(Transport* transport,
                                             ProcessId otherPid)
{
    gMainThread = MessageLoop::current();

    gParentThread = new Thread("ParentThread");
    if (!gParentThread->Start())
        fail("starting parent thread");

    TestOpensOpenedParent* a = new TestOpensOpenedParent(transport);
    gParentThread->message_loop()->PostTask(
        NewRunnableFunction(OpenParent, a, transport, otherPid));

    return a;
}

void
TestOpensParent::ActorDestroy(ActorDestroyReason why)
{
    // Stops the thread and joins it
    delete gParentThread;

    if (NormalShutdown != why)
        fail("unexpected destruction!");  
    passed("ok");
    QuitParent();
}

bool
TestOpensOpenedParent::RecvHello()
{
    AssertNotMainThread();
    return SendHi();
}

bool
TestOpensOpenedParent::RecvHelloSync()
{
    AssertNotMainThread();
    return true;
}

bool
TestOpensOpenedParent::AnswerHelloRpc()
{
    AssertNotMainThread();
    return CallHiRpc();
}

static void
ShutdownTestOpensOpenedParent(TestOpensOpenedParent* parent,
                              Transport* transport)
{
    delete parent;
}

void
TestOpensOpenedParent::ActorDestroy(ActorDestroyReason why)
{
    AssertNotMainThread();

    if (NormalShutdown != why)
        fail("unexpected destruction!");

    // ActorDestroy() is just a callback from IPDL-generated code,
    // which needs the top-level actor (this) to stay alive a little
    // longer so other things can be cleaned up.
    gParentThread->message_loop()->PostTask(
        NewRunnableFunction(ShutdownTestOpensOpenedParent,
                            this, mTransport));
}

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

static TestOpensChild* gOpensChild;
// Thread on which TestOpensOpenedChild runs
static Thread* gChildThread;

TestOpensChild::TestOpensChild()
{
    gOpensChild = this;
}

bool
TestOpensChild::RecvStart()
{
    if (!PTestOpensOpened::Open(this))
        fail("opening PTestOpensOpened");
    return true;
}

static void
OpenChild(TestOpensOpenedChild* aChild,
           Transport* aTransport, base::ProcessId aOtherPid)
{
    AssertNotMainThread();

    // Open the actor on the off-main thread to park it there.
    // Messages will be delivered to this thread's message loop
    // instead of the main thread's.
    if (!aChild->Open(aTransport, aOtherPid,
                      XRE_GetIOMessageLoop(), ipc::ChildSide))
        fail("opening Child");

    // Kick off the unit tests
    if (!aChild->SendHello())
        fail("sending Hello");
}

PTestOpensOpenedChild*
TestOpensChild::AllocPTestOpensOpenedChild(Transport* transport,
                                           ProcessId otherPid)
{
    gMainThread = MessageLoop::current();

    gChildThread = new Thread("ChildThread");
    if (!gChildThread->Start())
        fail("starting child thread");

    TestOpensOpenedChild* a = new TestOpensOpenedChild(transport);
    gChildThread->message_loop()->PostTask(
        NewRunnableFunction(OpenChild, a, transport, otherPid));

    return a;
}

void
TestOpensChild::ActorDestroy(ActorDestroyReason why)
{
    // Stops the thread and joins it
    delete gChildThread;

    if (NormalShutdown != why)
        fail("unexpected destruction!");
    QuitChild();
}

bool
TestOpensOpenedChild::RecvHi()
{
    AssertNotMainThread();

    if (!SendHelloSync())
        fail("sending HelloSync");
    if (!CallHelloRpc())
        fail("calling HelloRpc");
    if (!mGotHi)
        fail("didn't answer HiRpc");

    // Need to close the channel without message-processing frames on
    // the C++ stack
    MessageLoop::current()->PostTask(
        NewNonOwningRunnableMethod(this, &TestOpensOpenedChild::Close));
    return true;
}

bool
TestOpensOpenedChild::AnswerHiRpc()
{
    AssertNotMainThread();

    mGotHi = true;              // d00d
    return true;
}

static void
ShutdownTestOpensOpenedChild(TestOpensOpenedChild* child,
                             Transport* transport)
{
    delete child;

    // Kick off main-thread shutdown.
    gMainThread->PostTask(
        NewNonOwningRunnableMethod(gOpensChild, &TestOpensChild::Close));
}

void
TestOpensOpenedChild::ActorDestroy(ActorDestroyReason why)
{
    AssertNotMainThread();

    if (NormalShutdown != why)
        fail("unexpected destruction!");  

    // ActorDestroy() is just a callback from IPDL-generated code,
    // which needs the top-level actor (this) to stay alive a little
    // longer so other things can be cleaned up.  Defer shutdown to
    // let cleanup finish.
    gChildThread->message_loop()->PostTask(
        NewRunnableFunction(ShutdownTestOpensOpenedChild,
                            this, mTransport));
}

} // namespace mozilla