summaryrefslogtreecommitdiffstats
path: root/ipc/ipdl/test/cxx/TestUrgentHangs.cpp
blob: b798ae18d9d7af6d1f2647d2c5f13ffaa0663d92 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: sw=4 ts=4 et :
 */
#include "TestUrgentHangs.h"

#include "IPDLUnitTests.h"      // fail etc.
#include "prthread.h"
#if defined(OS_POSIX)
#include <unistd.h>
#else
#include <windows.h>
#endif

namespace mozilla {
namespace _ipdltest {

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

TestUrgentHangsParent::TestUrgentHangsParent()
  : mInnerCount(0),
    mInnerUrgentCount(0)
{
    MOZ_COUNT_CTOR(TestUrgentHangsParent);
}

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

void
TestUrgentHangsParent::Main()
{
    SetReplyTimeoutMs(1000);

    // Should succeed despite the nested sleep call because the content process
    // responded to the transaction.
    if (!SendTest1_1())
        fail("sending Test1_1");

    // Fails with a timeout.
    if (SendTest2())
        fail("sending Test2");

    // Also fails since we haven't gotten a response for Test2 yet.
    if (SendTest3())
        fail("sending Test3");

    // Do a second round of testing once the reply to Test2 comes back.
    MessageLoop::current()->PostDelayedTask(
        NewNonOwningRunnableMethod(this, &TestUrgentHangsParent::SecondStage),
        3000);
}

void
TestUrgentHangsParent::SecondStage()
{
    // Send an async message that waits 2 seconds and then sends a sync message
    // (which should be processed).
    if (!SendTest4())
        fail("sending Test4");

    // Send a sync message that will time out because the child is waiting
    // inside RecvTest4.
    if (SendTest4_1())
        fail("sending Test4_1");

    MessageLoop::current()->PostDelayedTask(
        NewNonOwningRunnableMethod(this, &TestUrgentHangsParent::ThirdStage),
        3000);
}

void
TestUrgentHangsParent::ThirdStage()
{
    // The third stage does the same thing as the second stage except that the
    // child sends an urgent message to us. In this case, we actually answer
    // that message unconditionally.

    // Send an async message that waits 2 seconds and then sends a sync message
    // (which should be processed).
    if (!SendTest5())
        fail("sending Test5");

    // Send a sync message that will time out because the child is waiting
    // inside RecvTest5.
    if (SendTest5_1())
        fail("sending Test5_1");

    // Close the channel after the child finishes its work in RecvTest5.
    MessageLoop::current()->PostDelayedTask(
        NewNonOwningRunnableMethod(this, &TestUrgentHangsParent::Close),
        3000);
}

bool
TestUrgentHangsParent::RecvTest1_2()
{
    if (!SendTest1_3())
        fail("sending Test1_3");
    return true;
}

bool
TestUrgentHangsParent::RecvTestInner()
{
    mInnerCount++;
    return true;
}

bool
TestUrgentHangsParent::RecvTestInnerUrgent()
{
    mInnerUrgentCount++;
    return true;
}

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

bool
TestUrgentHangsChild::RecvTest1_1()
{
    if (!SendTest1_2())
        fail("sending Test1_2");

    return true;
}

bool
TestUrgentHangsChild::RecvTest1_3()
{
    PR_Sleep(PR_SecondsToInterval(2));

    return true;
}

bool
TestUrgentHangsChild::RecvTest2()
{
    PR_Sleep(PR_SecondsToInterval(2));

    // Should fail because of the timeout.
    if (SendTestInner())
        fail("sending TestInner");

    return true;
}

bool
TestUrgentHangsChild::RecvTest3()
{
    fail("RecvTest3 should never be called");
    return true;
}

bool
TestUrgentHangsChild::RecvTest4()
{
    PR_Sleep(PR_SecondsToInterval(2));

    // This won't fail because we should handle Test4_1 here before actually
    // sending TestInner to the parent.
    if (!SendTestInner())
        fail("sending TestInner");

    return true;
}

bool
TestUrgentHangsChild::RecvTest4_1()
{
    // This should fail because Test4_1 timed out and hasn't gotten a response
    // yet.
    if (SendTestInner())
        fail("sending TestInner");

    return true;
}

bool
TestUrgentHangsChild::RecvTest5()
{
    PR_Sleep(PR_SecondsToInterval(2));

    // This message will actually be handled by the parent even though it's in
    // the timeout state.
    if (!SendTestInnerUrgent())
        fail("sending TestInner");

    return true;
}

bool
TestUrgentHangsChild::RecvTest5_1()
{
    // This message will actually be handled by the parent even though it's in
    // the timeout state.
    if (!SendTestInnerUrgent())
        fail("sending TestInner");

    return true;
}

TestUrgentHangsChild::TestUrgentHangsChild()
{
    MOZ_COUNT_CTOR(TestUrgentHangsChild);
}

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

} // namespace _ipdltest
} // namespace mozilla