os/kernelhwsrv/kerneltest/e32test/active/t_act.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1995-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\active\t_act.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test CTimer based timers and error handling in active objects and 
sl@0
    17
// active scheduler
sl@0
    18
// API Information:
sl@0
    19
// CTimer, CActiveScheduler
sl@0
    20
// Details:
sl@0
    21
// - Create and install the active scheduler
sl@0
    22
// - Create a timer, add it to the active scheduler and start the timer
sl@0
    23
// - Verify RunL is run when the timer fires off and test dequeuing itself from 
sl@0
    24
// the active scheduler
sl@0
    25
// - Test that when a leave in RunL occurs, the active object gets the chance to 
sl@0
    26
// handle it before the active scheduler
sl@0
    27
// - Test that leaves in RunL can be handled successfully in the active object
sl@0
    28
// - Test the active object can propagate the leave to the active scheduler
sl@0
    29
// - Test that leaves in RunL can be handled successfully in the active scheduler
sl@0
    30
// Platforms/Drives/Compatibility:
sl@0
    31
// All.
sl@0
    32
// Assumptions/Requirement/Pre-requisites:
sl@0
    33
// Failures and causes:
sl@0
    34
// Base Port information:
sl@0
    35
// 
sl@0
    36
//
sl@0
    37
sl@0
    38
#include <e32test.h>
sl@0
    39
#include <e32panic.h>
sl@0
    40
sl@0
    41
class CMyRequestManager : public CActiveScheduler
sl@0
    42
	{
sl@0
    43
public:
sl@0
    44
	enum TMode
sl@0
    45
		{
sl@0
    46
		EExpectError,
sl@0
    47
		EGenerateException,
sl@0
    48
		EPanic
sl@0
    49
	} iMode;
sl@0
    50
public:
sl@0
    51
	virtual void Error(TInt anError) const;
sl@0
    52
	void SetMode(TMode aExpect);
sl@0
    53
sl@0
    54
private:
sl@0
    55
	TBool iExpectError;
sl@0
    56
	};
sl@0
    57
sl@0
    58
class CMyTimer : public CTimer
sl@0
    59
	{
sl@0
    60
public:
sl@0
    61
	static CMyTimer* New();
sl@0
    62
	void Start();
sl@0
    63
	virtual void RunL();
sl@0
    64
	void StartLeave(TBool aHandleLocally, TBool aLeaveInOOM, CMyRequestManager::TMode aMode);
sl@0
    65
	virtual TInt RunError(TInt aError);
sl@0
    66
	void StopLeave();
sl@0
    67
	void StartImbalance();
sl@0
    68
	virtual ~CMyTimer();
sl@0
    69
protected:
sl@0
    70
	CMyTimer(TInt aPriority);
sl@0
    71
private:
sl@0
    72
	enum {EMaxCount=10,ETimeReq=100000};
sl@0
    73
	CBase* iDummy;
sl@0
    74
	TInt iCount;
sl@0
    75
	TBool iConstructed;
sl@0
    76
	TBool iLeave;
sl@0
    77
	TBool iHandleLocally;
sl@0
    78
	TBool iLeaveInOOM;
sl@0
    79
	TBool iImbalance;
sl@0
    80
	TBool iStopping;
sl@0
    81
	};
sl@0
    82
sl@0
    83
LOCAL_D RTest test(_L("T_ACT"));
sl@0
    84
sl@0
    85
void CMyRequestManager::Error(TInt anError) const
sl@0
    86
//
sl@0
    87
// Called if any Run() method leaves.
sl@0
    88
//
sl@0
    89
	{
sl@0
    90
	switch (iMode)
sl@0
    91
		{
sl@0
    92
		case EExpectError:
sl@0
    93
			{
sl@0
    94
			_LIT(KExpectError,"CMyRequestManager::Error handling error %d\n");
sl@0
    95
			test.Printf(KExpectError,anError);
sl@0
    96
			Stop();
sl@0
    97
			return;
sl@0
    98
			}
sl@0
    99
		case EGenerateException:
sl@0
   100
			{
sl@0
   101
sl@0
   102
			_LIT(KExpectError,"CMyRequestManager::Error about to generate exception...\n");
sl@0
   103
			test.Printf(KExpectError,anError);
sl@0
   104
sl@0
   105
			__UHEAP_FAILNEXT(1);
sl@0
   106
			TRAPD(ret,User::Leave(KErrArgument));
sl@0
   107
			__UHEAP_RESET;
sl@0
   108
	   		if (ret != KErrArgument) 
sl@0
   109
	   			{
sl@0
   110
				_LIT(KDoNotExpectError,"CMyRequestManager::Error unexpected");
sl@0
   111
				test.Panic(anError,KDoNotExpectError);
sl@0
   112
	   			}
sl@0
   113
			Stop();
sl@0
   114
			return;	
sl@0
   115
			}
sl@0
   116
		case EPanic:
sl@0
   117
		default:
sl@0
   118
			{
sl@0
   119
			_LIT(KDoNotExpectError,"CMyRequestManager::Error unexpected");
sl@0
   120
			test.Panic(anError,KDoNotExpectError);
sl@0
   121
			}
sl@0
   122
		}
sl@0
   123
	}
