Update contrib.
1 // Copyright (c) 1995-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\prime\t_timer.cpp
16 // Time and Timer tests.
20 // - Test relative timers using the RTimer::After() method. Verify
21 // results are as expected.
22 // - Set the date and time using TTime::HomeTime() and verify results
24 // - Test absolute timers using RTimer::At() method. Verify results
26 // - Test the timer is ok if its thread terminates.
27 // - Test synchronising time via the RTimer::Lock() method. Verify
28 // results are as expected.
29 // - Test locked timers abort when the system time changes.
30 // - Test User::ResetInactivityTime() results are as expected.
31 // Platforms/Drives/Compatibility:
33 // Assumptions/Requirement/Pre-requisites:
34 // Failures and causes:
35 // Base Port information:
39 // the following was used to help debug emulator implemenation of user mode callbacks
40 //#define REQUEST_STATUS_POLL_SOAK_TEST
42 #define __E32TEST_EXTENSION__
51 LOCAL_D RTest test(_L("T_TIMER"));
54 TInt AfterNegative(TAny*)
57 TInt r=t.CreateLocal();
64 TInt AfterTwice(TAny*)
67 TInt r=t.CreateLocal();
71 test(s==KRequestPending);
80 TDateTime dt(now.DateTime());
81 test.Printf(_L("Time: %02d:%02d:%02d:%06d\n"),dt.Hour(),dt.Minute(),dt.Second(),dt.MicroSecond());
84 TBool RequestIsComplete(TRequestStatus& s)
86 return s != KRequestPending;
90 LOCAL_C void testRel()
92 // Test relative timers.
95 test.Start(_L("After 0"));
97 TInt r=t.CreateLocal();
101 test(s==KRequestPending || s==KErrNone);
102 User::WaitForRequest(s);
105 test.Next(_L("After 1 tenth"));
108 // On WINS we can't guarantee thread scheduling so timer may already have
109 // completed before we get to test the status. Therefore, allow KErrNone.
110 test(s==KRequestPending || s==KErrNone);
112 test.Printf(_L("NOTE: completed 'early'"));
114 test(s==KRequestPending);
116 User::WaitForRequest(s);
119 test.Next(_L("After -1 millionth"));
121 r=thread.Create(_L("After -1"),AfterNegative,KDefaultStackSize,NULL,&thread);
124 test(s==KRequestPending);
125 TBool justInTime=User::JustInTime();
126 User::SetJustInTime(EFalse);
128 User::WaitForRequest(s);
129 test(s==ERTimerAfterTimeNegative);
130 test(thread.ExitCategory()==_L("USER"));
131 test(thread.ExitReason()==ERTimerAfterTimeNegative);
132 test(thread.ExitType()==EExitPanic);
133 CLOSE_AND_WAIT(thread);
134 User::SetJustInTime(justInTime);
136 test.Next(_L("After 1 second"));
138 test(s==KRequestPending);
139 User::WaitForRequest(s);
142 test.Next(_L("After 1 second polling"));
144 test(s==KRequestPending);
145 // Have to be careful the compiler doesn't optimise this away
146 while(!RequestIsComplete(s))
149 User::WaitForRequest(s);
151 test.Next(_L("Cancel"));
153 test(s==KRequestPending);
155 User::WaitForRequest(s);
159 test.Next(_L("Request twice"));
160 r=thread.Create(_L("After twice"),AfterTwice,KDefaultStackSize,NULL,&thread);
163 test(s==KRequestPending);
164 User::SetJustInTime(EFalse);
166 User::WaitForRequest(s);
167 test(s==ETimerAlreadyPending);
168 test(thread.ExitCategory()==_L("KERN-EXEC"));
169 test(thread.ExitReason()==ETimerAlreadyPending);
170 test(thread.ExitType()==EExitPanic);
171 CLOSE_AND_WAIT(thread);
172 User::SetJustInTime(justInTime);
177 #ifdef REQUEST_STATUS_POLL_SOAK_TEST
179 static volatile TBool PollTestRunning;
181 LOCAL_C TInt PollThread(TAny* aArg)
183 const TInt KMaxTimers = 1000;
185 TInt threadIndex = (TInt)aArg;
186 TInt64 seed = 5511498647534504549 + RThread().Id();
187 RTimer timers[KMaxTimers];
188 TRequestStatus statuses[KMaxTimers];
191 for (i = 0 ; i < KMaxTimers ; ++i)
193 test_KErrNone(timers[i].CreateLocal());
197 TInt totalComplete = 0;
198 TInt totalWaiting = 0;
200 while(PollTestRunning)
202 for (i = 0 ; i < KMaxTimers ; ++i)
204 switch(statuses[i].Int())
206 case KRequestPending:
212 User::WaitForRequest(statuses[i]);
218 TInt after = ((TUint)Math::Rand(seed) >> 28) + 1;
219 timers[i].HighRes(statuses[i], after);
224 return statuses[i].Int();
229 for (i = 0 ; i < KMaxTimers ; ++i)
231 User::WaitForRequest(statuses[i]);
232 if (statuses[i].Int() != KErrNone)
233 return statuses[i].Int();
237 RDebug::Printf("%d: %d %d\n", threadIndex, totalComplete, totalWaiting);
241 LOCAL_C void testPoll()
243 const TInt KMaxThreads = 10;
244 const TInt KSecondsToTest = 60;
246 RThread threads[KMaxThreads];
247 TRequestStatus statuses[KMaxThreads];
249 test.Start(_L("Test polling"));
251 PollTestRunning = ETrue;
254 for (i = 0 ; i < KMaxThreads ; ++i)
256 test_KErrNone(threads[i].Create(KNullDesC, PollThread, 0x1000, NULL, (TAny*)i));
257 threads[i].Logon(statuses[i]);
261 User::After(KSecondsToTest * 1000 * 1000);
263 PollTestRunning = EFalse;
265 for (i = 0 ; i < KMaxThreads ; ++i)
267 User::WaitForRequest(statuses[i]);
268 test_KErrNone(statuses[i].Int());
269 test_Equal(EExitKill, threads[i].ExitType());
279 LOCAL_C void testHomeTime()
286 for (TInt x=0;x<100;x++)
294 TDateTime dt=t2.DateTime();
295 test.Printf(_L("%d:%d\r\n"),dt.Second(),dt.MicroSecond());
301 test.Printf(_L("\r\n"));
308 TInt r=t.CreateLocal();
312 time.UniversalTime();
313 t.AtUTC(s,time+TTimeIntervalSeconds(1));
314 test(s==KRequestPending);
315 t.AtUTC(s,time+TTimeIntervalSeconds(2));
322 TInt r=t.CreateLocal();
326 time.UniversalTime();
327 t.AtUTC(s,time+TTimeIntervalSeconds(1));
328 test(s==KRequestPending);
336 TInt r=t.CreateLocal();
340 time.UniversalTime();
342 test(s==KRequestPending);
343 t.AtUTC(s,time+TTimeIntervalSeconds(2));
348 LOCAL_C void testAbs()
350 // Test absolute timers.
353 test.Start(_L("Now -1"));
355 TInt r=t.CreateLocal();
359 time.UniversalTime();
360 t.AtUTC(s,time+TTimeIntervalSeconds(-2));
361 test(s==KErrUnderflow); // =KRequestPending
362 User::WaitForRequest(s);
363 test(s==KErrUnderflow);
366 test.Next(_L("Synchronise to clock"));
367 time.UniversalTime();
368 TDateTime dateTime=time.DateTime();
369 dateTime.SetMicroSecond(0);
371 time+=TTimeIntervalSeconds(2);
373 User::WaitForRequest(s);
375 test.Next(_L("Now +1"));
376 time += TTimeIntervalSeconds(1);
378 test(s==KRequestPending);
379 User::WaitForRequest(s);
380 time2.UniversalTime();
382 TTimeIntervalMicroSeconds delay=time2.MicroSecondsFrom(time);
383 // Test we are in the same second as the requested time...
384 test(delay>=TTimeIntervalMicroSeconds(0));
385 test(delay<TTimeIntervalMicroSeconds(1000000));
387 test.Next(_L("Now +3"));
388 time += TTimeIntervalSeconds(3);
390 test(s==KRequestPending);
391 User::WaitForRequest(s);
392 time2.UniversalTime();
394 delay=time2.MicroSecondsFrom(time);
395 // Test we are in the same second as the requested time...
396 test(delay>=TTimeIntervalMicroSeconds(0));
397 test(delay<TTimeIntervalMicroSeconds(1000000));
399 test.Next(_L("UTC vs local"));
400 TTimeIntervalSeconds savedOffset = User::UTCOffset();
401 User::SetUTCOffset(3600);
404 time += TTimeIntervalSeconds(1);
406 test(s==KRequestPending);
407 User::WaitForRequest(s);
410 delay=time2.MicroSecondsFrom(time);
411 // Test we are in the same second as the requested time...
412 test(delay>=TTimeIntervalMicroSeconds(0));
413 test(delay<TTimeIntervalMicroSeconds(1000000));
415 time.UniversalTime();
416 time += TTimeIntervalSeconds(1);
418 test(s==KRequestPending);
419 User::WaitForRequest(s);
420 time2.UniversalTime();
422 delay=time2.MicroSecondsFrom(time);
423 // Test we are in the same second as the requested time...
424 test(delay>=TTimeIntervalMicroSeconds(0));
425 test(delay<TTimeIntervalMicroSeconds(1000000));
427 User::SetUTCOffset(savedOffset);
429 test.Next(_L("Cancel"));
430 time.UniversalTime();
431 t.AtUTC(s,time+TTimeIntervalSeconds(10));
432 test(s==KRequestPending);
434 User::WaitForRequest(s);
438 test.Next(_L("Request twice"));
440 r=thread.Create(_L("At twice"),AtTwice,KDefaultStackSize,NULL,&thread);
443 test(s==KRequestPending);
444 TBool justInTime=User::JustInTime();
445 User::SetJustInTime(EFalse);
447 User::WaitForRequest(s);
448 User::SetJustInTime(justInTime);
449 test(s==ETimerAlreadyPending);
450 test(thread.ExitCategory()==_L("KERN-EXEC"));
451 test(thread.ExitReason()==ETimerAlreadyPending);
452 test(thread.ExitType()==EExitPanic);
453 CLOSE_AND_WAIT(thread);
455 r=thread.Create(_L("At After"),AtAfter,KDefaultStackSize,NULL,&thread);
458 test(s==KRequestPending);
459 User::SetJustInTime(EFalse);
461 User::WaitForRequest(s);
462 User::SetJustInTime(justInTime);
463 test(s==ETimerAlreadyPending);
464 test(thread.ExitCategory()==_L("KERN-EXEC"));
465 test(thread.ExitReason()==ETimerAlreadyPending);
466 test(thread.ExitType()==EExitPanic);
467 CLOSE_AND_WAIT(thread);
469 r=thread.Create(_L("After At"),AfterAt,KDefaultStackSize,NULL,&thread);
472 test(s==KRequestPending);
473 User::SetJustInTime(EFalse);
475 User::WaitForRequest(s);
476 User::SetJustInTime(justInTime);
477 test(s==ETimerAlreadyPending);
478 test(thread.ExitCategory()==_L("KERN-EXEC"));
479 test(thread.ExitReason()==ETimerAlreadyPending);
480 test(thread.ExitType()==EExitPanic);
481 CLOSE_AND_WAIT(thread);
486 TInt LockTwice(TAny*)
489 test(t.CreateLocal()==KErrNone);
491 t.Lock(stat, ETwelveOClock);
492 User::WaitForRequest(stat);
493 test(stat==KErrGeneral);
494 t.Lock(stat, ETwelveOClock);
495 t.Lock(stat, ETwelveOClock);
500 LOCAL_C void testLock()
502 // Test locked timers
505 test.Start(_L("Test synchronise to ETwelveOClock"));
508 test(t.CreateLocal()==KErrNone);
510 t.Lock(stat, ETwelveOClock);
511 User::WaitForRequest(stat);
512 test(stat==KErrGeneral);
513 time.UniversalTime();
514 t.Lock(stat, ETwelveOClock);
515 User::WaitForRequest(stat);
516 test(stat==KErrNone);
517 time2.UniversalTime();
520 test.Next(_L("Test sync to EOneOClock for 4 seconds"));
521 t.Lock(stat, EOneOClock);
522 User::WaitForRequest(stat);
523 test(stat==KErrGeneral);
524 time.UniversalTime();
528 t.Lock(stat, EOneOClock);
529 User::WaitForRequest(stat);
530 test(stat==KErrNone);
531 test.Printf(_L("."));
533 time2.UniversalTime();
534 TTimeIntervalSeconds ti;
535 test(time2.SecondsFrom(time, ti)==KErrNone);
536 test(ti>=TTimeIntervalSeconds(4));
537 test.Printf(_L("\n"));
538 test.Next(_L("Test sync to every half second, from EFourOClock for 5 seconds"));
539 t.Lock(stat, ETwelveOClock);
540 User::WaitForRequest(stat);
543 t.Lock(stat, EFourOClock);
544 User::WaitForRequest(stat);
545 test(stat==KErrNone);
546 test.Printf(_L("."));
547 t.Lock(stat, ETenOClock);
548 User::WaitForRequest(stat);
549 test(stat==KErrNone);
550 test.Printf(_L(","));
552 test.Printf(_L("\n"));
553 test.Next(_L("Test KErrGeneral after delay"));
554 User::After(1000000);
555 t.Lock(stat,EThreeOClock);
556 User::WaitForRequest(stat);
557 test(stat==KErrGeneral);
558 test.Next(_L("Test cancel, and re-request immediately"));
559 User::After(1000000);
560 t.Lock(stat, ETwelveOClock);
561 User::WaitForRequest(stat);
562 test(stat==KErrGeneral);
563 t.Lock(stat, EElevenOClock);
565 User::WaitForRequest(stat);
566 test(stat==KErrCancel);
567 t.Lock(stat, EElevenOClock);
568 User::WaitForRequest(stat);
569 test(stat==KErrNone);
570 test.Next(_L("Test complete a request at 1, then cancel a request for 11, and re-request at 3 gives KErrGeneral"));
571 User::After(1000000);
572 t.Lock(stat, ETwelveOClock);
573 User::WaitForRequest(stat);
574 test(stat==KErrGeneral);
575 t.Lock(stat,EOneOClock);
576 User::WaitForRequest(stat);
577 test(stat==KErrNone);
578 t.Lock(stat,EElevenOClock);
579 User::After(400000); // ensure EThreeOClock is in the past
581 User::WaitForRequest(stat);
582 test(stat==KErrCancel);
583 t.Lock(stat,EThreeOClock);
584 User::WaitForRequest(stat);
585 // EThreeOClock should be more than one second away from the previous timer expiration
586 test(stat==KErrGeneral);
588 test.Next(_L("Lock twice"));
590 TInt r=thread.Create(_L("Lock twice"),LockTwice,KDefaultStackSize,NULL,&thread);
593 test(stat==KRequestPending);
594 TBool justInTime=User::JustInTime();
595 User::SetJustInTime(EFalse);
597 User::WaitForRequest(stat);
598 User::SetJustInTime(justInTime);
599 test(stat==ETimerAlreadyPending);
600 test(thread.ExitCategory()==_L("KERN-EXEC"));
601 test(thread.ExitReason()==ETimerAlreadyPending);
602 test(thread.ExitType()==EExitPanic);
603 CLOSE_AND_WAIT(thread);
605 #if !(defined(__EPOC32__) && defined(__X86__))
607 HAL::Get(HAL::EMachineUid, muid);
608 if(muid!=HAL::EMachineUid_Lubbock && muid!=HAL::EMachineUid_NE1_TB && muid!=HAL::EMachineUid_STE8500)
610 test.Next(_L("Test sequential locks fail over on/off"));
616 tat.AtUTC(sat, now+TTimeIntervalSeconds(10)); // turn on in 10 seconds
617 test(sat==KRequestPending);
618 t.Lock(stat, ETwelveOClock);
619 User::WaitForRequest(stat);
620 test(stat==KErrGeneral);
621 t.Lock(stat, EElevenOClock);
622 User::WaitForRequest(stat);
625 r = Power::EnableWakeupEvents(EPwStandby);
626 test (r == KErrNone);
627 r = Power::PowerDown();
628 test (r == KErrNone);
629 test(stat==KErrNone);
631 t.Lock(stat, EElevenOClock);
632 User::WaitForRequest(stat);
633 test(stat==KErrGeneral);
646 // Test locked timers abort when the system time changes
652 rr.Lock(stat, ETwelveOClock);
653 User::WaitForRequest(stat);
654 test(stat==KErrGeneral);
657 rrr.After(stat, 1000000);
658 User::WaitForRequest(stat);
661 TRequestStatus sstat;
664 r.Lock(stat,ETwelveOClock);
665 rr.Lock(sstat,EOneOClock);
666 User::WaitForRequest(stat);
667 test(stat==KErrGeneral);
668 User::WaitForRequest(sstat);
669 test(sstat==KErrGeneral);
670 r.Lock(stat,ETwelveOClock);
671 rr.Lock(sstat,EOneOClock);
672 User::WaitForRequest(stat);
673 test(stat==KErrNone);
674 User::WaitForRequest(sstat);
675 test(sstat==KErrNone);
677 r.Lock(stat,ETwelveOClock);
678 rr.Lock(sstat,EOneOClock);
679 TInt ret=User::SetUTCTime(t-TTimeIntervalSeconds(100));
682 ret=User::SetUTCTime(t+TTimeIntervalSeconds(100));
684 User::WaitForRequest(stat);
685 test(stat==KErrAbort);
686 User::WaitForRequest(sstat);
687 test(sstat==KErrAbort);
689 // Check that changing the *secure* time *doesn't* abort a locked timer
690 r.Lock(stat, ETwelveOClock);
691 User::WaitForRequest(stat); // stat will be KErrGeneral after abort above, but time will be TwelveOClock anyway
692 t.UniversalTimeSecure();
693 r.Lock(stat, EEightOClock);
694 ret = User::SetUTCTimeSecure(t+TTimeIntervalSeconds(100));
695 User::WaitForRequest(stat); // this timer should complete at EightOClock with status KErrNone, *not* KErrAbort
696 r.Lock(sstat, ETwelveOClock);
697 User::WaitForRequest(sstat); // this should complete one whole second after we read the secure time above
698 User::SetUTCTimeSecure(t+TTimeIntervalSeconds(1));
699 test(stat == KErrNone);
700 test(sstat == KErrNone);
702 RDebug::Printf("WARNING: Secure clock change test skipped because secure time could not be changed!");
709 void testInactivity()
711 test.Start(_L("Test User::ResetInactivityTime()"));
713 TRequestStatus stat,stat2;
716 User::ResetInactivityTime();
717 t.Inactivity(stat, 4);
718 t2.Inactivity(stat2, 2);
721 TInt r=User::SetUTCTime(now+TTimeIntervalDays(1));
723 test(stat==KRequestPending);
724 test(stat2==KRequestPending);
725 r=User::SetUTCTime(now-TTimeIntervalDays(1));
727 test(stat==KRequestPending);
728 test(stat2==KRequestPending);
729 r=User::SetUTCTime(now);
731 test(stat==KRequestPending);
732 test(stat2==KRequestPending);
733 User::After(1000000);
734 User::ResetInactivityTime();
735 test(stat==KRequestPending);
736 test(stat2==KRequestPending);
737 User::After(3000000);
738 User::ResetInactivityTime();
739 test(stat==KRequestPending);
740 test(stat2!=KRequestPending);
741 User::After(2000000);
742 User::ResetInactivityTime();
743 test(stat==KRequestPending);
744 User::After(2000000);
745 User::ResetInactivityTime();
746 test(stat==KRequestPending);
747 User::After(5000000);
748 test(stat!=KRequestPending);
753 GLDEF_C TInt E32Main()
759 TInt r=HAL::Get(HAL::EMachineUid,MachineUid);
761 test.Start(_L("Testing relative timers"));
764 #ifdef REQUEST_STATUS_POLL_SOAK_TEST
765 test.Next(_L("Testing polling"));
769 test.Next(_L("Testing HomeTime()"));
772 test.Next(_L("Testing absolute timers"));
775 test.Next(_L("Testing locked timers"));
778 test.Next(_L("Testing changing time"));
781 test.Next(_L("Testing inactivity timers"));