os/kernelhwsrv/kerneltest/e32test/active/t_cact.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_cact.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test the CActiveScheduler class, including scheduling priority, thread 
sl@0
    17
// panics and cancelling objects attached to the scheduler. 
sl@0
    18
// API Information:
sl@0
    19
// CActiveScheduler 
sl@0
    20
// Details:
sl@0
    21
// - Verify the thread is panicked when one of the following programming errors occurs:
sl@0
    22
// - start an uninstalled scheduler
sl@0
    23
// - install a scheduler twice
sl@0
    24
// - stop the scheduler twice 
sl@0
    25
// - stop an uninstalled scheduler
sl@0
    26
// - set active an active object twice
sl@0
    27
// - add an active object to an uninstalled scheduler
sl@0
    28
// - add a NULL pointer to the scheduler
sl@0
    29
// - add an active object twice
sl@0
    30
// - thread gets a stray signal
sl@0
    31
// - make request on an un-added active object
sl@0
    32
// - Create timers with different priorities
sl@0
    33
// - Check the timers are scheduled according to expiration time
sl@0
    34
// - Check the timers are scheduled in order of their priority when they expire at the
sl@0
    35
// same time
sl@0
    36
// - Check the timers are scheduled in order of the addition to the scheduler when
sl@0
    37
// they expire at the same time and they have the same priority
sl@0
    38
// - Verify that a leave in RunL method not handled by the active object calls the active 
sl@0
    39
// scheduler's Error method
sl@0
    40
// - Test a cancelled timer will not be scheduled 
sl@0
    41
// - Create a thread with its own heap and test that timers are scheduled in the order of
sl@0
    42
// their expiration time, priority and order of addition to the scheduler
sl@0
    43
// - Check the heap is not corrupted by all the tests
sl@0
    44
// Platforms/Drives/Compatibility:
sl@0
    45
// All
sl@0
    46
// Assumptions/Requirement/Pre-requisites:
sl@0
    47
// Failures and causes:
sl@0
    48
// Base Port information:
sl@0
    49
// 
sl@0
    50
//
sl@0
    51
sl@0
    52
#include <e32test.h>
sl@0
    53
#include <e32panic.h>
sl@0
    54
sl@0
    55
const TInt KmyErrorNum=999;
sl@0
    56
const TInt KLeaveCode=1234;  
sl@0
    57
const TInt KgTimerID=4321;
sl@0
    58
const TInt KCancelCode=1111;
sl@0
    59
const TInt KTestArraySize=10;
sl@0
    60
const TInt KPanicThreadRet=2222;
sl@0
    61
const TInt KHeapSize=0x2000;
sl@0
    62
sl@0
    63
enum TActivePriority {eLowest=-100,eLow=-1,eNone=0,eHigh=1,eHighest=100};
sl@0
    64
enum TDirective {EStartUninstalled,EInstallTwice,EStopTwice,EStopUninstalled,ESetActiveTwice,
sl@0
    65
				 EAddToUninstalled,EAddNull,EAddTwice,ECreateStray,ERequestUnadded,EStraySignalNoKRequestPending,EStraySignalNoSetActive,ENormal};
sl@0
    66
sl@0
    67
class TSecdulerTester
sl@0
    68
	{
sl@0
    69
public:
sl@0
    70
	void Test1();
sl@0
    71
	void Test2();
sl@0
    72
	void Test3();
sl@0
    73
	void Test4();
sl@0
    74
	};
sl@0
    75
sl@0
    76
class MyManager : public CActiveScheduler
sl@0
    77
	{
sl@0
    78
public:
sl@0
    79
	virtual void Error(TInt anError) const {CActiveScheduler::Halt(anError);}
sl@0
    80
	};
sl@0
    81
sl@0
    82
class myTimer : public CTimer
sl@0
    83
	{
sl@0
    84
public:
sl@0
    85
	myTimer(const TInt aPriority, const TInt anIdentifier):CTimer(aPriority){iIdentifier=anIdentifier;}
sl@0
    86
	virtual void RunL();
sl@0
    87
	void Start();		  
sl@0
    88
	void Setactive() {SetActive();}
sl@0
    89
	static void SetNum(const TInt aNum) {iNum=aNum; iCount=0;}
sl@0
    90
private:   
sl@0
    91
	TInt iIdentifier;
sl@0
    92
	static TInt iCount;
sl@0
    93
	static TInt iNum;
sl@0
    94
	};
sl@0
    95
sl@0
    96
class myThreadTimer : public CTimer
sl@0
    97
	{
sl@0
    98
public:
sl@0
    99
	myThreadTimer(const TInt aPriority, const TInt anIdentifier):CTimer(aPriority){iIdentifier=anIdentifier;}
sl@0
   100
	virtual void RunL();
sl@0
   101
	static void SetNum(const TInt aNum) {iNum=aNum; iCount=0;}
sl@0
   102
	void Start();
sl@0
   103
private:
sl@0
   104
	TInt iIdentifier;
sl@0
   105
	static TInt iCount;
sl@0
   106
	static TInt iNum;
sl@0
   107
	};
sl@0
   108
sl@0
   109
class CreateStray : public CActive
sl@0
   110
	{
sl@0
   111
public:
sl@0
   112
	CreateStray(TInt aPriority);
sl@0
   113
	void DoCancel() {}
sl@0
   114
	void RunL() {}
sl@0
   115
private:
sl@0
   116
	RTimer iTimer;
sl@0
   117
	};
sl@0
   118
sl@0
   119
