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