commsfwutils/commsbufs/version1/mbufmgr/TS_mbufmgr/Test15Concurrency.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2002-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 "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 // Contains IPC Test 15
       
    15 // 
       
    16 //
       
    17 
       
    18 // EPOC includes
       
    19 #include <e32base.h>
       
    20 #include <ss_std.h>
       
    21 // Test system includes
       
    22 #include <networking/log.h>
       
    23 #include <networking/teststep.h>
       
    24 #include "TestStepCTMbufmgr.h"
       
    25 #include "TestSuiteCTMbufmgr.h"
       
    26 
       
    27 #include "Test15Concurrency.h"
       
    28 
       
    29 // constructor
       
    30 CTest15Concurrency::CTest15Concurrency()
       
    31 	{
       
    32 	iTestStepName = _L("MBufMgrTest15");// Store the name of this test case
       
    33 	iMainThreadTries = 0;
       
    34 	iMainThreadFails = 0;
       
    35 	iHighThreadTries = 0;
       
    36 	iHighThreadFails = 0;
       
    37 	}
       
    38 
       
    39 // destructor
       
    40 CTest15Concurrency::~CTest15Concurrency()
       
    41 	{
       
    42 	}
       
    43 
       
    44 //
       
    45 TInt CTest15Concurrency::fHighPriorityThread(TAny* aInput)
       
    46 	{
       
    47 	CTest15Concurrency* pTestObject = (CTest15Concurrency*)aInput;
       
    48 	// We need to introduce this new client thread to the MBufMgr
       
    49 	pTestObject->iMBMngr->SetContext();
       
    50 	
       
    51 	CTrapCleanup* aCleanup = CTrapCleanup::New();
       
    52 
       
    53 	//Install active scheduler
       
    54     CActiveScheduler* aActSch = new CActiveScheduler;
       
    55 	if(aActSch==NULL)
       
    56 		{
       
    57 		return KErrNoMemory;
       
    58 		}
       
    59 	CActiveScheduler::Install(aActSch);
       
    60 
       
    61 	RTimer aTimer;
       
    62 	TRequestStatus aTimerStatus;	// Request status associated with timer
       
    63 	aTimer.CreateLocal();			// Create timer for this thread
       
    64 	//-------------- substep 1 --------------------
       
    65 
       
    66 	// Allocate two 500-bytes long descriptors (Des1 & Des2):"));
       
    67 	TBuf8<500> *aDes1 = NULL;
       
    68 	TBuf8<500> *aDes2 = NULL;
       
    69 
       
    70     TRAPD(ret, aDes1 = new(ELeave) TBuf8<500>);
       
    71 	if(ret!=KErrNone)
       
    72 		{
       
    73 		return ret;
       
    74 		}
       
    75 
       
    76     TRAP(ret, aDes2 = new(ELeave) TBuf8<500>);
       
    77 	if(ret!=KErrNone)
       
    78 		{
       
    79 		delete aDes1;
       
    80 		return ret;
       
    81 		}
       
    82 
       
    83 	aDes1->SetLength(500);
       
    84 	aDes2->SetLength(500);
       
    85 
       
    86 	//Fill in the Des1 with a pattern
       
    87 	pTestObject->StripeDes(*aDes1, 0, 500, 'a', 'z');
       
    88 
       
    89 	#ifdef __CFLOG_ACTIVE
       
    90 	__CFLOG_DECLARATION_VARIABLE;
       
    91 	TRAP(ret, __CFLOG_CREATEL);
       
    92 	if(ret!=KErrNone)
       
    93 		{
       
    94 		delete aDes1;
       
    95 		delete aDes2;
       
    96 		return ret;
       
    97 		}
       
    98 	__CFLOG_OPEN;
       
    99 #endif
       
   100 	
       
   101 	for (TInt i = 0 ; i<1000;i++)
       
   102 		{
       
   103 		//Allocate 500-bytes long RMBufChain
       
   104 		RMBufChain aChain;
       
   105 		TRAP(ret,aChain.AllocL(500));
       
   106 		if (ret != KErrNone)
       
   107 			{
       
   108 			break;
       
   109 			}
       
   110 		//Copy in Des1 into Chain
       
   111 		aChain.CopyIn(*aDes1);
       
   112 		//Fill in Des2 with zeros
       
   113 		pTestObject->StripeDes(*aDes2, 0, 500, 0, 0);
       
   114 		//Copy out Chain into Des2;
       
   115 		aChain.CopyOut(*aDes2);
       
   116 		//Compare the contents of Des1 & Des2
       
   117 		if(aDes1->Compare(*aDes2))
       
   118 			pTestObject->iHighThreadFails++;
       
   119 		pTestObject->iHighThreadTries++;
       
   120 		//Free chain
       
   121 		aChain.Free();
       
   122 		//Sleep for 5ms
       
   123 		aTimer.After(aTimerStatus,5000);
       
   124 		User::WaitForRequest(aTimerStatus);
       
   125 		}
       
   126 	delete aDes1;
       
   127 	delete aDes2;
       
   128 	CActiveScheduler::Install(NULL);
       
   129 	delete aActSch;
       
   130 	delete aCleanup;
       
   131 
       
   132 #ifdef __CFLOG_ACTIVE
       
   133 	__CFLOG_CLOSE;
       
   134 	__CFLOG_DELETE;
       
   135 #endif
       
   136 	return ret;
       
   137 }
       
   138 
       
   139 
       
   140 
       
   141 enum TVerdict CTest15Concurrency::doTestStepL(void)
       
   142 	{
       
   143 	__UHEAP_MARK;
       
   144 	
       
   145 #ifdef __CFLOG_ACTIVE
       
   146 	__CFLOG_CREATEL;
       
   147 	__CFLOG_OPEN;
       
   148 #endif
       
   149 	
       
   150 	volatile TExitType aExit;
       
   151 
       
   152 	//-------------- substep 1 --------------------
       
   153 	Log(_L("  01 Create CMBufManager and install active scheduler:"));
       
   154     CleanupStack::PushL( iActSch = new(ELeave) CActiveScheduler );
       
   155 	CActiveScheduler::Install(iActSch);
       
   156 	CleanupStack::PushL(CreateInstanceMBufMgrL(KMBufDefaultHeapSize));
       
   157 
       
   158 	//-------------------substep 01-----------------------------
       
   159 	Log(_L("  02 Create high priority thread and resume it:"));
       
   160 	RThread aThread;
       
   161 	
       
   162 	TInt err=aThread.Create(_L("testThreadRec"),
       
   163 		                       fHighPriorityThread,
       
   164  							   KDefaultStackSize,
       
   165 							   KDefaultHeapSize,
       
   166 							   KMaxHeapSize,
       
   167 							   (TAny*)this,
       
   168 							   EOwnerProcess);
       
   169 	if (err!=KErrNone)
       
   170 		{
       
   171 		_LIT(aLog,"Error:Could not create the thread. err = %d"); Log(aLog,err);
       
   172 
       
   173 #ifdef __CFLOG_ACTIVE
       
   174 	__CFLOG_CLOSE;
       
   175 	__CFLOG_DELETE;
       
   176 #endif
       
   177 	
       
   178 		User::Leave(EFail);
       
   179 		}
       
   180 	aThread.SetPriority(EPriorityAbsoluteHigh);
       
   181 	aThread.Resume();
       
   182 
       
   183 	//-------------------substep 03-----------------------------
       
   184 	Log(_L("  02 Do tests in loop end stop when the other finishes"));
       
   185 	Log(_L("     ...It will take a while (about 10 sec.):"));
       
   186 
       
   187 	// Allocate two 5000-bytes long descriptors (Des1 & Des2):"));
       
   188 	TBuf8<5000> *aDes1, *aDes2;
       
   189     CleanupStack::PushL( aDes1 = new(ELeave) TBuf8<5000> );
       
   190     CleanupStack::PushL( aDes2 = new(ELeave) TBuf8<5000> );
       
   191 	aDes1->SetLength(5000);
       
   192 	aDes2->SetLength(5000);
       
   193 	//Fill in the Des1 with a pattern:"));
       
   194 	StripeDes(*aDes1, 0, 5000, '@', 'Z');
       
   195 
       
   196 	do {
       
   197 		//Allocate 5000-bytes long RMBufChain
       
   198 		RMBufChain aChain;
       
   199 		TRAPD(ret,aChain.AllocL(5000));
       
   200 		if (ret != KErrNone)
       
   201 			{
       
   202 			Log(_L("Error: Couldn't allocate RMBuf:"));
       
   203 
       
   204 #ifdef __CFLOG_ACTIVE
       
   205 	__CFLOG_CLOSE;
       
   206 	__CFLOG_DELETE;
       
   207 #endif
       
   208 			User::Leave(EFail);
       
   209 			}
       
   210 		//Copy in Des1 into Chain
       
   211 		aChain.CopyIn(*aDes1);
       
   212 		//Fill in Des2 with zeros:"));
       
   213 		StripeDes(*aDes2, 0, 5000, 0, 0);
       
   214 		//Copy out Chain into Des2:"));
       
   215 		aChain.CopyOut(*aDes2);
       
   216 		//Compare the contents of Des1 & Des2:
       
   217 		if(aDes1->Compare(*aDes2))	iMainThreadFails++;
       
   218 		iMainThreadTries++;
       
   219 		//Free chain
       
   220 		aChain.Free();
       
   221 
       
   222 		//Check whether the other task has finished
       
   223 		aExit = aThread.ExitType();
       
   224 		}
       
   225 	while (aExit == EExitPending);
       
   226 
       
   227 	//Clean up stack
       
   228 	CleanupStack::PopAndDestroy(aDes2);
       
   229     CleanupStack::PopAndDestroy(aDes1);
       
   230     CleanupStack::PopAndDestroy(iMBMngr);
       
   231 	CActiveScheduler::Install(NULL);
       
   232 	CleanupStack::PopAndDestroy(iActSch);
       
   233 
       
   234 #ifdef __CFLOG_ACTIVE
       
   235 	__CFLOG_CLOSE;
       
   236 	__CFLOG_DELETE;
       
   237 #endif
       
   238 
       
   239 	//-------------------substep 02-----------------------------
       
   240 	Log(_L("     Info: Number of main thread checkings: %d with %d fails"),iMainThreadTries,iMainThreadFails);
       
   241 	Log(_L("     Info: Number of high priority thread checkings: %d with %d fails"),iHighThreadTries,iHighThreadFails);
       
   242 	aThread.Close();
       
   243 	if (iHighThreadTries <1000)
       
   244 		{
       
   245 		Log(_L("   Error: High priority thread ended before 1000 loops are finished"));
       
   246 		return EFail;
       
   247 		}
       
   248 	if ( iHighThreadFails || iMainThreadFails)
       
   249 		return EFail;
       
   250 
       
   251 	__UHEAP_MARKEND;
       
   252 	return EPass;
       
   253   }