class myStray : public CActive
sl@0
   120
	{
sl@0
   121
public:
sl@0
   122
	myStray(TInt aPriority) : CActive(aPriority) {};
sl@0
   123
	void DoCancel() {}
sl@0
   124
	void RunL() { CActiveScheduler::Stop(); }
sl@0
   125
	void Start1();
sl@0
   126
	void Start2();
sl@0
   127
	};
sl@0
   128
sl@0
   129
void myStray::Start1()
sl@0
   130
	{
sl@0
   131
	TRequestStatus* s=&iStatus;
sl@0
   132
	SetActive();
sl@0
   133
	RThread().RequestComplete(s,KErrNone);
sl@0
   134
	}
sl@0
   135
sl@0
   136
void myStray::Start2()
sl@0
   137
	{
sl@0
   138
	TRequestStatus* s=&iStatus;
sl@0
   139
	iStatus=KRequestPending;
sl@0
   140
	User::RequestComplete(s,KErrNone);
sl@0
   141
	}
sl@0
   142
sl@0
   143
TInt myTimer::iCount;
sl@0
   144
TInt myTimer::iNum;
sl@0
   145
TInt myThreadTimer::iCount;
sl@0
   146
TInt myThreadTimer::iNum;
sl@0
   147
sl@0
   148
LOCAL_D RTest test(_L("T_CACT"));
sl@0
   149
LOCAL_D myTimer* gTimer; // This is cancelled from within the run method 
sl@0
   150
LOCAL_D RSemaphore threadSemaphore;
sl@0
   151
LOCAL_D myTimer* pLowest=NULL;
sl@0
   152
LOCAL_D myTimer* pLow=NULL;
sl@0
   153
LOCAL_D myTimer* pNone=NULL;
sl@0
   154
LOCAL_D myTimer* pHigh=NULL;
sl@0
   155
LOCAL_D myTimer* pHigh2=NULL;
sl@0
   156
LOCAL_D myTimer* pHigh3=NULL;
sl@0
   157
LOCAL_D myTimer* pHighest=NULL;
sl@0
   158
LOCAL_D TInt order[KTestArraySize]; // When a timer expires its identifier is placed in here
sl@0
   159
LOCAL_D TInt threadArray[KTestArraySize];
sl@0
   160
sl@0
   161
LOCAL_C TInt myThreadEntryPoint(TAny*)
sl@0
   162
//
sl@0
   163
// myThread tests 
sl@0
   164
