os/kernelhwsrv/kerneltest/e32test/active/t_asyc.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/active/t_asyc.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,163 @@
     1.4 +// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\active\t_asyc.cpp
    1.18 +// Overview:
    1.19 +// Exercise the priority mechanism of active objects whereby active 
    1.20 +// objects are run in the priority order.
    1.21 +// API Information:
    1.22 +// CAsyncOneShot, CActiveScheduler.
    1.23 +// Details:
    1.24 +// - Install active scheduler.
    1.25 +// - Create active objects of different priorities and verify their RunL 
    1.26 +// is called in the priority order.
    1.27 +// - Verify that a very low priority active object will not get the chance
    1.28 +// to run if a higher priority object keeps rescheduling itself or a 
    1.29 +// higher priority object stops the active scheduler.
    1.30 +// Platforms/Drives/Compatibility:
    1.31 +// All.
    1.32 +// Assumptions/Requirement/Pre-requisites:
    1.33 +// Failures and causes:
    1.34 +// Base Port information:
    1.35 +// 
    1.36 +//
    1.37 +
    1.38 +#include <e32test.h>
    1.39 +
    1.40 +enum {ETopPriority=1000,EMiddlePriority=900,ELatePriority=800};
    1.41 +
    1.42 +const TInt KIToldYouSo=666;
    1.43 +
    1.44 +class CMyActiveScheduler : public CActiveScheduler
    1.45 +	{
    1.46 +public:
    1.47 +	virtual void Error(TInt anError) const;
    1.48 +	};
    1.49 +
    1.50 +LOCAL_D RTest test(_L("T_ASYC"));
    1.51 +
    1.52 +void CMyActiveScheduler::Error(TInt anError) const
    1.53 +//
    1.54 +// Called if any Run() method leaves.
    1.55 +//
    1.56 +	{
    1.57 +
    1.58 +	test.Panic(anError,_L("CMyActiveScheduler::Error"));
    1.59 +	}
    1.60 +
    1.61 +class CMyMultiShot : public CAsyncOneShot
    1.62 +	{
    1.63 +public:
    1.64 +	static CMyMultiShot* NewL(TInt aPriority,const TDesC& aMessage,TInt aCount);
    1.65 +	void RunL();
    1.66 +	CMyMultiShot(TInt aPriority,const TDesC& aMessage,TInt aCount);
    1.67 +private:
    1.68 +	TInt iCountRemaining;
    1.69 +	TPtrC iMessage;
    1.70 +	};
    1.71 +
    1.72 +class CShouldNeverRun : public CAsyncOneShot
    1.73 +	{
    1.74 +public:
    1.75 +	void RunL();
    1.76 +	static CShouldNeverRun* NewL();
    1.77 +	CShouldNeverRun();
    1.78 +	};
    1.79 +
    1.80 +class CStopTheScheduler : public CAsyncOneShot
    1.81 +	{
    1.82 +public:
    1.83 +	CStopTheScheduler(TInt aPriority);
    1.84 +	static CStopTheScheduler* NewL(TInt aPriority);
    1.85 +	void RunL();
    1.86 +	};
    1.87 +
    1.88 +CMyMultiShot* CMyMultiShot::NewL(TInt aPriority,const TDesC& aMessage,TInt aCount)
    1.89 +	{
    1.90 +	return new(ELeave)CMyMultiShot(aPriority,aMessage,aCount);
    1.91 +	}
    1.92 +
    1.93 +CMyMultiShot::CMyMultiShot(TInt aPriority,const TDesC& aMessage,TInt aCount)
    1.94 +	:CAsyncOneShot(aPriority),iMessage(aMessage)
    1.95 +	{
    1.96 +	iCountRemaining=aCount;
    1.97 +	}
    1.98 +
    1.99 +void CMyMultiShot::RunL()
   1.100 +	{
   1.101 +	if (iCountRemaining--)
   1.102 +		{
   1.103 +		test.Printf(_L("%S,%d\n\r"),&iMessage,iCountRemaining);
   1.104 +		Call();
   1.105 +		}
   1.106 +	}
   1.107 +CShouldNeverRun* CShouldNeverRun::NewL()
   1.108 +	{
   1.109 +	return new(ELeave)CShouldNeverRun;
   1.110 +	}
   1.111 +
   1.112 +CShouldNeverRun::CShouldNeverRun()
   1.113 +	:CAsyncOneShot(KMinTInt)
   1.114 +	{
   1.115 +	}
   1.116 +
   1.117 +void CShouldNeverRun::RunL()
   1.118 +	{
   1.119 +	User::Panic(_L("CShouldNeverRun"),KIToldYouSo);
   1.120 +	}
   1.121 +
   1.122 +CStopTheScheduler* CStopTheScheduler::NewL(TInt aPriority)
   1.123 +	{
   1.124 +	return new(ELeave)CStopTheScheduler(aPriority);
   1.125 +	}
   1.126 +
   1.127 +CStopTheScheduler::CStopTheScheduler(TInt aPriority)
   1.128 +	:CAsyncOneShot(aPriority)
   1.129 +	{
   1.130 +	}
   1.131 +
   1.132 +void CStopTheScheduler::RunL()
   1.133 +	{
   1.134 +	CActiveScheduler::Stop();
   1.135 +	}
   1.136 +
   1.137 +GLDEF_C TInt E32Main()
   1.138 +//
   1.139 +// Test idle objects.
   1.140 +//
   1.141 +    {
   1.142 +	test.Title();
   1.143 +	test.Start(_L("Testing idle object cancellation"));
   1.144 +//
   1.145 +	CMyActiveScheduler* pR=new CMyActiveScheduler;
   1.146 +	test(pR!=NULL);
   1.147 +	CActiveScheduler::Install(pR);
   1.148 +//
   1.149 +	CShouldNeverRun* pn=CShouldNeverRun::NewL();
   1.150 +	pn->Call();
   1.151 +
   1.152 +	CStopTheScheduler* ps=CStopTheScheduler::NewL(ELatePriority);
   1.153 +	ps->Call();
   1.154 +
   1.155 +	CMyMultiShot* multiShot1=CMyMultiShot::NewL(EMiddlePriority,_L("Call Ten times"),10);
   1.156 +	multiShot1->Call();
   1.157 +
   1.158 +	CMyMultiShot* multiShot2=CMyMultiShot::NewL(ETopPriority,_L("Call five times"),5);
   1.159 +	multiShot2->Call();
   1.160 +
   1.161 +	CActiveScheduler::Start();	
   1.162 +//
   1.163 +	test.End();
   1.164 +	return(0);
   1.165 +    }
   1.166 +