|
1 #include "TestUrgency.h" |
|
2 |
|
3 #include "IPDLUnitTests.h" // fail etc. |
|
4 #if defined(OS_POSIX) |
|
5 #include <unistd.h> |
|
6 #else |
|
7 #include <windows.h> |
|
8 #endif |
|
9 |
|
10 template<> |
|
11 struct RunnableMethodTraits<mozilla::_ipdltest::TestUrgencyParent> |
|
12 { |
|
13 static void RetainCallee(mozilla::_ipdltest::TestUrgencyParent* obj) { } |
|
14 static void ReleaseCallee(mozilla::_ipdltest::TestUrgencyParent* obj) { } |
|
15 }; |
|
16 |
|
17 namespace mozilla { |
|
18 namespace _ipdltest { |
|
19 |
|
20 #if defined(OS_POSIX) |
|
21 static void Sleep(int ms) |
|
22 { |
|
23 sleep(ms / 1000); |
|
24 } |
|
25 #endif |
|
26 |
|
27 //----------------------------------------------------------------------------- |
|
28 // parent |
|
29 |
|
30 TestUrgencyParent::TestUrgencyParent() |
|
31 : inreply_(false) |
|
32 { |
|
33 MOZ_COUNT_CTOR(TestUrgencyParent); |
|
34 } |
|
35 |
|
36 TestUrgencyParent::~TestUrgencyParent() |
|
37 { |
|
38 MOZ_COUNT_DTOR(TestUrgencyParent); |
|
39 } |
|
40 |
|
41 void |
|
42 TestUrgencyParent::Main() |
|
43 { |
|
44 if (!SendStart()) |
|
45 fail("sending Start"); |
|
46 } |
|
47 |
|
48 bool |
|
49 TestUrgencyParent::RecvTest1(uint32_t *value) |
|
50 { |
|
51 if (!CallReply1(value)) |
|
52 fail("sending Reply1"); |
|
53 if (*value != 99) |
|
54 fail("bad value"); |
|
55 return true; |
|
56 } |
|
57 |
|
58 bool |
|
59 TestUrgencyParent::RecvTest2() |
|
60 { |
|
61 uint32_t value; |
|
62 inreply_ = true; |
|
63 if (!CallReply2(&value)) |
|
64 fail("sending Reply2"); |
|
65 inreply_ = false; |
|
66 if (value != 500) |
|
67 fail("bad value"); |
|
68 return true; |
|
69 } |
|
70 |
|
71 bool |
|
72 TestUrgencyParent::RecvTest3(uint32_t *value) |
|
73 { |
|
74 if (inreply_) |
|
75 fail("nested non-urgent on top of urgent rpc"); |
|
76 *value = 1000; |
|
77 return true; |
|
78 } |
|
79 |
|
80 bool |
|
81 TestUrgencyParent::RecvFinalTest_Begin() |
|
82 { |
|
83 SetReplyTimeoutMs(2000); |
|
84 if (CallFinalTest_Hang()) |
|
85 fail("should have failed due to timeout"); |
|
86 if (!GetIPCChannel()->Unsound_IsClosed()) |
|
87 fail("channel should have closed"); |
|
88 |
|
89 MessageLoop::current()->PostTask( |
|
90 FROM_HERE, |
|
91 NewRunnableMethod(this, &TestUrgencyParent::Close)); |
|
92 return false; |
|
93 } |
|
94 |
|
95 //----------------------------------------------------------------------------- |
|
96 // child |
|
97 |
|
98 enum { |
|
99 kFirstTestBegin = 1, |
|
100 kFirstTestGotReply, |
|
101 kSecondTestBegin, |
|
102 kSecondTestGotReply, |
|
103 }; |
|
104 |
|
105 bool |
|
106 TestUrgencyChild::RecvStart() |
|
107 { |
|
108 uint32_t result; |
|
109 |
|
110 // Send a synchronous message, expect to get an urgent message while |
|
111 // blocked. |
|
112 test_ = kFirstTestBegin; |
|
113 if (!SendTest1(&result)) |
|
114 fail("calling SendTest1"); |
|
115 if (result != 99) |
|
116 fail("bad result in RecvStart"); |
|
117 if (test_ != kFirstTestGotReply) |
|
118 fail("never received urgent message"); |
|
119 |
|
120 // Initiate the next test by sending an asynchronous message, then becoming |
|
121 // blocked. This tests that the urgent message is still delivered properly, |
|
122 // and that the parent does not try to service the sync |
|
123 test_ = kSecondTestBegin; |
|
124 if (!SendTest2()) |
|
125 fail("calling SendTest2"); |
|
126 if (!SendTest3(&result)) |
|
127 fail("calling SendTest3"); |
|
128 if (test_ != kSecondTestGotReply) |
|
129 fail("never received urgent message #2"); |
|
130 if (result != 1000) |
|
131 fail("wrong value from test3"); |
|
132 |
|
133 // This must be the last test, since the child process may die. |
|
134 if (SendFinalTest_Begin()) |
|
135 fail("Final test should not have succeeded"); |
|
136 |
|
137 Close(); |
|
138 |
|
139 return true; |
|
140 } |
|
141 |
|
142 bool |
|
143 TestUrgencyChild::AnswerReply1(uint32_t *reply) |
|
144 { |
|
145 if (test_ != kFirstTestBegin) |
|
146 fail("wrong test # in AnswerReply1"); |
|
147 |
|
148 *reply = 99; |
|
149 test_ = kFirstTestGotReply; |
|
150 return true; |
|
151 } |
|
152 |
|
153 bool |
|
154 TestUrgencyChild::AnswerReply2(uint32_t *reply) |
|
155 { |
|
156 if (test_ != kSecondTestBegin) |
|
157 fail("wrong test # in AnswerReply2"); |
|
158 |
|
159 // sleep for 5 seconds so the parent process tries to deliver more messages. |
|
160 Sleep(5000); |
|
161 |
|
162 *reply = 500; |
|
163 test_ = kSecondTestGotReply; |
|
164 return true; |
|
165 } |
|
166 |
|
167 bool |
|
168 TestUrgencyChild::AnswerFinalTest_Hang() |
|
169 { |
|
170 Sleep(10); |
|
171 return true; |
|
172 } |
|
173 |
|
174 TestUrgencyChild::TestUrgencyChild() |
|
175 : test_(0) |
|
176 { |
|
177 MOZ_COUNT_CTOR(TestUrgencyChild); |
|
178 } |
|
179 |
|
180 TestUrgencyChild::~TestUrgencyChild() |
|
181 { |
|
182 MOZ_COUNT_DTOR(TestUrgencyChild); |
|
183 } |
|
184 |
|
185 } // namespace _ipdltest |
|
186 } // namespace mozilla |