sl@0
   124
sl@0
   125
sl@0
   126
void CMyRequestManager::SetMode(TMode aMode)
sl@0
   127
	{
sl@0
   128
	iMode = aMode;
sl@0
   129
	}
sl@0
   130
sl@0
   131
CMyTimer* CMyTimer::New()
sl@0
   132
//
sl@0
   133
// Create a new CMyTimer.
sl@0
   134
//
sl@0
   135
	{
sl@0
   136
sl@0
   137
	return(new CMyTimer(0));
sl@0
   138
	}
sl@0
   139
sl@0
   140
CMyTimer::CMyTimer(TInt aPriority)
sl@0
   141
//
sl@0
   142
// Constructor
sl@0
   143
//
sl@0
   144
	: CTimer(aPriority),iCount(0), iImbalance(EFalse), iStopping(EFalse)
sl@0
   145
	{}
sl@0
   146
sl@0
   147
CMyTimer::~CMyTimer()
sl@0
   148
	{
sl@0
   149
	delete iDummy;
sl@0
   150
	}
sl@0
   151
sl@0
   152
void CMyTimer::Start()
sl@0
   153
//
sl@0
   154
// Start the timer
sl@0
   155
//
sl@0
   156
	{
sl@0
   157
	if (!iConstructed)
sl@0
   158
		{
sl@0
   159
		TRAPD(r, ConstructL());
sl@0
   160
		test(r==KErrNone);
sl@0
   161
		iConstructed = ETrue;
sl@0
   162
		iDummy = new(ELeave)CBase();
sl@0
   163
		}
sl@0
   164
	CActiveScheduler::Add(this); // Previously caused panic in UREL after Deque()
sl@0
   165
	SetPriority(1);
sl@0
   166
	After(ETimeReq);
sl@0
   167
	}
sl@0
   168
sl@0
   169
void CMyTimer::StartLeave(TBool aHandleLocally, TBool aLeaveInOOM, CMyRequestManager::TMode aMode)
sl@0
   170
//
sl@0
   171
// Start the timer
sl@0
   172
//
sl@0
   173
	{
sl@0
   174
	CActiveScheduler::Add(this); 
sl@0
   175
	SetPriority(1);
sl@0
   176
	After(ETimeReq);
sl@0
   177
	iLeave = ETrue;
sl@0
   178
	iHandleLocally = aHandleLocally;
sl@0
   179
	iLeaveInOOM = aLeaveInOOM;
sl@0
   180
	//	STATIC_CAST(CMyRequestManager*,CActiveScheduler::Current())->ExpectError(!aHandleLocally);
sl@0
   181
	STATIC_CAST(CMyRequestManager*,CActiveScheduler::Current())->SetMode(aMode);
sl@0
   182
	}
sl@0
   183
sl@0
   184
void CMyTimer::StopLeave()
sl@0
   185
	{
sl@0
   186
	iLeave=EFalse;
sl@0
   187
	}
sl@0
   188
sl@0
   189
void CMyTimer::StartImbalance()
sl@0
   190
	{
sl@0
   191
	iImbalance=ETrue;
sl@0
   192
	}
sl@0
   193
sl@0
   194
void CMyTimer::RunL()
sl@0
   195
//
sl@0
   196
// The timer has completed.
sl@0
   197