//
sl@0
   165
	{
sl@0
   166
sl@0
   167
	__UHEAP_MARK;
sl@0
   168
	RTest test(_L("Separate thread tests"));
sl@0
   169
	test.Title();
sl@0
   170
	test.Start(_L("Create semaphore"));
sl@0
   171
	RTimer t;
sl@0
   172
	TInt ret=t.CreateLocal();
sl@0
   173
	test(ret==KErrNone);
sl@0
   174
//
sl@0
   175
	test.Next(_L("Install scheduler"));
sl@0
   176
	MyManager* pManager=new MyManager;
sl@0
   177
	CActiveScheduler::Install(pManager);
sl@0
   178
//
sl@0
   179
	test.Next(_L("Create active objects"));
sl@0
   180
	myThreadTimer* pLowest=new myThreadTimer(eLowest, eLowest);
sl@0
   181
	test(pLowest!=NULL);
sl@0
   182
	myThreadTimer* pLow=new myThreadTimer(eLow, eLow);
sl@0
   183
	test(pLow!=NULL);
sl@0
   184
	myThreadTimer* pNone=new myThreadTimer(eNone, eNone);
sl@0
   185
	test(pNone!=NULL);
sl@0
   186
	myThreadTimer* pHigh=new myThreadTimer(eHigh, eHigh);
sl@0
   187
	test(pHigh!=NULL);
sl@0
   188
	myThreadTimer* pHigh2=new myThreadTimer(eHigh, eHigh+2);
sl@0
   189
	test(pHigh2!=NULL);
sl@0
   190
	myThreadTimer* pHigh3=new myThreadTimer(eHigh, eHigh+3);
sl@0
   191
	test(pHigh3!=NULL);
sl@0
   192
	myThreadTimer* pHighest=new myThreadTimer(eHighest, eHighest);
sl@0
   193
	test(pHighest!=NULL);
sl@0
   194
//
sl@0
   195
	test.Next(_L("Test low is scheduled before lowest"));
sl@0
   196
	myThreadTimer::SetNum(2);
sl@0
   197
	pLowest->Start();
sl@0
   198
	pLowest->After(0);
sl@0
   199
	pLow->Start();
sl@0
   200
	pLow->After(0);
sl@0
   201
	TRequestStatus s;
sl@0
   202
	t.After(s,100000);
sl@0
   203
	test(s==KRequestPending);
sl@0
   204
	User::WaitForRequest(s);
sl@0
   205
	CActiveScheduler::Start();
sl@0
   206
	test(threadArray[0]==eLow && threadArray[1]==eLowest);
sl@0
   207
//
sl@0
   208
	test.Next(_L("Test none is scheduled before low"));
sl@0
   209
	myThreadTimer::SetNum(2);
sl@0
   210
	pLow->After(0);
sl@0
   211
	pNone->Start();
sl@0
   212
	pNone->After(0);
sl@0
   213
	t.After(s,100000);
sl@0
   214
	test(s==KRequestPending);
sl@0
   215
	User::WaitForRequest(s);
sl@0
   216
	CActiveScheduler::Start();
sl@0
   217
	test(threadArray[0]==eNone && threadArray[1]==eLow);
sl@0
   218
//
sl@0
   219
	test.Next(_L("Test high is scheduled before none"));
sl@0
   220
	myThreadTimer::SetNum(2);
sl@0
   221
	pHigh->Start();
sl@0
   222
	pHigh->After(0);
sl@0
   223
	pNone->After(0);
sl@0
   224
	t.After(s, 100000);
sl@0
   225
	test(s==KRequestPending);
sl@0
   226
	User::WaitForRequest(s);
sl@0
   227
	CActiveScheduler::Start();
sl@0
   228
	test(threadArray[0]==eHigh && threadArray[1]==eNone);
sl@0
   229
//
sl@0
   230
	test.Next(_L("Test highest is scheduled before high"));
sl@0
   231
	myThreadTimer::SetNum(2);
sl@0
   232
	pHighest->Start();
sl@0
   233
	pHighest->After(0);
sl@0
   234
	pHigh->After(0);
sl@0
   235
	t.After(s, 100000);
sl@0
   236
	test(s==KRequestPending);
sl@0
   237
	User::WaitForRequest(s);
sl@0
   238
	CActiveScheduler::Start();
sl@0
   239
	test(threadArray[0]==eHighest && threadArray[1]==eHigh);
sl@0
   240
//
sl@0
   241
	test.Next(_L("Test objects are scheduled according to priority"));
sl@0
   242
	myThreadTimer::SetNum(5);
sl@0
   243
	pLowest->After(0);
sl@0
   244
	pLow->After(0);
sl@0
   245
	pNone->After(0);
sl@0
   246
	pHigh->After(0);
sl@0
   247
	pHighest->After(0);
sl@0
   248
	t.After(s, 100000);
sl@0
   249
	test(s==KRequestPending);
sl@0
   250
	User::WaitForRequest(s);
sl@0
   251
	CActiveScheduler::Start();
sl@0
   252
	test(threadArray[0]==eHighest && threadArray[1]==eHigh && threadArray[2]==eNone && 
sl@0
   253
		 threadArray[3]==eLow && threadArray[4]==eLowest);
sl@0
   254
//
sl@0
   255
	test.Next(_L("Test objects are scheduled according to timer expiry"));
sl@0
   256
	myThreadTimer::SetNum(5);
sl@0
   257
	pLowest->After(0);
sl@0
   258
	pLow->After(500000);
sl@0
   259
	pNone->After(1000000);
sl@0
   260
	pHigh->After(1500000);
sl@0
   261
	pHighest->After(2000000);
sl@0
   262
	CActiveScheduler::Start();
sl@0
   263
	test(threadArray[4]==eHighest && threadArray[3]==eHigh && threadArray[2]==eNone && 
sl@0
   264
		 threadArray[1]==eLow && threadArray[0]==eLowest);
sl@0
   265
//
sl@0
   266
	test.Next(_L("Test with some objects having the same priority"));
sl@0
   267
	myThreadTimer::SetNum(7);
sl@0
   268
	pLowest->After(0);
sl@0
   269
	pLow->After(0);
sl@0
   270
	pNone->After(0);
sl@0
   271
	pHigh->After(0);
sl@0
   272
	pHigh2->Start();
sl@0
   273
	pHigh2->After(0);
sl@0
   274
	pHigh3->Start();
sl@0
   275
	pHigh3->After(0);
sl@0
   276
	pHighest->After(0);
sl@0
   277
	t.After(s, 100000);
sl@0
   278
	test(s==KRequestPending);
sl@0
   279
	User::WaitForRequest(s);
sl@0
   280
	CActiveScheduler::Start();
sl@0
   281
	test(threadArray[0]==eHighest && threadArray[1]==eHigh && threadArray[2]==eHigh+2 && 
sl@0
   282
		 threadArray[3]==eHigh+3 && threadArray[4]==eNone && threadArray[5]==eLow && threadArray[6]==eLowest);
sl@0
   283
//
sl@0
   284
	test.Next(_L("Tidying up"));
sl@0
   285
	delete pManager;
sl@0
   286
	delete pLowest;
sl@0
   287
	delete pLow;
sl@0
   288
	delete pNone;
sl@0
   289
	delete pHigh;
sl@0
   290
	delete pHigh2;
sl@0
   291
	delete pHigh3;
sl@0
   292
	delete pHighest;
sl@0
   293
	test.Close();
sl@0
   294
	__UHEAP_MARKEND;
sl@0
   295
	threadSemaphore.Signal();
sl@0
   296
	return(KErrNone);
sl@0
   297
	}
sl@0
   298
sl@0
   299
LOCAL_D TInt panicThread(TAny* aDirective)
sl@0
   300
//
sl@0
   301
// Test thread which panics
sl@0
   302
