os/persistentdata/persistentstorage/sql/TEST/t_sqlcompact2.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
#include <f32file.h>
sl@0
    17
#include <e32test.h>
sl@0
    18
#include <hal.h>
sl@0
    19
#include <stdlib.h>
sl@0
    20
#include <sqldb.h>
sl@0
    21
#include "sqlite3.h"
sl@0
    22
#include "SqliteSymbian.h"
sl@0
    23
#include "SqlSrvStatementUtil.h"
sl@0
    24
#include "SqlPanic.h"
sl@0
    25
#include "SqlCompact.h"
sl@0
    26
#include "SqlCompactConn.h"
sl@0
    27
#include "SqlCompactEntry.h"
sl@0
    28
#include "SqlUtil.h"
sl@0
    29
sl@0
    30
const TInt KOperationCount = 20;
sl@0
    31
const TInt KFreePageThresholdKb = 5;
sl@0
    32
const TInt KFreePageThreshold = 5;
sl@0
    33
sl@0
    34
const TInt KCompactStepInterval = 5;//ms
sl@0
    35
sl@0
    36
TSqlCompactSettings TheCompactionSettings;
sl@0
    37
sl@0
    38
static RTest 			TheTest(_L ("t_sqlcompact2.exe"));
sl@0
    39
static CTrapCleanup*	TheTrapCleanup = NULL;
sl@0
    40
static RFs 				TheFs;
sl@0
    41
static TBuf<KMaxFileName + 1> TheFileNameZ;
sl@0
    42
static TBuf8<KMaxFileName + 1> TheFileNameZ8;
sl@0
    43
sl@0
    44
const TInt KBlobMaxSize = 1024 * 32;
sl@0
    45
static TBuf8<KBlobMaxSize> TheBlob;
sl@0
    46
sl@0
    47
static sqlite3* 		TheDbHandle = NULL;
sl@0
    48
sl@0
    49
_LIT8(KFreePageCountPragma, "PRAGMA freelist_count\x0");
sl@0
    50
sl@0
    51
TBuf<256>  TheCmd;
sl@0
    52
TDriveName TheDriveName;
sl@0
    53
TParse     TheParse;
sl@0
    54
TFileName  TheDbName;
sl@0
    55
sl@0
    56
class CSqlCompactTestActive;
sl@0
    57
CSqlCompactTestActive* TheTestActive = NULL;
sl@0
    58
sl@0
    59
const TTimeIntervalMicroSeconds32 KInterval(200000);
sl@0
    60
sl@0
    61
static TInt TheProcessHandleCount = 0;
sl@0
    62
static TInt TheThreadHandleCount = 0;
sl@0
    63
static TInt TheAllocatedCellsCount = 0;
sl@0
    64
sl@0
    65
#ifdef _DEBUG
sl@0
    66
const TInt KBurstRate = 100;
sl@0
    67
#endif
sl@0
    68
sl@0
    69
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
    70
sl@0
    71
void DeleteTestFiles()
sl@0
    72
	{
sl@0
    73
	::CloseDbHandle(TheDbHandle);
sl@0
    74
	(void)TheFs.Delete(TheDbName);
sl@0
    75
	}
sl@0
    76
sl@0
    77
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    78
//Test macros and functions
sl@0
    79
void Check(TInt aValue, TInt aLine)
sl@0
    80
	{
sl@0
    81
	if(!aValue)
sl@0
    82
		{
sl@0
    83
		DeleteTestFiles();
sl@0
    84
		TheTest(EFalse, aLine);
sl@0
    85
		}
sl@0
    86
	}
sl@0
    87
void Check(TInt aValue, TInt aExpected, TInt aLine)
sl@0
    88
	{
sl@0
    89
	if(aValue != aExpected)
sl@0
    90
		{
sl@0
    91
		DeleteTestFiles();
sl@0
    92
		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
sl@0
    93
		TheTest(EFalse, aLine);
sl@0
    94
		}
sl@0
    95
	}
sl@0
    96
#define TEST(arg) ::Check((arg), __LINE__)
sl@0
    97
#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
sl@0
    98
sl@0
    99
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   100
sl@0
   101
void TestEnvCreate()
sl@0
   102
	{	
sl@0
   103
	TInt err = sqlite3SymbianLibInit();
sl@0
   104
	__ASSERT_ALWAYS(err == KErrNone, User::Invariant());
sl@0
   105
	TheFs = sqlite3SymbianFs();
sl@0
   106
	for(TInt i=0;i<('Z'-'A');++i)
sl@0
   107
		{
sl@0
   108
		TheFs.CreatePrivatePath(i);
sl@0
   109
		}
sl@0
   110
	err = TheFs.MkDir(TheDbName);
sl@0
   111
	TEST(err == KErrNone || err == KErrAlreadyExists);
sl@0
   112
	DeleteTestFiles();
sl@0
   113
	}
sl@0
   114
sl@0
   115
void TestEnvDestroy()
sl@0
   116
	{
sl@0
   117
	DeleteTestFiles();
sl@0
   118
	sqlite3SymbianLibFinalize();
sl@0
   119
	CloseSTDLIB();
sl@0
   120
	}
sl@0
   121
sl@0
   122
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   123
sl@0
   124
void MarkHandles()
sl@0
   125
	{
sl@0
   126
	RThread().HandleCount(TheProcessHandleCount, TheThreadHandleCount);
sl@0
   127
	}
sl@0
   128
	
sl@0
   129
void MarkAllocatedCells()
sl@0
   130
	{
sl@0
   131
	TheAllocatedCellsCount = User::CountAllocCells();
sl@0
   132
	}
sl@0
   133
sl@0
   134
void CheckHandles()
sl@0
   135
	{
sl@0
   136
	TInt processHandleCount = 0;
sl@0
   137
	TInt threadHandleCount = 0;
sl@0
   138
sl@0
   139
	RThread().HandleCount(processHandleCount, threadHandleCount);
sl@0
   140
sl@0
   141
	TEST(processHandleCount == TheProcessHandleCount);
sl@0
   142
	TEST(threadHandleCount == TheThreadHandleCount);
sl@0
   143
	}
sl@0
   144
	
sl@0
   145
void CheckAllocatedCells()
sl@0
   146
	{
sl@0
   147
	TInt allocatedCellsCount = User::CountAllocCells();
sl@0
   148
	TEST(allocatedCellsCount == TheAllocatedCellsCount);
sl@0
   149
	}
sl@0
   150
		
sl@0
   151
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   152
sl@0
   153
const TDesC& FileNameZ(const TDesC& aFileName)
sl@0
   154
	{
sl@0
   155
	TheFileNameZ.Copy(aFileName);
sl@0
   156
	TheFileNameZ.ZeroTerminate();
sl@0
   157
	return TheFileNameZ;
sl@0
   158
	}
sl@0
   159
sl@0
   160
const TDesC8& FileNameZ8(const TDesC& aFileName)
sl@0
   161
	{
sl@0
   162
	TheFileNameZ8.Copy(aFileName);
sl@0
   163
	TheFileNameZ8.ZeroTerminate();
sl@0
   164
	return TheFileNameZ8;
sl@0
   165
	}
sl@0
   166
sl@0
   167
