Update contrib.
1 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of the License "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\thread\t_killer.cpp
15 // Derived from T_MESSGE, tests threads killing each other, not cleaning up etc.
21 #include <e32std_private.h>
27 const TInt KHeapMinSize=0x1000;
28 const TInt KHeapMaxSize=0x1000;
30 class CTestServer : public CServer2
33 IMPORT_C CTestServer(TInt aPriority);
35 EInt0Error=1, EInt1Error, EInt2Error, EInt3Error,
36 EPtr0Error, EPtr1Error, EPtr2Error, EPtr3Error,
37 ECreateNameError, ENewSessionError,
38 EClientError, EWatcherError, EKilled
40 static void Panic(TPanicType aReason);
42 //override the pure virtual functions:
43 IMPORT_C virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2 &) const;
47 class CTestSession : public CSession2
50 enum {EStop,ETestInt,ETestPtr,ETestClient,ETestComplete,ETestPtrComplete,ETestCompletePanic,ETestOtherSession,ETestCompleteAfter};
51 //Override pure virtual
52 IMPORT_C virtual void ServiceL(const RMessage2& aMessage);
54 void TestInt(const RMessage2& aMessage);
55 void TestPtr(const RMessage2& aMessage);
56 void TestClient(const RMessage2& aMessage);
57 void TestComplete(const RMessage2& aMessage);
58 void TestPtrComplete(const RMessage2& aMessage);
59 void TestCompletePanic();
61 TInt count1;//initially ==0
62 RMessage2 messages[5];//Used in TestComplete()
64 TInt count2;//initially ==0
65 RMessagePtr2 messagePtrs[5];//User in TestPtrComplete()
69 class CMyActiveScheduler : public CActiveScheduler
72 virtual void Error(TInt anError) const; //override pure virtual error function
76 class RSession : public RSessionBase
79 TInt PublicSendReceive(TInt aFunction, const TIpcArgs& aArgs)
81 return (SendReceive(aFunction, aArgs));
83 void PublicSendReceive(TInt aFunction, const TIpcArgs& aArgs, TRequestStatus& aStatus)
85 SendReceive(aFunction, aArgs, aStatus);
87 TInt PublicCreateSession(const TDesC& aServer,TInt aMessageSlots)
89 return (CreateSession(aServer,User::Version(),aMessageSlots));
93 //=========================================================================
95 // CTestServer functions
97 CTestServer::CTestServer(TInt aPriority)
99 // Constructor - sets name
101 : CServer2(aPriority)
104 CSession2* CTestServer::NewSessionL(const TVersion& aVersion, const RMessage2 &) const
106 // Virtual fn - checks version supported and creates a CTestSession
109 TVersion version(KE32MajorVersionNumber,KE32MinorVersionNumber,KE32BuildVersionNumber);
110 if (User::QueryVersionSupported(version,aVersion)==EFalse)
111 User::Leave(KErrNotSupported);
113 CTestSession* newCTestSession = new CTestSession;
114 if (newCTestSession==NULL)
115 Panic(ENewSessionError);
116 return(newCTestSession);
119 RThread clientThread, serverThread, killerThread;
121 void CTestServer::Panic(TPanicType aReason) //static function
123 clientThread.Kill(KErrNone);
124 User::Panic(_L("CTestServer"),aReason);
127 // CTestSession funtions
129 RSession session, otherSession;
132 const TInt KTestInt[] = {-3866,30566,0,200};
133 const TIpcArgs KIpcArgInt(-3866,30566,0,200);
135 void CTestSession::TestInt(const RMessage2& aMessage)
137 // Tests to see that the correct Int0/1/2/3 have been received
140 if (aMessage.Int0()!=KTestInt[0])
141 CTestServer::Panic(CTestServer::EInt0Error);
142 if (aMessage.Int1()!=KTestInt[1])
143 CTestServer::Panic(CTestServer::EInt1Error);
144 if (aMessage.Int2()!=KTestInt[2])
145 CTestServer::Panic(CTestServer::EInt2Error);
146 if (aMessage.Int3()!=KTestInt[3])
147 CTestServer::Panic(CTestServer::EInt3Error);
150 const TAny* KTestPtr[]={&clientThread, &serverThread, &session, &sem};
151 const TIpcArgs KIpcArgPtr(&clientThread, &serverThread, &session, &sem);
153 void CTestSession::TestPtr(const RMessage2& aMessage)
155 // Tests to see that the correct Ptr0/1/2/3 have been received
158 if (aMessage.Ptr0()!=KTestPtr[0])
159 CTestServer::Panic(CTestServer::EPtr0Error);
160 if (aMessage.Ptr1()!=KTestPtr[1])
161 CTestServer::Panic(CTestServer::EPtr1Error);
162 if (aMessage.Ptr2()!=KTestPtr[2])
163 CTestServer::Panic(CTestServer::EPtr2Error);
164 if (aMessage.Ptr3()!=KTestPtr[3])
165 CTestServer::Panic(CTestServer::EPtr3Error);
168 TFullName clientName;
171 void CTestSession::TestClient(const RMessage2& aMessage)
177 // Under WINS, thread names are not prefixed with the process name
178 TFullName n=RProcess().Name();
183 TInt r = aMessage.Client(client);
184 if (r != KErrNone || client.FullName().CompareF(n)!=0)
187 clientThread.Kill(0);
188 CTestServer::Panic(CTestServer::EClientError);
193 void CTestSession::TestComplete(const RMessage2& aMessage)
195 // Stores messages up then Completes in reverse order
198 messages[count1] = aMessage;
200 for(count1=4; count1>=0; count1--)
201 messages[count1].Complete(5-count1); //Complete with different 'error messages'
204 void CTestSession::TestPtrComplete(const RMessage2& aMessage)
206 // Stores messages up as RMessagePtrs then Completes in reverse order
207 // Also tests RMessage2::MessagePtr()
210 messagePtrs[count2] = aMessage;
212 for(count2=4; count2>=0; count2--)
213 messagePtrs[count2].Complete(10-count2*2);
216 void CTestSession::ServiceL(const RMessage2& aMessage)
218 // Virtual message-handler
222 switch (aMessage.Function())
225 CActiveScheduler::Stop();
234 TestClient(aMessage);
237 TestComplete(aMessage);
239 case ETestPtrComplete:
240 TestPtrComplete(aMessage);
242 case ETestCompletePanic:
243 aMessage.Complete(KErrNone);
245 case ETestOtherSession:
247 case ETestCompleteAfter:
248 User::After(7000000);
254 aMessage.Complete(r);
258 void CMyActiveScheduler::Error(TInt anError) const
260 // Virtual error handler
263 User::Panic(_L("CMyActiveScheduer::Error"), anError);
266 LOCAL_D TInt64 TheSeed;
267 GLDEF_C TInt Random(TInt aRange)
269 return (Math::Rand(TheSeed)>>11)%aRange;
272 TInt KillerThread(TAny*)
274 // Wait a random time and then kill the client thread
277 RTest test(_L("T_KILLER...Killer"));
278 TRequestStatus clientStatus;
282 test.Start(_L("Logon to client"));
283 clientThread.Logon(clientStatus);
284 test.Next(_L("Delay...."));
289 if (clientStatus!=KRequestPending)
290 return KErrNone; // client has already finished
292 break; // Time to die!
294 test.Printf(_L("Kill client after %d ms\n"), delay);
295 clientThread.Kill(CTestServer::EKilled);
297 test.Close(); // close console immediately
298 // test.End(); // "Press ENTER to exit"
302 TInt ClientThread(TAny* aPtr)
304 // Passed as the first client thread - signals the server to do several tests
307 RTest test(_L("T_KILLER...client"));
308 TInt repeat = (TInt)aPtr;
311 test.Start(_L("Client thread"));
315 test.Next(_L("Client loop"));
316 test.Start(_L("Create Session"));
317 TInt r=session.PublicCreateSession(_L("CTestServer"),5);
319 User::Panic(_L("CreateSessn failure"),r);
321 test.Next(_L("Signal to test Int0/1/2/3()"));
322 r=session.PublicSendReceive(CTestSession::ETestInt, KIpcArgInt);
325 test.Next(_L("Signal to test Ptr0/1/2/3()"));
326 r=session.PublicSendReceive(CTestSession::ETestPtr, KIpcArgPtr);
329 test.Next(_L("Signal to test Client()"));
330 r=session.PublicSendReceive(CTestSession::ETestClient, TIpcArgs());
333 test.Next(_L("Test RMessage2::Complete()"));
334 TRequestStatus stat[7];
337 session.PublicSendReceive(CTestSession::ETestComplete, TIpcArgs(), stat[r]);
338 test(stat[r]==KRequestPending);
340 session.PublicSendReceive(CTestSession::ETestComplete, TIpcArgs(), stat[4]);
341 User::WaitForRequest(stat[0]);
343 test(stat[r]==5-r); //Test the 'error messages' set by Complete()
344 test.Next(_L("Test RMessagePtr2::Complete()"));
347 session.PublicSendReceive(CTestSession::ETestPtrComplete, TIpcArgs(), stat[r]);
348 test(stat[r]==KRequestPending);
350 session.PublicSendReceive(CTestSession::ETestPtrComplete, TIpcArgs(), stat[4]);
351 User::WaitForRequest(stat[0]);
353 test(stat[r]==10-r*2);
355 test.Next(_L("Try another session"));
356 r=otherSession.PublicCreateSession(_L("CTestServer"),5);
359 r=otherSession.PublicSendReceive(CTestSession::ETestOtherSession, TIpcArgs());
362 // test.Next(_L("Try to disconnect"));
363 // r=session.PublicSendReceive(RMessage2::EDisConnect,NULL);//Panics user
364 // test(r==KErrNone);
366 test.Next(_L("Saturate server"));
369 test.Printf(_L("Send %d\r"),r);
370 session.PublicSendReceive(CTestSession::ETestCompleteAfter,TIpcArgs(),stat[r]);
372 test(stat[r]==KRequestPending);
374 test(stat[r]==KErrServerBusy);
376 test.Printf(_L("\n"));
379 test.Printf(_L("Wait %d\r"),r);
380 User::WaitForRequest(stat[r]);
381 test(stat[r]==KErrNone);
383 test.Printf(_L("\n"));
386 while (--repeat > 0);
387 test.Start(_L("Signal to stop ActiveScheduler"));
388 session.PublicSendReceive(CTestSession::EStop, TIpcArgs());
394 TInt ServerThread(TAny*)
396 // Passed as the server thread in 2 tests - sets up and runs CTestServer
399 RTest test(_L("T_KILLER...server"));
402 test.Start(_L("Create and install ActiveScheduler"));
403 CMyActiveScheduler* pScheduler = new CMyActiveScheduler;
404 if (pScheduler==NULL)
406 clientThread.Kill(0);
407 User::Panic(_L("CreateSched failure"),KErrNoMemory);
410 CActiveScheduler::Install(pScheduler);
412 test.Next(_L("Creating and starting Server"));
413 CTestServer* pServer = new CTestServer(0);
416 clientThread.Kill(0);
417 User::Panic(_L("CreateServr failure"),KErrNoMemory);
420 TInt r=pServer->Start(_L("CTestServer"));//Starting a CServer2 also Adds it to the ActiveScheduler
423 clientThread.Kill(0);
424 User::Panic(_L("StartServr failure"),r);
428 test.Next(_L("Start ActiveScheduler and signal to client"));
429 test.Printf(_L(" There might be something going on beneath this window"));
431 CActiveScheduler::Start();
432 test.Next(_L("Destroy ActiveScheduler"));
441 const TInt KTestPanic = 14849;
443 TInt PanicTestThread (TAny*)
445 // Passed as a thread entry - just calls RMessage2::Panic()
449 message.Panic(_L("Testing Panic"),KTestPanic);
453 RTest test(_L("Main T_KILLER test"));
455 TInt CompletePanicClientThread (TAny*)
457 // Passed as the second client thread entry - signals to server to call Complete() twice
462 TInt r=session.PublicCreateSession(_L("CTestServer"),1);
465 r=session.PublicSendReceive(CTestSession::ETestCompletePanic, TIpcArgs());
468 session.PublicSendReceive(CTestSession::EStop, TIpcArgs());//panic should occur before this is serviced
472 void SimpleRMessage()
474 // Simple RMessage2 Tests - constructors and assignment
478 test.Start(_L("Default constructor"));
481 test.Next(_L("Copy constructor"));
482 RMessage2 message2(message1);
483 test(message1.Function()==message2.Function());
484 test(message1.Int0()==message2.Int0());
485 test(message1.Int1()==message2.Int1());
486 test(message1.Int2()==message2.Int2());
487 test(message1.Int3()==message2.Int3());
489 test(message1.Client(client1) == KErrNone);
491 test(message2.Client(client2) == KErrNone);
492 test(client1.Handle()==client2.Handle());
495 test.Next(_L("Assignment operator"));
496 RMessage2 message3(*(RMessage2*) SimpleRMessage);// Pass some rubbish so message3 is definitely != message1
498 test(message1.Function()==message3.Function());
499 test(message1.Int0()==message3.Int0());
500 test(message1.Int1()==message3.Int1());
501 test(message1.Int2()==message3.Int2());
502 test(message1.Int3()==message3.Int3());
504 test(message3.Client(client3) == KErrNone);
505 test(client1.Handle()==client3.Handle());
511 GLDEF_C TInt E32Main()
516 User::SetDebugMask(0xa04); // KSERVER+KTHREAD+KLOGON
520 test.Next(_L("Sending messages between two threads"));
521 TRequestStatus clientStat,killerStat,serverStat;
524 test.Start(_L("Create and start the server"));
526 serverThread.Create(_L("Server Thread"),ServerThread,KDefaultStackSize,KHeapMinSize,KHeapMaxSize,NULL);
527 serverThread.Logon(serverStat);
528 serverThread.Resume();
531 for (TInt i=0; serverStat==KRequestPending && i<100; i++)
533 test.Next(_L("Run and kill a client"));
534 clientName.Format(_L("Client Thread %d"),++clientNumber);
536 test.Start(_L("Create client and killer threads"));
537 err=clientThread.Create(clientName,ClientThread,KDefaultStackSize,KHeapMinSize,KHeapMaxSize,(TAny *)3);
539 test.Panic(_L("!!clientThread .Create failed"), err);
540 err=killerThread.Create(_L("Killer Thread"),KillerThread,KDefaultStackSize,KHeapMinSize,KHeapMaxSize,NULL);
542 test.Panic(_L("!!killerThread .Create failed"), err);
544 test.Next(_L("Logon to the threads"));
545 clientThread.Logon(clientStat);
546 killerThread.Logon(killerStat);
548 test.Next(_L("Start the threads"));
549 clientThread.Resume();
550 killerThread.Resume();
552 test.Next(_L("Wait for the client to stop"));
553 User::WaitForRequest(clientStat);
554 test.Next(_L("Wait for the killer to stop"));
555 User::WaitForRequest(killerStat);
556 exitType=clientThread.ExitType();
560 test.Printf(_L(" Client thread killed\n"));
563 test.Printf(_L("!!Client thread terminated:"));
564 test.Panic(clientThread.ExitCategory(), clientThread.ExitReason());
566 test.Printf(_L("!!Client thread panicked:"));
567 test.Panic(clientThread.ExitCategory(), clientThread.ExitReason());
569 test.Panic(_L("!!Client thread did something bizarre"), clientThread.ExitReason());
571 exitType=killerThread.ExitType();
575 test.Printf(_L(" Killer thread killed\n"));
578 test.Printf(_L("!!Killer thread terminated:"));
579 test.Panic(killerThread.ExitCategory(), killerThread.ExitReason());
581 test.Printf(_L("!!Killer thread panicked:"));
582 test.Panic(killerThread.ExitCategory(), killerThread.ExitReason());
584 // To catch a panic put a breakpoint in User::Panic() (in UCDT\UC_UNC.CPP).
587 test.Panic(_L("!!Killer thread did something bizarre"), killerThread.ExitReason());
589 test.Next(_L("Close the threads"));
590 clientThread.Close();
591 killerThread.Close();
592 // test.Next(_L("Pause for 1 second"));
593 // User::After(1000000);
596 test.Next(_L("Close the server thread"));
597 serverThread.Kill(0); // in case we got through the 100 iterations without killing it
598 User::WaitForRequest(serverStat);
599 exitType=serverThread.ExitType();
603 test.Printf(_L(" Server thread killed\n"));
606 test.Printf(_L("!!Server thread terminated:"));
607 test.Panic(serverThread.ExitCategory(), serverThread.ExitReason());
609 test.Printf(_L("!!Server thread panicked:"));
610 test.Panic(serverThread.ExitCategory(), serverThread.ExitReason());
612 // To catch a panic put a breakpoint in User::Panic() (in UCDT\UC_UNC.CPP).
615 test.Panic(_L("!!Server thread did something bizarre"), serverThread.ExitReason());
617 serverThread.Close();
620 test.Next(_L("The Panic() function"));
622 panicThread.Create(_L("Panic Test Thread"),PanicTestThread,KDefaultStackSize,KHeapMinSize,KHeapMaxSize,NULL);
624 panicThread.Logon(stat);
625 // don't want just in time debugging as we trap panics
626 TBool justInTime=User::JustInTime();
627 User::SetJustInTime(EFalse);
628 panicThread.Resume();
629 User::WaitForRequest(stat);
630 test(panicThread.ExitType()==EExitPanic);
631 test(panicThread.ExitCategory().Compare(_L("Testing Panic"))==0);
632 test(panicThread.ExitReason()==KTestPanic);
633 panicThread.Close(); //If this Close() is missed out Wins build 48 throws a wobbler when we next connect a server
636 test.Next(_L("Check it Panics if you try to Complete a message twice"));
637 test.Start(_L("Create client and server threads"));
638 clientThread.Create(_L("Client Thread"),CompletePanicClientThread,KDefaultStackSize,KHeapMinSize,KHeapMaxSize,NULL);
639 serverThread.Create(_L("Server Thread"),ServerThread,KDefaultStackSize,KHeapMinSize,KHeapMaxSize,NULL);
641 test.Next(_L("Logon to the threads"));
642 clientThread.Logon(clientStat);
643 serverThread.Logon(serverStat);
645 test.Next(_L("Start the threads"));
647 clientThread.Resume();
648 serverThread.Resume();
650 test.Next(_L("Wait for the threads to stop"));
651 User::WaitForRequest(clientStat);
652 User::WaitForRequest(serverStat);
653 test.Next(_L("Check the exit categories"));
654 test(clientThread.ExitType()==EExitKill);
655 test(clientThread.ExitCategory().Compare(_L("Kill"))==0);
656 test(clientThread.ExitReason()==KErrNone);
658 test(serverThread.ExitType()==EExitPanic);
659 test(serverThread.ExitCategory().Compare(_L("USER"))==0);
660 test(serverThread.ExitReason()==ETMesCompletion);
662 User::SetJustInTime(justInTime);
664 test.Next(_L("Close the threads"));
665 clientThread.Close();
666 serverThread.Close();