persistentstorage/sql/TEST/t_sqlmulti.cpp
changeset 55 44f437012c90
parent 0 08ec8eefde2f
equal deleted inserted replaced
51:7d4490026038 55:44f437012c90
    41 		DeleteTestFiles();
    41 		DeleteTestFiles();
    42 		if(aPrintThreadName)
    42 		if(aPrintThreadName)
    43 			{
    43 			{
    44 			RThread th;
    44 			RThread th;
    45 			TName name = th.Name();
    45 			TName name = th.Name();
    46 			RDebug::Print(_L("*** Thread %S, Line %d\r\n"), &name, aLine);
    46 			RDebug::Print(_L("*** Thread %S, Line %d. Expression evaluated to false\r\n"), &name, aLine);
    47 			}
    47 			}
    48 		else
    48 		else
    49 			{
    49 			{
    50 			RDebug::Print(_L("*** Line %d\r\n"), aLine);
    50 			TheTest.Printf(_L("*** Line %d. Expression evaluated to false\r\n"), aLine);
    51 			}
    51 			}
    52 		TheTest(EFalse, aLine);
    52 		TheTest(EFalse, aLine);
    53 		}
    53 		}
    54 	}
    54 	}
    55 void Check2(TInt aValue, TInt aExpected, TInt aLine, TBool aPrintThreadName = EFalse)
    55 void Check2(TInt aValue, TInt aExpected, TInt aLine, TBool aPrintThreadName = EFalse)
    63 			TName name = th.Name();
    63 			TName name = th.Name();
    64 			RDebug::Print(_L("*** Thread %S, Line %d Expected error: %d, got: %d\r\n"), &name, aLine, aExpected, aValue);
    64 			RDebug::Print(_L("*** Thread %S, Line %d Expected error: %d, got: %d\r\n"), &name, aLine, aExpected, aValue);
    65 			}
    65 			}
    66 		else
    66 		else
    67 			{
    67 			{
    68 			RDebug::Print(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
    68 			TheTest.Printf(_L("*** Line %d, Expected error: %d, got: %d\r\n"), aLine, aExpected, aValue);
    69 			}
    69 			}
    70 		TheTest(EFalse, aLine);
    70 		TheTest(EFalse, aLine);
    71 		}
    71 		}
    72 	}
    72 	}
    73 #define TEST(arg) ::Check1((arg), __LINE__)
    73 #define TEST(arg) ::Check1((arg), __LINE__)
   109 	RSqlDatabase db1;
   109 	RSqlDatabase db1;
   110 	TInt err = db1.Create(KTestDbName1);
   110 	TInt err = db1.Create(KTestDbName1);
   111 	TEST2(err, KErrNone);
   111 	TEST2(err, KErrNone);
   112 
   112 
   113 	//Create test database
   113 	//Create test database
   114 	RDebug::Print(_L("###Create test database\r\n"));
   114 	TheTest.Printf(_L("###Create test database\r\n"));
   115 	_LIT8(KCreateSql, "CREATE TABLE A(Id INTEGER PRIMARY KEY AUTOINCREMENT, Data INTEGER)");
   115 	_LIT8(KCreateSql, "CREATE TABLE A(Id INTEGER PRIMARY KEY AUTOINCREMENT, Data INTEGER)");
   116 	err = db1.Exec(KCreateSql);
   116 	err = db1.Exec(KCreateSql);
   117 	TEST(err >= 0);
   117 	TEST(err >= 0);
   118 	
   118 	
   119 	//Connection 2
   119 	//Connection 2
   120 	RSqlDatabase db2;
   120 	RSqlDatabase db2;
   121 	err = db2.Open(KTestDbName1);
   121 	err = db2.Open(KTestDbName1);
   122 	TEST2(err, KErrNone);
   122 	TEST2(err, KErrNone);
   123 	
   123 	
   124 	//Insert some records using both connections
   124 	//Insert some records using both connections
   125 	RDebug::Print(_L("###Insert some records\r\n"));
   125 	TheTest.Printf(_L("###Insert some records\r\n"));
   126 	const TInt KRecNum = 100;
   126 	const TInt KRecNum = 100;
   127 	_LIT8(KInsertSql, "INSERT INTO A(Data) VALUES(");
   127 	_LIT8(KInsertSql, "INSERT INTO A(Data) VALUES(");
   128 	for(TInt i=0;i<KRecNum;++i)
   128 	for(TInt i=0;i<KRecNum;++i)
   129 		{
   129 		{
   130 		TBuf8<100> sql(KInsertSql);
   130 		TBuf8<100> sql(KInsertSql);
   132 		sql.Append(_L(");"));
   132 		sql.Append(_L(");"));
   133 		err = (i%2) ? db1.Exec(sql) : db2.Exec(sql);
   133 		err = (i%2) ? db1.Exec(sql) : db2.Exec(sql);
   134 		if(err < 0)
   134 		if(err < 0)
   135 			{
   135 			{
   136 			TPtrC msg = (i%2) ? db1.LastErrorMessage() : db2.LastErrorMessage();
   136 			TPtrC msg = (i%2) ? db1.LastErrorMessage() : db2.LastErrorMessage();
   137 			RDebug::Print(_L("##Db Error msg: \"%S\"\n\r"), &msg);
   137 			TheTest.Printf(_L("##Db Error msg: \"%S\"\n\r"), &msg);
   138 			}
   138 			}
   139 		TEST2(err, 1);
   139 		TEST2(err, 1);
   140 		}
   140 		}
   141 		
   141 		
   142 	//Check the database content
   142 	//Check the database content
   143 	RDebug::Print(_L("###Check the database content\r\n"));
   143 	TheTest.Printf(_L("###Check the database content\r\n"));
   144 	_LIT8(KSelectSql, "SELECT * FROM A");
   144 	_LIT8(KSelectSql, "SELECT * FROM A");
   145 	RSqlStatement stmt;
   145 	RSqlStatement stmt;
   146 	err = stmt.Prepare(db1, KSelectSql);
   146 	err = stmt.Prepare(db1, KSelectSql);
   147 	TEST2(err, KErrNone);
   147 	TEST2(err, KErrNone);
   148 	
   148 	
   159 	stmt.Close();
   159 	stmt.Close();
   160 
   160 
   161 	//Cleanup	
   161 	//Cleanup	
   162 	db2.Close();
   162 	db2.Close();
   163 	db1.Close();
   163 	db1.Close();
   164 	RDebug::Print(_L("###Delete the test database\r\n"));
   164 	TheTest.Printf(_L("###Delete the test database\r\n"));
   165 	(void)RSqlDatabase::Delete(KTestDbName1);
   165 	(void)RSqlDatabase::Delete(KTestDbName1);
   166 	}
   166 	}
   167 
   167 
   168 ///////////////////////////////////////////////////////////////////////////////////////
   168 ///////////////////////////////////////////////////////////////////////////////////////
   169 
   169 
   249                         REQ5793
   249                         REQ5793
   250 */	
   250 */	
   251 void TestMultiConnDiffThread()
   251 void TestMultiConnDiffThread()
   252 	{
   252 	{
   253 	//Create a test database
   253 	//Create a test database
   254 	RDebug::Print(_L("+++:MainThread: Create test database\r\n"));
   254 	TheTest.Printf(_L("+++:MainThread: Create test database\r\n"));
   255 	RSqlDatabase db;
   255 	RSqlDatabase db;
   256 	TInt err = db.Create(KTestDbName1);
   256 	TInt err = db.Create(KTestDbName1);
   257 	TEST2(err, KErrNone);
   257 	TEST2(err, KErrNone);
   258 	
   258 	
   259 	//Create a test table
   259 	//Create a test table
   260 	RDebug::Print(_L("+++:MainThread: Create a table in the test database\r\n"));
   260 	TheTest.Printf(_L("+++:MainThread: Create a table in the test database\r\n"));
   261 	_LIT8(KCreateSql, "CREATE TABLE A(Id INTEGER PRIMARY KEY)");
   261 	_LIT8(KCreateSql, "CREATE TABLE A(Id INTEGER PRIMARY KEY)");
   262 	err = db.Exec(KCreateSql);
   262 	err = db.Exec(KCreateSql);
   263 	TEST(err >= 0);
   263 	TEST(err >= 0);
   264 	
   264 	
   265 	const TInt KThreadCnt = 4;
   265 	const TInt KThreadCnt = 4;
   281 		for(TInt isolLevel=0;isolLevel<KIsolationLevelCnt;++isolLevel)
   281 		for(TInt isolLevel=0;isolLevel<KIsolationLevelCnt;++isolLevel)
   282 			{
   282 			{
   283 			TInt low = 1;
   283 			TInt low = 1;
   284 			TInt high = KRange;
   284 			TInt high = KRange;
   285 			
   285 			
   286 			RDebug::Print(_L("+++:MainThread: Test: thread count %d, records %d, trans type %d, isolation level: %S\r\n"), 
   286 			TheTest.Printf(_L("+++:MainThread: Test: thread count %d, records %d, trans type %d, isolation level: %S\r\n"), 
   287 									KThreadCnt, KRange, transType, &KIsolationLevelName[isolLevel]);
   287 									KThreadCnt, KRange, transType, &KIsolationLevelName[isolLevel]);
   288 									
   288 									
   289 			RThread thread[KThreadCnt];
   289 			RThread thread[KThreadCnt];
   290 			TRequestStatus status[KThreadCnt];
   290 			TRequestStatus status[KThreadCnt];
   291 			TThreadData	data[KThreadCnt];
   291 			TThreadData	data[KThreadCnt];
   318 				TEST(thread[j].ExitType() != EExitPanic);
   318 				TEST(thread[j].ExitType() != EExitPanic);
   319 				thread[j].Close();
   319 				thread[j].Close();
   320 				}
   320 				}
   321 
   321 
   322 			//Check that all records which are esupposed to be in the database, are there.
   322 			//Check that all records which are esupposed to be in the database, are there.
   323 			RDebug::Print(_L("+++:MainThread: Check that all records have been written\r\n"));
   323 			TheTest.Printf(_L("+++:MainThread: Check that all records have been written\r\n"));
   324 			_LIT8(KSelectSql1, "SELECT COUNT(*) FROM A;");
   324 			_LIT8(KSelectSql1, "SELECT COUNT(*) FROM A;");
   325 			RSqlStatement stmt;
   325 			RSqlStatement stmt;
   326 			err = stmt.Prepare(db, KSelectSql1);
   326 			err = stmt.Prepare(db, KSelectSql1);
   327 			TEST2(err, KErrNone);
   327 			TEST2(err, KErrNone);
   328 			err = stmt.Next();
   328 			err = stmt.Next();
   330 			TInt cnt = stmt.ColumnInt(0);
   330 			TInt cnt = stmt.ColumnInt(0);
   331 			TEST2(cnt, KThreadCnt * KRange);
   331 			TEST2(cnt, KThreadCnt * KRange);
   332 			stmt.Close();
   332 			stmt.Close();
   333 			
   333 			
   334 			//Check that all records have expected column values.
   334 			//Check that all records have expected column values.
   335 			RDebug::Print(_L("+++:MainThread: Check that all records have expected column values\r\n"));
   335 			TheTest.Printf(_L("+++:MainThread: Check that all records have expected column values\r\n"));
   336 			_LIT8(KSelectSql2, "SELECT * FROM A;");
   336 			_LIT8(KSelectSql2, "SELECT * FROM A;");
   337 			err = stmt.Prepare(db, KSelectSql2);
   337 			err = stmt.Prepare(db, KSelectSql2);
   338 			TEST2(err, KErrNone);
   338 			TEST2(err, KErrNone);
   339 			for(TInt k=0;k<(KThreadCnt*KRange);++k)
   339 			for(TInt k=0;k<(KThreadCnt*KRange);++k)
   340 				{
   340 				{
   344 				TEST(val > 0 && val <= (KThreadCnt * KRange));
   344 				TEST(val > 0 && val <= (KThreadCnt * KRange));
   345 				}
   345 				}
   346 			stmt.Close();
   346 			stmt.Close();
   347 
   347 
   348 			//Prepare for the next test run - delete all records.
   348 			//Prepare for the next test run - delete all records.
   349 			RDebug::Print(_L("+++:MainThread: Delete all records\r\n"));
   349 			TheTest.Printf(_L("+++:MainThread: Delete all records\r\n"));
   350 			_LIT8(KDeleteSql, "DELETE FROM A");
   350 			_LIT8(KDeleteSql, "DELETE FROM A");
   351 			err = db.Exec(KDeleteSql);
   351 			err = db.Exec(KDeleteSql);
   352 			TEST(err >= 0);
   352 			TEST(err >= 0);
   353 			}//end of "for(TInt isolLevel=0;isolLevel<KIsolationLevelCnt;++isolLevel)"
   353 			}//end of "for(TInt isolLevel=0;isolLevel<KIsolationLevelCnt;++isolLevel)"
   354 		}//end of "for(TInt transType=0;transType<KTransTypeCnt;++transType)"
   354 		}//end of "for(TInt transType=0;transType<KTransTypeCnt;++transType)"
   355 		
   355 		
   356 	db.Close();
   356 	db.Close();
   357 	RDebug::Print(_L("+++:MainThread: Delete the test database\r\n"));
   357 	TheTest.Printf(_L("+++:MainThread: Delete the test database\r\n"));
   358 	(void)RSqlDatabase::Delete(KTestDbName1);
   358 	(void)RSqlDatabase::Delete(KTestDbName1);
   359 	}
   359 	}
   360 
   360 
   361 ///////////////////////////////////////////////////////////////////////////////////////
   361 ///////////////////////////////////////////////////////////////////////////////////////
   362 
   362 
   426 @SYMREQ					REQ5792
   426 @SYMREQ					REQ5792
   427                         REQ5793
   427                         REQ5793
   428 */	
   428 */	
   429 void TestIsolationLevel()
   429 void TestIsolationLevel()
   430 	{
   430 	{
   431 	RDebug::Print(_L("+++:MainThread: Create critical sections\r\n"));
   431 	TheTest.Printf(_L("+++:MainThread: Create critical sections\r\n"));
   432 	TEST2(UpdateThreadCrS.CreateLocal(), KErrNone);
   432 	TEST2(UpdateThreadCrS.CreateLocal(), KErrNone);
   433 	UpdateThreadCrS.Wait();
   433 	UpdateThreadCrS.Wait();
   434 	TEST2(MainThreadCrS.CreateLocal(), KErrNone);
   434 	TEST2(MainThreadCrS.CreateLocal(), KErrNone);
   435 	MainThreadCrS.Wait();
   435 	MainThreadCrS.Wait();
   436 	
   436 	
   437 	RDebug::Print(_L("+++:MainThread: Create test database\r\n"));
   437 	TheTest.Printf(_L("+++:MainThread: Create test database\r\n"));
   438 	RSqlDatabase db;
   438 	RSqlDatabase db;
   439 	TInt err = db.Create(KTestDbName1);
   439 	TInt err = db.Create(KTestDbName1);
   440 	TEST2(err, KErrNone);
   440 	TEST2(err, KErrNone);
   441 
   441 
   442 	RDebug::Print(_L("+++:MainThread: Set the isolation level to \"Read uncommitted\"\r\n"));
   442 	TheTest.Printf(_L("+++:MainThread: Set the isolation level to \"Read uncommitted\"\r\n"));
   443 	err = db.SetIsolationLevel(RSqlDatabase::EReadUncommitted);
   443 	err = db.SetIsolationLevel(RSqlDatabase::EReadUncommitted);
   444 	TEST2(err, KErrNone);
   444 	TEST2(err, KErrNone);
   445 	
   445 	
   446 	RDebug::Print(_L("+++:MainThread: Create a table in the test database\r\n"));
   446 	TheTest.Printf(_L("+++:MainThread: Create a table in the test database\r\n"));
   447 	_LIT8(KCreateSql, "CREATE TABLE A(Id INTEGER)");
   447 	_LIT8(KCreateSql, "CREATE TABLE A(Id INTEGER)");
   448 	err = db.Exec(KCreateSql);
   448 	err = db.Exec(KCreateSql);
   449 	TEST(err >= 0);
   449 	TEST(err >= 0);
   450 
   450 
   451 	RDebug::Print(_L("+++:MainThread: Insert one record in the table\r\n"));
   451 	TheTest.Printf(_L("+++:MainThread: Insert one record in the table\r\n"));
   452 	_LIT8(KInsertSql, "INSERT INTO A(Id) VALUES(");
   452 	_LIT8(KInsertSql, "INSERT INTO A(Id) VALUES(");
   453 	TBuf8<64> sql(KInsertSql);
   453 	TBuf8<64> sql(KInsertSql);
   454 	sql.AppendNum((TInt64)KInitialValue);
   454 	sql.AppendNum((TInt64)KInitialValue);
   455 	sql.Append(_L(")"));
   455 	sql.Append(_L(")"));
   456 	err = db.Exec(sql);
   456 	err = db.Exec(sql);
   457 	TEST2(err, 1);
   457 	TEST2(err, 1);
   458 
   458 
   459 	RDebug::Print(_L("+++:MainThread: Create the \"update\" thread\r\n"));
   459 	TheTest.Printf(_L("+++:MainThread: Create the \"update\" thread\r\n"));
   460 	_LIT(KThreadName, "UpdTh");
   460 	_LIT(KThreadName, "UpdTh");
   461 	RThread thread;
   461 	RThread thread;
   462 	TEST2(thread.Create(KThreadName, &UpdateThreadFunc, 0x2000, 0x1000, 0x10000, NULL, EOwnerThread), KErrNone);
   462 	TEST2(thread.Create(KThreadName, &UpdateThreadFunc, 0x2000, 0x1000, 0x10000, NULL, EOwnerThread), KErrNone);
   463 	TRequestStatus status;
   463 	TRequestStatus status;
   464 	thread.Logon(status);
   464 	thread.Logon(status);
   465 	TEST2(status.Int(), KRequestPending);
   465 	TEST2(status.Int(), KRequestPending);
   466 	thread.Resume();
   466 	thread.Resume();
   467 
   467 
   468 	RDebug::Print(_L("+++:MainThread: Wait for record update completion...\r\n"));
   468 	TheTest.Printf(_L("+++:MainThread: Wait for record update completion...\r\n"));
   469 	MainThreadCrS.Wait();
   469 	MainThreadCrS.Wait();
   470 
   470 
   471 	RDebug::Print(_L("+++:MainThread: Read the record and check the data...\r\n"));
   471 	TheTest.Printf(_L("+++:MainThread: Read the record and check the data...\r\n"));
   472 	_LIT8(KSelectSql, "SELECT * FROM A");
   472 	_LIT8(KSelectSql, "SELECT * FROM A");
   473 	RSqlStatement stmt;
   473 	RSqlStatement stmt;
   474 	err = stmt.Prepare(db, KSelectSql);
   474 	err = stmt.Prepare(db, KSelectSql);
   475 	TEST2(err, KErrNone);
   475 	TEST2(err, KErrNone);
   476 	err = stmt.Next();
   476 	err = stmt.Next();
   477 	TEST2(err, KSqlAtRow);
   477 	TEST2(err, KSqlAtRow);
   478 	TInt val = stmt.ColumnInt(0);
   478 	TInt val = stmt.ColumnInt(0);
   479 	TEST(val == KUpdatedValue);
   479 	TEST(val == KUpdatedValue);
   480 	stmt.Close();
   480 	stmt.Close();
   481 
   481 
   482 	RDebug::Print(_L("+++:MainThread: Notify the update thread that it can rollback\r\n"));
   482 	TheTest.Printf(_L("+++:MainThread: Notify the update thread that it can rollback\r\n"));
   483 	UpdateThreadCrS.Signal();
   483 	UpdateThreadCrS.Signal();
   484 
   484 
   485 	RDebug::Print(_L("+++:MainThread: Wait for  rollback  completion...\r\n"));
   485 	TheTest.Printf(_L("+++:MainThread: Wait for  rollback  completion...\r\n"));
   486 	MainThreadCrS.Wait();
   486 	MainThreadCrS.Wait();
   487 
   487 
   488 	RDebug::Print(_L("+++:MainThread: Read the record and check the data...\r\n"));
   488 	TheTest.Printf(_L("+++:MainThread: Read the record and check the data...\r\n"));
   489 	err = stmt.Prepare(db, KSelectSql);
   489 	err = stmt.Prepare(db, KSelectSql);
   490 	TEST2(err, KErrNone);
   490 	TEST2(err, KErrNone);
   491 	err = stmt.Next();
   491 	err = stmt.Next();
   492 	TEST2(err, KSqlAtRow);
   492 	TEST2(err, KSqlAtRow);
   493 	val = stmt.ColumnInt(0);
   493 	val = stmt.ColumnInt(0);
   496 
   496 
   497 	User::WaitForRequest(status);
   497 	User::WaitForRequest(status);
   498 	thread.Close();
   498 	thread.Close();
   499 
   499 
   500 	db.Close();
   500 	db.Close();
   501 	RDebug::Print(_L("+++:MainThread: Delete the test database\r\n"));
   501 	TheTest.Printf(_L("+++:MainThread: Delete the test database\r\n"));
   502 	(void)RSqlDatabase::Delete(KTestDbName1);
   502 	(void)RSqlDatabase::Delete(KTestDbName1);
   503 
   503 
   504 	RDebug::Print(_L("+++:MainThread: Close critical sections\r\n"));
   504 	TheTest.Printf(_L("+++:MainThread: Close critical sections\r\n"));
   505 	MainThreadCrS.Close();
   505 	MainThreadCrS.Close();
   506 	UpdateThreadCrS.Close();
   506 	UpdateThreadCrS.Close();
   507 	}
   507 	}
   508 
   508 
   509 ///////////////////////////////////////////////////////////////////////////////////////
   509 ///////////////////////////////////////////////////////////////////////////////////////