TInt FreePageCount()
sl@0
   168
	{
sl@0
   169
	sqlite3_stmt* stmtHandle = NULL;
sl@0
   170
	TInt err = ::StmtPrepare8(TheDbHandle, KFreePageCountPragma, stmtHandle);
sl@0
   171
	TEST2(err, KErrNone);
sl@0
   172
	TEST(stmtHandle != NULL);
sl@0
   173
	err = ::StmtNext(stmtHandle);
sl@0
   174
	TEST2(err, KSqlAtRow);
sl@0
   175
	TInt pageCount = sqlite3_column_int(stmtHandle, 0);
sl@0
   176
	TEST(pageCount >= 0);
sl@0
   177
	::FinalizeStmtHandle(stmtHandle);
sl@0
   178
	return pageCount;
sl@0
   179
	}
sl@0
   180
sl@0
   181
void PrintInfo(TInt aProcessedPages, const TDesC& aMediaTypeName, TUint32 aStartTicks, TUint32 aEndTicks)
sl@0
   182
	{
sl@0
   183
	static TInt freq = 0;
sl@0
   184
	if(freq == 0)
sl@0
   185
		{
sl@0
   186
		TEST2(HAL::Get(HAL::EFastCounterFrequency, freq), KErrNone);
sl@0
   187
		}
sl@0
   188
	TInt64 diffTicks = (TInt64)aEndTicks - (TInt64)aStartTicks;
sl@0
   189
	if(diffTicks < 0)
sl@0
   190
		{
sl@0
   191
		diffTicks = KMaxTUint32 + diffTicks + 1;
sl@0
   192
		}
sl@0
   193
	const TInt KMicroSecIn1Sec = 1000000;
sl@0
   194
	TInt32 us = (diffTicks * KMicroSecIn1Sec) / freq;
sl@0
   195
	TheTest.Printf(_L("####Media type: %S. Processed pages: %d. Ticks: %ld. Execution time: %d ms\r\n"), 
sl@0
   196
							&aMediaTypeName, aProcessedPages, diffTicks, us / 1000);
sl@0
   197
	}
sl@0
   198
sl@0
   199
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   200
//////////////////////////////        CSqlCompactTestActive declaration              /////////////////////////////////////////////
sl@0
   201
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   202
sl@0
   203
class CSqlCompactTestActive : public CActive
sl@0
   204
	{
sl@0
   205
private:
sl@0
   206
	enum TCommand
sl@0
   207
		{
sl@0
   208
		ECmdInvalidTest,	
sl@0
   209
		ECmdBeginTest1,
sl@0
   210
		ECmdEndTest1,
sl@0
   211
		ECmdBeginTest2,
sl@0
   212
		ECmdEndTest2,
sl@0
   213
		ECmdBeginTest3,
sl@0
   214
		ECmdEndTest3,
sl@0
   215
		ECmdStopTests	
sl@0
   216
		};
sl@0
   217
	
sl@0
   218
public:
sl@0
   219
	static void New();
sl@0
   220
	virtual ~CSqlCompactTestActive();
sl@0
   221
	void OomTest();
sl@0
   222
	void FileIoErrTest();
sl@0
   223
	void PerformanceTest();
sl@0
   224
	void FreePageUpdateTest();
sl@0
   225
	
sl@0
   226
protected:		
sl@0
   227
	virtual void DoCancel();
sl@0
   228
	virtual void RunL();
sl@0
   229
	virtual TInt RunError(TInt aError);
sl@0
   230
	
sl@0
   231
private:
sl@0
   232
	CSqlCompactTestActive();
sl@0
   233
	void Complete(TCommand aNextCommand);
sl@0
   234
	void Schedule(TCommand aNextCommand, TTimeIntervalMicroSeconds32 aInterval);
sl@0
   235
sl@0
   236
	void CreateTestDatabase();
sl@0
   237
	void CreateTestDatabase2();
sl@0
   238
	void PrepareDb(TBool aNewDb);
sl@0
   239
	void InsertTestRecords(TInt aOpCount = KOperationCount);
sl@0
   240
	void UpdateTestRecords(TInt aOpCount = KOperationCount);
sl@0
   241
	void DeleteTestRecords(TInt aOpCount = KOperationCount);
sl@0
   242
	void DeleteTestRecords2();
sl@0
   243
	void TestEnd();
sl@0
   244
	
sl@0
   245
	void UpdateTestBegin();
sl@0
   246
	void UpdateTestEnd();
sl@0
   247
	void DeleteTestBegin();
sl@0
   248
	void DeleteTestEnd();
sl@0
   249
	void SingleOpCompactTestBegin();
sl@0
   250
	void SingleOpCompactTestEnd();
sl@0
   251
	void DoOomTest1();
sl@0
   252
	void DoOomTest2();
sl@0
   253
	void DoOomTest3();
sl@0
   254
	void DoOomTest4();
sl@0
   255
	void DoOomTest5();
sl@0
   256
sl@0
   257
private:
sl@0
   258
	TInt			iCommand;
sl@0
   259
	CSqlCompactor*	iCompactor;
sl@0
   260
	RTimer			iTimer;
sl@0
   261
sl@0
   262
	};
sl@0
   263
sl@0
   264
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   265
//////////////////////////////        CSqlCompactTestActive implementation             ///////////////////////////////////////////
sl@0
   266
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
   267
sl@0
   268
void CSqlCompactTestActive::New()
sl@0
   269
	{
sl@0
   270
	TheTestActive = new CSqlCompactTestActive;
sl@0
   271
	TEST(TheTestActive != NULL);
sl@0
   272
	}
sl@0
   273
sl@0
   274
CSqlCompactTestActive::~CSqlCompactTestActive()
sl@0
   275
	{
sl@0
   276
	Cancel();
sl@0
   277
	iTimer.Close();
sl@0
   278
	delete iCompactor;
sl@0
   279
	}
sl@0
   280
	
sl@0
   281
void CSqlCompactTestActive::DoCancel()
sl@0
   282
	{
sl@0
   283
	iTimer.Cancel();
sl@0
   284
	TRequestStatus* stat = &iStatus;
sl@0
   285
	User::RequestComplete(stat, KErrNone);
sl@0
   286
	}
sl@0
   287
	
sl@0
   288
void CSqlCompactTestActive::RunL()
sl@0
   289
	{
sl@0
   290
	switch(iCommand)
sl@0
   291
		{
sl@0
   292
		case CSqlCompactTestActive::ECmdBeginTest1:
sl@0
   293
			TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4053 Update test"));
sl@0
   294
			UpdateTestBegin();
sl@0
   295
			Schedule(CSqlCompactTestActive::ECmdEndTest1, KInterval);
sl@0
   296
			break;
sl@0
   297
		case CSqlCompactTestActive::ECmdEndTest1:
sl@0
   298
			UpdateTestEnd();
sl@0
   299
			Complete(CSqlCompactTestActive::ECmdBeginTest2);
sl@0
   300
			break;
sl@0
   301
		case CSqlCompactTestActive::ECmdBeginTest2:
sl@0
   302
			TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4054 Delete test"));
sl@0
   303
			DeleteTestBegin();
sl@0
   304
			Schedule(CSqlCompactTestActive::ECmdEndTest2, KInterval);
sl@0
   305
			break;
sl@0
   306
		case CSqlCompactTestActive::ECmdEndTest2:
sl@0
   307
			DeleteTestEnd();
sl@0
   308
			Complete(CSqlCompactTestActive::ECmdBeginTest3);
sl@0
   309
			break;
sl@0
   310
		case CSqlCompactTestActive::ECmdBeginTest3:
sl@0
   311
			TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4055 Single operation - compaction test"));
sl@0
   312
			SingleOpCompactTestBegin();
sl@0
   313
			Schedule(CSqlCompactTestActive::ECmdEndTest3, KInterval);
sl@0
   314
			break;
sl@0
   315
		case CSqlCompactTestActive::ECmdEndTest3:
sl@0
   316
			SingleOpCompactTestEnd();
sl@0
   317
			Complete(CSqlCompactTestActive::ECmdStopTests);
sl@0
   318
			break;
sl@0
   319
		case CSqlCompactTestActive::ECmdStopTests:
sl@0
   320
			CActiveScheduler::Stop();
sl@0
   321
			break;
sl@0
   322
		case CSqlCompactTestActive::ECmdInvalidTest:
sl@0
   323
		default:
sl@0
   324
			TEST(0);
sl@0
   325
			break;
sl@0
   326
		}
sl@0
   327
	}
