sl@0
|
1 |
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
2 |
// All rights reserved.
|
sl@0
|
3 |
// This component and the accompanying materials are made available
|
sl@0
|
4 |
// under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
5 |
// which accompanies this distribution, and is available
|
sl@0
|
6 |
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
7 |
//
|
sl@0
|
8 |
// Initial Contributors:
|
sl@0
|
9 |
// Nokia Corporation - initial contribution.
|
sl@0
|
10 |
//
|
sl@0
|
11 |
// Contributors:
|
sl@0
|
12 |
//
|
sl@0
|
13 |
// Description:
|
sl@0
|
14 |
// e32test\thread\t_threadedserver.cpp
|
sl@0
|
15 |
// Tests the multi-threaded server/session support in CServer2/CSession2
|
sl@0
|
16 |
// Overview:
|
sl@0
|
17 |
// Tests the CServer2::SetMaster and CSession2:SetServer functions
|
sl@0
|
18 |
// API Information:
|
sl@0
|
19 |
// CServer2, CSession2
|
sl@0
|
20 |
// Details:
|
sl@0
|
21 |
// - Test creating multiple sessions with a single-threaded server
|
sl@0
|
22 |
// - Test creating multiple sessions with a multi-threaded server
|
sl@0
|
23 |
// - Verify that requests to a multi-threaded server can run in parallel and complete out-of-order
|
sl@0
|
24 |
// Platforms/Drives/Compatibility:
|
sl@0
|
25 |
// All.
|
sl@0
|
26 |
// Assumptions/Requirement/Pre-requisites:
|
sl@0
|
27 |
// Failures and causes:
|
sl@0
|
28 |
// Base Port information:
|
sl@0
|
29 |
//
|
sl@0
|
30 |
|
sl@0
|
31 |
|
sl@0
|
32 |
|
sl@0
|
33 |
#define __E32TEST_EXTENSION__
|
sl@0
|
34 |
#include <e32test.h>
|
sl@0
|
35 |
#include <e32panic.h>
|
sl@0
|
36 |
#include <e32debug.h>
|
sl@0
|
37 |
|
sl@0
|
38 |
_LIT(KMyName, "T_THREADEDSERVER");
|
sl@0
|
39 |
_LIT(KServerName,"MyServer");
|
sl@0
|
40 |
const TInt KHeapSize = 4096;
|
sl@0
|
41 |
|
sl@0
|
42 |
LOCAL_D RTest test(KMyName);
|
sl@0
|
43 |
|
sl@0
|
44 |
class CMySession : public CSession2
|
sl@0
|
45 |
{
|
sl@0
|
46 |
public:
|
sl@0
|
47 |
enum TMyRequests { EFinish, EDelay5, EPing };
|
sl@0
|
48 |
CMySession(TInt aId);
|
sl@0
|
49 |
virtual void ServiceL(const RMessage2& aMessage);
|
sl@0
|
50 |
private:
|
sl@0
|
51 |
const TInt iD;
|
sl@0
|
52 |
};
|
sl@0
|
53 |
|
sl@0
|
54 |
CMySession::CMySession(TInt aId) :
|
sl@0
|
55 |
iD(aId)
|
sl@0
|
56 |
{
|
sl@0
|
57 |
}
|
sl@0
|
58 |
|
sl@0
|
59 |
void CMySession::ServiceL(const RMessage2& aMessage)
|
sl@0
|
60 |
{
|
sl@0
|
61 |
TInt err = KErrNotSupported;
|
sl@0
|
62 |
|
sl@0
|
63 |
RDebug::Printf("Session %d: received message %d", iD, aMessage.Function());
|
sl@0
|
64 |
switch (aMessage.Function())
|
sl@0
|
65 |
{
|
sl@0
|
66 |
case EFinish:
|
sl@0
|
67 |
CActiveScheduler::Stop();
|
sl@0
|
68 |
err = KErrNone;
|
sl@0
|
69 |
break;
|
sl@0
|
70 |
|
sl@0
|
71 |
case EDelay5:
|
sl@0
|
72 |
User::After(5000000);
|
sl@0
|
73 |
err = KErrNone;
|
sl@0
|
74 |
break;
|
sl@0
|
75 |
|
sl@0
|
76 |
case EPing:
|
sl@0
|
77 |
User::After(1000000);
|
sl@0
|
78 |
err = KErrNone;
|
sl@0
|
79 |
break;
|
sl@0
|
80 |
|
sl@0
|
81 |
default:
|
sl@0
|
82 |
break;
|
sl@0
|
83 |
}
|
sl@0
|
84 |
|
sl@0
|
85 |
RDebug::Printf("Session %d: completing message %d, err %d", iD, aMessage.Function(), err);
|
sl@0
|
86 |
if (err == KErrNone)
|
sl@0
|
87 |
aMessage.Complete(err);
|
sl@0
|
88 |
else
|
sl@0
|
89 |
aMessage.Panic(KServerName, err);
|
sl@0
|
90 |
}
|
sl@0
|
91 |
|
sl@0
|
92 |
|
sl@0
|
93 |
class CMyServer : public CServer2
|
sl@0
|
94 |
{
|
sl@0
|
95 |
public:
|
sl@0
|
96 |
CMyServer();
|
sl@0
|
97 |
virtual CSession2* NewSessionL(const TVersion& aVersion, const RMessage2& aMessage) const;
|
sl@0
|
98 |
private:
|
sl@0
|
99 |
mutable TInt iCount;
|
sl@0
|
100 |
};
|
sl@0
|
101 |
|
sl@0
|
102 |
static CMyServer* TheMaster = NULL;
|
sl@0
|
103 |
static CMyServer* TheSlave = NULL;
|
sl@0
|
104 |
|
sl@0
|
105 |
CMyServer::CMyServer() :
|
sl@0
|
106 |
CServer2(0),
|
sl@0
|
107 |
iCount(0)
|
sl@0
|
108 |
{
|
sl@0
|
109 |
}
|
sl@0
|
110 |
|
sl@0
|
111 |
CSession2* CMyServer::NewSessionL(const TVersion&, const RMessage2&) const
|
sl@0
|
112 |
{
|
sl@0
|
113 |
RDebug::Printf("Server: creating session, slave is $%x", TheSlave);
|
sl@0
|
114 |
iCount += 1;
|
sl@0
|
115 |
CSession2* mySession = new (ELeave) CMySession(iCount);
|
sl@0
|
116 |
if (iCount > 1 && TheSlave != NULL)
|
sl@0
|
117 |
mySession->SetServer(TheSlave);
|
sl@0
|
118 |
RDebug::Printf("Server: created session %d", iCount);
|
sl@0
|
119 |
return mySession;
|
sl@0
|
120 |
}
|
sl@0
|
121 |
|
sl@0
|
122 |
TInt RunServerCode(TAny* aFlag)
|
sl@0
|
123 |
{
|
sl@0
|
124 |
TInt mode = (TInt)aFlag;
|
sl@0
|
125 |
TInt r = KErrNoMemory;
|
sl@0
|
126 |
RDebug::Printf("Server: start, mode %d", mode);
|
sl@0
|
127 |
|
sl@0
|
128 |
CTrapCleanup* cleanup = CTrapCleanup::New();
|
sl@0
|
129 |
if (cleanup == NULL)
|
sl@0
|
130 |
return r;
|
sl@0
|
131 |
|
sl@0
|
132 |
CActiveScheduler* pR = new CActiveScheduler;
|
sl@0
|
133 |
if (pR == NULL)
|
sl@0
|
134 |
return r;
|
sl@0
|
135 |
|
sl@0
|
136 |
CActiveScheduler::Install(pR);
|
sl@0
|
137 |
CMyServer* pS = new CMyServer();
|
sl@0
|
138 |
if (pS == NULL)
|
sl@0
|
139 |
return r;
|
sl@0
|
140 |
|
sl@0
|
141 |
switch (mode)
|
sl@0
|
142 |
{
|
sl@0
|
143 |
case 0:
|
sl@0
|
144 |
r = pS->Start(KServerName);
|
sl@0
|
145 |
break;
|
sl@0
|
146 |
case 1:
|
sl@0
|
147 |
TheMaster = pS;
|
sl@0
|
148 |
pS->SetMaster(TheMaster);
|
sl@0
|
149 |
r = pS->Start(KServerName);
|
sl@0
|
150 |
break;
|
sl@0
|
151 |
case 2:
|
sl@0
|
152 |
TheSlave = pS;
|
sl@0
|
153 |
pS->SetMaster(TheMaster);
|
sl@0
|
154 |
r = pS->Start(KNullDesC);
|
sl@0
|
155 |
break;
|
sl@0
|
156 |
}
|
sl@0
|
157 |
if (r != KErrNone)
|
sl@0
|
158 |
return r;
|
sl@0
|
159 |
|
sl@0
|
160 |
RThread::Rendezvous(KErrNone);
|
sl@0
|
161 |
TRAP(r, CActiveScheduler::Start());
|
sl@0
|
162 |
if (r != KErrNone)
|
sl@0
|
163 |
return r;
|
sl@0
|
164 |
|
sl@0
|
165 |
delete pS;
|
sl@0
|
166 |
delete pR;
|
sl@0
|
167 |
delete cleanup;
|
sl@0
|
168 |
|
sl@0
|
169 |
RDebug::Printf("Server: exit");
|
sl@0
|
170 |
return KErrNone;
|
sl@0
|
171 |
}
|
sl@0
|
172 |
|
sl@0
|
173 |
|
sl@0
|
174 |
class RSession : public RSessionBase
|
sl@0
|
175 |
{
|
sl@0
|
176 |
public:
|
sl@0
|
177 |
TInt Create();
|
sl@0
|
178 |
void Request(TInt aRequest, TRequestStatus& aStatus);
|
sl@0
|
179 |
void Request(TInt aRequest);
|
sl@0
|
180 |
};
|
sl@0
|
181 |
|
sl@0
|
182 |
TInt RSession::Create()
|
sl@0
|
183 |
{
|
sl@0
|
184 |
return CreateSession(KServerName, TVersion());
|
sl@0
|
185 |
}
|
sl@0
|
186 |
|
sl@0
|
187 |
void RSession::Request(TInt aRequest, TRequestStatus& aStatus)
|
sl@0
|
188 |
{
|
sl@0
|
189 |
SendReceive(aRequest, TIpcArgs(), aStatus);
|
sl@0
|
190 |
}
|
sl@0
|
191 |
|
sl@0
|
192 |
void RSession::Request(TInt aRequest)
|
sl@0
|
193 |
{
|
sl@0
|
194 |
SendReceive(aRequest, TIpcArgs());
|
sl@0
|
195 |
}
|
sl@0
|
196 |
|
sl@0
|
197 |
TInt RunClientCode(TAny* aFlag)
|
sl@0
|
198 |
{
|
sl@0
|
199 |
TInt mode = (TInt)aFlag;
|
sl@0
|
200 |
RDebug::Printf("Client: open sessions");
|
sl@0
|
201 |
RSession session1, session2;
|
sl@0
|
202 |
session1.Create();
|
sl@0
|
203 |
session2.Create();
|
sl@0
|
204 |
|
sl@0
|
205 |
TBool ooc = EFalse;
|
sl@0
|
206 |
TRequestStatus status1 (KRequestPending), status2 (KRequestPending);
|
sl@0
|
207 |
RDebug::Printf("Client: send request 1");
|
sl@0
|
208 |
session1.Request(CMySession::EDelay5, status1);
|
sl@0
|
209 |
User::After(1000000);
|
sl@0
|
210 |
RDebug::Printf("Client: send request 2");
|
sl@0
|
211 |
session2.Request(CMySession::EPing, status2);
|
sl@0
|
212 |
|
sl@0
|
213 |
User::WaitForRequest(status1, status2);
|
sl@0
|
214 |
if (status1 == KRequestPending)
|
sl@0
|
215 |
{
|
sl@0
|
216 |
// request2 finished first ...
|
sl@0
|
217 |
ooc = ETrue;
|
sl@0
|
218 |
RDebug::Printf("Client: request 2 completed %d", status2.Int());
|
sl@0
|
219 |
User::WaitForRequest(status1);
|
sl@0
|
220 |
}
|
sl@0
|
221 |
else
|
sl@0
|
222 |
{
|
sl@0
|
223 |
RDebug::Printf("Client: request 1 completed %d", status1.Int());
|
sl@0
|
224 |
User::WaitForRequest(status2);
|
sl@0
|
225 |
}
|
sl@0
|
226 |
RDebug::Printf("Client: status1 %d status2 %d OOC %d", status1.Int(), status2.Int(), ooc);
|
sl@0
|
227 |
test_KErrNone(status1.Int());
|
sl@0
|
228 |
test_KErrNone(status2.Int());
|
sl@0
|
229 |
test_Equal(ooc, mode > 0);
|
sl@0
|
230 |
|
sl@0
|
231 |
if (mode)
|
sl@0
|
232 |
session2.Request(CMySession::EFinish);
|
sl@0
|
233 |
session1.Request(CMySession::EFinish);
|
sl@0
|
234 |
RDebug::Printf("Client: exit");
|
sl@0
|
235 |
return KErrNone;
|
sl@0
|
236 |
}
|
sl@0
|
237 |
|
sl@0
|
238 |
|
sl@0
|
239 |
void TestSequential()
|
sl@0
|
240 |
{
|
sl@0
|
241 |
TRequestStatus status;
|
sl@0
|
242 |
|
sl@0
|
243 |
RDebug::Printf("Main: start server");
|
sl@0
|
244 |
RThread serverThread;
|
sl@0
|
245 |
test_KErrNone(serverThread.Create(_L("server"), RunServerCode, KHeapSize, NULL, (TAny*)0));
|
sl@0
|
246 |
serverThread.Rendezvous(status);
|
sl@0
|
247 |
serverThread.Resume();
|
sl@0
|
248 |
User::WaitForRequest(status);
|
sl@0
|
249 |
test_KErrNone(status.Int());
|
sl@0
|
250 |
|
sl@0
|
251 |
RDebug::Printf("Main: start client");
|
sl@0
|
252 |
RThread clientThread;
|
sl@0
|
253 |
test_KErrNone(clientThread.Create(_L("client"), RunClientCode, KHeapSize, NULL, (TAny*)0));
|
sl@0
|
254 |
clientThread.Logon(status);
|
sl@0
|
255 |
clientThread.Resume();
|
sl@0
|
256 |
User::WaitForRequest(status);
|
sl@0
|
257 |
test_KErrNone(clientThread.ExitReason());
|
sl@0
|
258 |
test_Equal(EExitKill, clientThread.ExitType());
|
sl@0
|
259 |
|
sl@0
|
260 |
serverThread.Logon(status);
|
sl@0
|
261 |
User::WaitForRequest(status);
|
sl@0
|
262 |
test_KErrNone(serverThread.ExitReason());
|
sl@0
|
263 |
test_Equal(EExitKill, serverThread.ExitType());
|
sl@0
|
264 |
|
sl@0
|
265 |
User::After(1000);
|
sl@0
|
266 |
RDebug::Printf("Main: exit");
|
sl@0
|
267 |
}
|
sl@0
|
268 |
|
sl@0
|
269 |
void TestParallel()
|
sl@0
|
270 |
{
|
sl@0
|
271 |
TRequestStatus status;
|
sl@0
|
272 |
|
sl@0
|
273 |
RDebug::Printf("Main: start master server");
|
sl@0
|
274 |
RThread masterThread;
|
sl@0
|
275 |
test_KErrNone(masterThread.Create(_L("master"), RunServerCode, KHeapSize, NULL, (TAny*)1));
|
sl@0
|
276 |
masterThread.Rendezvous(status);
|
sl@0
|
277 |
masterThread.Resume();
|
sl@0
|
278 |
User::WaitForRequest(status);
|
sl@0
|
279 |
test_KErrNone(status.Int());
|
sl@0
|
280 |
|
sl@0
|
281 |
RDebug::Printf("Main: start slave server");
|
sl@0
|
282 |
RThread slaveThread;
|
sl@0
|
283 |
test_KErrNone(slaveThread.Create(_L("slave"), RunServerCode, KHeapSize, NULL, (TAny*)2));
|
sl@0
|
284 |
slaveThread.Rendezvous(status);
|
sl@0
|
285 |
slaveThread.Resume();
|
sl@0
|
286 |
User::WaitForRequest(status);
|
sl@0
|
287 |
test_KErrNone(status.Int());
|
sl@0
|
288 |
|
sl@0
|
289 |
RDebug::Printf("Main: start client");
|
sl@0
|
290 |
RThread clientThread;
|
sl@0
|
291 |
test_KErrNone(clientThread.Create(_L("client2"), RunClientCode, KHeapSize, NULL, (TAny*)3));
|
sl@0
|
292 |
clientThread.Logon(status);
|
sl@0
|
293 |
clientThread.Resume();
|
sl@0
|
294 |
User::WaitForRequest(status);
|
sl@0
|
295 |
test_KErrNone(clientThread.ExitReason());
|
sl@0
|
296 |
test_Equal(EExitKill, clientThread.ExitType());
|
sl@0
|
297 |
|
sl@0
|
298 |
slaveThread.Logon(status);
|
sl@0
|
299 |
User::WaitForRequest(status);
|
sl@0
|
300 |
test_KErrNone(slaveThread.ExitReason());
|
sl@0
|
301 |
test_Equal(EExitKill, slaveThread.ExitType());
|
sl@0
|
302 |
|
sl@0
|
303 |
masterThread.Logon(status);
|
sl@0
|
304 |
User::WaitForRequest(status);
|
sl@0
|
305 |
test_KErrNone(masterThread.ExitReason());
|
sl@0
|
306 |
test_Equal(EExitKill, masterThread.ExitType());
|
sl@0
|
307 |
|
sl@0
|
308 |
User::After(1000);
|
sl@0
|
309 |
RDebug::Printf("Main: exit");
|
sl@0
|
310 |
}
|
sl@0
|
311 |
|
sl@0
|
312 |
|
sl@0
|
313 |
GLDEF_C TInt E32Main()
|
sl@0
|
314 |
//
|
sl@0
|
315 |
// Main
|
sl@0
|
316 |
//
|
sl@0
|
317 |
{
|
sl@0
|
318 |
test.Title();
|
sl@0
|
319 |
__UHEAP_MARK;
|
sl@0
|
320 |
|
sl@0
|
321 |
test.Start(_L("Test threaded server support"));
|
sl@0
|
322 |
test.Next(_L("Test two sessions to the same server (single-threaded)"));
|
sl@0
|
323 |
TestSequential();
|
sl@0
|
324 |
test.Next(_L("Test two sessions to the same server (multi-threaded)"));
|
sl@0
|
325 |
TestParallel();
|
sl@0
|
326 |
test.End();
|
sl@0
|
327 |
|
sl@0
|
328 |
__UHEAP_MARKEND;
|
sl@0
|
329 |
return(KErrNone);
|
sl@0
|
330 |
}
|