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 // f32test\server\t_alert.cpp
24 GLDEF_D RTest test(_L("T_ALERT"));
26 LOCAL_D TInt gFileWrites;
27 LOCAL_D TInt gFileReads;
28 LOCAL_D TInt gFileCreates;
29 LOCAL_D TInt gFileDeletes;
30 LOCAL_D TInt gNotifies;
31 LOCAL_D TInt gNotifyCancels;
32 LOCAL_D TInt gMediaChanges;
34 LOCAL_D const TInt gMaxIteration=100;
35 LOCAL_D const TInt KHeapSize=0x4000;
36 LOCAL_D const TInt gMaxDelay=100;
37 LOCAL_D const TInt gMaxMediaChangeInterval=1000;
38 LOCAL_D const TInt gUpdateInterval=10000;
39 LOCAL_D const TInt gMaxFiles=4;
40 LOCAL_D const TInt gMaxTempSize=1024;
41 LOCAL_D const TFileName gPathThread1=_L("C:\\F32-TST\\TALERT\\");
44 LOCAL_D const TInt gHeartBeatInterval=10000000;
45 LOCAL_D TInt gThreadTick;
46 LOCAL_D TInt gFileCreateFail;
49 LOCAL_D const TFileName gPathThread0=_L("X:\\F32-TST\\TALERT\\");
50 LOCAL_D const TFileName gPathThread2=_L("X:\\F32-TST\\TALERT\\");
52 LOCAL_D TFileName gPathThread0=_L("?:\\F32-TST\\TALERT\\");
53 LOCAL_D TFileName gPathThread2=_L("?:\\F32-TST\\TALERT\\");
56 LOCAL_C void WaitForMedia()
58 // Wait until the media change is serviced
63 TInt r=TheFs.MkDir(gPathThread0);
64 if (r==KErrNone || r==KErrAlreadyExists)
69 LOCAL_C TInt FileAccess(TAny* aPathPtr)
71 // Do lots of file access - ignore all errors
75 TFileName file[gMaxFiles];
76 TFileName sessionPath=*(TDesC*)aPathPtr;
77 HBufC8* tempPtr=HBufC8::New(gMaxTempSize);
78 HBufC* tempPtrx=HBufC::New(gMaxTempSize);
79 TPtr8 temp(tempPtr->Des());
80 TPtr tempx(tempPtrx->Des());
84 fs.SetSessionPath(sessionPath);
88 switch(Math::Rand(gSeed)%4)
92 TInt fileNum=Math::Rand(gSeed)%gMaxFiles;
93 if (file[fileNum].Length()==0)
96 TInt r=f.Open(fs,file[fileNum],EFileRead|EFileWrite);
99 CreateLongName(tempx,gSeed,Math::Rand(gSeed)%gMaxTempSize);
109 TInt fileNum=Math::Rand(gSeed)%gMaxFiles;
110 if (file[fileNum].Length()==0)
113 TInt r=f.Open(fs,file[fileNum],EFileRead|EFileWrite);
124 TInt fileNum=Math::Rand(gSeed)%gMaxFiles;
125 if (file[fileNum].Length()!=0)
128 TInt r=f.Temp(fs,sessionPath,file[fileNum],EFileRead|EFileWrite);
136 TInt fileNum=Math::Rand(gSeed)%gMaxFiles;
137 if (file[fileNum].Length()==0)
139 TInt r=fs.Delete(file[fileNum]);
142 file[fileNum].SetLength(0);
158 LOCAL_C void StartThread0()
160 // Start a thread that reads and writes to D:
164 RThread clientThread;
165 TInt r=clientThread.Create(_L("TALERT_Thread0"),FileAccess,0x4000,KHeapSize,KHeapSize,(TAny*)&gPathThread0,EOwnerThread);
167 clientThread.Resume();
168 clientThread.Close();
171 LOCAL_C void StartThread1()
173 // Start a thread that reads and writes to C:
177 RThread clientThread;
178 TInt r=clientThread.Create(_L("TALERT_Thread1"),FileAccess,0x4000,KHeapSize,KHeapSize,(TAny*)&gPathThread1,EOwnerThread);
180 clientThread.Resume();
181 clientThread.Close();
184 LOCAL_C void StartThread2()
186 // Start a thread that reads and writes to D:
190 RThread clientThread;
191 TInt r=clientThread.Create(_L("TALERT_Thread2"),FileAccess,0x4000,KHeapSize,KHeapSize,(TAny*)&gPathThread2,EOwnerThread);
193 clientThread.Resume();
194 clientThread.Close();
198 LOCAL_C TInt NotifyAccess(TAny*)
200 // Create, wait for and cancel notifiers.
206 TRequestStatus status;
210 switch(Math::Rand(gSeed)%4)
214 fs.NotifyChange(ENotifyAll,status);
215 User::WaitForRequest(status);
219 fs.NotifyChange(ENotifyEntry,status);
220 User::WaitForRequest(status);
224 fs.NotifyChange(ENotifyAll,status);
225 User::After(Math::Rand(gSeed)%gMaxDelay);
226 fs.NotifyChangeCancel();
227 User::WaitForRequest(status);
231 fs.NotifyChange(ENotifyEntry,status);
232 User::After(Math::Rand(gSeed)%gMaxDelay);
233 fs.NotifyChangeCancel();
234 User::WaitForRequest(status);
242 LOCAL_C void StartThread3()
244 // Start a thread that creates and waits/cancel RFs::Notifiers
248 RThread clientThread;
249 TInt r=clientThread.Create(_L("TALERT_Thread3"),NotifyAccess,0x4000,KHeapSize,KHeapSize,NULL,EOwnerThread);
251 clientThread.Resume();
252 clientThread.Close();
255 LOCAL_C TInt MediaChange(TAny*)
257 // Cause media changes a random intervals
263 TInt interval=Math::Rand(gSeed)%gMaxMediaChangeInterval;
264 User::After(interval);
265 // UserSvr::ForceRemountMedia(ERemovableMedia0); // Generate media change
272 LOCAL_C void StartThread4()
274 // Start a thread that creates media changes
278 RThread clientThread;
279 TInt r=clientThread.Create(_L("TALERT_Thread4"),MediaChange,0x4000,KHeapSize,KHeapSize,NULL,EOwnerThread);
281 clientThread.SetPriority(EPriorityMore);
282 clientThread.Resume();
283 clientThread.Close();
286 LOCAL_C void StartClock()
288 // Display ongoing reads/writes. Getch to exit
292 test.Console()->ClearScreen();
295 User::After(gUpdateInterval);
296 test.Console()->SetPos(0,5);
297 test.Printf(_L("File writes = %d \n"),gFileWrites);
298 test.Printf(_L("File reads = %d \n"),gFileReads);
299 test.Printf(_L("File creates = %d \n"),gFileCreates);
300 test.Printf(_L("File deletes = %d \n"),gFileDeletes);
301 test.Printf(_L("File notifies = %d \n"),gNotifies);
302 test.Printf(_L("File notifies cancelled = %d \n"),gNotifyCancels);
303 // test.Printf(_L("Press any key to exit\n"));
304 // TKeyCode keycode=test.Console()->KeyCode();
305 // if (keycode!=EKeyNull)
307 if (gFileWrites>gMaxIteration)
312 LOCAL_C void KillThreads()
317 test.Printf(_L("+Kill threads"));
320 TInt r=t.Open(_L("TALERT_Thread0"),EOwnerThread);
327 test(r==KErrNotFound);
329 r=t.Open(_L("TALERT_Thread1"),EOwnerThread);
336 test(r==KErrNotFound);
338 r=t.Open(_L("TALERT_Thread2"),EOwnerThread);
345 test(r==KErrNotFound);
347 r=t.Open(_L("TALERT_Thread3"),EOwnerThread);
354 test(r==KErrNotFound);
356 r=t.Open(_L("TALERT_Thread4"),EOwnerThread);
363 test(r==KErrNotFound);
365 /* TFindThread threadFinder(_L("TALERT_*"));
368 TFullName threadName;
369 TInt r=threadFinder.Next(threadName);
370 test.Printf(_L("r=%d"),r);
374 test.Printf(_L("Killing Thread %S\n"),&threadName);
376 r=t.Open(threadName,EOwnerThread);
381 test.Printf(_L("-Kill threads"));
386 // Create lots of threads and change notifiers
389 test.Next(_L("Create lots of threads and change notifiers"));
390 TInt r=TheFs.MkDirAll(gPathThread1);
391 test(r==KErrNone || r==KErrAlreadyExists);
392 r=TheFs.MkDir(gPathThread2);
393 test(r==KErrNone || r==KErrAlreadyExists);
395 StartThread0(); // Read and write to D:
396 StartThread1(); // Read and write to C:
397 StartThread2(); // Read and write to D: (again)
398 StartThread3(); // Set RFs::Notifiers
399 StartThread4(); // Generate media changes
400 StartClock(); // Time how long test has run
404 LOCAL_C TInt ThreadHangTest(TAny*)
406 // Keep writing to the fileserver and setting gThreadTick
412 fs.SetSessionPath(gPathThread0);
419 TInt r=f.Replace(fs,_L("TwiddleThumbs"),EFileRead|EFileWrite);
425 fs.Delete(_L("TwiddleThumbs"));
434 // Create a hung server then kill the thread it wants to write to.
438 test.Next(_L("Create a hung server and kill the thread it is writing to"));
439 TInt r=TheFs.MkDir(gPathThread0);
440 test(r==KErrNone || r==KErrAlreadyExists);
441 StartThread4(); // Generate media changes
443 RThread clientThread;
444 r=clientThread.Create(_L("TALERT_ThreadHangTest"),ThreadHangTest,0x4000,KHeapSize,KHeapSize,NULL,EOwnerThread);
446 TRequestStatus status;
447 clientThread.Logon(status);
448 clientThread.Resume();
450 test.Next(_L("ThreadHangTest is running"));
455 test.Printf(_L("Thread tick = %d File create failures = %d \n"),count,gFileCreateFail);
456 test.Printf(_L("Media changes = %d\n"),gMediaChanges);
457 User::After(gHeartBeatInterval);
464 test.Next(_L("Thread is hung"));
465 clientThread.Kill(KErrCancel);
466 clientThread.Close();
467 User::WaitForRequest(status);
469 User::After(1000000);
471 test.Printf(_L("Press return to the clear notifier, then any key to continue test"));
474 test.Next(_L("Test fileserver is still alive after thread is killed"));
476 r=TheFs.MkDir(gPathThread0); // Check fileserver ok
477 if(r!=KErrNone && r!=KErrAlreadyExists)
478 test.Printf(_L("r=%d"),r);
479 test(r==KErrNone || r==KErrAlreadyExists);
483 GLDEF_C void CallTestsL()
485 // Do tests relative to the session path
489 RThread().SetPriority(EPriorityLess);
490 #if defined(__WINS__)
491 if (gSessionPath[0]!='X')
494 // Test on two drives where possible. This should be C: together with
495 // the drive referenced by the default path. Where the default path is
496 // C:, then only C: is used. The standard way to run the test is to
497 // set the default path to D:
499 gPathThread0[0]=gSessionPath[0];
500 gPathThread2[0]=gSessionPath[0];
505 //This test can leave the drive corrupt so a format is required
506 #if defined(__WINS__)
511 //clean up the talert directory after the test completes
512 /* CFileMan* FileMan=NULL;
513 FileMan=CFileMan::NewL(TheFs);
514 TInt r=FileMan->RmDir(_L("\\F32-TST\\TALERT\\"));
515 test.Printf(_L("r=%d"),r);
516 test(r==KErrNone || r==KErrPathNotFound);