sl@0
   328
sl@0
   329
TInt CSqlCompactTestActive::RunError(TInt aError)
sl@0
   330
	{
sl@0
   331
	TEST2(aError, KErrNone);
sl@0
   332
	return aError;
sl@0
   333
	}
sl@0
   334
sl@0
   335
CSqlCompactTestActive::CSqlCompactTestActive() :
sl@0
   336
	CActive(CActive::EPriorityStandard),
sl@0
   337
	iCommand(CSqlCompactTestActive::ECmdInvalidTest),
sl@0
   338
	iCompactor(NULL)
sl@0
   339
	{
sl@0
   340
	TInt err = sqlite3_enable_shared_cache(1);
sl@0
   341
	TEST2(err, SQLITE_OK);
sl@0
   342
	TRAP(err, iCompactor = CSqlCompactor::NewL(&SqlCreateCompactConnL, KCompactStepInterval));
sl@0
   343
	TEST2(err, KErrNone);
sl@0
   344
	err = iTimer.CreateLocal();
sl@0
   345
	TEST2(err, KErrNone);
sl@0
   346
	CActiveScheduler::Add(this);
sl@0
   347
	Complete(CSqlCompactTestActive::ECmdBeginTest1);
sl@0
   348
	}
sl@0
   349
sl@0
   350
void CSqlCompactTestActive::Complete(CSqlCompactTestActive::TCommand aNextCommand)
sl@0
   351
	{
sl@0
   352
	TEST(!IsActive());
sl@0
   353
	iCommand = aNextCommand;
sl@0
   354
	TRequestStatus* stat = &iStatus;
sl@0
   355
	User::RequestComplete(stat, KErrNone);
sl@0
   356
	SetActive();	
sl@0
   357
	}
sl@0
   358
sl@0
   359
void CSqlCompactTestActive::Schedule(TCommand aNextCommand, TTimeIntervalMicroSeconds32 aInterval)
sl@0
   360
	{
sl@0
   361
	TEST(!IsActive());
sl@0
   362
	iCommand = aNextCommand;
sl@0
   363
	iTimer.After(iStatus, aInterval);
sl@0
   364
	TEST2(iStatus.Int(), KRequestPending);
sl@0
   365
	SetActive();
sl@0
   366
	}
sl@0
   367
sl@0
   368
void CSqlCompactTestActive::CreateTestDatabase()
sl@0
   369
	{
sl@0
   370
	TInt err = ::CreateDbHandle8(::FileNameZ8(TheDbName), TheDbHandle);
sl@0
   371
	TEST2(err, KErrNone);
sl@0
   372
	_LIT8(KCreateTableSql, "CREATE TABLE A(I INTEGRER, B BLOB)\x0");
sl@0
   373
	err = ::DbExecStmt8(TheDbHandle, KCreateTableSql);
sl@0
   374
	TEST2(err, KErrNone);
sl@0
   375
	}
sl@0
   376
sl@0
   377
void CSqlCompactTestActive::InsertTestRecords(TInt aOpCount)
sl@0
   378
	{
sl@0
   379
	TheBlob.SetLength(SQLITE_DEFAULT_PAGE_SIZE);
sl@0
   380
	for(TInt i=0;i<aOpCount;++i)
sl@0
   381
		{
sl@0
   382
		_LIT8(KInsertSql, "INSERT INTO A VALUES(%d, :Prm)\x0");
sl@0
   383
		TBuf8<sizeof(KInsertSql) + 10> sqlBuf1;
sl@0
   384
		sqlBuf1.Format(KInsertSql, i + 1);
sl@0
   385
		sqlite3_stmt* stmtHandle = NULL;
sl@0
   386
		TInt err = ::StmtPrepare8(TheDbHandle, sqlBuf1, stmtHandle);
sl@0
   387
		TEST2(err, KErrNone);
sl@0
   388
		TEST(stmtHandle != NULL);
sl@0
   389
		err = sqlite3_bind_blob(stmtHandle, 1, TheBlob.Ptr(), SQLITE_DEFAULT_PAGE_SIZE, SQLITE_STATIC);
sl@0
   390
		TEST2(err, SQLITE_OK);
sl@0
   391
		err = ::StmtExec(stmtHandle);
sl@0
   392
		TEST2(err, KErrNone);
sl@0
   393
		::FinalizeStmtHandle(stmtHandle);
sl@0
   394
		}
sl@0
   395
	}
sl@0
   396
sl@0
   397
//Creates a test database (with KDbName name). 
sl@0
   398
void CSqlCompactTestActive::CreateTestDatabase2()
sl@0
   399
    {
sl@0
   400
    //Create the database
sl@0
   401
    const TInt KPageSize = 1024;
sl@0
   402
    _LIT8(KConfigStr, "page_size=");
sl@0
   403
    TBuf8<100> config;
sl@0
   404
    config.Copy(KConfigStr);
sl@0
   405
    config.AppendNum(KPageSize);
sl@0
   406
    TInt err = KErrNone;
sl@0
   407
    err = ::CreateDbHandle8(::FileNameZ8(TheDbName), TheDbHandle);
sl@0
   408
    TEST2(err, KErrNone);  
sl@0
   409
    _LIT8(KCreateTableSql, "CREATE TABLE A(I INTEGER, T TEXT)\x0");
sl@0
   410
    err = ::DbExecStmt8(TheDbHandle, KCreateTableSql);
sl@0
   411
    TEST2(err, KErrNone);
sl@0
   412
    }
sl@0
   413
sl@0
   414
//Insert 1000 records. The record size is such that there is only two records per page.
sl@0
   415
void CSqlCompactTestActive::PrepareDb(TBool aDeleteRecords)
sl@0
   416
    {
sl@0
   417
    //Insert records
sl@0
   418
    const TInt KRecordCount = 1000;
sl@0
   419
    const TInt KTextLen = 400;
sl@0
   420
    TBuf<KTextLen> TheText;
sl@0
   421
    TBuf<KTextLen + 100> TheSqlBuf;
sl@0
   422
    TheText.SetLength(TheText.MaxLength());
sl@0
   423
    TheText.Fill(TChar('A'));
sl@0
   424
    for(TInt i=0;i<KRecordCount;++i)
sl@0
   425
        {
sl@0
   426
        TheSqlBuf.Format(_L("INSERT INTO A VALUES(%d, '%S')"), i + 1, &TheText);
sl@0
   427
        _LIT(KZero, "\x0");
sl@0
   428
        TheSqlBuf.Append(KZero);
sl@0
   429
        TInt err = ::DbExecStmt16(TheDbHandle, TheSqlBuf);
sl@0
   430
        TEST2(err, KErrNone);
sl@0
   431
        }
sl@0
   432
    if(aDeleteRecords)
sl@0
   433
        {   
sl@0
   434
        //Delete all records to make a lot of free pages. 
sl@0
   435
        _LIT(KDeleteAll, "DELETE FROM A WHERE 1\x0");
sl@0
   436
        TheSqlBuf = KDeleteAll;
sl@0
   437
        TInt err = ::DbExecStmt16(TheDbHandle, TheSqlBuf);
sl@0
   438
        TEST2(err, KErrNone);
sl@0
   439
        }
sl@0
   440
    }
