Update contrib.
1 // Copyright (c) 1998-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_thread2.cpp
15 // More tests the RThread class (T_THREAD was getting too big)
17 // Tests the RThread class
21 // - Test running a thread that suspends itself. This activity has
22 // deadlocked the Emulator in the past.
23 // - Test a server panicking a thread in another process that's already exited
24 // Platforms/Drives/Compatibility:
26 // Assumptions/Requirement/Pre-requisites:
27 // Failures and causes:
28 // Base Port information:
32 #define __E32TEST_EXTENSION__
37 const TInt KHeapSize=0x200;
39 _LIT(KMyName, "T_THREAD2");
41 LOCAL_D RTest test(KMyName);
43 LOCAL_C TInt SuspendThread(TAny*)
51 LOCAL_D void TestSelfSuspend(TOwnerType anOwnerType)
53 // Test running a thread that suspends itself. This activity has
54 // deadlocked the Emulator in the past
58 RThread suspendThread;
61 TInt jit=User::JustInTime();
62 test.Start(_L("Test running a thread which suspends itself"));
63 test.Next(_L("Create the thread"));
64 r=suspendThread.Create(KNullDesC,SuspendThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)NULL,anOwnerType);
66 suspendThread.Logon(s);
67 suspendThread.Resume();
68 test.Next(_L("Wait a second"));
70 User::SetJustInTime(EFalse);
71 suspendThread.Panic(_L("FEDCBA9876543210fedcba"),999);
72 User::WaitForRequest(s);
73 User::SetJustInTime(jit);
74 test(suspendThread.ExitType()==EExitPanic);
75 test(suspendThread.ExitReason()==999);
76 test(suspendThread.ExitCategory()==_L("FEDCBA9876543210"));
77 CLOSE_AND_WAIT(suspendThread);
82 _LIT(KServerName,"MyServer");
84 RSemaphore TheSemaphore;
86 class CMySession : public CSession2
90 virtual void ServiceL(const RMessage2& aMessage);
93 class CMyServer : public CServer2
97 virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
100 class RSession : public RSessionBase
104 void Test(TRequestStatus& aStatus);
107 CMySession::CMySession()
111 CMyServer::CMyServer() :
116 CSession2* CMyServer::NewSessionL(const TVersion&, const RMessage2&) const
118 RDebug::Printf("Server: create session");
119 return new (ELeave) CMySession;
122 void CMySession::ServiceL(const RMessage2& aMessage)
124 RDebug::Printf("Server: receive message");
126 RDebug::Printf("Server: panic client");
127 aMessage.Panic(_L("!!!"), 1);
128 CActiveScheduler::Stop();
131 TInt RSession::Open()
133 return CreateSession(KServerName, TVersion());
136 void RSession::Test(TRequestStatus& aStatus)
138 RDebug::Printf("Client: send message");
139 SendReceive(0, TIpcArgs(), aStatus);
140 RDebug::Printf("Client: send done");
143 TInt ServerThread(TAny*)
145 RDebug::Printf("Server: start");
147 CTrapCleanup* cleanup = CTrapCleanup::New();
149 CActiveScheduler* pR = new CActiveScheduler;
152 CActiveScheduler::Install(pR);
154 CMyServer* pS = new CMyServer;
157 TInt r = pS->Start(KServerName);
160 RThread::Rendezvous(KErrNone);
162 TRAP(r, CActiveScheduler::Start());
170 RDebug::Printf("Server: exit");
176 RDebug::Printf("Client: open session");
179 TRequestStatus status;
180 RDebug::Printf("Client: send request");
181 session.Test(status);
182 RDebug::Printf("Client: exit");
186 void TestServerPanic()
188 TRequestStatus status;
190 test_KErrNone(TheSemaphore.CreateLocal(0));
192 RDebug::Printf("Main: start server");
193 RThread serverThread;
194 test_KErrNone(serverThread.Create(_L("server"), ServerThread, 4096, NULL, NULL));
195 serverThread.Rendezvous(status);
196 serverThread.Resume();
197 User::WaitForRequest(status);
198 test_KErrNone(status.Int());
200 RDebug::Printf("Main: start client");
201 RProcess clientProcess;
202 test_KErrNone(clientProcess.Create(KMyName, _L("client")));
203 clientProcess.Resume();
204 clientProcess.Logon(status);
205 User::WaitForRequest(status);
206 test_KErrNone(clientProcess.ExitReason());
207 test_Equal(EExitKill, clientProcess.ExitType());
209 RDebug::Printf("Main: kick server");
210 TheSemaphore.Signal();
212 RDebug::Printf("Main: wait for server to exit");
213 serverThread.Logon(status);
214 User::WaitForRequest(status);
215 test_KErrNone(serverThread.ExitReason());
216 test_Equal(EExitKill, serverThread.ExitType());
219 RDebug::Printf("Main: exit");
223 GLDEF_C TInt E32Main()
229 if (User::CommandLineLength())
230 return ClientProcess();
235 test.Start(_L("Test threads"));
237 test.Next(_L("Test a thread suspending itself"));
238 TestSelfSuspend(EOwnerProcess);
239 TestSelfSuspend(EOwnerThread);
241 test.Next(_L("Test a server panicking a thread in another process that's already exited"));