persistentstorage/sql/SRC/Client/SqlResourceTest.cpp
changeset 0 08ec8eefde2f
child 11 211563e4b919
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 // Copyright (c) 2006-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 //
       
    15 
       
    16 #include <sqldb.h>				//TSqlResourceTester
       
    17 #include "SqlResourceTest.h"	//TSqlResourceTestData
       
    18 #include "SqlPanic.h"			//__SQLASSERT, ESqlPanicInternalError
       
    19 #include "SqlDbSession.h"		//RSqlDbSession
       
    20 #include "SqlResourceTester.h"		//TSqlResourceTester
       
    21 
       
    22 ///////////////////////////////////////////////////////////////////////////////////////////////////////
       
    23 ////////////////////////             TSqlResourceTestData                 /////////////////////////////
       
    24 ///////////////////////////////////////////////////////////////////////////////////////////////////////
       
    25 
       
    26 #ifdef _DEBUG
       
    27 
       
    28 /**
       
    29 Ensures that TSqlResourceTestData singleton is created and returns a pointer to the object.
       
    30 
       
    31 The function has a meaningfull implementation only in _DEBUG mode.
       
    32 
       
    33 @return A pointer to TSqlResourceTestData singleton. The return result might be NULL.
       
    34 */
       
    35 TSqlResourceTestData* TSqlResourceTestData::Instance()
       
    36 	{
       
    37 	TSqlResourceTestData* instance = static_cast <TSqlResourceTestData*> (Dll::Tls());
       
    38 	if(!instance)
       
    39 		{
       
    40 		instance = new TSqlResourceTestData;
       
    41 		if(instance)
       
    42 			{
       
    43 			if(Dll::SetTls(instance) != KErrNone)
       
    44 				{
       
    45 				delete instance;	
       
    46 				instance = NULL;
       
    47 				}
       
    48 			}
       
    49 		}
       
    50 	return instance;
       
    51 	}
       
    52 
       
    53 /**
       
    54 Destroys TSqlResourceTestData singleton.
       
    55 
       
    56 The function has a meaningfull implementation only in _DEBUG mode.
       
    57 */
       
    58 void TSqlResourceTestData::Release()
       
    59 	{
       
    60 	TSqlResourceTestData* instance = static_cast <TSqlResourceTestData*> (Dll::Tls());
       
    61 	delete instance;
       
    62 	(void)Dll::SetTls(NULL);
       
    63 	}
       
    64 		
       
    65 /**
       
    66 Initializes TSqlResourceTestData singleton with a reference to RSqlDbSession instance.
       
    67 If a test data has been previously set by calling TSqlResourceTestData::Set(), then
       
    68 the test data will be sent now to the SQL server.
       
    69 
       
    70 The function has a meaningfull implementation only in _DEBUG mode.
       
    71 
       
    72 @param aDbSession A reference to RDbSession instance.
       
    73 
       
    74 @return KErrNone The call completed successfully, system-wide error code otherwise.
       
    75 */
       
    76 TInt TSqlResourceTestData::Init(RSqlDbSession& aDbSession)
       
    77 	{
       
    78 	iDbSession = &aDbSession;
       
    79 	TInt rc = KErrNone;
       
    80 	if(iRqPending)
       
    81 		{
       
    82 		rc = iDbSession->SendReceive(iFunction, TIpcArgs(iAllocFailType, iRate));
       
    83 		}
       
    84 	iRqPending = EFalse;
       
    85 	return rc;
       
    86 	}
       
    87 	
       
    88 /**
       
    89 If the TSqlResourceTestData singleton is already initialized (TSqlResourceTestData::Init() call),
       
    90 then the test command and data will be sent directly to the SQL server. Othwerwise the test command
       
    91 and data will be stored and sent later when the TSqlResourceTestData singleton gets initialized.
       
    92 
       
    93 The function has a meaningfull implementation only in _DEBUG mode.
       
    94 
       
    95 @param aFunction Test command to be sent to the SQL server
       
    96 @param aAllocFailType Heap failure allocation type
       
    97 @param arate Heap failure rate
       
    98 
       
    99 @return KErrNone The call completed successfully, system-wide error code otherwise.
       
   100 */
       
   101 TInt TSqlResourceTestData::Set(TSqlSrvFunction aFunction, TInt aAllocFailType, TInt aRate)
       
   102 	{
       
   103 	__SQLASSERT(!iRqPending, ESqlPanicMisuse);
       
   104 	if(iDbSession)	
       
   105 		{
       
   106 		return iDbSession->SendReceive(aFunction, TIpcArgs(aAllocFailType, aRate));
       
   107 		}
       
   108 	else
       
   109 		{
       
   110 		iFunction = aFunction;
       
   111 		iAllocFailType = aAllocFailType;
       
   112 		iRate = aRate;
       
   113 		iRqPending = ETrue;
       
   114 		return KErrNone;
       
   115 		}
       
   116 	}
       
   117 
       
   118 /**
       
   119 The function has a meaningfull implementation only in _DEBUG mode.
       
   120 */
       
   121 TSqlResourceTestData::TSqlResourceTestData() :
       
   122 	iRqPending(EFalse),
       
   123 	iDbSession(NULL),
       
   124 	iFunction(ESqlSrvTestBase),
       
   125 	iAllocFailType(RHeap::ENone),
       
   126 	iRate(0)
       
   127 	{
       
   128 	}
       
   129 
       
   130 ///////////////////////////////////////////////////////////////////////////////////////////////////////
       
   131 ////////////////////////             TSqlResourceTester                 ///////////////////////////////
       
   132 ///////////////////////////////////////////////////////////////////////////////////////////////////////
       
   133 
       
   134 //Sends a test command to the SQL server.
       
   135 //aFunction parameter is the test command code to be sent, 
       
   136 //aAllocFailType is the heap failure type, aRate is the heap failure rate.
       
   137 //
       
   138 //The function will get a pointer to the TSqlResourceTestData instance and call its Set() method to
       
   139 //send the test command and data.
       
   140 static TInt SendCommand(TSqlSrvFunction aFunction, TInt aAllocFailType, TInt aRate)
       
   141 	{
       
   142 	TInt rc = KErrNoMemory;
       
   143 	TSqlResourceTestData* instance = TSqlResourceTestData::Instance();
       
   144 	if(instance)
       
   145 		{
       
   146 		rc = instance->Set(aFunction, aAllocFailType, aRate);
       
   147 		}
       
   148 	return rc;
       
   149 	}
       
   150 
       
   151 //Sends a test command to the SQL server.
       
   152 //aFunction parameter is the test command code to be sent, 
       
   153 //
       
   154 //The function will get a pointer to the TSqlResourceTestData instance and call its Set() method to
       
   155 //send the test command and data.
       
   156 static TInt SendCommand(TSqlSrvFunction aFunction)
       
   157 	{
       
   158 	return SendCommand(aFunction, RHeap::ENone, 0);
       
   159 	}
       
   160 
       
   161 /**
       
   162 Sends a request to the SQL server to mark the allocated resources.
       
   163 
       
   164 The function has a meaningfull implementation only in _DEBUG mode.
       
   165 */
       
   166 EXPORT_C void TSqlResourceTester::Mark()
       
   167 	{
       
   168 	(void)::SendCommand(ESqlSrvResourceMark);
       
   169 	}
       
   170 	
       
   171 /**
       
   172 Sends a request to the SQL server to check the allocated resources.
       
   173 (to compare their count with the marked resource count, made by the 
       
   174  TSqlResourceTester::Mark() call)
       
   175 
       
   176 The function has a meaningfull implementation only in _DEBUG mode.
       
   177 */
       
   178 EXPORT_C void TSqlResourceTester::Check()
       
   179 	{
       
   180 	(void)::SendCommand(ESqlSrvResourceCheck);
       
   181 	}
       
   182 	
       
   183 /**
       
   184 @return Count of the allocated SQL server resources.
       
   185 
       
   186 The function has a meaningfull implementation only in _DEBUG mode.
       
   187 */
       
   188 EXPORT_C TInt TSqlResourceTester::Count()
       
   189 	{
       
   190 	return ::SendCommand(ESqlSrvResourceCount);
       
   191 	}
       
   192 
       
   193 /**
       
   194 Sends a request to the SQL server to simulate out of memory failure.
       
   195 This call can be used to test the server side of RSqlDatabase class.
       
   196 
       
   197 The function has a meaningfull implementation only in _DEBUG mode.
       
   198 
       
   199 @param aAllocFailType Heap failure allocation type
       
   200 	   	If bit 12 of aAllocFailType is set, then the SQL server will delay
       
   201 	   	the heap failure simulation until the database is opened.
       
   202 @param arate Heap failure rate
       
   203 */
       
   204 EXPORT_C void TSqlResourceTester::SetDbHeapFailure(TInt aAllocFailType, TInt aRate)
       
   205 	{
       
   206 	if(static_cast <RHeap::TAllocFail> (aAllocFailType) == RHeap::ENone)
       
   207 		{
       
   208 		//This is a command to reset the memory allocation failure simulation.
       
   209 		//Execute it only if there is a valid TSqlResourceTestData instance.
       
   210 		//Otherwise this function will try to allocate memory for TSqlResourceTestData instance
       
   211 		//and this happens at the moment when the request is for stopping the simulation (so the OOM test).
       
   212 		//The test application will find one more memory cell is allocated and will fail.
       
   213 		TSqlResourceTestData* instance = static_cast <TSqlResourceTestData*> (Dll::Tls());
       
   214 		if(!instance)
       
   215 			{
       
   216 			return;	
       
   217 			}
       
   218 		}
       
   219 	(void)::SendCommand(ESqlSrvSetDbHeapFailure, aAllocFailType, aRate);
       
   220 	}
       
   221 	
       
   222 /**
       
   223 Sends a request to the SQL server to simulate out of memory failure.
       
   224 This call can be used to test the server side of RSqlStatement class.
       
   225 
       
   226 The function has a meaningfull implementation only in _DEBUG mode.
       
   227 
       
   228 @param aAllocFailType Heap failure allocation type
       
   229 @param arate Heap failure rate
       
   230 */
       
   231 EXPORT_C void TSqlResourceTester::SetHeapFailure(TInt aAllocFailType, TInt aRate)
       
   232 	{
       
   233 	if(static_cast <RHeap::TAllocFail> (aAllocFailType) == RHeap::ENone)
       
   234 		{
       
   235 		//This is a command to reset the memory allocation failure simulation.
       
   236 		//Execute it only if there is a valid TSqlResourceTestData instance.
       
   237 		//Otherwise this function will try to allocate memory for TSqlResourceTestData instance
       
   238 		//and this happens at the moment when the request is for stopping the simulation (so the OOM test).
       
   239 		//The test application will find one more memory cell is allocated and will fail.
       
   240 		TSqlResourceTestData* instance = static_cast <TSqlResourceTestData*> (Dll::Tls());
       
   241 		if(!instance)
       
   242 			{
       
   243 			return;	
       
   244 			}
       
   245 		}
       
   246 	(void)::SendCommand(ESqlSrvSetHeapFailure, aAllocFailType, aRate);
       
   247 	}
       
   248 
       
   249 #else //_DEBUG
       
   250 
       
   251 void TSqlResourceTestData::Release()
       
   252 	{
       
   253 	}
       
   254 
       
   255 EXPORT_C void TSqlResourceTester::Mark()
       
   256 	{
       
   257 	}
       
   258 
       
   259 EXPORT_C void TSqlResourceTester::Check()
       
   260 	{
       
   261 	}
       
   262 
       
   263 EXPORT_C TInt TSqlResourceTester::Count()
       
   264 	{
       
   265 	return 0;
       
   266 	}
       
   267 
       
   268 EXPORT_C void TSqlResourceTester::SetDbHeapFailure(TInt, TInt)
       
   269 	{
       
   270 	}
       
   271 
       
   272 EXPORT_C void TSqlResourceTester::SetHeapFailure(TInt, TInt)
       
   273 	{
       
   274 	}
       
   275 
       
   276 #endif//_DEBUG