sl@0
   441
sl@0
   442
void CSqlCompactTestActive::UpdateTestRecords(TInt aOpCount)
sl@0
   443
	{
sl@0
   444
	for(TInt i=0;i<aOpCount;++i)
sl@0
   445
		{
sl@0
   446
		_LIT8(KUpdateSql, "UPDATE A SET B=x'1122' WHERE I=%d\x0");
sl@0
   447
		TBuf8<sizeof(KUpdateSql) + 10> sqlBuf2;
sl@0
   448
		sqlBuf2.Format(KUpdateSql, i + 1);
sl@0
   449
		TInt err = ::DbExecStmt8(TheDbHandle, sqlBuf2);
sl@0
   450
		TEST2(err, KErrNone);
sl@0
   451
		}
sl@0
   452
	}
sl@0
   453
sl@0
   454
void CSqlCompactTestActive::DeleteTestRecords(TInt aOpCount)
sl@0
   455
	{
sl@0
   456
	for(TInt i=0;i<aOpCount;++i)
sl@0
   457
		{
sl@0
   458
		_LIT8(KDeleteSql, "DELETE FROM A WHERE I=%d\x0");
sl@0
   459
		TBuf8<sizeof(KDeleteSql) + 10> sqlBuf2;
sl@0
   460
		sqlBuf2.Format(KDeleteSql, i + 1);
sl@0
   461
		TInt err = ::DbExecStmt8(TheDbHandle, sqlBuf2);
sl@0
   462
		TEST2(err, KErrNone);
sl@0
   463
		}
sl@0
   464
	}
sl@0
   465
sl@0
   466
void CSqlCompactTestActive::DeleteTestRecords2()
sl@0
   467
	{
sl@0
   468
	_LIT8(KDeleteSql, "DELETE FROM A\x0");
sl@0
   469
	TInt err = ::DbExecStmt8(TheDbHandle, KDeleteSql);
sl@0
   470
	TEST2(err, KErrNone);
sl@0
   471
	}
sl@0
   472
sl@0
   473
void CSqlCompactTestActive::TestEnd()
sl@0
   474
	{
sl@0
   475
	TInt freePageCount = ::FreePageCount();
sl@0
   476
	TEST2(freePageCount, 0);
sl@0
   477
	iCompactor->ReleaseEntry(TheDbName);
sl@0
   478
	::CloseDbHandle(TheDbHandle);
sl@0
   479
	TheDbHandle = NULL;
sl@0
   480
	(void)TheFs.Delete(TheDbName);
sl@0
   481
	}
sl@0
   482
sl@0
   483
/**
sl@0
   484
@SYMTestCaseID			SYSLIB-SQL-UT-4053
sl@0
   485
@SYMTestCaseDesc		Background compaction scheduled by a set of UPDATE operations.
sl@0
   486
						The test uses the server background compaction classes directly.
sl@0
   487
						The test creates a database, inserts records and updates the records.
sl@0
   488
						The update operations free enough disk space to kick-off the background compaction.
sl@0
   489
						The test active object, that simulates the SQL server, is activated and the
sl@0
   490
						background compaction - executed.
sl@0
   491
						The test checks at the end that the background compaction really happened - in
sl@0
   492
						CSqlCompactTestActive::UpdateTestEnd().
sl@0
   493
@SYMTestPriority		Medium
sl@0
   494
@SYMTestActions			Background compaction scheduled by a set of UPDATE operations.
sl@0
   495
@SYMTestExpectedResults Test must not fail
sl@0
   496
@SYMREQ					REQ10271
sl@0
   497
                        REQ10272
sl@0
   498
*/
sl@0
   499
void CSqlCompactTestActive::UpdateTestBegin()
sl@0
   500
	{
sl@0
   501
	CreateTestDatabase();
sl@0
   502
	TRAPD(err, iCompactor->AddEntryL(TheDbName, TheCompactionSettings));
sl@0
   503
	TEST2(err, KErrNone);
sl@0
   504
	InsertTestRecords();
sl@0
   505
	UpdateTestRecords();
sl@0
   506
	TInt freePageCount = ::FreePageCount();
sl@0
   507
	TEST(freePageCount > KFreePageThreshold);
sl@0
   508
	}
sl@0
   509
sl@0
   510
void CSqlCompactTestActive::UpdateTestEnd()
sl@0
   511
	{
sl@0
   512
	TestEnd();
sl@0
   513
	}
sl@0
   514
sl@0
   515
/**
sl@0
   516
@SYMTestCaseID			SYSLIB-SQL-UT-4054
sl@0
   517
@SYMTestCaseDesc		Background compaction scheduled by a set of DELETE operations.
sl@0
   518
						The test uses the server background compaction classes directly.
sl@0
   519
						The test creates a database, inserts records and deletes the records.
sl@0
   520
						The delete operations free enough disk space to kick-off the background compaction.
sl@0
   521
						The test active object, that simulates the SQL server, is activated and the
sl@0
   522
						background compaction - executed.
sl@0
   523
						The test checks at the end that the background compaction really happened - in
sl@0
   524
						CSqlCompactTestActive::DeleteTestEnd().
sl@0
   525
@SYMTestPriority		Medium
sl@0
   526
@SYMTestActions			Background compaction scheduled by a set of DELETE operations.
sl@0
   527
@SYMTestExpectedResults Test must not fail
sl@0
   528
@SYMREQ					REQ10271
sl@0
   529
                        REQ10272
sl@0
   530
*/
sl@0
   531
void CSqlCompactTestActive::DeleteTestBegin()
sl@0
   532
	{
sl@0
   533
	CreateTestDatabase();
sl@0
   534
	TRAPD(err, iCompactor->AddEntryL(TheDbName, TheCompactionSettings));
sl@0
   535
	TEST2(err, KErrNone);
sl@0
   536
	InsertTestRecords();
sl@0
   537
	DeleteTestRecords();
sl@0
   538
	TInt freePageCount = ::FreePageCount();
sl@0
   539
	TEST(freePageCount >= KFreePageThreshold);
sl@0
   540
	}
sl@0
   541
	
sl@0
   542
void CSqlCompactTestActive::DeleteTestEnd()
sl@0
   543
	{
sl@0
   544
	TestEnd();
sl@0
   545
	}
sl@0
   546
sl@0
   547
/**
sl@0
   548
@SYMTestCaseID			SYSLIB-SQL-UT-4055
sl@0
   549
@SYMTestCaseDesc		Background compaction, initiated by a single operation.
sl@0
   550
						The test uses the server background compaction classes directly.
sl@0
   551
						The test creates a database, inserts records and deletes the records using just
sl@0
   552
						a single DELETE SQL statement.
sl@0
   553
						The test active object, that simulates the SQL server, schedules 
sl@0
   554
						CSqlCompactTestActive::SingleOpCompactTestEnd() for execution. The code in
sl@0
   555
						SingleOpCompactTestEnd() checks that the background compaction has been activated and closes the
sl@0
   556
						database connection. The "database close" operation should start the compaction
sl@0
   557
						because the total size of free pages is above the "free pages" threshold (in Kb).
sl@0
   558
@SYMTestPriority		Medium
sl@0
   559
@SYMTestActions			Background compaction, initiated by a single operation.
sl@0
   560
@SYMTestExpectedResults Test must not fail
sl@0
   561
@SYMREQ					REQ10271
sl@0
   562
                        REQ10272
sl@0
   563
*/
sl@0
   564