//
sl@0
   198
	{
sl@0
   199
	if (iLeave) 
sl@0
   200
		{
sl@0
   201
		Deque(); // Test removal from scheduler
sl@0
   202
		if (iLeaveInOOM)
sl@0
   203
			{
sl@0
   204
			__UHEAP_FAILNEXT(1);
sl@0
   205
			}
sl@0
   206
		User::Leave(KErrGeneral);
sl@0
   207
		}
sl@0
   208
	
sl@0
   209
	// This switch is used when testing for imbalance in the cleanupstack
sl@0
   210
	if(iImbalance)
sl@0
   211
		{
sl@0
   212
		if(iStopping)
sl@0
   213
			{
sl@0
   214
			
sl@0
   215
			iStopping=EFalse;
sl@0
   216
			//CleanupStack::PopAndDestroy(iDummy);
sl@0
   217
			CActiveScheduler::Stop();
sl@0
   218
			return;
sl@0
   219
			}
sl@0
   220
		else 
sl@0
   221
			{
sl@0
   222
			// Push something onto the stack, but dont take it off 
sl@0
   223
			//- deal in CActiveScheduler::DoRunL
sl@0
   224
			CleanupStack::PushL(iDummy);
sl@0
   225
			iStopping=ETrue; //Stop the scheduler the next time
sl@0
   226
			iCount=EMaxCount; 
sl@0
   227
//			CActiveScheduler::Stop();
sl@0
   228
			After(ETimeReq);
sl@0
   229
			return;
sl@0
   230
			}
sl@0
   231
		}
sl@0
   232
sl@0
   233
	iCount++;
sl@0
   234
	SetPriority(iCount);
sl@0
   235
	test.Printf(_L("\r%03d"),iCount);
sl@0
   236
sl@0
   237
	if (iCount<EMaxCount)
sl@0
   238
		After(ETimeReq);
sl@0
   239
	else
sl@0
   240
		{
sl@0
   241
		test.Printf(_L("\n"));
sl@0
   242
		CActiveScheduler::Stop();
sl@0
   243
		Deque(); // Test removal from scheduler
sl@0
   244
		iCount = 0;
sl@0
   245
		}
sl@0
   246
	}
sl@0
   247
sl@0
   248
TInt CMyTimer::RunError(TInt aError)
sl@0
   249
//
sl@0
   250
// Handle leave from RunL
sl@0
   251
//
sl@0
   252
	{
sl@0
   253
	if (iHandleLocally)
sl@0
   254
		{
sl@0
   255
		_LIT(KExpectError,"CMyTimer::RunError handling error %d\n");
sl@0
   256
		test.Printf(KExpectError,aError);
sl@0
   257
		CActiveScheduler::Stop();
sl@0
   258
		return KErrNone;
sl@0
   259
		}
sl@0
   260
	else 
sl@0
   261
		return aError; // Let the scheduler handle this error
sl@0
   262
	}
sl@0
   263
sl@0
   264
sl@0
   265
TInt DoImbalanceTest(TAny* /*aAny*/)
sl@0
   266
// This function is the first executing fuction of the cleanupstack imbalace
sl@0
   267
// testing thread - RunLCleanupImbalance
sl@0
   268
// see ImbalanceTest()
sl@0
   269
	{
sl@0
   270
	// Set up cleanup stack & scheduler
sl@0
   271
	RTest test(_L("Thread:RunLCleanupImbalance - DoImbalanceTest()"));
sl@0
   272
	test.Start(_L("DoImbalanceTest()"));
sl@0
   273
sl@0
   274
	//Create a cleanup stack and scheduler
sl@0
   275
	CTrapCleanup::New();
sl@0
   276
	CActiveScheduler* cas = new(ELeave) CActiveScheduler();
sl@0
   277
	CActiveScheduler::Install(cas);
sl@0
   278
sl@0
   279
	// Create a new AO.
sl@0
   280
	CMyTimer* myTimer = CMyTimer::New();
sl@0
   281
	myTimer->StopLeave();
sl@0
   282
	myTimer->StartImbalance(); 
sl@0
   283
	// Start the AO
sl@0
   284
	test.Next(_L("Start Imblance Test"));
sl@0
   285
	myTimer->Start();
sl@0
   286
	
sl@0
   287
	test.Next(_L("Start Scheduler"));
sl@0
   288
	// The following is expected to panic (with EUSER-CBase 90 EClnCheckFailed)
sl@0
   289
	cas->Start(); 
sl@0
   290
	
sl@0
   291
	delete myTimer;
sl@0
   292
	delete cas;
sl@0
   293
	test.End();
sl@0
   294
	return 0;
sl@0
   295
	}
sl@0
   296
sl@0
   297
#ifdef _DEBUG
sl@0
   298
LOCAL_C void ImbalanceTest()
sl@0
   299
// this test will test whether the cleanup stack is imbalanced after
sl@0
   300