//
sl@0
   303
	{
sl@0
   304
	// cause panics in various ways depending upon aDirective
sl@0
   305
	MyManager* pManager=new MyManager;
sl@0
   306
	
sl@0
   307
	switch((TInt)aDirective)
sl@0
   308
		{
sl@0
   309
	case EStartUninstalled:
sl@0
   310
		CActiveScheduler::Start(); // Start an uninstalled active schedler
sl@0
   311
		break;
sl@0
   312
	case EInstallTwice:
sl@0
   313
		CActiveScheduler::Install(pManager); // Install the scheduler twice
sl@0
   314
		CActiveScheduler::Install(pManager);
sl@0
   315
		break;
sl@0
   316
	case EStopTwice:
sl@0
   317
		CActiveScheduler::Install(pManager); // Stop a scheduler twice
sl@0
   318
		CActiveScheduler::Stop();
sl@0
   319
		CActiveScheduler::Stop();
sl@0
   320
		break;
sl@0
   321
	case EStopUninstalled:
sl@0
   322
		CActiveScheduler::Stop(); // Stop an uninstalled active scheduler
sl@0
   323
		break;
sl@0
   324
	case ESetActiveTwice:
sl@0
   325
		{
sl@0
   326
//
sl@0
   327
// Set an active object to active twice
sl@0
   328
//
sl@0
   329
		myTimer* pTimer=new myTimer(eNone,eNone);
sl@0
   330
		CActiveScheduler::Install(pManager);
sl@0
   331
		CActiveScheduler::Add(pTimer);
sl@0
   332
		pTimer->Setactive();
sl@0
   333
		pTimer->Setactive();
sl@0
   334
		}
sl@0
   335
		break;
sl@0
   336
	case EAddToUninstalled:
sl@0
   337
		{
sl@0
   338
//
sl@0
   339
// Add an active object to an uninstalled scheduler
sl@0
   340
//
sl@0
   341
		myTimer* pTimer=new myTimer(eNone,eNone);
sl@0
   342
		CActiveScheduler::Add(pTimer); 
sl@0
   343
		}
sl@0
   344
		break;
sl@0
   345
	case EAddNull:
sl@0
   346
//
sl@0
   347
// Add Null to the scheduling queue
sl@0
   348
//
sl@0
   349
		CActiveScheduler::Install(pManager);  
sl@0
   350
		CActiveScheduler::Add(NULL);
sl@0
   351
		break;
sl@0
   352
	case EAddTwice:
sl@0
   353
		{
sl@0
   354
//
sl@0
   355
// Add the same object twice to the scheduling queue
sl@0
   356
//
sl@0
   357
		myTimer* pTimer=new myTimer(eNone,eNone);
sl@0
   358
		CActiveScheduler::Install(pManager);  
sl@0
   359
		CActiveScheduler::Add(pTimer);
sl@0
   360
		CActiveScheduler::Add(pTimer);
sl@0
   361
		}
sl@0
   362
		break;
sl@0
   363
	case ECreateStray:
sl@0
   364
		{
sl@0
   365
//
sl@0
   366
// Create a stray signal
sl@0
   367
//
sl@0
   368
		CActiveScheduler::Install(pManager); 
sl@0
   369
		new CreateStray(1);
sl@0
   370
		CActiveScheduler::Start();
sl@0
   371
		}
sl@0
   372
		break;
sl@0
   373
	case ERequestUnadded:
sl@0
   374
		{
sl@0
   375
//
sl@0
   376
// Make a request of an active object not added to the scheduling queue
sl@0
   377
//
sl@0
   378
		CActiveScheduler::Install(pManager);
sl@0
   379
		myTimer* pTimer=new myTimer(eNone,eNone);
sl@0
   380
		pTimer->Setactive();
sl@0
   381
		}
sl@0
   382
		break;
sl@0
   383
	case EStraySignalNoKRequestPending:
sl@0
   384
		{
sl@0
   385
		CActiveScheduler::Install(pManager);
sl@0
   386
		myStray* pStray = new myStray(eNone);
sl@0
   387
		CActiveScheduler::Add(pStray);
sl@0
   388
		pStray->Start1();
sl@0
   389
		CActiveScheduler::Start();
sl@0
   390
		}
sl@0
   391
		break;
sl@0
   392
	case EStraySignalNoSetActive:
sl@0
   393
		{
sl@0
   394
		CActiveScheduler::Install(pManager);
sl@0
   395
		myStray* pStray = new myStray(eNone);
sl@0
   396
		CActiveScheduler::Add(pStray);
sl@0
   397
		pStray->Start2();
sl@0
   398
		CActiveScheduler::Start();
sl@0
   399
		}
sl@0
   400
		break;
sl@0
   401
	case ENormal:
sl@0
   402
	default:
sl@0
   403
		break;
sl@0
   404
		}
sl@0
   405
	delete pManager;
sl@0
   406
	return(KPanicThreadRet);
sl@0
   407
	}
sl@0
   408
sl@0
   409
void myTimer::RunL()
sl@0
   410
//
sl@0
   411
// Handle the timer completion
sl@0
   412
//
sl@0
   413
	{
sl@0
   414
sl@0
   415
	if (iIdentifier==KLeaveCode)	// Used to test that the request manager error() method is called
sl@0
   416
		User::Leave(KmyErrorNum);
sl@0
   417
	if (iIdentifier==KCancelCode) // Used to test cancelling an object
sl@0
   418
		gTimer->Cancel();
sl@0
   419
	order[iCount++]=iIdentifier;
sl@0
   420
	if (iCount>=iNum)
sl@0
   421
		CActiveScheduler::Stop();
sl@0
   422
	}
sl@0
   423
sl@0
   424
void myTimer::Start()
sl@0
   425
//
sl@0
   426
// Start a timer
sl@0
   427
//
sl@0
   428
	{
sl@0
   429
sl@0
   430
	ConstructL();
sl@0
   431
	CActiveScheduler::Add(this);
sl@0
   432
	}
sl@0
   433
sl@0
   434
void myThreadTimer::RunL()
sl@0
   435
//
sl@0
   436
// Handle timer completion
sl@0
   437
//
sl@0
   438
	{
sl@0
   439
	
sl@0
   440
	threadArray[iCount++]=iIdentifier;
sl@0
   441
	if(iCount>=iNum)
sl@0
   442
		CActiveScheduler::Stop();
sl@0
   443
	}
sl@0
   444
sl@0
   445
