0
|
1 |
// Copyright (c) 1996-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".
|
|
7 |
//
|
|
8 |
// Initial Contributors:
|
|
9 |
// Nokia Corporation - initial contribution.
|
|
10 |
//
|
|
11 |
// Contributors:
|
|
12 |
//
|
|
13 |
// Description:
|
|
14 |
// e32test\bench\t_svr2.cpp
|
|
15 |
// Overview:
|
|
16 |
// Tests the Client/Server architecture of the Symbian platform.
|
|
17 |
// Tests that messages are completed when a server terminates.
|
|
18 |
// API Information:
|
|
19 |
// CSession2, CServer2, RSessionBase.
|
|
20 |
// Details:
|
|
21 |
// - Create and start a server thread. Verify results are as expected.
|
|
22 |
// - Create and close client sessions to enforce granularity expansion.
|
|
23 |
// - Open and close a session and verify that the heap is OK.
|
|
24 |
// - Open a connection specifying 0 message slots for the session and verify
|
|
25 |
// that the server returns KErrNone.
|
|
26 |
// - Create lot of timers to eliminate kernel granularities.
|
|
27 |
// - Create a client session and validate kernel heap is ok if client is killed.
|
|
28 |
// - Create a client session and validate kernel heap is ok if server is killed
|
|
29 |
// when client has outstanding request to server.
|
|
30 |
// Platforms/Drives/Compatibility:
|
|
31 |
// All
|
|
32 |
// Assumptions/Requirement/Pre-requisites:
|
|
33 |
// Failures and causes:
|
|
34 |
// Base Port information:
|
|
35 |
//
|
|
36 |
//
|
|
37 |
|
|
38 |
#include <e32base.h>
|
|
39 |
#include <e32base_private.h>
|
|
40 |
#include <e32test.h>
|
|
41 |
#include <e32def.h>
|
|
42 |
#include <e32def_private.h>
|
|
43 |
|
|
44 |
const TInt KHeapSize=0x4000;
|
|
45 |
const TInt KMajorVersionNumber=1;
|
|
46 |
const TInt KMinorVersionNumber=0;
|
|
47 |
const TInt KBuildVersionNumber=1;
|
|
48 |
|
|
49 |
_LIT(KServerName,"MyServer");
|
|
50 |
|
|
51 |
class CMySession : public CSession2
|
|
52 |
{
|
|
53 |
public:
|
|
54 |
enum {ETest, ENeverComplete};
|
|
55 |
public:
|
|
56 |
CMySession();
|
|
57 |
virtual void ServiceL(const RMessage2& aMessage);//Overloading pure virtual
|
|
58 |
};
|
|
59 |
|
|
60 |
class CMyServer : public CServer2
|
|
61 |
{
|
|
62 |
public:
|
|
63 |
CMyServer(TInt aPriority);
|
|
64 |
static CMyServer *New(TInt aPriority);
|
|
65 |
virtual CSession2* NewSessionL(const TVersion& aVersion,const RMessage2& aMessage) const ;
|
|
66 |
};
|
|
67 |
|
|
68 |
class CMyActiveScheduler : public CActiveScheduler
|
|
69 |
{
|
|
70 |
public:
|
|
71 |
virtual void Error(TInt anError) const; //Overloading pure virtual
|
|
72 |
};
|
|
73 |
|
|
74 |
|
|
75 |
class RMySessionBase : public RSessionBase
|
|
76 |
{
|
|
77 |
public:
|
|
78 |
TInt Open();
|
|
79 |
TInt Test();
|
|
80 |
TInt NeverComplete();
|
|
81 |
TVersion Version();
|
|
82 |
};
|
|
83 |
|
|
84 |
_LIT(KSvrSemName, "svrSem");
|
|
85 |
_LIT(KSvrSem2Name, "svrSem2");
|
|
86 |
_LIT(KClientSemName, "clientSem");
|
|
87 |
|
|
88 |
CMySession::CMySession()
|
|
89 |
{}
|
|
90 |
|
|
91 |
void CMySession::ServiceL(const RMessage2& aMessage)
|
|
92 |
//
|
|
93 |
// Handle messages for this server.
|
|
94 |
//
|
|
95 |
{
|
|
96 |
RSemaphore svrSem;
|
|
97 |
|
|
98 |
//CMySession &c=(CMySession &)aClient;// passed as CSession2 since fn overloads pure virtual fn
|
|
99 |
TInt r=KErrNone;
|
|
100 |
switch (aMessage.Function())
|
|
101 |
{
|
|
102 |
case ETest:
|
|
103 |
break;
|
|
104 |
case ENeverComplete:
|
|
105 |
r = svrSem.OpenGlobal(KSvrSemName);
|
|
106 |
if (r != KErrNone)
|
|
107 |
break;
|
|
108 |
svrSem.Signal(); // all ready for the server to be killed
|
|
109 |
svrSem.Close();
|
|
110 |
return;
|
|
111 |
default:
|
|
112 |
r=KErrNotSupported;
|
|
113 |
}
|
|
114 |
aMessage.Complete(r);
|
|
115 |
}
|
|
116 |
|
|
117 |
CMyServer* CMyServer::New(TInt aPriority)
|
|
118 |
//
|
|
119 |
// Create a new CMyServer.
|
|
120 |
//
|
|
121 |
{
|
|
122 |
|
|
123 |
return new CMyServer(aPriority);
|
|
124 |
}
|
|
125 |
|
|
126 |
CMyServer::CMyServer(TInt aPriority)
|
|
127 |
//
|
|
128 |
// Constructor.
|
|
129 |
//
|
|
130 |
: CServer2(aPriority)
|
|
131 |
{}
|
|
132 |
|
|
133 |
CSession2* CMyServer::NewSessionL(const TVersion&, const RMessage2&) const
|
|
134 |
//
|
|
135 |
// Create a new client for this server.
|
|
136 |
//
|
|
137 |
{
|
|
138 |
return new(ELeave) CMySession();
|
|
139 |
}
|
|
140 |
|
|
141 |
void CMyActiveScheduler::Error(TInt anError) const
|
|
142 |
//
|
|
143 |
// Called if any Run() method leaves.
|
|
144 |
//
|
|
145 |
{
|
|
146 |
RTest testSvr(_L("Server"));
|
|
147 |
testSvr.Panic(anError,_L("CMyActiveScheduler::Error"));
|
|
148 |
}
|
|
149 |
|
|
150 |
|
|
151 |
TInt RMySessionBase::Open()
|
|
152 |
//
|
|
153 |
// Open the server.
|
|
154 |
//
|
|
155 |
{
|
|
156 |
return CreateSession(KServerName,Version(),0);
|
|
157 |
}
|
|
158 |
|
|
159 |
TInt RMySessionBase::Test()
|
|
160 |
//
|
|
161 |
// Send a message and wait for completion.
|
|
162 |
//
|
|
163 |
{
|
|
164 |
return SendReceive(CMySession::ETest, TIpcArgs());
|
|
165 |
}
|
|
166 |
|
|
167 |
TInt RMySessionBase::NeverComplete()
|
|
168 |
//
|
|
169 |
// Send a message and wait for completion.
|
|
170 |
//
|
|
171 |
{
|
|
172 |
return SendReceive(CMySession::ENeverComplete, TIpcArgs());
|
|
173 |
}
|
|
174 |
|
|
175 |
TVersion RMySessionBase::Version()
|
|
176 |
//
|
|
177 |
// Return the current version.
|
|
178 |
//
|
|
179 |
{
|
|
180 |
TVersion v(KMajorVersionNumber,KMinorVersionNumber,KBuildVersionNumber);
|
|
181 |
return v;
|
|
182 |
}
|
|
183 |
|
|
184 |
LOCAL_C TInt serverThreadEntryPoint(TAny*)
|
|
185 |
//
|
|
186 |
// The entry point for the server thread.
|
|
187 |
//
|
|
188 |
{
|
|
189 |
CMyActiveScheduler *pR = new CMyActiveScheduler;
|
|
190 |
if (pR == NULL)
|
|
191 |
return KErrNoMemory;
|
|
192 |
CActiveScheduler::Install(pR);
|
|
193 |
|
|
194 |
CMyServer* pS = CMyServer::New(0);
|
|
195 |
if (pS == NULL)
|
|
196 |
return KErrNoMemory;
|
|
197 |
TInt r = pS->Start(KServerName);
|
|
198 |
if (r != KErrNone)
|
|
199 |
return r;
|
|
200 |
|
|
201 |
RSemaphore svrSem2;
|
|
202 |
r = svrSem2.OpenGlobal(KSvrSem2Name);
|
|
203 |
if (r != KErrNone)
|
|
204 |
return r;
|
|
205 |
svrSem2.Signal();
|
|
206 |
svrSem2.Close();
|
|
207 |
CActiveScheduler::Start();
|
|
208 |
return KErrNone;
|
|
209 |
}
|
|
210 |
|
|
211 |
|
|
212 |
LOCAL_C TInt clientThreadEntryPoint(TAny *param)
|
|
213 |
//
|
|
214 |
// The entry point for the client thread.
|
|
215 |
//
|
|
216 |
{
|
|
217 |
RSemaphore svrSem2;
|
|
218 |
RSemaphore clientSem;
|
|
219 |
TInt r = clientSem.OpenGlobal(KClientSemName);
|
|
220 |
if (r != KErrNone)
|
|
221 |
return r;
|
|
222 |
RMySessionBase t;
|
|
223 |
RMySessionBase t2;
|
|
224 |
// RTimer rt[40];
|
|
225 |
// TUint c;
|
|
226 |
|
|
227 |
switch ((TUint32)param)
|
|
228 |
{
|
|
229 |
case 1:
|
|
230 |
r=t.Open();
|
|
231 |
if (r != KErrNone)
|
|
232 |
break;
|
|
233 |
clientSem.Signal();
|
|
234 |
clientSem.Close();
|
|
235 |
FOREVER
|
|
236 |
;
|
|
237 |
case 2:
|
|
238 |
// Now create a lot of timers to eliminate kernel granularities
|
|
239 |
/* for (c=0;c<40;c++)
|
|
240 |
{
|
|
241 |
TInt r=rt[c].CreateLocal();
|
|
242 |
test(r==KErrNone);
|
|
243 |
}
|
|
244 |
for (c=0;c<39;c++)
|
|
245 |
rt[c].Close();
|
|
246 |
*/
|
|
247 |
r=svrSem2.OpenGlobal(KSvrSem2Name);
|
|
248 |
if (r != KErrNone)
|
|
249 |
break;
|
|
250 |
clientSem.Signal();
|
|
251 |
svrSem2.Wait();
|
|
252 |
svrSem2.Close();
|
|
253 |
User::After(2000000);
|
|
254 |
r=t2.Open();
|
|
255 |
if (r != KErrNone)
|
|
256 |
break;
|
|
257 |
r=t2.NeverComplete(); // r should be KErrServerTerminated
|
|
258 |
if (r != KErrServerTerminated)
|
|
259 |
break;
|
|
260 |
t2.Close();
|
|
261 |
clientSem.Signal();
|
|
262 |
clientSem.Close();
|
|
263 |
FOREVER
|
|
264 |
;
|
|
265 |
default:
|
|
266 |
break;
|
|
267 |
}
|
|
268 |
return r;
|
|
269 |
}
|
|
270 |
|
|
271 |
GLDEF_C TInt E32Main()
|
|
272 |
//
|
|
273 |
// Test server & session cleanup.
|
|
274 |
//
|
|
275 |
{
|
|
276 |
RSemaphore svrSem;
|
|
277 |
RSemaphore svrSem2;
|
|
278 |
RSemaphore clientSem;
|
|
279 |
RTest test(_L("T_SVR2"));
|
|
280 |
test.Title();
|
|
281 |
|
|
282 |
__KHEAP_MARK;
|
|
283 |
|
|
284 |
test.Start(_L("Creating server semaphore"));
|
|
285 |
TInt r=svrSem.CreateGlobal(KSvrSemName, 0);
|
|
286 |
test(r==KErrNone);
|
|
287 |
test.Next(_L("Creating server semaphore 2"));
|
|
288 |
r=svrSem2.CreateGlobal(KSvrSem2Name, 0);
|
|
289 |
test(r==KErrNone);
|
|
290 |
test.Next(_L("Creating client semaphore"));
|
|
291 |
r=clientSem.CreateGlobal(KClientSemName, 0);
|
|
292 |
test(r==KErrNone);
|
|
293 |
|
|
294 |
test.Next(_L("Creating server"));
|
|
295 |
RThread server;
|
|
296 |
r=server.Create(_L("MyServer"),serverThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)1);
|
|
297 |
test(r==KErrNone);
|
|
298 |
server.Resume();
|
|
299 |
svrSem2.Wait(); // server up & running
|
|
300 |
|
|
301 |
test.Next(_L("Forcing granularity expansion"));
|
|
302 |
const TInt KNumberOfSessions=10;
|
|
303 |
TInt i;
|
|
304 |
RMySessionBase ts[KNumberOfSessions];
|
|
305 |
for (i=0; i<KNumberOfSessions; i++)
|
|
306 |
ts[i].Open();
|
|
307 |
for (i=0; i<KNumberOfSessions; i++)
|
|
308 |
ts[i].Close();
|
|
309 |
|
|
310 |
test.Next(_L("Opening a session and closing it"));
|
|
311 |
RMySessionBase t;
|
|
312 |
r=t.Open();
|
|
313 |
test(r==KErrNone);
|
|
314 |
t.Close();
|
|
315 |
RThread clientx;
|
|
316 |
r=clientx.Create(_L("MyClientx"),clientThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)1);
|
|
317 |
test(r==KErrNone);
|
|
318 |
clientx.Resume();
|
|
319 |
clientSem.Wait(); // client connected
|
|
320 |
|
|
321 |
test.Next(_L("Creating client & killing it"));
|
|
322 |
|
|
323 |
RThread client;
|
|
324 |
r=client.Create(_L("MyClient"),clientThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)1);
|
|
325 |
test(r==KErrNone);
|
|
326 |
client.Resume();
|
|
327 |
clientSem.Wait(); // client connected
|
|
328 |
User::After(1000000);
|
|
329 |
client.Kill(666); // kill the client
|
|
330 |
|
|
331 |
clientx.Kill(666); // kill the client
|
|
332 |
CLOSE_AND_WAIT(clientx);
|
|
333 |
CLOSE_AND_WAIT(client);
|
|
334 |
|
|
335 |
test.Next(_L("Creating client and killing server"));
|
|
336 |
server.Kill(0); // first kill the existing server
|
|
337 |
CLOSE_AND_WAIT(server);
|
|
338 |
RThread client2; // and create the client so the heap is in a good state for the mark
|
|
339 |
r=client2.Create(_L("MyClient"),clientThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)2);
|
|
340 |
test(r==KErrNone);
|
|
341 |
client2.Resume();
|
|
342 |
clientSem.Wait(); // client running but not connected
|
|
343 |
|
|
344 |
RThread server2;
|
|
345 |
r=server2.Create(_L("MyServer"),serverThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)2);
|
|
346 |
test(r==KErrNone);
|
|
347 |
server2.Resume();
|
|
348 |
svrSem.Wait(); // client has request outstanding to server
|
|
349 |
server2.Kill(666); // kill the server
|
|
350 |
clientSem.Wait(); // client's request has completed & client has closed session
|
|
351 |
User::After(1000000);
|
|
352 |
client2.Kill(666);
|
|
353 |
CLOSE_AND_WAIT(client2);
|
|
354 |
CLOSE_AND_WAIT(server2);
|
|
355 |
|
|
356 |
svrSem.Close();
|
|
357 |
svrSem2.Close();
|
|
358 |
clientSem.Close();
|
|
359 |
|
|
360 |
__KHEAP_MARKEND; // and check the kernel's heap is OK
|
|
361 |
|
|
362 |
test.End();
|
|
363 |
return(KErrNone);
|
|
364 |
}
|
|
365 |
|
|
366 |
|
|
367 |
|