os/kernelhwsrv/kerneltest/e32test/active/t_cactw.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
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".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32test\active\t_cactw.cpp
    15 // Overview:
    16 // Test the CActiveSchedulerWait class. 
    17 // API Information:
    18 // CActiveSchedulerWait
    19 // Details:
    20 // - Verify the thread is panicked when one of the following programming errors occurs:
    21 // - the scheduler is started twice. 
    22 // - the scheduler is stopped without starting scheduler.
    23 // - the scheduler is started and then stopped twice.
    24 // - the CanStopNow method of scheduler is called without starting scheduler.
    25 // - Run different combinations of wait, start, async stop, async stop with a callback,
    26 // canstopnow and nested calls to start and async stop operations and verify they run
    27 // correctly
    28 // - Check the heap is not corrupted by all the tests.
    29 // Platforms/Drives/Compatibility:
    30 // All.
    31 // Assumptions/Requirement/Pre-requisites:
    32 // Failures and causes:
    33 // Base Port information:
    34 // 
    35 //
    36 
    37 #include <e32test.h>
    38 #include <e32panic.h>
    39 
    40 const TInt KPanicThreadRet = 222;
    41 const TInt KHeapSize = 0x1000;
    42 const TInt KMaxActions = 32;
    43 const TInt KNumWaits = 2;
    44 
    45 enum TDirective {ENormal,EStartTwice,EStopUnstarted,EStopTwice,ECanStopNotStarted};
    46 enum TActionType {EStartWait, EStopWait, EStopWaitCallBack, EIsWaitStarted, ECanStopWait, EStart, EStop};
    47 
    48 struct TAction
    49 	{
    50 	TActionType iType;
    51 	TInt iId;
    52 	};
    53 
    54 class TSchedulerTester
    55 	{
    56 public:
    57 	void Test1();
    58 	void Test2();
    59 private:
    60 	void SubTest2(TAction* aActions, TInt aCount, const TDesC& aResult);
    61 	};
    62 
    63 class CActionRunner : public CActive
    64 	{
    65 public:
    66 	CActionRunner();
    67 	~CActionRunner();
    68 	void SetActions(TAction* aActions, TInt aCount);
    69 	void Start();
    70 	const TDesC& Trace() const;
    71 private:
    72 	void DoCancel();
    73 	void RunL();
    74 	static TInt CallBack(TAny* aThis);
    75 private:
    76 	TInt iStep;
    77 	TInt iNumActions;
    78 	TAction iActions[KMaxActions];
    79 	CActiveSchedulerWait iWait[KNumWaits];
    80 	TBuf<256> iTrace;
    81 	};
    82 
    83 
    84 LOCAL_D RTest test(_L("T_CACTW"));
    85 LOCAL_D RSemaphore threadSemaphore;
    86 
    87 
    88 CActionRunner::CActionRunner() : CActive(EPriorityStandard)
    89 	{
    90 	CActiveScheduler::Add(this);
    91 	}
    92 
    93 CActionRunner::~CActionRunner()
    94 	{
    95 	Cancel();
    96 	}
    97 
    98 void CActionRunner::SetActions(TAction* aActions, TInt aCount)
    99 	{
   100 	iNumActions = aCount;
   101 	for (TInt ii=0; ii<aCount && ii<KMaxActions; ii++)
   102 		iActions[ii] = aActions[ii];
   103 	}
   104 
   105 void CActionRunner::Start()
   106 	{
   107 	TRequestStatus* s = &iStatus;
   108 	SetActive();
   109 	User::RequestComplete(s, KErrNone);
   110 	}
   111 
   112 void CActionRunner::DoCancel()
   113 	{
   114 	}
   115 
   116 void CActionRunner::RunL()
   117 	{
   118 	Start();
   119 	if (iStep < iNumActions)
   120 		{
   121 		TAction& action = iActions[iStep];
   122 		CActiveSchedulerWait& wait = iWait[action.iId];
   123 		iTrace.AppendFormat(_L("%d%dS"), action.iId, action.iType);
   124 		iStep++;
   125 		switch (action.iType)
   126 			{
   127 			case EStartWait:
   128 				wait.Start();
   129 				break;
   130 			case EStopWait:
   131 				wait.AsyncStop();
   132 				break;
   133 			case EStopWaitCallBack:
   134 				wait.AsyncStop(TCallBack(CallBack, this));
   135 				break;
   136 			case EIsWaitStarted:
   137 				iTrace.AppendFormat(_L("%d"), wait.IsStarted()!=0);
   138 				break;
   139 			case ECanStopWait:
   140 				iTrace.AppendFormat(_L("%d"), wait.CanStopNow()!=0);
   141 				break;
   142 			case EStart:
   143 				CActiveScheduler::Start();
   144 				break;
   145 			case EStop:
   146 				CActiveScheduler::Stop();
   147 				break;
   148 			default:
   149 				break;
   150 			}
   151 		iTrace.AppendFormat(_L("%d%dE"), action.iId, action.iType);
   152 		}
   153 	}
   154 
   155 const TDesC& CActionRunner::Trace() const
   156 	{
   157 	return iTrace;
   158 	}
   159 
   160 TInt CActionRunner::CallBack(TAny* aThis)
   161 	{
   162 	CActionRunner* self = (CActionRunner*) aThis;
   163 	self->iTrace.Append(_L("C"));
   164 	return 0;
   165 	}
   166 
   167 
   168 LOCAL_D TInt panicThread(TAny* aDirective)
   169 //
   170 // Test thread which panics
   171 //
   172 	{
   173 	// cause panics in various ways depending upon aDirective
   174 	CActiveScheduler* pManager=new CActiveScheduler;
   175 	CActiveScheduler::Install(pManager);
   176 
   177 	CActionRunner* r = new(ELeave)CActionRunner;
   178 	
   179 	switch((TInt)aDirective)
   180 		{
   181 		case ENormal:
   182 			{
   183 			TAction actions[] = {{EStop, 0}};
   184 			r->SetActions(actions, sizeof(actions)/sizeof(*actions));
   185 			break;
   186 			}
   187 		case EStartTwice:
   188 			{
   189 			TAction actions[] = {{EStartWait, 0}, {EStartWait, 0}, {EStop, 0}};
   190 			r->SetActions(actions, sizeof(actions)/sizeof(*actions));
   191 			break;
   192 			}
   193 		case EStopUnstarted:
   194 			{
   195 			TAction actions[] = {{EStopWait, 0}, {EStop, 0}};
   196 			r->SetActions(actions, sizeof(actions)/sizeof(*actions));
   197 			break;
   198 			}
   199 		case EStopTwice:
   200 			{
   201 			TAction actions[] = {{EStartWait, 0}, {EStopWait, 0}, {EStopWait, 0}, {EStop, 0}};
   202 			r->SetActions(actions, sizeof(actions)/sizeof(*actions));
   203 			break;
   204 			}
   205 		case ECanStopNotStarted:
   206 			{
   207 			TAction actions[] = {{ECanStopWait, 0}, {EStop, 0}};
   208 			r->SetActions(actions, sizeof(actions)/sizeof(*actions));
   209 			break;
   210 			}
   211 		default:
   212 			break;
   213 		}
   214 
   215 	r->Start();
   216 	CActiveScheduler::Start();
   217 	delete r;
   218 	delete pManager;
   219 	return(KPanicThreadRet);
   220 	}
   221 
   222 
   223 void TSchedulerTester::Test1()
   224 //
   225 // Test 1
   226 //
   227 	{
   228 
   229 //
   230 // Test the panics
   231 //
   232 
   233 	RThread thread;
   234 	TRequestStatus stat;
   235 //
   236 	test.Start(_L("First test normal thread termination"));
   237 	TInt r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)ENormal);
   238 	test(r==KErrNone);
   239 	thread.Logon(stat);
   240 	thread.Resume();
   241 	User::WaitForRequest(stat);
   242 	test(thread.ExitCategory().Compare(_L("Kill"))==0);
   243 	test(thread.ExitReason()==KPanicThreadRet);	  
   244 	test(thread.ExitType()==EExitKill);
   245 	CLOSE_AND_WAIT(thread);
   246 //
   247 	test.Next(_L("Two starts panics"));
   248 	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EStartTwice);
   249 	test(r==KErrNone);
   250 	thread.Logon(stat);
   251 	thread.Resume();
   252 	User::WaitForRequest(stat);
   253 	test(thread.ExitReason()==EActiveSchedulerWaitAlreadyStarted);
   254 	test(thread.ExitType()==EExitPanic);
   255 	CLOSE_AND_WAIT(thread);
   256 //
   257 	test.Next(_L("Stop without start panics"));
   258 	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EStopUnstarted);
   259 	test(r==KErrNone);
   260 	thread.Logon(stat);
   261 	thread.Resume();
   262 	User::WaitForRequest(stat);
   263 	test(thread.ExitReason()==EActiveSchedulerWaitNotStarted);
   264 	test(thread.ExitType()==EExitPanic);
   265 	CLOSE_AND_WAIT(thread);
   266 //
   267 	test.Next(_L("Start then two stops panics"));
   268 	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EStopTwice);
   269 	test(r==KErrNone);
   270 	thread.Logon(stat);
   271 	thread.Resume();
   272 	User::WaitForRequest(stat);
   273 	test(thread.ExitReason()==EActiveSchedulerWaitNotStarted);
   274 	test(thread.ExitType()==EExitPanic);
   275 	CLOSE_AND_WAIT(thread);
   276 //
   277 	test.Next(_L("Can stop now, without start panics"));
   278 	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)ECanStopNotStarted);
   279 	test(r==KErrNone);
   280 	thread.Logon(stat);
   281 	thread.Resume();
   282 	User::WaitForRequest(stat);
   283 	test(thread.ExitReason()==EActiveSchedulerWaitNotStarted);
   284 	test(thread.ExitType()==EExitPanic);
   285 	CLOSE_AND_WAIT(thread);
   286 
   287 	test.End();
   288 	}
   289 
   290 void TSchedulerTester::Test2()
   291 //
   292 // Test 2
   293 //
   294 	{
   295 //
   296 // Test sequencing
   297 	test.Start(_L("Scheduler wait sequencing"));
   298 		{
   299 		test.Next(_L("First a simple stop"));
   300 		TAction a[] = {{EStop, 0}};
   301 		SubTest2(a, sizeof(a)/sizeof(*a), _L("06S06E"));
   302 		}
   303 		{
   304 		test.Next(_L("Simple wait stop"));
   305 		TAction a[] = { {EIsWaitStarted, 0}, {EStartWait, 0}, {EIsWaitStarted, 0}, 
   306 						{ECanStopWait, 0}, {EStopWait, 0}, {EIsWaitStarted, 0}, 
   307 						{EStop, 0}};
   308 		SubTest2(a, sizeof(a)/sizeof(*a), _L("03S003E00S03S103E04S104E01S01E00E03S003E06S06E"));
   309 		}
   310 		{
   311 		test.Next(_L("Properly nested wait"));
   312 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, 
   313 						{EStopWait, 1}, {EStopWait, 0}, {EStop, 0}};
   314 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S11S11E10E01S01E00E06S06E"));
   315 		}
   316 		{
   317 		test.Next(_L("Properly nested scheduler"));
   318 		TAction a[] = { {EStart, 0}, {EStart, 1}, 
   319 						{EStop, 1}, {EStop, 0}, {EStop, 0}};
   320 		SubTest2(a, sizeof(a)/sizeof(*a), _L("05S15S16S16E15E06S06E05E06S06E"));
   321 		}
   322 		{
   323 		test.Next(_L("Badly nested wait"));
   324 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, 
   325 						{EStopWait, 0}, {EStopWait, 1}, {EStop, 0}};
   326 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S01S01E11S11E10E00E06S06E"));
   327 		}
   328 		{
   329 		test.Next(_L("Badly nested scheduler"));
   330 		TAction a[] = { {EStart, 0}, {EStart, 1}, 
   331 						{EStop, 0}, {EStop, 1}, {EStop, 0}};
   332 		SubTest2(a, sizeof(a)/sizeof(*a), _L("05S15S06S06E15E16S16E05E06S06E"));
   333 		}
   334 		{
   335 		test.Next(_L("Bad mixed nesting 1"));
   336 		TAction a[] = { {EStartWait, 0}, {EStart, 1}, 
   337 						{EStopWait, 0}, {EStop, 1}, {EStop, 0}};
   338 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S15S01S01E16S16E15E00E06S06E"));
   339 		}
   340 		{
   341 		test.Next(_L("Bad mixed nesting 2"));
   342 		TAction a[] = { {EStart, 0}, {EStartWait, 1}, 
   343 						{EStop, 0}, {EStopWait, 1}, {EStop, 0}};
   344 		SubTest2(a, sizeof(a)/sizeof(*a), _L("05S10S06S06E11S11E10E05E06S06E"));
   345 		}
   346 		{
   347 		test.Next(_L("Properly nested wait callback 1"));
   348 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, 
   349 						{EStopWaitCallBack, 1}, {EStopWait, 0}, {EStop, 0}};
   350 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S12S12EC10E01S01E00E06S06E"));
   351 		}
   352 		{
   353 		test.Next(_L("Properly nested wait callback 2"));
   354 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, 
   355 						{EStopWait, 1}, {EStopWaitCallBack, 0}, {EStop, 0}};
   356 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S11S11E10E02S02EC00E06S06E"));
   357 		}
   358 		{
   359 		test.Next(_L("Badly nested wait callback 1"));
   360 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, 
   361 						{EStopWaitCallBack, 0}, {EStopWait, 1}, {EStop, 0}};
   362 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S02S02E11S11E10EC00E06S06E"));
   363 		}
   364 		{
   365 		test.Next(_L("Badly nested wait callback 2"));
   366 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, 
   367 						{EStopWait, 0}, {EStopWaitCallBack, 1}, {EStop, 0}};
   368 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S01S01E12S12EC10E00E06S06E"));
   369 		}
   370 		{
   371 		test.Next(_L("Properly nested wait can stop now"));
   372 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, {ECanStopWait, 0}, {ECanStopWait, 1}, 
   373 						{EStopWait, 1}, {ECanStopWait, 0}, {EStopWait, 0}, {EStop, 0}};
   374 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S04S004E14S114E11S11E10E04S104E01S01E00E06S06E"));
   375 		}
   376 		{
   377 		test.Next(_L("Badly nested wait can stop now"));
   378 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, {ECanStopWait, 0}, {ECanStopWait, 1}, 
   379 						{EStopWait, 0}, {ECanStopWait, 1}, {EStopWait, 1}, {EStop, 0}};
   380 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S04S004E14S114E01S01E14S114E11S11E10E00E06S06E"));
   381 		}
   382 		{
   383 		test.Next(_L("Properly nested wait is started"));
   384 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, {EIsWaitStarted, 0}, {EIsWaitStarted, 1}, 
   385 						{EStopWait, 1}, {EIsWaitStarted, 0}, {EIsWaitStarted, 1}, {EStopWait, 0}, {EStop, 0}};
   386 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S03S103E13S113E11S11E10E03S103E13S013E01S01E00E06S06E"));
   387 		}
   388 		{
   389 		test.Next(_L("Badly nested wait is started"));
   390 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, {EIsWaitStarted, 0}, {EIsWaitStarted, 1}, 
   391 						{EStopWait, 0}, {EIsWaitStarted, 0}, {EIsWaitStarted, 1}, {EStopWait, 1}, {EStop, 0}};
   392 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S03S103E13S113E01S01E03S003E13S113E11S11E10E00E06S06E"));
   393 		}
   394 		{
   395 		test.Next(_L("Interleaved badly nested wait with callback"));
   396 		TAction a[] = { {EStartWait, 0}, {EStartWait, 1}, {EStopWaitCallBack, 0}, 
   397 						{EStartWait, 0}, {EStopWaitCallBack, 1}, {EStopWaitCallBack, 0}, 
   398 						{EStop, 0}};
   399 		SubTest2(a, sizeof(a)/sizeof(*a), _L("00S10S02S02E00S12S12E02S02EC00EC10EC00E06S06E"));
   400 		}
   401 	test.End();
   402 	}
   403 
   404 void TSchedulerTester::SubTest2(TAction* aActions, TInt aCount, const TDesC& aResult)
   405 	{
   406 	CActiveScheduler* pManager=new CActiveScheduler;
   407 	CActiveScheduler::Install(pManager);
   408 
   409 	CActionRunner* r = new(ELeave)CActionRunner;
   410 	r->SetActions(aActions, aCount);
   411 	r->Start();
   412 	CActiveScheduler::Start();
   413 	test(r->Trace() == aResult);
   414 
   415 	delete r;
   416 	delete pManager;
   417 	}
   418 
   419 GLDEF_C TInt E32Main()
   420     {
   421 
   422 	// don't want just in time debugging as we trap panics
   423 	TBool justInTime=User::JustInTime(); 
   424 	User::SetJustInTime(EFalse); 
   425 
   426 	test.Title();
   427 	__UHEAP_MARK;
   428 //
   429 	test.Start(_L("Test1"));
   430 	TSchedulerTester sched;
   431 	sched.Test1();
   432 //
   433 	test.Next(_L("Test2"));
   434 	sched.Test2();
   435 //
   436 	test.End();
   437 	__UHEAP_MARKEND;
   438 
   439 	User::SetJustInTime(justInTime);
   440 	return(KErrNone);
   441     }