void myThreadTimer::Start()
sl@0
   446
//
sl@0
   447
// Start a timer
sl@0
   448
//
sl@0
   449
	{
sl@0
   450
sl@0
   451
	ConstructL();
sl@0
   452
	CActiveScheduler::Add(this);
sl@0
   453
	}
sl@0
   454
sl@0
   455
CreateStray::CreateStray(TInt aPriority)
sl@0
   456
//
sl@0
   457
// Constructor
sl@0
   458
//
sl@0
   459
	: CActive(aPriority)
sl@0
   460
	{
sl@0
   461
sl@0
   462
	iTimer.CreateLocal();
sl@0
   463
	CActiveScheduler::Add(this);
sl@0
   464
	iTimer.After(iStatus, 1000000);
sl@0
   465
	}
sl@0
   466
sl@0
   467
void TSecdulerTester::Test1()
sl@0
   468
//
sl@0
   469
// Test 1
sl@0
   470
//
sl@0
   471
	{
sl@0
   472
sl@0
   473
//
sl@0
   474
// Test the panics
sl@0
   475
//
sl@0
   476
sl@0
   477
	RThread thread;
sl@0
   478
	TRequestStatus stat;
sl@0
   479
//
sl@0
   480
	test.Start(_L("First test normal thread termination"));
sl@0
   481
	TInt r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)ENormal);
sl@0
   482
	test(r==KErrNone);
sl@0
   483
	thread.Logon(stat);
sl@0
   484
	thread.Resume();
sl@0
   485
	User::WaitForRequest(stat);
sl@0
   486
	test(thread.ExitCategory().Compare(_L("Kill"))==0);
sl@0
   487
	test(thread.ExitReason()==KPanicThreadRet);	  
sl@0
   488
	test(thread.ExitType()==EExitKill);
sl@0
   489
	CLOSE_AND_WAIT(thread);
sl@0
   490
//
sl@0
   491
	test.Next(_L("Starting an uninstalled scheduler panics"));
sl@0
   492
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EStartUninstalled);
sl@0
   493
	test(r==KErrNone);
sl@0
   494
	thread.Logon(stat);
sl@0
   495
	thread.Resume();
sl@0
   496
	User::WaitForRequest(stat);
sl@0
   497
	test(thread.ExitReason()==EReqManagerDoesNotExist);
sl@0
   498
	test(thread.ExitType()==EExitPanic);
sl@0
   499
	CLOSE_AND_WAIT(thread);
sl@0
   500
//
sl@0
   501
	test.Next(_L("Installing the scheduler twice panics"));
sl@0
   502
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EInstallTwice);
sl@0
   503
	test(r==KErrNone);
sl@0
   504
	thread.Logon(stat);
sl@0
   505
	thread.Resume();
sl@0
   506
	User::WaitForRequest(stat);
sl@0
   507
	test(thread.ExitReason()==EReqManagerAlreadyExists);
sl@0
   508
	test(thread.ExitType()==EExitPanic);
sl@0
   509
	CLOSE_AND_WAIT(thread);
sl@0
   510
//
sl@0
   511
	test.Next(_L("Stopping the scheduler twice panics"));
sl@0
   512
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EStopTwice);
sl@0
   513
	test(r==KErrNone);
sl@0
   514
	thread.Logon(stat);
sl@0
   515
	thread.Resume();
sl@0
   516
	User::WaitForRequest(stat);
sl@0
   517
	test(thread.ExitReason()==EReqTooManyStops);
sl@0
   518
	test(thread.ExitType()==EExitPanic);
sl@0
   519
	CLOSE_AND_WAIT(thread);
sl@0
   520
//
sl@0
   521
	test.Next(_L("Stopping an uninstalled scheduler panics"));
sl@0
   522
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EStopUninstalled);
sl@0
   523
	test(r==KErrNone);
sl@0
   524
	thread.Logon(stat);
sl@0
   525
	thread.Resume();
sl@0
   526
	User::WaitForRequest(stat);
sl@0
   527
	test(thread.ExitReason()==EReqManagerDoesNotExist);
sl@0
   528
	test(thread.ExitType()==EExitPanic);
sl@0
   529
	CLOSE_AND_WAIT(thread);
sl@0
   530
//
sl@0
   531
	test.Next(_L("Setting an active object to active twice panics"));
sl@0
   532
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)ESetActiveTwice);
sl@0
   533
	test(r==KErrNone);
sl@0
   534
	thread.Logon(stat);
sl@0
   535
	thread.Resume();
sl@0
   536
	User::WaitForRequest(stat);
sl@0
   537
	test(thread.ExitReason()==EReqAlreadyActive);
sl@0
   538
	test(thread.ExitType()==EExitPanic);
sl@0
   539
	CLOSE_AND_WAIT(thread);
sl@0
   540
//
sl@0
   541
	test.Next(_L("Adding an active object to an uninstalled scheduler panics"));
sl@0
   542
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EAddToUninstalled);
sl@0
   543
	test(r==KErrNone);
sl@0
   544
	thread.Logon(stat);
sl@0
   545
	thread.Resume();
sl@0
   546
	User::WaitForRequest(stat);
sl@0
   547
	test(thread.ExitReason()==EReqManagerDoesNotExist);
sl@0
   548
	test(thread.ExitType()==EExitPanic);
sl@0
   549
	CLOSE_AND_WAIT(thread);
sl@0
   550
//
sl@0
   551
	test.Next(_L("Adding NULL panics"));
sl@0
   552
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EAddNull);
sl@0
   553
	test(r==KErrNone);
sl@0
   554
	thread.Logon(stat);
