Update contrib.
1 // Copyright (c) 1998-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "t_dbstress.h"
18 const TInt KCashLimit=500;
19 const TInt KOverdraftLimit=100;
21 const TInt KMinHeap=0x2000;
22 const TInt KMaxHeap=0x20000;
24 const TInt KAllocFailRate=1000;
27 LOCAL_D TBuf<256> Sql;
28 LOCAL_D TBuf8<256> LogBuf;
30 class CThread : public CBase
33 static TInt Entry(TAny*);
48 void AccountL(TInt anAccount);
49 void StatementTimeL();
57 RDbStoreDatabase iData;
65 GLDEF_C TInt StartThread(RThread& aThread,TRequestStatus& aStat)
67 TInt r=aThread.Create(_L("Thread"),CThread::Entry,KDefaultStackSize,KMinHeap,KMaxHeap,NULL);
70 aThread.SetPriority(EPriorityLess);
78 TInt CThread::Entry(TAny*)
80 CTrapCleanup* cleanup=CTrapCleanup::New();
88 void CThread::EntryL()
90 CThread* self=new(ELeave) CThread;
91 CleanupStack::PushL(self);
93 self->GoL(); // never returns
97 {//The destructor is never ever executed! See CThread::EntryL() for details!
103 TInt err = iFs.Delete(KLogFile);
106 RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &KLogFile);
111 void CThread::ConstructL()
113 User::LeaveIfError(iFs.Connect());
114 User::LeaveIfError(iFs.SetSessionPath(KTestDir));
115 User::LeaveIfError(iLog.Replace(iFs,KLogFile,EFileWrite|EFileStreamText));
116 iStore=CFileStore::OpenL(iFs,KTestDatabase,EFileRead|EFileWrite|EFileWriteDirectIO);
118 iData.OpenL(iStore,iStore->Root());
126 __UHEAP_SETFAIL(RHeap::ETrueRandom,KAllocFailRate);
131 LogBuf.Format(_L8(" *** Point %d with code %d"),iPoint,r);
143 // Report the file size
145 void CThread::LogSize()
148 if (iStore->File().Size(size)==KErrNone)
150 LogBuf.Format(_L8("\nFile size=%d"),size);
155 void CThread::Rollback()
159 iLog.Write(_L8("\nCancel"));
163 if (iData.InTransaction())
165 iLog.Write(_L8("\nRollback"));
170 void CThread::Reset()
172 iLog.Write(_L8("\nReset"));
178 void CThread::RecoverL()
181 User::LeaveIfError(iLog.Write(_L8("\nRecovering")));
188 User::LeaveIfError(iData.Recover());
191 void CThread::CompactL()
194 User::LeaveIfError(iLog.Write(_L8("\nCompacting")));
195 TInt b=iStore->ReclaimL();
196 b-=iStore->CompactL();
198 LogBuf.Format(_L8(": %d bytes reclaimed"),b);
199 User::LeaveIfError(iLog.Write(LogBuf));
202 void CThread::OpenViewsL()
205 User::LeaveIfError(iLog.Write(_L8("\nOpening")));
206 User::LeaveIfError(iAccs.Prepare(iData,_L("select id,balance,statement_balance from accounts")));
207 TInt r=iTrans.Prepare(iData,_L("select t_date,from_id,to_id,amount from transactions"));
216 void CThread::WorkL()
245 case 4: // case 5: case 6: case 7: case 8: case 9:
255 void CThread::AccountL(TInt anAccount)
257 Sql.Format(_L("id=%d"),anAccount);
259 TEST(iAccs.FindL(iAccs.EForwards,Sql)>=0);
263 // generate and add a single transaction
265 void CThread::TransactionL()
268 User::LeaveIfError(iLog.Write(_L8("\nTransaction")));
273 from=Random(KAccountIDs);
274 TInt to=(Random(KAccountIDs-1)+from+1)%KAccountIDs;
277 TEST(iAccs.ColInt(1)==from);
278 TInt avail=iAccs.ColInt(2)+KOverdraftLimit;
284 amount=10*(1+Random(Min(avail,KCashLimit)/10));
288 amount=1+Random(100);
299 LogBuf.Format(_L8(" %08d: %d -> %d, %5d"),++TransId,from,to,amount);
300 User::LeaveIfError(iLog.Write(LogBuf));
304 iTrans.SetColL(1,TransId);
305 iTrans.SetColL(2,from);
306 iTrans.SetColL(3,to);
307 iTrans.SetColL(4,amount);
311 iAccs.UpdateL(); // from
312 TInt frombalance=iAccs.ColInt(2)-amount;
313 iAccs.SetColL(2,frombalance);
319 iAccs.UpdateL(); // to
320 TInt tobalance=iAccs.ColInt(2)+amount;
321 iAccs.SetColL(2,tobalance);
325 // this will invoke commit, so update counts now
327 TInt r=iData.Commit();
335 LogBuf.Format(_L8("; [%d,%d]"),frombalance,tobalance);
340 // deliver statements
342 void CThread::StatementTimeL()
345 User::LeaveIfError(iLog.Write(_L8("\nStatement")));
348 // discard transactions
351 for (iTrans.BeginningL();iTrans.NextL();)
353 for (iAccs.BeginningL();iAccs.NextL();)
356 iAccs.SetColL(3,iAccs.ColInt(2)); // set statement balance
361 TInt r=iData.Commit();
370 void CThread::VerifyL()
372 TInt balance[KAccountIDs];
373 TEST(iAccs.CountL()==KAccountIDs);
374 for (iAccs.BeginningL();iAccs.NextL();)
377 TInt id=iAccs.ColInt(1);
378 balance[id]=iAccs.ColInt(3);
381 for (iTrans.BeginningL();iTrans.NextL();)
385 TInt from=iTrans.ColInt(2);
386 TInt to=iTrans.ColInt(3);
387 TInt amount=iTrans.ColInt(4);
388 balance[from]-=amount;
391 TEST(transact==iTrans.CountL());
392 for (iAccs.BeginningL();iAccs.NextL();)
395 TInt id=iAccs.ColInt(1);
396 if (balance[id]!=iAccs.ColInt(2))
397 User::Panic(_L("Oh-oh"),4321);