ipc/ipdl/test/cxx/TestInterruptRaces.cpp

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

     1 #include "TestInterruptRaces.h"
     3 #include "IPDLUnitTests.h"      // fail etc.
     5 using mozilla::ipc::MessageChannel;
     7 template<>
     8 struct RunnableMethodTraits<mozilla::_ipdltest::TestInterruptRacesParent>
     9 {
    10     static void RetainCallee(mozilla::_ipdltest::TestInterruptRacesParent* obj) { }
    11     static void ReleaseCallee(mozilla::_ipdltest::TestInterruptRacesParent* obj) { }
    12 };
    15 namespace mozilla {
    16 namespace _ipdltest {
    18 ipc::RacyInterruptPolicy
    19 MediateRace(const MessageChannel::Message& parent,
    20             const MessageChannel::Message& child)
    21 {
    22     return (PTestInterruptRaces::Msg_Child__ID == parent.type()) ?
    23         ipc::RIPParentWins : ipc::RIPChildWins;
    24 }
    26 //-----------------------------------------------------------------------------
    27 // parent
    28 void
    29 TestInterruptRacesParent::Main()
    30 {
    31     if (!SendStart())
    32         fail("sending Start()");
    33 }
    35 bool
    36 TestInterruptRacesParent::RecvStartRace()
    37 {
    38     MessageLoop::current()->PostTask(
    39         FROM_HERE,
    40         NewRunnableMethod(this, &TestInterruptRacesParent::OnRaceTime));
    41     return true;
    42 }
    44 void
    45 TestInterruptRacesParent::OnRaceTime()
    46 {
    47     if (!CallRace(&mChildHasReply))
    48         fail("problem calling Race()");
    50     if (!mChildHasReply)
    51         fail("child should have got a reply already");
    53     mHasReply = true;
    55     MessageLoop::current()->PostTask(
    56         FROM_HERE,
    57         NewRunnableMethod(this, &TestInterruptRacesParent::Test2));
    58 }
    60 bool
    61 TestInterruptRacesParent::AnswerRace(bool* hasReply)
    62 {
    63     if (mHasReply)
    64         fail("apparently the parent won the Interrupt race!");
    65     *hasReply = hasReply;
    66     return true;
    67 }
    69 void
    70 TestInterruptRacesParent::Test2()
    71 {
    72     puts("  passed");
    73     puts("Test 2");
    75     mHasReply = false;
    76     mChildHasReply = false;
    78     if (!CallStackFrame())
    79         fail("can't set up a stack frame");
    81     puts("  passed");
    83     MessageLoop::current()->PostTask(
    84         FROM_HERE,
    85         NewRunnableMethod(this, &TestInterruptRacesParent::Test3));
    86 }
    88 bool
    89 TestInterruptRacesParent::AnswerStackFrame()
    90 {
    91     if (!SendWakeup())
    92         fail("can't wake up the child");
    94     if (!CallRace(&mChildHasReply))
    95         fail("can't set up race condition");
    96     mHasReply = true;
    98     if (!mChildHasReply)
    99         fail("child should have got a reply already");
   101     return true;
   102 }
   104 void
   105 TestInterruptRacesParent::Test3()
   106 {
   107     puts("Test 3");
   109     if (!CallStackFrame3())
   110         fail("can't set up a stack frame");
   112     puts("  passed");
   114     Close();
   115 }
   117 bool
   118 TestInterruptRacesParent::AnswerStackFrame3()
   119 {
   120     if (!SendWakeup3())
   121         fail("can't wake up the child");
   123     if (!CallChild())
   124         fail("can't set up race condition");
   126     return true;
   127 }
   129 bool
   130 TestInterruptRacesParent::AnswerParent()
   131 {
   132     mAnsweredParent = true;
   133     return true;
   134 }
   136 bool
   137 TestInterruptRacesParent::RecvGetAnsweredParent(bool* answeredParent)
   138 {
   139     *answeredParent = mAnsweredParent;
   140     return true;
   141 }
   143 //-----------------------------------------------------------------------------
   144 // child
   145 bool
   146 TestInterruptRacesChild::RecvStart()
   147 {
   148     puts("Test 1");
   150     if (!SendStartRace())
   151         fail("problem sending StartRace()");
   153     bool dontcare;
   154     if (!CallRace(&dontcare))
   155         fail("problem calling Race()");
   157     mHasReply = true;
   158     return true;
   159 }
   161 bool
   162 TestInterruptRacesChild::AnswerRace(bool* hasReply)
   163 {
   164     if (!mHasReply)
   165         fail("apparently the child lost the Interrupt race!");
   167     *hasReply = mHasReply;
   169     return true;
   170 }
   172 bool
   173 TestInterruptRacesChild::AnswerStackFrame()
   174 {
   175     // reset for the second test
   176     mHasReply = false;
   178     if (!CallStackFrame())
   179         fail("can't set up stack frame");
   181     if (!mHasReply)
   182         fail("should have had reply by now");
   184     return true;
   185 }
   187 bool
   188 TestInterruptRacesChild::RecvWakeup()
   189 {
   190     bool dontcare;
   191     if (!CallRace(&dontcare))
   192         fail("can't set up race condition");
   194     mHasReply = true;
   195     return true;
   196 }
   198 bool
   199 TestInterruptRacesChild::AnswerStackFrame3()
   200 {
   201     if (!CallStackFrame3())
   202         fail("can't set up stack frame");
   203     return true;
   204 }
   206 bool
   207 TestInterruptRacesChild::RecvWakeup3()
   208 {
   209     if (!CallParent())
   210         fail("can't set up race condition");
   211     return true;
   212 }
   214 bool
   215 TestInterruptRacesChild::AnswerChild()
   216 {
   217     bool parentAnsweredParent;
   218     // the parent is supposed to win the race, which means its
   219     // message, Child(), is supposed to be processed before the
   220     // child's message, Parent()
   221     if (!SendGetAnsweredParent(&parentAnsweredParent))
   222         fail("sending GetAnsweredParent");
   224     if (parentAnsweredParent)
   225         fail("parent was supposed to win the race!");
   227     return true;
   228 }
   230 } // namespace _ipdltest
   231 } // namespace mozilla

mercurial