sl@0
   555
	thread.Resume();
sl@0
   556
	User::WaitForRequest(stat);
sl@0
   557
	test(thread.ExitReason()==EReqNull);
sl@0
   558
	test(thread.ExitType()==EExitPanic);
sl@0
   559
	CLOSE_AND_WAIT(thread);
sl@0
   560
//
sl@0
   561
	test.Next(_L("Adding an active object twice panics"));
sl@0
   562
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EAddTwice);
sl@0
   563
	test(r==KErrNone);
sl@0
   564
	thread.Logon(stat);
sl@0
   565
	thread.Resume();
sl@0
   566
	User::WaitForRequest(stat);
sl@0
   567
	test(thread.ExitReason()==EReqAlreadyAdded);
sl@0
   568
	test(thread.ExitType()==EExitPanic);
sl@0
   569
	CLOSE_AND_WAIT(thread);
sl@0
   570
//
sl@0
   571
	test.Next(_L("A stray signal causes a panic"));
sl@0
   572
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)ECreateStray);
sl@0
   573
	test(r==KErrNone);
sl@0
   574
	thread.Logon(stat);
sl@0
   575
	thread.Resume();
sl@0
   576
	User::WaitForRequest(stat);
sl@0
   577
	test(thread.ExitReason()==EReqStrayEvent);
sl@0
   578
	test(thread.ExitType()==EExitPanic);
sl@0
   579
	CLOSE_AND_WAIT(thread);
sl@0
   580
//
sl@0
   581
	test.Next(_L("Making a request on an unadded active object panics"));
sl@0
   582
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)ERequestUnadded);
sl@0
   583
	test(r==KErrNone);
sl@0
   584
	thread.Logon(stat);
sl@0
   585
	thread.Resume();
sl@0
   586
	User::WaitForRequest(stat);
sl@0
   587
	test(thread.ExitReason()==EActiveNotAdded);
sl@0
   588
	test(thread.ExitType()==EExitPanic);
sl@0
   589
	CLOSE_AND_WAIT(thread);
sl@0
   590
//
sl@0
   591
	test.Next(_L("The service provider does not set the status to KRequestPending"));
sl@0
   592
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EStraySignalNoKRequestPending);
sl@0
   593
	test(r==KErrNone);
sl@0
   594
	thread.Logon(stat);
sl@0
   595
	thread.Resume();
sl@0
   596
	User::WaitForRequest(stat);
sl@0
   597
#ifdef _DEBUG
sl@0
   598
	//this might fail if we're using a UREL euser
sl@0
   599
	test(thread.ExitReason()==EReqStrayEvent);
sl@0
   600
	test(thread.ExitType()==EExitPanic);
sl@0
   601
#else
sl@0
   602
	test(thread.ExitCategory().Compare(_L("Kill"))==0);
sl@0
   603
	test(thread.ExitReason()==KPanicThreadRet);	  
sl@0
   604
	test(thread.ExitType()==EExitKill);
sl@0
   605
#endif 
sl@0
   606
	CLOSE_AND_WAIT(thread);
sl@0
   607
//
sl@0
   608
#ifdef _DEBUG
sl@0
   609
	//this might fail if we're using a UREL euser
sl@0
   610
	test.Next(_L("The active object does not call SetActive"));
sl@0
   611
	r=thread.Create(_L("myThread"),panicThread,KDefaultStackSize,KHeapSize,KHeapSize,(TAny*)EStraySignalNoSetActive);
sl@0
   612
	test(r==KErrNone);
sl@0
   613
	thread.Logon(stat);
sl@0
   614
	thread.Resume();
sl@0
   615
	User::WaitForRequest(stat);
sl@0
   616
	test(thread.ExitReason()==EReqStrayEvent);
sl@0
   617
	test(thread.ExitType()==EExitPanic);
sl@0
   618
	CLOSE_AND_WAIT(thread);
sl@0
   619
#endif
sl@0
   620
//
sl@0
   621
	test.End();
sl@0
   622
	}
sl@0
   623
sl@0
   624
void TSecdulerTester::Test2()
sl@0
   625
//
sl@0
   626
// Test 2
sl@0
   627
