Update contrib.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include "t_testrunner.h"
20 #include "t_testaction.h"
23 // CTestRunner /////////////////////////////////////////////////////////////////
25 EXPORT_C CTestRunner::CTestRunner(Output& aOut) :
26 CActive(EPriorityNormal),
29 CActiveScheduler::Add(this);
32 EXPORT_C CTestRunner::~CTestRunner()
37 EXPORT_C TInt CTestRunner::PerformPrerequisiteL(CTestAction* aAction)
40 while (!aAction->Finished() && aAction->iActionState == CTestAction::EPrerequisite)
42 err = RunAsyncMethodL(&CTestAction::PerformPrerequisite, aAction, err);
47 EXPORT_C TInt CTestRunner::PerformActionL(CTestAction* aAction)
50 while (!aAction->Finished() && aAction->iActionState == CTestAction::EAction)
52 err = RunAsyncMethodL(&CTestAction::PerformAction, aAction, err);
57 EXPORT_C TInt CTestRunner::PerformPostrequisiteL(CTestAction* aAction, TInt aInitialStatus)
59 TInt err = aInitialStatus;
60 while (!aAction->Finished() && aAction->iActionState == CTestAction::EPostrequisite)
62 err = RunAsyncMethodL(&CTestAction::PerformPostrequisite, aAction, err);
67 TInt CTestRunner::RunAsyncMethodL(TTestMethod aMethod, CTestAction* aAction, TInt aInitialStatus)
69 iStatus = aInitialStatus;
70 TRAPD(err, (aAction->*aMethod)(iStatus));
74 if (err != KErrNoMemory)
76 aAction->iActionState = CTestAction::EPostrequisite;
88 void CTestRunner::RunSchedulerL()
90 iSchedulerRunning = ETrue;
91 CActiveScheduler::Start();
94 TBool CTestRunner::StepScheduler()
96 User::WaitForAnyRequest();
98 if (!CActiveScheduler::Current()->RunIfReady(err, EPriorityNull))
105 EXPORT_C void CTestRunner::RunL()
107 if (iSchedulerRunning)
109 iSchedulerRunning = EFalse;
110 CActiveScheduler::Stop();
114 EXPORT_C TInt CTestRunner::RunError(TInt /*aError*/)
116 return KErrGeneral; // RunL() can never leave
119 EXPORT_C void CTestRunner::DoCancel()
123 // COOMTestRunnerBase ////////////////////////////////////////////////////////////////////
125 /// Max OOM fail count, to prevent runaway tests
126 const TInt KOOMFailLimit = 10000;
128 EXPORT_C COOMTestRunnerBase::COOMTestRunnerBase(Output& aOut) :
133 EXPORT_C COOMTestRunnerBase::~COOMTestRunnerBase()
137 EXPORT_C TInt COOMTestRunnerBase::PerformActionL(CTestAction* aAction)
139 iOut.writeString(_L("Running OOM test..."));
141 iOut.writeString(_L("Fail point: Heap used: Action state: Status:"));
146 TInt allocStart = AllocCount();
150 for (failCount = 1 ; failCount < KOOMFailLimit ; ++failCount)
155 TInt actionState = 0;
156 while (!aAction->Finished() && aAction->iActionState == CTestAction::EAction && err != KErrNoMemory)
159 err = RunAsyncMethodL(&CTestAction::PerformAction, aAction, err);
162 TInt allocEnd = AllocCount();
166 buffer.Format(_L(" %8d %8d %8d %8d"), failCount, allocEnd - allocStart, actionState, err);
167 iOut.writeString(buffer);
170 if (err != KErrNoMemory || aAction->Finished() || aAction->iActionState != CTestAction::EAction)
176 aAction->AfterOOMFailure();
178 aAction->ResetState();
183 if (failCount == KOOMFailLimit)
186 iOut.writeString(_L("OOM test failed to terminate"));
194 // COOMTestRunner ////////////////////////////////////////////////////////////////////////
196 COOMTestRunner::COOMTestRunner(Output& aOut) :
197 COOMTestRunnerBase(aOut)
201 COOMTestRunner::~COOMTestRunner()
205 void COOMTestRunner::StartOOMTestL()
210 void COOMTestRunner::IncHeapFailPoint()
213 __UHEAP_SETFAIL(RHeap::EDeterministic, iFailPoint);
216 void COOMTestRunner::ResetHeapFail()
221 TInt COOMTestRunner::AllocCount()
223 return User::CountAllocCells();
226 void COOMTestRunner::EndOOMTestL()
230 // CCancelTestRunner /////////////////////////////////////////////////////////////////////
232 /// Max cancel step, to prevent runaway tests
233 const TInt KCancelStepLimit = 200;
235 CCancelTestRunner::CCancelTestRunner(Output& aOut) :
240 CCancelTestRunner::~CCancelTestRunner()
245 * Run the async PerformAction method for a specified number of steps and then
246 * cancel it. This does the equivalent of RunAsyncMethod, but calling
247 * PerformAction and cancelling it.
249 TInt CCancelTestRunner::RunAndCancelPeformActionMethod(CTestAction* aAction, TInt aInitialStatus,
250 TInt aCancelStep, TInt& aStep)
252 iStatus = aInitialStatus;
253 TRAPD(err, aAction->PerformAction(iStatus));
261 // This is our equivalent of an active scheduler loop
266 // Check if we can cancel this step
267 if (iStatus.Int() == KRequestPending)
270 // Check if this is the step we want to cancel
271 if (aStep == aCancelStep)
274 aAction->PerformCancel();
276 // Check request completed immediately
277 if (iStatus.Int() == KRequestPending)
279 iOut.writeString(_L("Cancelled request not completed immediately!"));
287 return iAbort ? KErrGeneral : iStatus.Int();
291 * Run the test action for a specified number of steps and then cancel it.
293 TInt CCancelTestRunner::RunAndCancelTestAction(CTestAction* aAction, TInt aCancelStep)
297 TInt actionState = 0;
298 while (!iAbort && !aAction->Finished() && aAction->iActionState == CTestAction::EAction && err != KErrCancel)
301 err = RunAndCancelPeformActionMethod(aAction, err, aCancelStep, step);
305 buffer.Format(_L(" %8d %8d %8d %8d"), aCancelStep, step, actionState, err);
306 iOut.writeString(buffer);
312 TInt CCancelTestRunner::PerformActionL(CTestAction* aAction)
314 iOut.writeString(_L("Running cancellation test..."));
316 iOut.writeString(_L("Fail step: Total steps: Action state: Status:"));
320 for (TInt step = 1 ; step <= KCancelStepLimit ; ++step)
322 TInt err = RunAndCancelTestAction(aAction, step);
324 if (iAbort || aAction->Finished() || aAction->iActionState != CTestAction::EAction)
332 // Runaway cancel test
333 iOut.writeString(_L("Cancel test failed to terminate"));