void CSqlCompactTestActive::SingleOpCompactTestBegin()
sl@0
   565
	{
sl@0
   566
	CreateTestDatabase();
sl@0
   567
	TRAPD(err, iCompactor->AddEntryL(TheDbName, TheCompactionSettings));
sl@0
   568
	TEST2(err, KErrNone);
sl@0
   569
	InsertTestRecords();
sl@0
   570
	DeleteTestRecords2();
sl@0
   571
	TInt freePageCount = ::FreePageCount();
sl@0
   572
	TEST(freePageCount >= KFreePageThreshold);
sl@0
   573
	}
sl@0
   574
sl@0
   575
void CSqlCompactTestActive::SingleOpCompactTestEnd()
sl@0
   576
	{
sl@0
   577
	TestEnd();
sl@0
   578
	}
sl@0
   579
sl@0
   580
//Background compaction - OOM test.
sl@0
   581
//CSqlCompactor::NewL() is the function tested in an OOM simulation loop.
sl@0
   582
//The expectation is that if the iteration fails with KErrNoMemory, no memory leak will occur and the compactor object won't be created.
sl@0
   583
void CSqlCompactTestActive::DoOomTest1()
sl@0
   584
	{
sl@0
   585
	TInt err = KErrNoMemory;
sl@0
   586
	TInt failingAllocationNo = 0;
sl@0
   587
	while(err == KErrNoMemory)
sl@0
   588
		{
sl@0
   589
		MarkHandles();
sl@0
   590
		MarkAllocatedCells();
sl@0
   591
				
sl@0
   592
		__UHEAP_MARK;
sl@0
   593
		
sl@0
   594
		__UHEAP_SETBURSTFAIL(RAllocator::EBurstFailNext, ++failingAllocationNo, KBurstRate);
sl@0
   595
		
sl@0
   596
		CSqlCompactor* compactor = NULL;
sl@0
   597
		TRAP(err, compactor = CSqlCompactor::NewL(&SqlCreateCompactConnL, KCompactStepInterval));
sl@0
   598
		
sl@0
   599
		__UHEAP_RESET;
sl@0
   600
sl@0
   601
		if(err == KErrNone)	
sl@0
   602
			{
sl@0
   603
			TEST(compactor != NULL);
sl@0
   604
			delete compactor;
sl@0
   605
			}
sl@0
   606
		else
sl@0
   607
			{
sl@0
   608
			TEST(!compactor);
sl@0
   609
			TEST2(err, KErrNoMemory);	
sl@0
   610
			}
sl@0
   611
							
sl@0
   612
		__UHEAP_MARKEND;
sl@0
   613
sl@0
   614
		CheckAllocatedCells();	    	
sl@0
   615
		CheckHandles();	    	
sl@0
   616
		}
sl@0
   617
	TEST2(err, KErrNone);
sl@0
   618
	TheTest.Printf(_L("=== CSqlCompactor::NewL() OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
sl@0
   619
	}
sl@0
   620
sl@0
   621
//Background compaction - OOM test.
sl@0
   622
//CSqlCompactor::AddEntryL() is the function tested in an OOM simulation loop.
sl@0
   623
//The expectation is that no memory leak will occur if OOM iteration fails with KErrNoMemory.
sl@0
   624
//The expectation also is that if the iteration fails with KErrNoMemory, no entry will be added to the compactor.
sl@0
   625
void CSqlCompactTestActive::DoOomTest2()
sl@0
   626
	{
sl@0
   627
	CSqlCompactor* compactor = NULL;
sl@0
   628
	TRAPD(err, compactor = CSqlCompactor::NewL(&SqlCreateCompactConnL, KCompactStepInterval));
sl@0
   629
	TEST2(err, KErrNone);
sl@0
   630
	TEST(compactor != NULL);
sl@0
   631
	
sl@0
   632
	err = KErrNoMemory;
sl@0
   633
	TInt failingAllocationNo = 0;
sl@0
   634
	while(err == KErrNoMemory)
sl@0
   635
		{
sl@0
   636
		MarkHandles();
sl@0
   637
		MarkAllocatedCells();
sl@0
   638
				
sl@0
   639
		__UHEAP_MARK;
sl@0
   640
		
sl@0
   641
		__UHEAP_SETBURSTFAIL(RAllocator::EBurstFailNext, ++failingAllocationNo, KBurstRate);
sl@0
   642
		
sl@0
   643
		TRAP(err, compactor->AddEntryL(TheDbName, TheCompactionSettings));
sl@0
   644
		
sl@0
   645
		__UHEAP_RESET;
sl@0
   646
sl@0
   647
		if(err == KErrNone)	
sl@0
   648
			{
sl@0
   649
			TEST2(compactor->iEntries.Count(), 1);
sl@0
   650
			compactor->ReleaseEntry(TheDbName);
sl@0
   651
			compactor->iEntries.Compress();
sl@0
   652
			}
sl@0
   653
		else
sl@0
   654
			{
sl@0
   655
			TEST2(compactor->iEntries.Count(), 0);
sl@0
   656
			TEST2(err, KErrNoMemory);	
sl@0
   657
			}
sl@0
   658
							
sl@0
   659
		__UHEAP_MARKEND;
sl@0
   660
sl@0
   661
		CheckAllocatedCells();	    	
sl@0
   662
		CheckHandles();	    	
sl@0
   663
		}
sl@0
   664
	delete compactor;
sl@0
   665
	TEST2(err, KErrNone);
sl@0
   666
	TheTest.Printf(_L("=== CSqlCompactor::AddEntryL() OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
sl@0
   667
	}
sl@0
   668
sl@0
   669
//Background compaction - OOM test.
sl@0
   670
//CSqlCompactor::NewL() and CSqlCompactor::AddEntryL() are the functions tested in an OOM simulation loop.
sl@0
   671
//At the end of the iteration CSqlCompactor::ReleaseEntry() is not called. 
sl@0
   672
//The CSqlCompactor's destructor should properly release the entry if the compactor and the entry have been created successfully.
sl@0
   673
//The expectation is that no memory leak will occur if OOM iteration fails with KErrNoMemory.
sl@0
   674
void CSqlCompactTestActive::DoOomTest3()
sl@0
   675
	{
sl@0
   676
	TInt err = KErrNoMemory;
sl@0
   677
	TInt failingAllocationNo = 0;
sl@0
   678
	while(err == KErrNoMemory)
sl@0
   679
		{
sl@0
   680
		MarkHandles();
sl@0
   681
		MarkAllocatedCells();
sl@0
   682
				
sl@0
   683
		__UHEAP_MARK;
sl@0
   684
		
sl@0
   685
		__UHEAP_SETBURSTFAIL(RAllocator::EBurstFailNext, ++failingAllocationNo, KBurstRate);
sl@0
   686
		
sl@0
   687
		CSqlCompactor* compactor = NULL;
sl@0
   688
		TRAP(err, compactor = CSqlCompactor::NewL(&SqlCreateCompactConnL, KCompactStepInterval));
sl@0
   689
		if(err == KErrNone)
sl@0
   690
			{
sl@0
   691
			TRAP(err, (void)compactor->AddEntryL(TheDbName, TheCompactionSettings));
sl@0
   692
			}
sl@0
   693
		
sl@0
   694
		__UHEAP_RESET;
sl@0
   695
sl@0
   696
		if(err == KErrNone)	
sl@0
   697
			{
sl@0
   698
			TEST(compactor != NULL);
sl@0
   699
			}
sl@0
   700
		else
sl@0
   701
			{
sl@0
   702
			TEST2(err, KErrNoMemory);	
sl@0
   703
			}
sl@0
   704
		delete compactor;
sl@0
   705
							
sl@0
   706
		__UHEAP_MARKEND;
sl@0
   707
sl@0
   708
		CheckAllocatedCells();	    	
sl@0
   709
		CheckHandles();	    	
sl@0
   710
		}
sl@0
   711
	TEST2(err, KErrNone);
sl@0
   712
	TheTest.Printf(_L("=== CSqlCompactor::NewL()+CSqlCompactor::AddEntryL() OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
sl@0
   713
	}
sl@0
   714
sl@0
   715
//Background compaction - OOM test.
sl@0
   716
//The test database is created inside the OOM loop, at the beginning of each OOM iteration. The database has enough free space.
sl@0
   717
//Then the Compact() method is called. The expectation is that if the iteration fails with KErrNoMemory error, no memory leak will occur.
sl@0
   718
void CSqlCompactTestActive::DoOomTest4()
sl@0
   719
	{
sl@0
   720
	TInt err = KErrNoMemory;
sl@0
   721
	TInt failingAllocationNo = 0;
sl@0
   722
	while(err == KErrNoMemory)
sl@0
   723
		{
sl@0
   724
		MarkHandles();
sl@0
   725
		MarkAllocatedCells();
sl@0
   726
						
sl@0
   727
		__UHEAP_MARK;
sl@0
   728
		
sl@0
   729
		(void)TheFs.Delete(TheDbName);
sl@0
   730
		CreateTestDatabase();
sl@0
   731
		CSqlCompactor* compactor = NULL;
sl@0
   732
		TRAP(err, compactor = CSqlCompactor::NewL(&SqlCreateCompactConnL, KCompactStepInterval));
sl@0
   733
		TEST2(err, KErrNone);
sl@0
   734
		TRAP(err, compactor->AddEntryL(TheDbName, TheCompactionSettings));
sl@0
   735
		TEST2(err, KErrNone);
sl@0
   736
		InsertTestRecords();
sl@0
   737
		DeleteTestRecords();
sl@0
   738
		TInt freePageCount = ::FreePageCount();
sl@0
   739
		TEST(freePageCount >= KFreePageThreshold);
sl@0
   740
		
sl@0
   741
		__UHEAP_SETBURSTFAIL(RAllocator::EBurstFailNext, ++failingAllocationNo, KBurstRate);
sl@0
   742
sl@0
   743
		CSqlCompactEntry* impl = compactor->iEntries[0];
sl@0
   744
		err = impl->Compact();
sl@0
   745
		
sl@0
   746
		__UHEAP_RESET;
sl@0
   747
sl@0
   748
		if(err != KErrNone)	
sl@0
   749
			{
sl@0
   750
			TEST2(err, KErrNoMemory);	
sl@0
   751
			}
sl@0
   752
		else
sl@0
   753
			{
sl@0
   754
			TInt freePageCount2 = ::FreePageCount();
sl@0
   755
			TEST(freePageCount2 < freePageCount);
sl@0
   756
			}
sl@0
   757
		delete compactor;
sl@0
   758
		::CloseDbHandle(TheDbHandle);
sl@0
   759
		TheDbHandle = NULL;
sl@0
   760
							
sl@0
   761
		__UHEAP_MARKEND;
sl@0
   762
sl@0
   763
		CheckAllocatedCells();	    	
sl@0
   764
		CheckHandles();	    	
sl@0
   765
		}
sl@0
   766
	TEST2(err, KErrNone);
sl@0
   767
	TheTest.Printf(_L("=== CSqlCompactEntry::Compact() OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
sl@0
   768
	(void)TheFs.Delete(TheDbName);
sl@0
   769
	}
sl@0
   770
sl@0
   771
//Background compaction - OOM test.
sl@0
   772
//The test database is created outside the OOM loop. The database has enough free space.
sl@0
   773
//Then the Compact() method is called under OOM simulation. 
sl@0
   774
//The expectation is that if the iteration fails with KErrNoMemory error, no memory leak will occur and the number of 
sl@0
   775
//the free pages is the same as it was at the beginning of the OOM iteration.
sl@0
   776
void CSqlCompactTestActive::DoOomTest5()
sl@0
   777
	{
sl@0
   778
	__UHEAP_MARK;
sl@0
   779
	
sl@0
   780
	CreateTestDatabase();
sl@0
   781
	CSqlCompactor* compactor = NULL;
sl@0
   782
	TRAPD(err, compactor = CSqlCompactor::NewL(&SqlCreateCompactConnL, KCompactStepInterval));
sl@0
   783
	TEST2(err, KErrNone);
sl@0
   784
	TRAP(err, compactor->AddEntryL(TheDbName, TheCompactionSettings));
sl@0
   785
	TEST2(err, KErrNone);
sl@0
   786
	InsertTestRecords();
sl@0
   787
	DeleteTestRecords();
sl@0
   788
	TInt freePageCount = ::FreePageCount();
sl@0
   789
	TEST(freePageCount >= KFreePageThreshold);
sl@0
   790
	err = KErrNoMemory;
sl@0
   791
	TInt failingAllocationNo = 0;
sl@0
   792
	while(err == KErrNoMemory)
sl@0
   793
		{
sl@0
   794
		TInt freePageCount2 = ::FreePageCount();
sl@0
   795
		
sl@0
   796
		__UHEAP_SETBURSTFAIL(RAllocator::EBurstFailNext, ++failingAllocationNo, KBurstRate);
sl@0
   797
sl@0
   798
		CSqlCompactEntry* impl = compactor->iEntries[0];
sl@0
   799
		impl->iPageCount = freePageCount2;
sl@0
   800
		err = impl->Compact();
sl@0
   801
		
sl@0
   802
		__UHEAP_RESET;
sl@0
   803
sl@0
   804
		if(err != KErrNone)	
sl@0
   805
			{
sl@0
   806
			TEST2(err, KErrNoMemory);	
sl@0
   807
			TInt freePageCount3 = ::FreePageCount();
sl@0
   808
			TEST2(freePageCount2, freePageCount3);
sl@0
   809
			}
sl@0
   810
	}
sl@0
   811
	TEST2(err, KErrNone);
sl@0
   812
	TInt freePageCount4 = ::FreePageCount();
sl@0
   813
	TEST(freePageCount4 < freePageCount);
sl@0
   814
sl@0
   815
	compactor->ReleaseEntry(TheDbName);
sl@0
   816
	delete compactor;
sl@0
   817
	::CloseDbHandle(TheDbHandle);
sl@0
   818
	TheDbHandle = NULL;
sl@0
   819
	
sl@0
   820
	__UHEAP_MARKEND;
sl@0
   821
	
sl@0
   822
	TheTest.Printf(_L("=== CSqlCompactEntry::Compact()-2 OOM test succeeded at heap failure rate of %d ===\r\n"), failingAllocationNo);
sl@0
   823
	(void)TheFs.Delete(TheDbName);
sl@0
   824
	}
sl@0
   825
sl@0
   826
/**
sl@0
   827
@SYMTestCaseID			SYSLIB-SQL-UT-4050
sl@0
   828
@SYMTestCaseDesc		Background compaction - OOM tests.
sl@0
   829
						The test uses directly the SQL server background compaction classes and does OOM tests for:
sl@0
   830
						creating the database compaction object (CSqlCompactor), adding a new background database connection, 
sl@0
   831
						calling directly the background compaction method.
sl@0
   832
@SYMTestPriority		Medium
sl@0
   833
@SYMTestActions			Background compaction - OOM tests.
sl@0
   834
@SYMTestExpectedResults Test must not fail
sl@0
   835
@SYMREQ					REQ10271
sl@0
   836
*/
sl@0
   837
void CSqlCompactTestActive::OomTest()
sl@0
   838
	{
sl@0
   839
	CreateTestDatabase();
sl@0
   840
	::CloseDbHandle(TheDbHandle);
sl@0
   841
	TheDbHandle = NULL;
sl@0
   842
	
sl@0
   843
	DoOomTest1();
sl@0
   844
	DoOomTest2();
sl@0
   845
	DoOomTest3();
sl@0
   846
sl@0
   847
	(void)TheFs.Delete(TheDbName);
sl@0
   848
	
sl@0
   849
	DoOomTest4();
sl@0
   850
	DoOomTest5();
sl@0
   851
	}
sl@0
   852
sl@0
   853
/**
sl@0
   854
@SYMTestCaseID			SYSLIB-SQL-UT-4051
sl@0
   855
@SYMTestCaseDesc		Background compaction - file I/O error simulation test.
sl@0
   856
						The test calls the background compaction method, CSqlCompactEntry::Compact(),
sl@0
   857
						in a file I/O error simulation loop.
sl@0
   858
@SYMTestPriority		Medium
sl@0
   859
@SYMTestActions			Background compaction - file I/O error simulation test.
sl@0
   860
@SYMTestExpectedResults Test must not fail
sl@0
   861
@SYMREQ					REQ10271
sl@0
   862
*/
sl@0
   863
void CSqlCompactTestActive::FileIoErrTest()
sl@0
   864
	{
sl@0
   865
	CreateTestDatabase();
sl@0
   866
	CSqlCompactor* compactor = NULL;
sl@0
   867
	TRAPD(err, compactor = CSqlCompactor::NewL(&SqlCreateCompactConnL, KCompactStepInterval));
sl@0
   868
	TEST2(err, KErrNone);
sl@0
   869
	TRAP(err, compactor->AddEntryL(TheDbName, TheCompactionSettings));
sl@0
   870
	TEST2(err, KErrNone);
sl@0
   871
	InsertTestRecords();
sl@0
   872
	DeleteTestRecords();
sl@0
   873
	TInt freePageCount = ::FreePageCount();
sl@0
   874
	TEST(freePageCount >= KFreePageThreshold);
sl@0
   875
	err = KErrGeneral;
sl@0
   876
	TInt ioCounter = 0;
sl@0
   877
	while(err != KErrNone)
sl@0
   878
		{
sl@0
   879
		TInt freePageCount2 = ::FreePageCount();
sl@0
   880
		if(freePageCount2 == 0)
sl@0
   881
			{
sl@0
   882
			err = KErrNone;
sl@0
   883
			break;	
sl@0
   884
			}
sl@0
   885
		
sl@0
   886
		(void)TheFs.SetErrorCondition(err, ioCounter++);
sl@0
   887
sl@0
   888
		CSqlCompactEntry* impl = compactor->iEntries[0];
sl@0
   889
		impl->iPageCount = freePageCount2;
sl@0
   890
		err = impl->Compact();
sl@0
   891
		
sl@0
   892
		(void)TheFs.SetErrorCondition(KErrNone);
sl@0
   893
sl@0
   894
		//check the database free pages count - all bets are off in a case of an I/O error. 
sl@0
   895
		//The free page count may actually have been reduced.
sl@0
   896
		TInt freePageCount3 = ::FreePageCount();
sl@0
   897
		TEST(freePageCount3  <= freePageCount2);
sl@0
   898
		}
sl@0
   899
	TEST2(err, KErrNone);
sl@0
   900
	TInt freePageCount4 = ::FreePageCount();
sl@0
   901
sl@0
   902
	compactor->ReleaseEntry(TheDbName);
sl@0
   903
	delete compactor;
sl@0
   904
	::CloseDbHandle(TheDbHandle);
sl@0
   905
	TheDbHandle = NULL;
sl@0
   906
	
sl@0
   907
	TheTest.Printf(_L("=== CSqlCompactEntry::Compact() \"file I/O\" error simulation test succeeded at iteration %d, free pages %d ===\r\n"), ioCounter, freePageCount4);
sl@0
   908
	(void)TheFs.Delete(TheDbName);
sl@0
   909
	}
sl@0
   910
sl@0
   911
/**
sl@0
   912
@SYMTestCaseID			SYSLIB-SQL-UT-4052
sl@0
   913
@SYMTestCaseDesc		Compaction - performance test.
sl@0
   914
						The test creates a test database (the default drive is C:, but different drive 
sl@0
   915
						can be specified as a test argument) and runs a compaction performance test. 
sl@0
   916
						The performance result is printed out.
sl@0
   917
@SYMTestPriority		Medium
sl@0
   918
@SYMTestActions			Compaction - performance test.
sl@0
   919
@SYMTestExpectedResults Test must not fail
sl@0
   920
@SYMREQ					REQ10271
sl@0
   921
                        REQ10272
sl@0
   922
*/
sl@0
   923
void CSqlCompactTestActive::PerformanceTest()
sl@0
   924
	{
sl@0
   925
	TInt err = TheParse.Set(TheDbName, NULL, NULL);
sl@0
   926
	TEST2(err, KErrNone);
sl@0
   927
	TPtrC driveName = TheParse.Drive();
sl@0
   928
	TEST(driveName.Length() > 0);
sl@0
   929
	TInt driveNumber = -1;
sl@0
   930
	err = RFs::CharToDrive(driveName[0], driveNumber);
sl@0
   931
	TEST2(err, KErrNone);
sl@0
   932
	TDriveNumber driveNo = static_cast <TDriveNumber> (driveNumber);
sl@0
   933
	TDriveInfo driveInfo;
sl@0
   934
	err = TheFs.Drive(driveInfo, driveNo);
sl@0
   935
	TEST2(err, KErrNone);
sl@0
   936
	
sl@0
   937
	_LIT(KType1, "Not present");
sl@0
   938
	_LIT(KType2, "Unknown");
sl@0
   939
	_LIT(KType3, "Floppy");
sl@0
   940
	_LIT(KType4, "Hard disk");
sl@0
   941
	_LIT(KType5, "CD ROM");
sl@0
   942
	_LIT(KType6, "RAM disk");
sl@0
   943
	_LIT(KType7, "Flash");
sl@0
   944
	_LIT(KType8, "ROM drive");
sl@0
   945
	_LIT(KType9, "Remote drive");
sl@0
   946
	_LIT(KType10,"NAND flash");
sl@0
   947
	_LIT(KType11,"Rotating media");
sl@0
   948
	TPtrC KMediaTypeNames[] = {KType1(), KType2(), KType3(), KType4(), KType5(), KType6(), KType7(), KType8(), KType9(), KType10(), KType11()};
sl@0
   949
	TheTest.Printf(_L("Drive: %C: %S. File: \"%S\"\r\n"), 'A' + driveNo, &KMediaTypeNames[driveInfo.iType], &TheDbName);
sl@0
   950
sl@0
   951
	(void)TheFs.Delete(TheDbName);
sl@0
   952
	CreateTestDatabase();
sl@0
   953
	const TInt KRecCount = 90;
sl@0
   954
	InsertTestRecords(KRecCount);
sl@0
   955
	DeleteTestRecords2();
sl@0
   956
	TInt freePageCount = ::FreePageCount();
sl@0
   957
	TInt processedPages = 0;
sl@0
   958
	TheTest.Printf(_L("   Free pages count = %d\r\n"), freePageCount);
sl@0
   959
	TUint32 start = User::FastCounter();
sl@0
   960
	err = ::DbCompact(TheDbHandle, KNullDesC, freePageCount, processedPages);
sl@0
   961
	TUint32 end = User::FastCounter();
sl@0
   962
	TEST2(err, KErrNone);
sl@0
   963
	TEST2(processedPages, freePageCount);
sl@0
   964
	::CloseDbHandle(TheDbHandle);
sl@0
   965
	TheDbHandle = NULL;
sl@0
   966
	(void)TheFs.Delete(TheDbName);
sl@0
   967
	PrintInfo(processedPages, KMediaTypeNames[driveInfo.iType], start, end);
sl@0
   968
	}
sl@0
   969
sl@0
   970
/**
sl@0
   971
@SYMTestCaseID          PDS-SQL-CT-4239
sl@0
   972
@SYMTestCaseDesc        Free page update test.
sl@0
   973
                        The test creates a database with some records and deletes them all. The records are inserted such that when
sl@0
   974
                        they get deleted, it leaves a great deal of free pages.
sl@0
   975
                        Then the test refill the pages which ware empty. After that, the test call ::DbCompact(...) with the number of free
sl@0
   976
                        pages previously. The free page count should be updated with "0" since all free pages have been refilled since. 
sl@0
   977
@SYMTestPriority        Medium
sl@0
   978
@SYMTestExpectedResults Test must not fail
sl@0
   979
*/
sl@0
   980
void CSqlCompactTestActive::FreePageUpdateTest()
sl@0
   981
    {
sl@0
   982
     (void)TheFs.Delete(TheDbName);
sl@0
   983
    
sl@0
   984
    //Create the database with 1000 records and then delete all of them
sl@0
   985
    CreateTestDatabase2();
sl@0
   986
    
sl@0
   987
    CSqlCompactor* compactor = NULL;
sl@0
   988
    TRAPD(err, compactor = CSqlCompactor::NewL(&SqlCreateCompactConnL, KCompactStepInterval));
sl@0
   989
    TEST2(err, KErrNone);
sl@0
   990
    TRAP(err, compactor->AddEntryL(TheDbName, TheCompactionSettings));
sl@0
   991
    TEST2(err, KErrNone);
sl@0
   992
sl@0
   993
    PrepareDb(ETrue);
sl@0
   994
    TInt freePageCount = ::FreePageCount();
sl@0
   995
    TEST(freePageCount > KSqlCompactFreePageThresholdKb);
sl@0
   996
    TheTest.Printf(_L("   Free pages count = %d\r\n"), freePageCount);
sl@0
   997
  
sl@0
   998
    //Refill the database
sl@0
   999
    PrepareDb(EFalse);
sl@0
  1000
   
sl@0
  1001
    CSqlCompactEntry* impl = compactor->iEntries[0];
sl@0
  1002
    impl->iPageCount = freePageCount;
sl@0
  1003
    err = impl->Compact();
sl@0
  1004
    TEST2(err, KErrNone);
sl@0
  1005
    TEST2(impl->iPageCount, 0);
sl@0
  1006
sl@0
  1007
    compactor->ReleaseEntry(TheDbName);
sl@0
  1008
    delete compactor;
sl@0
  1009
    ::CloseDbHandle(TheDbHandle);
sl@0
  1010
    TheDbHandle = NULL;
sl@0
  1011
    (void)TheFs.Delete(TheDbName);
sl@0
  1012
    }
sl@0
  1013
sl@0
  1014
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
  1015
sl@0
  1016
void DoTests()
sl@0
  1017
	{
sl@0
  1018
	CActiveScheduler* scheduler = new CActiveScheduler;
sl@0
  1019
	TEST(scheduler != NULL);
sl@0
  1020
	CActiveScheduler::Install(scheduler);
sl@0
  1021
	
sl@0
  1022
	CSqlCompactTestActive::New();
sl@0
  1023
	
sl@0
  1024
	TheCompactionSettings.iFreePageThresholdKb = KFreePageThresholdKb;
sl@0
  1025
sl@0
  1026
	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4050 \"Out of memory\" test"));
sl@0
  1027
	TheTestActive->OomTest();
sl@0
  1028
	
sl@0
  1029
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4051 \"File I/O\" error simulation test"));
sl@0
  1030
	TheTestActive->FileIoErrTest();
sl@0
  1031
sl@0
  1032
	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4052 Compaction - performance test"));
sl@0
  1033
	TheTestActive->PerformanceTest();
sl@0
  1034
	
sl@0
  1035
	TheTest.Next(_L(" @SYMTestCaseID:PDS-SQL-CT-4239 Free page update test"));
sl@0
  1036
	TheTestActive->FreePageUpdateTest();
sl@0
  1037
sl@0
  1038
	CActiveScheduler::Start();
sl@0
  1039
	
sl@0
  1040
	delete TheTestActive;
sl@0
  1041
	TheTestActive = NULL;
sl@0
  1042
	delete scheduler;
sl@0
  1043
	}
sl@0
  1044
sl@0
  1045
//////////////////////////////////////////////////////////////////////////////////////////////////
sl@0
  1046
sl@0
  1047
//Usage: "t_sqlcompact2 [<drive letter>:]"
sl@0
  1048
sl@0
  1049
TInt E32Main()
sl@0
  1050
	{
sl@0
  1051
	TheTest.Title();
sl@0
  1052
	
sl@0
  1053
	TheTrapCleanup = CTrapCleanup::New ();
sl@0
  1054
	__ASSERT_ALWAYS(TheTrapCleanup != NULL, User::Invariant());
sl@0
  1055
sl@0
  1056
	__UHEAP_MARK;
sl@0
  1057
sl@0
  1058
	User::CommandLine(TheCmd);
sl@0
  1059
	TheCmd.TrimAll();
sl@0
  1060
	if(TheCmd.Length() > 0)
sl@0
  1061
		{
sl@0
  1062
		TheDriveName.Copy(TheCmd);
sl@0
  1063
		}
sl@0
  1064
sl@0
  1065
	_LIT(KDbName, "c:\\test\\t_sqlcompact2_1.db");
sl@0
  1066
	TheParse.Set(TheDriveName, &KDbName, 0);
sl@0
  1067
	const TDesC& dbFilePath = TheParse.FullName();
sl@0
  1068
	TheDbName.Copy(dbFilePath);
sl@0
  1069
	
sl@0
  1070
	TestEnvCreate();
sl@0
  1071
	
sl@0
  1072
	DoTests();
sl@0
  1073
sl@0
  1074
	TestEnvDestroy();
sl@0
  1075
	
sl@0
  1076
	__UHEAP_MARKEND;
sl@0
  1077
sl@0
  1078
	TheTest.End();
sl@0
  1079
	TheTest.Close();
sl@0
  1080
sl@0
  1081
	delete TheTrapCleanup;
sl@0
  1082
sl@0
  1083
	return KErrNone;
sl@0
  1084
	}