//
sl@0
   628
	{
sl@0
   629
sl@0
   630
	test.Start(_L("Create timer"));
sl@0
   631
	RTimer t;
sl@0
   632
	TInt ret=t.CreateLocal();
sl@0
   633
	test(ret==KErrNone);
sl@0
   634
	TRequestStatus s;
sl@0
   635
//
sl@0
   636
	test.Next(_L("Test low is scheduled before lowest when timing equal"));
sl@0
   637
	myTimer::SetNum(2);
sl@0
   638
	pLowest->After(0);
sl@0
   639
	pLow->After(0);
sl@0
   640
	t.After(s, 100000);
sl@0
   641
	test(s==KRequestPending);
sl@0
   642
	User::WaitForRequest(s);
sl@0
   643
	CActiveScheduler::Start();
sl@0
   644
	test(order[0]==eLow && order[1]==eLowest);
sl@0
   645
//
sl@0
   646
	test.Next(_L("Test low is not before lowest when it expires later"));
sl@0
   647
	myTimer::SetNum(2);
sl@0
   648
	pLowest->After(0);
sl@0
   649
	pLow->After(1000000);
sl@0
   650
	CActiveScheduler::Start();
sl@0
   651
	test(order[0]==eLowest && order[1]==eLow);
sl@0
   652
//
sl@0
   653
	test.Next(_L("Test none is scheduled before low"));
sl@0
   654
	myTimer::SetNum(2);
sl@0
   655
	pLow->After(0);
sl@0
   656
	pNone->After(0);
sl@0
   657
	t.After(s, 100000);
sl@0
   658
	test(s==KRequestPending);
sl@0
   659
	User::WaitForRequest(s);
sl@0
   660
	CActiveScheduler::Start();
sl@0
   661
	test(order[0]==eNone && order[1]==eLow);
sl@0
   662
//
sl@0
   663
	test.Next(_L("Test none is not before low when it expires later"));
sl@0
   664
	myTimer::SetNum(2);
sl@0
   665
	pLow->After(0);
sl@0
   666
	pNone->After(1000000);
sl@0
   667
	CActiveScheduler::Start();
sl@0
   668
	test(order[0]==eLow && order[1]==eNone);
sl@0
   669
//
sl@0
   670
	test.Next(_L("Test high is scheduled before none"));
sl@0
   671
	myTimer::SetNum(2);
sl@0
   672
	pHigh->After(0);
sl@0
   673
	pNone->After(0);
sl@0
   674
	t.After(s, 100000);
sl@0
   675
	test(s==KRequestPending);
sl@0
   676
	User::WaitForRequest(s);
sl@0
   677
	CActiveScheduler::Start();
sl@0
   678
	test(order[0]==eHigh && order[1]==eNone);
sl@0
   679
//
sl@0
   680
	test.Next(_L("Test high is not before none when it expires later"));
sl@0
   681
	myTimer::SetNum(2);
sl@0
   682
	pHigh->After(1000000);
sl@0
   683
	pNone->After(0);
sl@0
   684
	CActiveScheduler::Start();
sl@0
   685
	test(order[0]==eNone && order[1]==eHigh);
sl@0
   686
//
sl@0
   687
	test.Next(_L("Test highest is scheduled before high"));
sl@0
   688
	myTimer::SetNum(2);
sl@0
   689
	pHighest->After(0);
sl@0
   690
	pHigh->After(0);
sl@0
   691
	t.After(s, 100000);
sl@0
   692
	test(s==KRequestPending);
sl@0
   693
	User::WaitForRequest(s);
sl@0
   694
	CActiveScheduler::Start();
sl@0
   695
	test(order[0]==eHighest && order[1]==eHigh);
sl@0
   696
//
sl@0
   697
	test.Next(_L("Test highest is not before high when it expires later"));
sl@0
   698
	myTimer::SetNum(2);
sl@0
   699
	pHighest->After(1000000);
sl@0
   700
	pHigh->After(0);
sl@0
   701
	CActiveScheduler::Start();
sl@0
   702
	test(order[0]==eHigh && order[1]==eHighest);
sl@0
   703
//			  
sl@0
   704
	test.Next(_L("Test all objects are scheduled in priority order"));
sl@0
   705
	myTimer::SetNum(5);
sl@0
   706
	pLowest->After(0);
sl@0
   707
	pLow->After(0);
sl@0
   708
	pNone->After(0);
sl@0
   709
	pHigh->After(0);
sl@0
   710
	pHighest->After(0);
sl@0
   711
	t.After(s, 100000);
sl@0
   712
	test(s==KRequestPending);
sl@0
   713
	User::WaitForRequest(s);
sl@0
   714
	CActiveScheduler::Start();
sl@0
   715
	test(order[0]==eHighest && order[1]==eHigh && order[2]==eNone && order[3]==eLow && order[4]==eLowest);
sl@0
   716
//
sl@0
   717
	test.Next(_L("Test objects are scheduled according to expiry of timers"));
sl@0
   718
	myTimer::SetNum(5);
sl@0
   719
	pLowest->After(0);
sl@0
   720
	pLow->After(500000);
sl@0
   721
	pNone->After(1000000);
sl@0
   722
	pHigh->After(1500000);
sl@0
   723
	pHighest->After(2000000);
sl@0
   724
	CActiveScheduler::Start();
sl@0
   725
	test(order[4]==eHighest && order[3]==eHigh && order[2]==eNone && order[1]==eLow && order[0]==eLowest);
sl@0
   726
//
sl@0
   727
	test.Next(_L("Test with some objects having the same priority"));
sl@0
   728
	myTimer::SetNum(7);
sl@0
   729
	pLowest->After(0);
sl@0
   730
	pLow->After(0);
sl@0
   731
	pNone->After(0);
sl@0
   732
	pHigh->After(0);
sl@0
   733
	pHigh2->After(0);
sl@0
   734
	pHigh3->After(0);
sl@0
   735
	pHighest->After(0);	  
sl@0
   736
	t.After(s, 100000);
sl@0
   737
	test(s==KRequestPending);
sl@0
   738
	User::WaitForRequest(s);
sl@0
   739
	CActiveScheduler::Start();
sl@0
   740
	test(order[0]==eHighest && order[1]==eHigh && order[2]==eHigh+2 && order[3]==eHigh+3 && order[4]==eNone && order[5]==eLow && order[6]==eLowest);
sl@0
   741
//
sl@0
   742
	test.End();
sl@0
   743
	}
sl@0
   744
sl@0
   745
void TSecdulerTester::Test3()
sl@0
   746
//
sl@0
   747
// Test 3
sl@0
   748
