os/persistentdata/persistentstorage/sql/SRC/Client/SqlResourceTest.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/persistentdata/persistentstorage/sql/SRC/Client/SqlResourceTest.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,280 @@
     1.4 +// Copyright (c) 2006-2010 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 "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 +//
    1.18 +
    1.19 +#include <sqldb.h>				//TSqlResourceTester
    1.20 +#include "SqlResourceTest.h"	//TSqlResourceTestData
    1.21 +#include "SqlAssert.h"			//ESqlPanicInternalError
    1.22 +#include "SqlDbSession.h"		//RSqlDbSession
    1.23 +#include "SqlResourceTester.h"		//TSqlResourceTester
    1.24 +
    1.25 +///////////////////////////////////////////////////////////////////////////////////////////////////////
    1.26 +////////////////////////             TSqlResourceTestData                 /////////////////////////////
    1.27 +///////////////////////////////////////////////////////////////////////////////////////////////////////
    1.28 +
    1.29 +#pragma BullseyeCoverage off
    1.30 +
    1.31 +#ifdef _DEBUG
    1.32 +
    1.33 +/**
    1.34 +Ensures that TSqlResourceTestData singleton is created and returns a pointer to the object.
    1.35 +
    1.36 +The function has a meaningfull implementation only in _DEBUG mode.
    1.37 +
    1.38 +@return A pointer to TSqlResourceTestData singleton. The return result might be NULL.
    1.39 +*/
    1.40 +TSqlResourceTestData* TSqlResourceTestData::Instance()
    1.41 +	{
    1.42 +	TSqlResourceTestData* instance = static_cast <TSqlResourceTestData*> (Dll::Tls());
    1.43 +	if(!instance)
    1.44 +		{
    1.45 +		instance = new TSqlResourceTestData;
    1.46 +		if(instance)
    1.47 +			{
    1.48 +			if(Dll::SetTls(instance) != KErrNone)
    1.49 +				{
    1.50 +				delete instance;	
    1.51 +				instance = NULL;
    1.52 +				}
    1.53 +			}
    1.54 +		}
    1.55 +	return instance;
    1.56 +	}
    1.57 +
    1.58 +/**
    1.59 +Destroys TSqlResourceTestData singleton.
    1.60 +
    1.61 +The function has a meaningfull implementation only in _DEBUG mode.
    1.62 +*/
    1.63 +void TSqlResourceTestData::Release()
    1.64 +	{
    1.65 +	TSqlResourceTestData* instance = static_cast <TSqlResourceTestData*> (Dll::Tls());
    1.66 +	delete instance;
    1.67 +	(void)Dll::SetTls(NULL);
    1.68 +	}
    1.69 +		
    1.70 +/**
    1.71 +Initializes TSqlResourceTestData singleton with a reference to RSqlDbSession instance.
    1.72 +If a test data has been previously set by calling TSqlResourceTestData::Set(), then
    1.73 +the test data will be sent now to the SQL server.
    1.74 +
    1.75 +The function has a meaningfull implementation only in _DEBUG mode.
    1.76 +
    1.77 +@param aDbSession A reference to RDbSession instance.
    1.78 +
    1.79 +@return KErrNone The call completed successfully, system-wide error code otherwise.
    1.80 +*/
    1.81 +TInt TSqlResourceTestData::Init(RSqlDbSession& aDbSession)
    1.82 +	{
    1.83 +	iDbSession = &aDbSession;
    1.84 +	TInt rc = KErrNone;
    1.85 +	if(iRqPending)
    1.86 +		{
    1.87 +		rc = iDbSession->SendReceive(iFunction, TIpcArgs(iAllocFailType, iRate));
    1.88 +		}
    1.89 +	iRqPending = EFalse;
    1.90 +	return rc;
    1.91 +	}
    1.92 +	
    1.93 +/**
    1.94 +If the TSqlResourceTestData singleton is already initialized (TSqlResourceTestData::Init() call),
    1.95 +then the test command and data will be sent directly to the SQL server. Othwerwise the test command
    1.96 +and data will be stored and sent later when the TSqlResourceTestData singleton gets initialized.
    1.97 +
    1.98 +The function has a meaningfull implementation only in _DEBUG mode.
    1.99 +
   1.100 +@param aFunction Test command to be sent to the SQL server
   1.101 +@param aAllocFailType Heap failure allocation type
   1.102 +@param arate Heap failure rate
   1.103 +
   1.104 +@return KErrNone The call completed successfully, system-wide error code otherwise.
   1.105 +*/
   1.106 +TInt TSqlResourceTestData::Set(TSqlSrvFunction aFunction, TInt aAllocFailType, TInt aRate)
   1.107 +	{
   1.108 +	__ASSERT_DEBUG(!iRqPending, __SQLPANIC(ESqlPanicMisuse));
   1.109 +	if(iDbSession)	
   1.110 +		{
   1.111 +		return iDbSession->SendReceive(aFunction, TIpcArgs(aAllocFailType, aRate));
   1.112 +		}
   1.113 +	else
   1.114 +		{
   1.115 +		iFunction = aFunction;
   1.116 +		iAllocFailType = aAllocFailType;
   1.117 +		iRate = aRate;
   1.118 +		iRqPending = ETrue;
   1.119 +		return KErrNone;
   1.120 +		}
   1.121 +	}
   1.122 +
   1.123 +/**
   1.124 +The function has a meaningfull implementation only in _DEBUG mode.
   1.125 +*/
   1.126 +TSqlResourceTestData::TSqlResourceTestData() :
   1.127 +	iRqPending(EFalse),
   1.128 +	iDbSession(NULL),
   1.129 +	iFunction(ESqlSrvTestBase),
   1.130 +	iAllocFailType(RHeap::ENone),
   1.131 +	iRate(0)
   1.132 +	{
   1.133 +	}
   1.134 +
   1.135 +///////////////////////////////////////////////////////////////////////////////////////////////////////
   1.136 +////////////////////////             TSqlResourceTester                 ///////////////////////////////
   1.137 +///////////////////////////////////////////////////////////////////////////////////////////////////////
   1.138 +
   1.139 +//Sends a test command to the SQL server.
   1.140 +//aFunction parameter is the test command code to be sent, 
   1.141 +//aAllocFailType is the heap failure type, aRate is the heap failure rate.
   1.142 +//
   1.143 +//The function will get a pointer to the TSqlResourceTestData instance and call its Set() method to
   1.144 +//send the test command and data.
   1.145 +static TInt SendCommand(TSqlSrvFunction aFunction, TInt aAllocFailType, TInt aRate)
   1.146 +	{
   1.147 +	TInt rc = KErrNoMemory;
   1.148 +	TSqlResourceTestData* instance = TSqlResourceTestData::Instance();
   1.149 +	if(instance)
   1.150 +		{
   1.151 +		rc = instance->Set(aFunction, aAllocFailType, aRate);
   1.152 +		}
   1.153 +	return rc;
   1.154 +	}
   1.155 +
   1.156 +//Sends a test command to the SQL server.
   1.157 +//aFunction parameter is the test command code to be sent, 
   1.158 +//
   1.159 +//The function will get a pointer to the TSqlResourceTestData instance and call its Set() method to
   1.160 +//send the test command and data.
   1.161 +static TInt SendCommand(TSqlSrvFunction aFunction)
   1.162 +	{
   1.163 +	return SendCommand(aFunction, RHeap::ENone, 0);
   1.164 +	}
   1.165 +
   1.166 +/**
   1.167 +Sends a request to the SQL server to mark the allocated resources.
   1.168 +
   1.169 +The function has a meaningfull implementation only in _DEBUG mode.
   1.170 +*/
   1.171 +EXPORT_C void TSqlResourceTester::Mark()
   1.172 +	{
   1.173 +	(void)::SendCommand(ESqlSrvResourceMark);
   1.174 +	}
   1.175 +	
   1.176 +/**
   1.177 +Sends a request to the SQL server to check the allocated resources.
   1.178 +(to compare their count with the marked resource count, made by the 
   1.179 + TSqlResourceTester::Mark() call)
   1.180 +
   1.181 +The function has a meaningfull implementation only in _DEBUG mode.
   1.182 +*/
   1.183 +EXPORT_C void TSqlResourceTester::Check()
   1.184 +	{
   1.185 +	(void)::SendCommand(ESqlSrvResourceCheck);
   1.186 +	}
   1.187 +	
   1.188 +/**
   1.189 +@return Count of the allocated SQL server resources.
   1.190 +
   1.191 +The function has a meaningfull implementation only in _DEBUG mode.
   1.192 +*/
   1.193 +EXPORT_C TInt TSqlResourceTester::Count()
   1.194 +	{
   1.195 +	return ::SendCommand(ESqlSrvResourceCount);
   1.196 +	}
   1.197 +
   1.198 +/**
   1.199 +Sends a request to the SQL server to simulate out of memory failure.
   1.200 +This call can be used to test the server side of RSqlDatabase class.
   1.201 +
   1.202 +The function has a meaningfull implementation only in _DEBUG mode.
   1.203 +
   1.204 +@param aAllocFailType Heap failure allocation type
   1.205 +	   	If bit 12 of aAllocFailType is set, then the SQL server will delay
   1.206 +	   	the heap failure simulation until the database is opened.
   1.207 +@param arate Heap failure rate
   1.208 +*/
   1.209 +EXPORT_C void TSqlResourceTester::SetDbHeapFailure(TInt aAllocFailType, TInt aRate)
   1.210 +	{
   1.211 +	if(static_cast <RHeap::TAllocFail> (aAllocFailType) == RHeap::ENone)
   1.212 +		{
   1.213 +		//This is a command to reset the memory allocation failure simulation.
   1.214 +		//Execute it only if there is a valid TSqlResourceTestData instance.
   1.215 +		//Otherwise this function will try to allocate memory for TSqlResourceTestData instance
   1.216 +		//and this happens at the moment when the request is for stopping the simulation (so the OOM test).
   1.217 +		//The test application will find one more memory cell is allocated and will fail.
   1.218 +		TSqlResourceTestData* instance = static_cast <TSqlResourceTestData*> (Dll::Tls());
   1.219 +		if(!instance)
   1.220 +			{
   1.221 +			return;	
   1.222 +			}
   1.223 +		}
   1.224 +	(void)::SendCommand(ESqlSrvSetDbHeapFailure, aAllocFailType, aRate);
   1.225 +	}
   1.226 +	
   1.227 +/**
   1.228 +Sends a request to the SQL server to simulate out of memory failure.
   1.229 +This call can be used to test the server side of RSqlStatement class.
   1.230 +
   1.231 +The function has a meaningfull implementation only in _DEBUG mode.
   1.232 +
   1.233 +@param aAllocFailType Heap failure allocation type
   1.234 +@param arate Heap failure rate
   1.235 +*/
   1.236 +EXPORT_C void TSqlResourceTester::SetHeapFailure(TInt aAllocFailType, TInt aRate)
   1.237 +	{
   1.238 +	if(static_cast <RHeap::TAllocFail> (aAllocFailType) == RHeap::ENone)
   1.239 +		{
   1.240 +		//This is a command to reset the memory allocation failure simulation.
   1.241 +		//Execute it only if there is a valid TSqlResourceTestData instance.
   1.242 +		//Otherwise this function will try to allocate memory for TSqlResourceTestData instance
   1.243 +		//and this happens at the moment when the request is for stopping the simulation (so the OOM test).
   1.244 +		//The test application will find one more memory cell is allocated and will fail.
   1.245 +		TSqlResourceTestData* instance = static_cast <TSqlResourceTestData*> (Dll::Tls());
   1.246 +		if(!instance)
   1.247 +			{
   1.248 +			return;	
   1.249 +			}
   1.250 +		}
   1.251 +	(void)::SendCommand(ESqlSrvSetHeapFailure, aAllocFailType, aRate);
   1.252 +	}
   1.253 +
   1.254 +#else //_DEBUG
   1.255 +
   1.256 +void TSqlResourceTestData::Release()
   1.257 +	{
   1.258 +	}
   1.259 +
   1.260 +EXPORT_C void TSqlResourceTester::Mark()
   1.261 +	{
   1.262 +	}
   1.263 +
   1.264 +EXPORT_C void TSqlResourceTester::Check()
   1.265 +	{
   1.266 +	}
   1.267 +
   1.268 +EXPORT_C TInt TSqlResourceTester::Count()
   1.269 +	{
   1.270 +	return 0;
   1.271 +	}
   1.272 +
   1.273 +EXPORT_C void TSqlResourceTester::SetDbHeapFailure(TInt, TInt)
   1.274 +	{
   1.275 +	}
   1.276 +
   1.277 +EXPORT_C void TSqlResourceTester::SetHeapFailure(TInt, TInt)
   1.278 +	{
   1.279 +	}
   1.280 +
   1.281 +#endif//_DEBUG
   1.282 +
   1.283 +#pragma BullseyeCoverage on