1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
1 // Copyright (c) 2008-2010 Nokia Corporation and/or its subsidiary(-ies). |
2 // All rights reserved. |
2 // All rights reserved. |
3 // This component and the accompanying materials are made available |
3 // This component and the accompanying materials are made available |
4 // under the terms of "Eclipse Public License v1.0" |
4 // under the terms of "Eclipse Public License v1.0" |
5 // which accompanies this distribution, and is available |
5 // which accompanies this distribution, and is available |
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
75 #define TEST(arg) ::Check((arg), __LINE__) |
75 #define TEST(arg) ::Check((arg), __LINE__) |
76 #define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__) |
76 #define TEST2(aValue, aExpected) ::Check2(aValue, aExpected, __LINE__) |
77 |
77 |
78 /////////////////////////////////////////////////////////////////////////////////////// |
78 /////////////////////////////////////////////////////////////////////////////////////// |
79 |
79 |
|
80 //t_sqlcompact1 timeouts in WDP builds. |
|
81 //This function is used to check whether the time limit is reaqched or not. |
|
82 TBool IsTimeLimitReached() |
|
83 { |
|
84 struct TStartTime |
|
85 { |
|
86 TStartTime() |
|
87 { |
|
88 iTime.HomeTime(); |
|
89 } |
|
90 TTime iTime; |
|
91 }; |
|
92 |
|
93 static TStartTime startTime; |
|
94 const TInt KTestTimeLimit = 100;//seconds |
|
95 |
|
96 TTime currTime; |
|
97 currTime.HomeTime(); |
|
98 |
|
99 TTimeIntervalSeconds s; |
|
100 TInt err = currTime.SecondsFrom(startTime.iTime, s); |
|
101 TEST2(err, KErrNone); |
|
102 return s.Int() > KTestTimeLimit; |
|
103 } |
|
104 |
|
105 void GetHomeTimeAsString(TDes& aStr) |
|
106 { |
|
107 TTime time; |
|
108 time.HomeTime(); |
|
109 TDateTime dt = time.DateTime(); |
|
110 aStr.Format(_L("%02d:%02d:%02d.%06d"), dt.Hour(), dt.Minute(), dt.Second(), dt.MicroSecond()); |
|
111 } |
|
112 |
80 void CreateTestEnv() |
113 void CreateTestEnv() |
81 { |
114 { |
82 RFs fs; |
115 RFs fs; |
83 TInt err = fs.Connect(); |
116 TInt err = fs.Connect(); |
84 TEST2(err, KErrNone); |
117 TEST2(err, KErrNone); |
114 |
147 |
115 void InsertRecords(const TDesC& aDbName) |
148 void InsertRecords(const TDesC& aDbName) |
116 { |
149 { |
117 TInt err = TheDb.Open(aDbName); |
150 TInt err = TheDb.Open(aDbName); |
118 TEST2(err, KErrNone); |
151 TEST2(err, KErrNone); |
|
152 err = TheDb.Exec(_L("BEGIN TRANSACTION")); |
|
153 TEST(err >= 0); |
119 TheText.SetLength(TheText.MaxLength()); |
154 TheText.SetLength(TheText.MaxLength()); |
120 TheText.Fill(TChar('A')); |
155 TheText.Fill(TChar('A')); |
121 for(TInt i=0;i<100;++i) |
156 for(TInt i=0;i<100;++i) |
122 { |
157 { |
123 TheSqlBuf.Format(_L("INSERT INTO A VALUES(%d, '%S')"), i + 1, &TheText); |
158 TheSqlBuf.Format(_L("INSERT INTO A VALUES(%d, '%S')"), i + 1, &TheText); |
124 err = TheDb.Exec(TheSqlBuf); |
159 err = TheDb.Exec(TheSqlBuf); |
125 TEST2(err, 1); |
160 TEST2(err, 1); |
126 } |
161 } |
|
162 err = TheDb.Exec(_L("COMMIT TRANSACTION")); |
|
163 TEST(err >= 0); |
127 TheDb.Close(); |
164 TheDb.Close(); |
128 } |
165 } |
129 |
166 |
130 TInt DeleteRecords(const TDesC& aDbName, TInt aPageCount, TInt aPageSize) |
167 TInt DeleteRecords(const TDesC& aDbName, TInt aPageCount, TInt aPageSize) |
131 { |
168 { |
132 TInt freePageCount = -1; |
169 TInt freePageCount = -1; |
133 TInt err = TheDb.Open(aDbName); |
170 TInt err = TheDb.Open(aDbName); |
134 TEST2(err, KErrNone); |
171 TEST2(err, KErrNone); |
|
172 err = TheDb.Exec(_L("BEGIN TRANSACTION")); |
|
173 TEST(err >= 0); |
135 for(TInt i=0;;++i) |
174 for(TInt i=0;;++i) |
136 { |
175 { |
137 TheSqlBuf.Format(_L("DELETE FROM A WHERE I=%d"), i + 1); |
176 TheSqlBuf.Format(_L("DELETE FROM A WHERE I=%d"), i + 1); |
138 err = TheDb.Exec(TheSqlBuf); |
177 err = TheDb.Exec(TheSqlBuf); |
139 TEST2(err, 1); |
178 TEST2(err, 1); |
175 TInt freePageCount[] ={0, 0, 0, 0}; |
216 TInt freePageCount[] ={0, 0, 0, 0}; |
176 const TInt KSize = sizeof(KDbName) / sizeof(KDbName[0]); |
217 const TInt KSize = sizeof(KDbName) / sizeof(KDbName[0]); |
177 |
218 |
178 TInt i; |
219 TInt i; |
179 |
220 |
|
221 TBuf<50> timeBuf; |
|
222 GetHomeTimeAsString(timeBuf); |
|
223 TheTest.Printf(_L("===Time1: %S\r\n"), &timeBuf); |
|
224 |
180 //Create databases, tables, insert records, delete part of the just inserted records. |
225 //Create databases, tables, insert records, delete part of the just inserted records. |
181 for(i=0;i<KSize;++i) |
226 for(i=0;i<KSize;++i) |
182 { |
227 { |
183 ReplaceDb(KDbName[i], KDbPageSize[i]); |
228 ReplaceDb(KDbName[i], KDbPageSize[i]); |
184 CreateTable(KDbName[i]); |
229 CreateTable(KDbName[i]); |
185 InsertRecords(KDbName[i]); |
230 InsertRecords(KDbName[i]); |
186 freePageCount[i] = DeleteRecords(KDbName[i], KFreePageCount[i], KDbPageSize[i]); |
231 freePageCount[i] = DeleteRecords(KDbName[i], KFreePageCount[i], KDbPageSize[i]); |
187 } |
232 } |
188 |
233 |
|
234 GetHomeTimeAsString(timeBuf); |
|
235 TheTest.Printf(_L("===Time2: %S\r\n"), &timeBuf); |
|
236 |
189 //Open the first database, attach all others. |
237 //Open the first database, attach all others. |
190 TInt err = TheDb.Open(KDbName1()); |
238 TInt err = TheDb.Open(KDbName1()); |
191 TEST2(err, KErrNone); |
239 TEST2(err, KErrNone); |
192 for(i=0;i<KSize;++i) |
240 for(i=0;i<KSize;++i) |
193 { |
241 { |
194 err = TheDb.Attach(KDbName[i], KDbAttachName[i]); |
242 err = TheDb.Attach(KDbName[i], KDbAttachName[i]); |
195 TEST2(err, KErrNone); |
243 TEST2(err, KErrNone); |
196 } |
244 } |
|
245 |
|
246 GetHomeTimeAsString(timeBuf); |
|
247 TheTest.Printf(_L("===Time3: %S\r\n"), &timeBuf); |
197 |
248 |
198 //Check the size of the main database. |
249 //Check the size of the main database. |
199 RSqlDatabase::TSize size; |
250 RSqlDatabase::TSize size; |
200 err = TheDb.Size(size); |
251 err = TheDb.Size(size); |
201 TEST2(err, KErrNone); |
252 TEST2(err, KErrNone); |
217 TInt count = size.iFree / KDbPageSize[i]; |
268 TInt count = size.iFree / KDbPageSize[i]; |
218 expected = freePageCount[i] - KCompactPageCount; |
269 expected = freePageCount[i] - KCompactPageCount; |
219 TEST2(count, expected); |
270 TEST2(count, expected); |
220 } |
271 } |
221 |
272 |
|
273 GetHomeTimeAsString(timeBuf); |
|
274 TheTest.Printf(_L("===Time4: %S\r\n"), &timeBuf); |
|
275 |
222 //Detach databases and close the main database. |
276 //Detach databases and close the main database. |
223 for(i=0;i<KSize;++i) |
277 for(i=0;i<KSize;++i) |
224 { |
278 { |
225 err = TheDb.Detach(KDbAttachName[i]); |
279 err = TheDb.Detach(KDbAttachName[i]); |
226 TEST2(err, KErrNone); |
280 TEST2(err, KErrNone); |
227 } |
281 } |
228 TheDb.Close(); |
282 TheDb.Close(); |
229 |
283 |
|
284 GetHomeTimeAsString(timeBuf); |
|
285 TheTest.Printf(_L("===Time5: %S\r\n"), &timeBuf); |
|
286 |
230 //Cleanup. |
287 //Cleanup. |
231 for(i=0;i<KSize;++i) |
288 for(i=0;i<KSize;++i) |
232 { |
289 { |
233 (void)RSqlDatabase::Delete(KDbName[i]); |
290 (void)RSqlDatabase::Delete(KDbName[i]); |
234 } |
291 } |
250 config.Append(_L(";compaction=manual;")); |
307 config.Append(_L(";compaction=manual;")); |
251 } |
308 } |
252 TInt err = TheDb.Create(KDbName1, &config); |
309 TInt err = TheDb.Create(KDbName1, &config); |
253 TEST2(err, KErrNone); |
310 TEST2(err, KErrNone); |
254 |
311 |
|
312 err = TheDb.Exec(_L("BEGIN TRANSACTION")); |
|
313 TEST(err >= 0); |
255 err = TheDb.Exec(_L("CREATE TABLE A(I INTEGER, T TEXT)")); |
314 err = TheDb.Exec(_L("CREATE TABLE A(I INTEGER, T TEXT)")); |
256 TEST(err >= 0); |
315 TEST(err >= 0); |
257 //Insert records |
316 //Insert records |
258 TheText.SetLength(TheText.MaxLength()); |
317 TheText.SetLength(TheText.MaxLength()); |
259 TheText.Fill(TChar('A')); |
318 TheText.Fill(TChar('A')); |
261 { |
320 { |
262 TheSqlBuf.Format(_L("INSERT INTO A VALUES(%d, '%S')"), i + 1, &TheText); |
321 TheSqlBuf.Format(_L("INSERT INTO A VALUES(%d, '%S')"), i + 1, &TheText); |
263 err = TheDb.Exec(TheSqlBuf); |
322 err = TheDb.Exec(TheSqlBuf); |
264 TEST2(err, 1); |
323 TEST2(err, 1); |
265 } |
324 } |
|
325 err = TheDb.Exec(_L("COMMIT TRANSACTION")); |
|
326 TEST(err >= 0); |
266 //Delete all records making a lot of free pages. This operation should kick-off the background compaction |
327 //Delete all records making a lot of free pages. This operation should kick-off the background compaction |
267 err = TheDb.Exec(_L("DELETE FROM A WHERE 1")); |
328 err = TheDb.Exec(_L("DELETE FROM A WHERE 1")); |
268 TEST2(err, aRecordCount); |
329 TEST2(err, aRecordCount); |
269 } |
330 } |
270 |
331 |
286 @SYMREQ REQ10271 |
347 @SYMREQ REQ10271 |
287 REQ10272 |
348 REQ10272 |
288 */ |
349 */ |
289 void CompactDbTest2() |
350 void CompactDbTest2() |
290 { |
351 { |
|
352 TBuf<50> timeBuf; |
|
353 GetHomeTimeAsString(timeBuf); |
|
354 TheTest.Printf(_L("===Time1: %S\r\n"), &timeBuf); |
|
355 |
291 const TInt KPageSize = 1024; |
356 const TInt KPageSize = 1024; |
292 //Number of records to be added and removed from database. Need to be increased when testing on a faster |
357 //Number of records to be added and removed from database. Need to be increased when testing on a faster |
293 // hardware, otherwise at fastest case the background compaction could be finished in just 1 step. |
358 // hardware, otherwise at fastest case the background compaction could be finished in just 1 step. |
294 const TInt KRecordCount = 2000; |
359 const TInt KRecordCount = 2000; |
295 PrepareDb(KPageSize, KRecordCount); |
360 PrepareDb(KPageSize, KRecordCount); |
|
361 |
|
362 GetHomeTimeAsString(timeBuf); |
|
363 TheTest.Printf(_L("===Time2: %S\r\n"), &timeBuf); |
296 |
364 |
297 //Check the free space-1 |
365 //Check the free space-1 |
298 RSqlDatabase::TSize size1; |
366 RSqlDatabase::TSize size1; |
299 TInt err = TheDb.Size(size1); |
367 TInt err = TheDb.Size(size1); |
300 TEST2(err, KErrNone); |
368 TEST2(err, KErrNone); |
319 } |
387 } |
320 else |
388 else |
321 { |
389 { |
322 TEST(size2.iFree == size1.iFree); |
390 TEST(size2.iFree == size1.iFree); |
323 } |
391 } |
|
392 |
|
393 GetHomeTimeAsString(timeBuf); |
|
394 TheTest.Printf(_L("===Time3: %S\r\n"), &timeBuf); |
324 |
395 |
325 //Wait (KSqlCompactStepIntervalMs + KSqlCompactStepLengthMs) ms. During the pause only part of the free pages |
396 //Wait (KSqlCompactStepIntervalMs + KSqlCompactStepLengthMs) ms. During the pause only part of the free pages |
326 //should be removed (whatever can be completed for KSqlCompactStepLengthMs ms). |
397 //should be removed (whatever can be completed for KSqlCompactStepLengthMs ms). |
327 User::After((KSqlCompactStepIntervalMs + KSqlCompactStepLengthMs) * 1000); |
398 User::After((KSqlCompactStepIntervalMs + KSqlCompactStepLengthMs) * 1000); |
328 //Check the free space-3 |
399 //Check the free space-3 |
333 if(size3.iFree == 0) |
404 if(size3.iFree == 0) |
334 { |
405 { |
335 TheTest.Printf(_L("WARNING: Background compaction finished in 1 step. Initial number of records need to be increased.\r\n")); |
406 TheTest.Printf(_L("WARNING: Background compaction finished in 1 step. Initial number of records need to be increased.\r\n")); |
336 } |
407 } |
337 TEST(size3.iFree > 0 && size3.iFree < size2.iFree); |
408 TEST(size3.iFree > 0 && size3.iFree < size2.iFree); |
|
409 |
|
410 GetHomeTimeAsString(timeBuf); |
|
411 TheTest.Printf(_L("===Time4: %S\r\n"), &timeBuf); |
338 |
412 |
339 //Wait another (KSqlCompactStepIntervalMs + KSqlCompactStepLengthMs) ms. During the pause only part of the free pages |
413 //Wait another (KSqlCompactStepIntervalMs + KSqlCompactStepLengthMs) ms. During the pause only part of the free pages |
340 //should be removed (whatever can be completed for KSqlCompactStepLengthMs ms). |
414 //should be removed (whatever can be completed for KSqlCompactStepLengthMs ms). |
341 User::After((KSqlCompactStepIntervalMs + KSqlCompactStepLengthMs) * 1000); |
415 User::After((KSqlCompactStepIntervalMs + KSqlCompactStepLengthMs) * 1000); |
342 //Check the free space-4 |
416 //Check the free space-4 |
343 RSqlDatabase::TSize size4; |
417 RSqlDatabase::TSize size4; |
344 err = TheDb.Size(size4); |
418 err = TheDb.Size(size4); |
345 TEST2(err, KErrNone); |
419 TEST2(err, KErrNone); |
346 TheTest.Printf(_L("===Free space after compaction-3, pages=%d\r\n"), size4.iFree / KPageSize); |
420 TheTest.Printf(_L("===Free space after compaction-3, pages=%d\r\n"), size4.iFree / KPageSize); |
347 TEST((size4.iFree > 0 && size4.iFree < size3.iFree) || (size4.iFree == 0)); |
421 TEST((size4.iFree > 0 && size4.iFree < size3.iFree) || (size4.iFree == 0)); |
|
422 |
|
423 GetHomeTimeAsString(timeBuf); |
|
424 TheTest.Printf(_L("===Time5: %S\r\n"), &timeBuf); |
348 |
425 |
349 //Cleanup |
426 //Cleanup |
350 TheDb.Close(); |
427 TheDb.Close(); |
351 (void)RSqlDatabase::Delete(KDbName1); |
428 (void)RSqlDatabase::Delete(KDbName1); |
352 } |
429 } |
368 @SYMREQ REQ10271 |
445 @SYMREQ REQ10271 |
369 REQ10272 |
446 REQ10272 |
370 */ |
447 */ |
371 void CompactDbTest3() |
448 void CompactDbTest3() |
372 { |
449 { |
|
450 TBuf<50> timeBuf; |
|
451 GetHomeTimeAsString(timeBuf); |
|
452 TheTest.Printf(_L("===Time1: %S\r\n"), &timeBuf); |
|
453 |
373 const TInt KPageSize = 1024; |
454 const TInt KPageSize = 1024; |
374 const TInt KRecordCount = 1000; |
455 const TInt KRecordCount = 1000; |
375 PrepareDb(KPageSize, KRecordCount); |
456 PrepareDb(KPageSize, KRecordCount); |
376 |
457 |
|
458 GetHomeTimeAsString(timeBuf); |
|
459 TheTest.Printf(_L("===Time2: %S\r\n"), &timeBuf); |
|
460 |
377 //Check the free space-1 |
461 //Check the free space-1 |
378 RSqlDatabase::TSize size1; |
462 RSqlDatabase::TSize size1; |
379 TInt err = TheDb.Size(size1); |
463 TInt err = TheDb.Size(size1); |
380 TEST2(err, KErrNone); |
464 TEST2(err, KErrNone); |
381 TheTest.Printf(_L("===Free space before operations, pages=%d. Db.Size=%d, Db.Free=%d\r\n"), size1.iFree / KPageSize, size1.iSize, size1.iFree); |
465 TheTest.Printf(_L("===Free space before operations, pages=%d. Db.Size=%d, Db.Free=%d\r\n"), size1.iFree / KPageSize, size1.iSize, size1.iFree); |
404 if(time > ((KSqlCompactStepIntervalMs + KSqlCompactStepLengthMs))) |
488 if(time > ((KSqlCompactStepIntervalMs + KSqlCompactStepLengthMs))) |
405 { |
489 { |
406 break; |
490 break; |
407 } |
491 } |
408 } |
492 } |
|
493 |
|
494 GetHomeTimeAsString(timeBuf); |
|
495 TheTest.Printf(_L("===Time3: %S\r\n"), &timeBuf); |
|
496 |
409 //Check the free space-2 |
497 //Check the free space-2 |
410 RSqlDatabase::TSize size2; |
498 RSqlDatabase::TSize size2; |
411 err = TheDb.Size(size2); |
499 err = TheDb.Size(size2); |
412 TEST2(err, KErrNone); |
500 TEST2(err, KErrNone); |
413 TheTest.Printf(_L("===%d operations completed for %d ms\r\n"), count, time); |
501 TheTest.Printf(_L("===%d operations completed for %d ms\r\n"), count, time); |
430 @SYMREQ REQ10271 |
518 @SYMREQ REQ10271 |
431 REQ10272 |
519 REQ10272 |
432 */ |
520 */ |
433 void ManualCompactTest() |
521 void ManualCompactTest() |
434 { |
522 { |
|
523 TBuf<50> timeBuf; |
|
524 GetHomeTimeAsString(timeBuf); |
|
525 TheTest.Printf(_L("===Time1: %S\r\n"), &timeBuf); |
|
526 |
435 //Create a database with 1000 free pages |
527 //Create a database with 1000 free pages |
436 const TInt KPageSize = 1024; |
528 const TInt KPageSize = 1024; |
437 const TInt KRecordCount = 1000; |
529 const TInt KRecordCount = 1000; |
438 PrepareDb(KPageSize, KRecordCount, ETrue);//create the database with manual compaction mode |
530 PrepareDb(KPageSize, KRecordCount, ETrue);//create the database with manual compaction mode |
|
531 |
|
532 GetHomeTimeAsString(timeBuf); |
|
533 TheTest.Printf(_L("===Time2: %S\r\n"), &timeBuf); |
|
534 |
439 //Check the free space-1 |
535 //Check the free space-1 |
440 RSqlDatabase::TSize size1; |
536 RSqlDatabase::TSize size1; |
441 TInt err = TheDb.Size(size1); |
537 TInt err = TheDb.Size(size1); |
442 TEST2(err, KErrNone); |
538 TEST2(err, KErrNone); |
443 const TInt KFreePageCount = size1.iFree / KPageSize; |
539 const TInt KFreePageCount = size1.iFree / KPageSize; |
444 TheTest.Printf(_L("===Free space before operations, pages=%d\r\n"), KFreePageCount); |
540 TheTest.Printf(_L("===Free space before operations, pages=%d\r\n"), KFreePageCount); |
445 TEST(size1.iSize >= (KRecordCount * KPageSize)); |
541 TEST(size1.iSize >= (KRecordCount * KPageSize)); |
446 //Compact |
542 //Compact |
447 err = TheDb.Compact(RSqlDatabase::EMaxCompaction); |
543 err = TheDb.Compact(RSqlDatabase::EMaxCompaction); |
448 TEST2(err, size1.iFree); |
544 TEST2(err, size1.iFree); |
|
545 |
|
546 GetHomeTimeAsString(timeBuf); |
|
547 TheTest.Printf(_L("===Time3: %S\r\n"), &timeBuf); |
|
548 |
449 //Cleanup |
549 //Cleanup |
450 TheDb.Close(); |
550 TheDb.Close(); |
451 (void)RSqlDatabase::Delete(KDbName1); |
551 (void)RSqlDatabase::Delete(KDbName1); |
452 } |
552 } |
453 |
553 |
454 void DoTestsL() |
554 void DoTestsL() |
455 { |
555 { |
456 TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4072 Manual Compact() - attached databases, different page sizes")); |
556 TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-SQL-UT-4072 Manual Compact() - attached databases, different page sizes")); |
457 CompactDbTest1(); |
557 CompactDbTest1(); |
458 |
558 |
|
559 if(IsTimeLimitReached()) |
|
560 { |
|
561 TheTest.Printf(_L("===Test timeout!\r\n")); |
|
562 return; |
|
563 } |
|
564 |
459 TheTest.Next( _L(" @SYMTestCaseID:SYSLIB-SQL-UT-4073 Background compaction steps test")); |
565 TheTest.Next( _L(" @SYMTestCaseID:SYSLIB-SQL-UT-4073 Background compaction steps test")); |
460 CompactDbTest2(); |
566 CompactDbTest2(); |
461 |
567 |
|
568 if(IsTimeLimitReached()) |
|
569 { |
|
570 TheTest.Printf(_L("===Test timeout!\r\n")); |
|
571 return; |
|
572 } |
|
573 |
462 TheTest.Next( _L(" @SYMTestCaseID:SYSLIB-SQL-UT-4074 Background compaction timer test")); |
574 TheTest.Next( _L(" @SYMTestCaseID:SYSLIB-SQL-UT-4074 Background compaction timer test")); |
463 CompactDbTest3(); |
575 CompactDbTest3(); |
464 |
576 |
|
577 if(IsTimeLimitReached()) |
|
578 { |
|
579 TheTest.Printf(_L("===Test timeout!\r\n")); |
|
580 return; |
|
581 } |
|
582 |
465 TheTest.Next( _L(" @SYMTestCaseID:SYSLIB-SQL-UT-4103 Big manual compaction test")); |
583 TheTest.Next( _L(" @SYMTestCaseID:SYSLIB-SQL-UT-4103 Big manual compaction test")); |
466 ManualCompactTest(); |
584 ManualCompactTest(); |
467 } |
585 } |
468 |
586 |
469 TInt E32Main() |
587 TInt E32Main() |