//
sl@0
   749
	{
sl@0
   750
sl@0
   751
//
sl@0
   752
// Test that a leave in a Run method calls the requestmanager error() method
sl@0
   753
//
sl@0
   754
	myTimer* pTimer=new myTimer(0, KLeaveCode);
sl@0
   755
	pTimer->Start();
sl@0
   756
	pTimer->After(100000);
sl@0
   757
	TRAPD(ret,CActiveScheduler::Start());
sl@0
   758
	test(ret==KmyErrorNum);		
sl@0
   759
//
sl@0
   760
// Test cancelling an object
sl@0
   761
// Without the cancel the schedule order should be: pTimer1, gTimer, pTimer3
sl@0
   762
// gTimer is cancelled so should not be scheduled 
sl@0
   763
//
sl@0
   764
	myTimer::SetNum(2);
sl@0
   765
	myTimer* pTimer2=new myTimer(eHighest,KCancelCode);
sl@0
   766
	myTimer* pTimer3=new myTimer(eLowest,eLowest);
sl@0
   767
	pTimer2->Start();
sl@0
   768
	pTimer2->After(100000);
sl@0
   769
	gTimer->Start();
sl@0
   770
	gTimer->After(1000000);
sl@0
   771
	pTimer3->Start();
sl@0
   772
	pTimer3->After(2000000);
sl@0
   773
	CActiveScheduler::Start();
sl@0
   774
	test(order[0]==KCancelCode && order[1]==eLowest);
sl@0
   775
	delete pTimer;
sl@0
   776
	delete pTimer2;
sl@0
   777
	delete pTimer3;
sl@0
   778
	}
sl@0
   779
sl@0
   780
void TSecdulerTester::Test4()
sl@0
   781
//
sl@0
   782
// Create a thread with its own scheduler
sl@0
   783
//
sl@0
   784
	{	  
sl@0
   785
sl@0
   786
	threadSemaphore.CreateLocal(0);
sl@0
   787
	RThread myThread;
sl@0
   788
	test(myThread.Create(_L("myThread"),myThreadEntryPoint,KDefaultStackSize,KHeapSize,KHeapSize,NULL)==KErrNone);
sl@0
   789
	myThread.Resume();
sl@0
   790
	myThread.Close();
sl@0
   791
	Test2();
sl@0
   792
	threadSemaphore.Wait();
sl@0
   793
	User::After(100000);
sl@0
   794
	}
sl@0
   795
sl@0
   796
GLDEF_C TInt E32Main()
sl@0
   797
    {
sl@0
   798
sl@0
   799
	// don't want just in time debugging as we trap panics
sl@0
   800
	TBool justInTime=User::JustInTime(); 
sl@0
   801
	User::SetJustInTime(EFalse); 
sl@0
   802
sl@0
   803
	test.Title();
sl@0
   804
	__UHEAP_MARK;
sl@0
   805
//
sl@0
   806
	test.Start(_L("Test1"));
sl@0
   807
	TSecdulerTester sched;
sl@0
   808
	sched.Test1();
sl@0
   809
	MyManager* pManager=new MyManager;
sl@0
   810
	test(pManager!=NULL);
sl@0
   811
	CActiveScheduler::Install(pManager);
sl@0
   812
	test(CActiveScheduler::Current()==pManager);
sl@0
   813
	MyManager* pManager2=new MyManager;
sl@0
   814
	test(pManager2!=NULL);
sl@0
   815
	delete pManager2;
sl@0
   816
	test(CActiveScheduler::Current()==pManager);
sl@0
   817
	pLowest=new myTimer(eLowest,eLowest);
sl@0
   818
	test(pLowest!=NULL);
sl@0
   819
	pLow=new myTimer(eLow,eLow);
sl@0
   820
	test(pLow!=NULL);
sl@0
   821
	pNone=new myTimer(eNone,eNone);
sl@0
   822
	test(pNone!=NULL);
sl@0
   823
	pHigh=new myTimer(eHigh,eHigh);
sl@0
   824
	test(pHigh!=NULL);
sl@0
   825
	pHigh2=new myTimer(eHigh,eHigh+2);
sl@0
   826
	test(pHigh2!=NULL);
sl@0
   827
	pHigh3=new myTimer(eHigh,eHigh+3);
sl@0
   828
	test(pHigh3!=NULL);
sl@0
   829
	pHighest=new myTimer(eHighest,eHighest);
sl@0
   830
	test(pHighest!=NULL);
sl@0
   831
	pLowest->Start();
sl@0
   832
	pLow->Start();
sl@0
   833
	pNone->Start();
sl@0
   834
	pHigh->Start();
sl@0
   835
	pHigh2->Start();
sl@0
   836
	pHigh3->Start();
sl@0
   837
	pHighest->Start();
sl@0
   838
//
sl@0
   839
	test.Next(_L("Test2"));
sl@0
   840
	sched.Test2();	
sl@0
   841
	User::Check();
sl@0
   842
//
sl@0
   843
	test.Next(_L("Test3"));
sl@0
   844
	gTimer=new myTimer(eNone, KgTimerID);
sl@0
   845
	sched.Test3();
sl@0
   846
//
sl@0
   847
	test.Next(_L("Test4"));
sl@0
   848
	sched.Test4();
sl@0
   849
	delete gTimer;
sl@0
   850
	User::Check();
sl@0
   851
	delete pManager;
sl@0
   852
	delete pLowest;
sl@0
   853
	delete pLow;
sl@0
   854
	delete pNone;
sl@0
   855
	delete pHigh;
sl@0
   856
	delete pHigh2;
sl@0
   857
	delete pHigh3;
sl@0
   858
	delete pHighest;
sl@0
   859
//
sl@0
   860
	test.End();
sl@0
   861
	__UHEAP_MARKEND;
sl@0
   862
sl@0
   863
	User::SetJustInTime(justInTime);
sl@0
   864
	return(KErrNone);
sl@0
   865
    }