// a runL of an Active object.
sl@0
   301
	{
sl@0
   302
	TBool imbalanced = ETrue;
sl@0
   303
	User::SetJustInTime(EFalse);
sl@0
   304
	RThread t;
sl@0
   305
	TRequestStatus s;
sl@0
   306
	test.Next(_L("Create a thread (RunLCleanupImbalance)"));
sl@0
   307
	TInt r=t.Create(_L("RunLCleanupImbalance"),DoImbalanceTest,KDefaultStackSize,KDefaultStackSize,KDefaultStackSize,&imbalanced);
sl@0
   308
	test(r==KErrNone);
sl@0
   309
	t.Logon(s);
sl@0
   310
	test.Next(_L("Resume and wait for panic (E32USER-CBase 90 EClnCheckFailed) due to imbalance"));
sl@0
   311
	t.Resume();
sl@0
   312
	User::WaitForRequest(s);
sl@0
   313
	
sl@0
   314
	test.Printf(_L("Exit Type %d\r\n"),(TInt)t.ExitType());
sl@0
   315
	test.Printf(_L("Exit Reason %d\r\n"),(TInt)t.ExitReason());
sl@0
   316
	
sl@0
   317
	test(t.ExitReason()==EClnCheckFailed);
sl@0
   318
	
sl@0
   319
	CLOSE_AND_WAIT(t);
sl@0
   320
sl@0
   321
	}
sl@0
   322
#endif
sl@0
   323
sl@0
   324
GLDEF_C TInt E32Main()
sl@0
   325
//
sl@0
   326
// Test timers.
sl@0
   327
//
sl@0
   328
    {
sl@0
   329
 	test.Title();
sl@0
   330
	test.Start(_L("Creating CActiveScheduler"));
sl@0
   331
	CMyRequestManager* pR=new CMyRequestManager;
sl@0
   332
	test(pR!=NULL);
sl@0
   333
	CActiveScheduler::Install(pR);
sl@0
   334
//
sl@0
   335
	test.Next(_L("Testing relative timers"));
sl@0
   336
	CMyTimer* pT=CMyTimer::New();
sl@0
   337
	test(pT!=NULL);
sl@0
   338
//
sl@0
   339
	test.Next(_L("Start timer"));
sl@0
   340
	pT->Start();
sl@0
   341
//
sl@0
   342
	test.Next(_L("Start CMyRequestManager"));
sl@0
   343
	CActiveScheduler::Start();
sl@0
   344
//
sl@0
   345
	test.Next(_L("Start timer again"));
sl@0
   346
	pT->Start();
sl@0
   347
//
sl@0
   348
	test.Next(_L("Start CMyRequestManager"));
sl@0
   349
	CActiveScheduler::Start();
sl@0
   350
//
sl@0
   351
	test.Next(_L("Start timer, leave in RunL, handle in scheduler"));
sl@0
   352
	pT->StartLeave(EFalse, EFalse, CMyRequestManager::EExpectError );
sl@0
   353
//
sl@0
   354
	test.Next(_L("Start CMyRequestManager"));
sl@0
   355
	CActiveScheduler::Start();
sl@0
   356
#ifdef _DEBUG
sl@0
   357
//
sl@0
   358
	test.Next(_L("Start timer, leave in RunL, generate nested exception under OOM condition"));
sl@0
   359
	pT->StartLeave(EFalse, ETrue, CMyRequestManager::EGenerateException);
sl@0
   360
//
sl@0
   361
	test.Next(_L("Start CMyRequestManager"));
sl@0
   362
	CActiveScheduler::Start();
sl@0
   363
	__UHEAP_RESET;
sl@0
   364
#endif
sl@0
   365
	//
sl@0
   366
	test.Next(_L("Start timer, leave in RunL, handle in object"));
sl@0
   367
	pT->StartLeave(ETrue, EFalse, CMyRequestManager::EPanic);
sl@0
   368
//
sl@0
   369
	test.Next(_L("Start CMyRequestManager"));
sl@0
   370
	CActiveScheduler::Start();
sl@0
   371
//
sl@0
   372
#ifdef _DEBUG
sl@0
   373
// Test the cleanupstack imbalances
sl@0
   374
	test.Next(_L("Test : Check Cleanupstack imbalance in RunL, handle(panic) in scheduler"));
sl@0
   375
	ImbalanceTest();
sl@0
   376
#endif
sl@0
   377
//
sl@0
   378
	test.End();
sl@0
   379
	return(0); 
sl@0
   380
    }