1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/ipdl/test/cxx/TestUrgency.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,186 @@ 1.4 +#include "TestUrgency.h" 1.5 + 1.6 +#include "IPDLUnitTests.h" // fail etc. 1.7 +#if defined(OS_POSIX) 1.8 +#include <unistd.h> 1.9 +#else 1.10 +#include <windows.h> 1.11 +#endif 1.12 + 1.13 +template<> 1.14 +struct RunnableMethodTraits<mozilla::_ipdltest::TestUrgencyParent> 1.15 +{ 1.16 + static void RetainCallee(mozilla::_ipdltest::TestUrgencyParent* obj) { } 1.17 + static void ReleaseCallee(mozilla::_ipdltest::TestUrgencyParent* obj) { } 1.18 +}; 1.19 + 1.20 +namespace mozilla { 1.21 +namespace _ipdltest { 1.22 + 1.23 +#if defined(OS_POSIX) 1.24 +static void Sleep(int ms) 1.25 +{ 1.26 + sleep(ms / 1000); 1.27 +} 1.28 +#endif 1.29 + 1.30 +//----------------------------------------------------------------------------- 1.31 +// parent 1.32 + 1.33 +TestUrgencyParent::TestUrgencyParent() 1.34 + : inreply_(false) 1.35 +{ 1.36 + MOZ_COUNT_CTOR(TestUrgencyParent); 1.37 +} 1.38 + 1.39 +TestUrgencyParent::~TestUrgencyParent() 1.40 +{ 1.41 + MOZ_COUNT_DTOR(TestUrgencyParent); 1.42 +} 1.43 + 1.44 +void 1.45 +TestUrgencyParent::Main() 1.46 +{ 1.47 + if (!SendStart()) 1.48 + fail("sending Start"); 1.49 +} 1.50 + 1.51 +bool 1.52 +TestUrgencyParent::RecvTest1(uint32_t *value) 1.53 +{ 1.54 + if (!CallReply1(value)) 1.55 + fail("sending Reply1"); 1.56 + if (*value != 99) 1.57 + fail("bad value"); 1.58 + return true; 1.59 +} 1.60 + 1.61 +bool 1.62 +TestUrgencyParent::RecvTest2() 1.63 +{ 1.64 + uint32_t value; 1.65 + inreply_ = true; 1.66 + if (!CallReply2(&value)) 1.67 + fail("sending Reply2"); 1.68 + inreply_ = false; 1.69 + if (value != 500) 1.70 + fail("bad value"); 1.71 + return true; 1.72 +} 1.73 + 1.74 +bool 1.75 +TestUrgencyParent::RecvTest3(uint32_t *value) 1.76 +{ 1.77 + if (inreply_) 1.78 + fail("nested non-urgent on top of urgent rpc"); 1.79 + *value = 1000; 1.80 + return true; 1.81 +} 1.82 + 1.83 +bool 1.84 +TestUrgencyParent::RecvFinalTest_Begin() 1.85 +{ 1.86 + SetReplyTimeoutMs(2000); 1.87 + if (CallFinalTest_Hang()) 1.88 + fail("should have failed due to timeout"); 1.89 + if (!GetIPCChannel()->Unsound_IsClosed()) 1.90 + fail("channel should have closed"); 1.91 + 1.92 + MessageLoop::current()->PostTask( 1.93 + FROM_HERE, 1.94 + NewRunnableMethod(this, &TestUrgencyParent::Close)); 1.95 + return false; 1.96 +} 1.97 + 1.98 +//----------------------------------------------------------------------------- 1.99 +// child 1.100 + 1.101 +enum { 1.102 + kFirstTestBegin = 1, 1.103 + kFirstTestGotReply, 1.104 + kSecondTestBegin, 1.105 + kSecondTestGotReply, 1.106 +}; 1.107 + 1.108 +bool 1.109 +TestUrgencyChild::RecvStart() 1.110 +{ 1.111 + uint32_t result; 1.112 + 1.113 + // Send a synchronous message, expect to get an urgent message while 1.114 + // blocked. 1.115 + test_ = kFirstTestBegin; 1.116 + if (!SendTest1(&result)) 1.117 + fail("calling SendTest1"); 1.118 + if (result != 99) 1.119 + fail("bad result in RecvStart"); 1.120 + if (test_ != kFirstTestGotReply) 1.121 + fail("never received urgent message"); 1.122 + 1.123 + // Initiate the next test by sending an asynchronous message, then becoming 1.124 + // blocked. This tests that the urgent message is still delivered properly, 1.125 + // and that the parent does not try to service the sync 1.126 + test_ = kSecondTestBegin; 1.127 + if (!SendTest2()) 1.128 + fail("calling SendTest2"); 1.129 + if (!SendTest3(&result)) 1.130 + fail("calling SendTest3"); 1.131 + if (test_ != kSecondTestGotReply) 1.132 + fail("never received urgent message #2"); 1.133 + if (result != 1000) 1.134 + fail("wrong value from test3"); 1.135 + 1.136 + // This must be the last test, since the child process may die. 1.137 + if (SendFinalTest_Begin()) 1.138 + fail("Final test should not have succeeded"); 1.139 + 1.140 + Close(); 1.141 + 1.142 + return true; 1.143 +} 1.144 + 1.145 +bool 1.146 +TestUrgencyChild::AnswerReply1(uint32_t *reply) 1.147 +{ 1.148 + if (test_ != kFirstTestBegin) 1.149 + fail("wrong test # in AnswerReply1"); 1.150 + 1.151 + *reply = 99; 1.152 + test_ = kFirstTestGotReply; 1.153 + return true; 1.154 +} 1.155 + 1.156 +bool 1.157 +TestUrgencyChild::AnswerReply2(uint32_t *reply) 1.158 +{ 1.159 + if (test_ != kSecondTestBegin) 1.160 + fail("wrong test # in AnswerReply2"); 1.161 + 1.162 + // sleep for 5 seconds so the parent process tries to deliver more messages. 1.163 + Sleep(5000); 1.164 + 1.165 + *reply = 500; 1.166 + test_ = kSecondTestGotReply; 1.167 + return true; 1.168 +} 1.169 + 1.170 +bool 1.171 +TestUrgencyChild::AnswerFinalTest_Hang() 1.172 +{ 1.173 + Sleep(10); 1.174 + return true; 1.175 +} 1.176 + 1.177 +TestUrgencyChild::TestUrgencyChild() 1.178 + : test_(0) 1.179 +{ 1.180 + MOZ_COUNT_CTOR(TestUrgencyChild); 1.181 +} 1.182 + 1.183 +TestUrgencyChild::~TestUrgencyChild() 1.184 +{ 1.185 + MOZ_COUNT_DTOR(TestUrgencyChild); 1.186 +} 1.187 + 1.188 +} // namespace _ipdltest 1.189 +} // namespace mozilla