landmarks/locationlandmarks/tsrc/LandmarkTestModule/src/FT_CPosTp33.cpp
branchRCL_3
changeset 44 2b4ea9893b66
equal deleted inserted replaced
42:02ba3f1733c6 44:2b4ea9893b66
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *   ?description_line
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 //  INCLUDES
       
    21 
       
    22 #include "FT_CPosTp33.h"
       
    23 #include <EPos_CPosLandmarkDatabase.h>
       
    24 #include <ss_std.h>
       
    25 
       
    26 //EPosLmServer
       
    27 _LIT(KPosLandmarksServerName, "*eposlmserver*");
       
    28     
       
    29 // ================= MEMBER FUNCTIONS =======================
       
    30 
       
    31 // ---------------------------------------------------------
       
    32 // CPosTp33::GetName
       
    33 //
       
    34 // (other items were commented in a header).
       
    35 // ---------------------------------------------------------
       
    36 //
       
    37 void CPosTp33::GetName(TDes& aName) const
       
    38     {
       
    39     _LIT(KTestName, "Tp33 - LM Server shutdown");
       
    40     aName = KTestName;
       
    41     }
       
    42 
       
    43 void CPosTp33::CloseTest()
       
    44     {
       
    45     delete iLmd1;
       
    46     iLmd1 = NULL;
       
    47     delete iLmd2;
       
    48     iLmd2 = NULL;
       
    49     for (TInt i=0; i<iThreads.Count(); i++)
       
    50         {
       
    51         iThreads[i].Close();
       
    52         }
       
    53 
       
    54     iThreads.Close();
       
    55     }
       
    56 
       
    57 
       
    58 // ---------------------------------------------------------
       
    59 // CPosTp33::StartL
       
    60 //
       
    61 // (other items were commented in a header).
       
    62 // ---------------------------------------------------------
       
    63 //
       
    64 void CPosTp33::StartL()
       
    65     {  
       
    66     MakeSurePanicDebugFileExistsL();
       
    67     RemoveDefaultDbL();
       
    68     
       
    69 /*    ListAllServers(_L("*"));
       
    70 	ListAllProcesses(_L("*"));
       
    71 	ListAllThreads(_L("*"));
       
    72 */	
       
    73 	// When running on security SDK, not possible to kill LM Server
       
    74     //KillTestL();
       
    75     
       
    76     RemoveDefaultDbL();
       
    77 
       
    78     iLmd1 = CPosLandmarkDatabase::OpenL();
       
    79     
       
    80     TBool alive = ServerAlive(KPosLandmarksServerName);
       
    81     
       
    82     MonitorHeapL(ETrue);
       
    83 
       
    84 	ListAllServers(_L("*"));
       
    85 	ListAllProcesses(_L("*"));
       
    86 	ListAllThreads(_L("*"));
       
    87 	
       
    88     alive = ServerAlive(KPosLandmarksServerName);
       
    89     if(!alive) LogErrorAndLeave(_L("Server not alive"));
       
    90 
       
    91     if (iLmd1->IsInitializingNeeded())
       
    92         {
       
    93         iLog -> Put(_L("Doing InitializeL()"));
       
    94         ExecuteAndDeleteLD(iLmd1->InitializeL()); 
       
    95         }
       
    96 
       
    97     MonitorHeapL(ETrue);
       
    98     
       
    99     alive = ServerAlive(KPosLandmarksServerName);
       
   100     if(!alive) LogErrorAndLeave(_L("Server not alive"));
       
   101 
       
   102     iLmd2 = CPosLandmarkDatabase::OpenL();
       
   103 
       
   104     alive = ServerAlive(KPosLandmarksServerName);
       
   105     if(!alive) LogErrorAndLeave(_L("Server not alive"));
       
   106 
       
   107     delete iLmd1;
       
   108     iLmd1 = NULL;
       
   109     User::After(4000000); //just to make sure that timing does not matter
       
   110 
       
   111     alive = ServerAlive(KPosLandmarksServerName);
       
   112     if(!alive) LogErrorAndLeave(_L("Server not alive"));
       
   113 
       
   114     delete iLmd2;
       
   115     iLmd2 = NULL;
       
   116     User::After(4000000);
       
   117 
       
   118     alive = ServerAlive(KPosLandmarksServerName);
       
   119     if(alive) LogErrorAndLeave(_L("Server alive"));
       
   120 
       
   121     //stress test below
       
   122     StressTestL();
       
   123 
       
   124     //thread test to verify that heap does not grow when threads are killed.
       
   125     ThreadTestL();
       
   126 	}
       
   127 
       
   128 // ---------------------------------------------------------
       
   129 // CPosTp33::StressTestL
       
   130 // 
       
   131 // (other items were commented in a header).
       
   132 // ---------------------------------------------------------
       
   133 //
       
   134 void CPosTp33::StressTestL()
       
   135     {
       
   136     iLog -> Put(_L("StressTestL() -->"));
       
   137     for(TInt i = 0; i < 50; i++)
       
   138         {
       
   139         iLmd1 = CPosLandmarkDatabase::OpenL();
       
   140         iLmd2 = CPosLandmarkDatabase::OpenL();
       
   141         delete iLmd1;
       
   142         iLmd1 = NULL;
       
   143         delete iLmd2;
       
   144         iLmd2 = NULL;
       
   145         }
       
   146 	//    MonitorHeapL();
       
   147 	MonitorHeapL(ETrue);
       
   148     TBool alive = ServerAlive(KPosLandmarksServerName);
       
   149     if(!alive) LogErrorAndLeave(_L("Server not alive in StressTestL"));
       
   150 
       
   151     User::After(4000000);
       
   152 
       
   153     alive = ServerAlive(KPosLandmarksServerName);
       
   154     if(alive) LogErrorAndLeave(_L("Server alive in StressTestL"));
       
   155     iLog -> Put(_L("StressTestL() <--"));
       
   156     }
       
   157 // ---------------------------------------------------------
       
   158 // CPosTp33::ListAllServers
       
   159 // 
       
   160 // (other items were commented in a header).
       
   161 // ---------------------------------------------------------
       
   162 //
       
   163 void CPosTp33::ListAllServers(const TDesC& aServerName)
       
   164     {
       
   165 	TFindServer find(aServerName);
       
   166     TFullName   name;
       
   167     
       
   168     //TraceL(_L("ListAllServers"), aTraceFile);
       
   169     
       
   170     iLog->Put(_L("ListAllServers"));
       
   171 
       
   172 	while (find.Next(name) == KErrNone)
       
   173 		{
       
   174 		//iLog->Put(name);
       
   175         //TraceL(name, aTraceFile);
       
   176         iLog->Put(name);
       
   177 
       
   178 		}
       
   179 	//TraceL(_L("--------------------"), aTraceFile);
       
   180 	iLog->Put(_L("--------------------"));
       
   181     }
       
   182 
       
   183 // ---------------------------------------------------------
       
   184 // CPosTp33::ListAllProcesses
       
   185 // 
       
   186 // (other items were commented in a header).
       
   187 // ---------------------------------------------------------
       
   188 //
       
   189 void CPosTp33::ListAllProcesses(const TDesC& aProcessName)
       
   190     {
       
   191 	TFindProcess find(aProcessName);
       
   192     TFullName   name;
       
   193     
       
   194     iLog->Put(_L("ListAllProcesses"));
       
   195     //TraceL(_L("ListAllProcesses"), aTraceFile);
       
   196 
       
   197 	while (find.Next(name) == KErrNone)
       
   198 		{
       
   199 		//iLog->Put(name);
       
   200         //TraceL(name, aTraceFile);
       
   201         iLog->Put(name);
       
   202 		}
       
   203 	//TraceL(_L("--------------------"), aTraceFile);
       
   204 	iLog->Put(_L("--------------------"));
       
   205     }
       
   206 
       
   207 // ---------------------------------------------------------
       
   208 // CPosTp33::ListAllThreads
       
   209 // 
       
   210 // (other items were commented in a header).
       
   211 // ---------------------------------------------------------
       
   212 //
       
   213 void CPosTp33::ListAllThreads(const TDesC& aThreadName)
       
   214     {
       
   215 	TFindThread find(aThreadName);
       
   216     TFullName   name;
       
   217 
       
   218 	iLog->Put(_L("ListAllThreads"));
       
   219     //TraceL(_L("ListAllThreads"), aTraceFile);
       
   220 	while (find.Next(name) == KErrNone)
       
   221 		{
       
   222 		//TraceL(name, aTraceFile);
       
   223 		iLog->Put(name);
       
   224 		}
       
   225 	//TraceL(_L("--------------------"), aTraceFile);
       
   226 	iLog->Put(_L("--------------------"));
       
   227     }
       
   228     
       
   229 // ---------------------------------------------------------
       
   230 // CPosTp33::KillTestL
       
   231 // 
       
   232 // (other items were commented in a header).
       
   233 // ---------------------------------------------------------
       
   234 //
       
   235 void CPosTp33::KillTestL()
       
   236     {
       
   237 
       
   238     CPosLandmarkDatabase* lmd = CPosLandmarkDatabase::OpenL();
       
   239     CleanupStack::PushL(lmd);
       
   240 
       
   241     if (lmd->IsInitializingNeeded())
       
   242         {
       
   243         ExecuteAndDeleteLD(lmd->InitializeL()); 
       
   244         }
       
   245     
       
   246 	TFullName targetName;
       
   247 
       
   248     #ifndef __WINS__
       
   249 	    TFindProcess finderLMSERV(KPosLandmarksServerName);
       
   250         RProcess process_or_thread_lmserv;    
       
   251     #else
       
   252         TFindThread finderLMSERV(KPosLandmarksServerName);
       
   253         RThread process_or_thread_lmserv;
       
   254     #endif
       
   255     
       
   256      if (finderLMSERV.Next(targetName) == KErrNone)
       
   257 		{		
       
   258 		process_or_thread_lmserv.Open(finderLMSERV);
       
   259 		process_or_thread_lmserv.Kill(KErrGeneral);
       
   260 		process_or_thread_lmserv.Close();
       
   261 		}
       
   262 
       
   263     if (ServerAlive(KPosLandmarksServerName))
       
   264         {
       
   265         LogErrorAndLeave(_L("Server alive, error in TC!"));
       
   266         }
       
   267 
       
   268     CPosLandmark* landmark = CPosLandmark::NewLC();
       
   269         
       
   270     landmark->SetLandmarkNameL(_L("TP33")); 
       
   271     
       
   272     TRAPD(err, lmd->AddLandmarkL(*landmark));
       
   273     if (err != KErrServerTerminated)
       
   274         {
       
   275         LogErrorAndLeave(_L("AddLanmdark does not Leave with -15 after the server has been killed"));
       
   276         }
       
   277 
       
   278     TRAP(err, lmd->UpdateLandmarkL(*landmark));
       
   279     if (err != KErrServerTerminated)
       
   280         {
       
   281         LogErrorAndLeave(_L("UpdateLandmark does not Leave with -15 after the server has been killed"));
       
   282         }
       
   283 
       
   284     CleanupStack::PopAndDestroy(2, lmd); 
       
   285     }
       
   286 
       
   287 // ---------------------------------------------------------
       
   288 // CPosTp33::MonitorHeapL
       
   289 // 
       
   290 // (other items were commented in a header).
       
   291 // ---------------------------------------------------------
       
   292 //
       
   293 void CPosTp33::MonitorHeapL(TBool aMaxSet)
       
   294     {
       
   295     iLog->Put(_L("MonitorHeapL"));
       
   296     TInt landmarkHeap=0, landmarkHeapUsed=0;
       
   297 
       
   298     TBuf<150> buf;
       
   299     TInt err = GetHeapSizeL(KPosLandmarksServerName, landmarkHeap, landmarkHeapUsed);
       
   300     if (err != KErrNone)
       
   301         {
       
   302         buf.Zero();
       
   303         buf.Format(_L("GetHeapSizeL for Landmark server returned error, err %d"), err);
       
   304         iLog->Put(buf);
       
   305         }
       
   306     if (aMaxSet) 
       
   307         {
       
   308         iMaxHeap = 2*landmarkHeapUsed;
       
   309         }
       
   310     else
       
   311         {
       
   312         if(landmarkHeapUsed > iMaxHeap)
       
   313             {
       
   314             LogErrorAndLeave(_L("Used heap to large"));
       
   315             }
       
   316         }
       
   317 
       
   318     buf.Zero();
       
   319     buf.Format(_L("Landmark Heap; %d Landmark Heap Used: %d"), landmarkHeap, landmarkHeapUsed);
       
   320     iLog->Put(buf);
       
   321     }
       
   322 
       
   323 
       
   324 // ---------------------------------------------------------
       
   325 // CPosTp33::GetHeapSizeL
       
   326 // 
       
   327 // (other items were commented in a header).
       
   328 // ---------------------------------------------------------
       
   329 //
       
   330 TInt CPosTp33::GetHeapSizeL(const TDesC& aThreadName, TInt& aSize, TInt& aUsed)
       
   331     {
       
   332     RThread thread;
       
   333     TFindThread findThread(aThreadName);
       
   334 
       
   335     TFullName fullName;
       
   336     
       
   337     if (findThread.Next(fullName) != KErrNone)
       
   338         {
       
   339         iLog->Put(_L("Thread not found"));
       
   340         aSize=-1;aUsed=-1;
       
   341         return KErrNotFound;
       
   342         }
       
   343 
       
   344     TInt err = thread.Open(fullName);
       
   345 
       
   346     if (err != KErrNone)
       
   347         {
       
   348         iLog->Put(_L("Thread could not be opened"));
       
   349         aSize=-1;aUsed=-1;
       
   350         return err;
       
   351         }
       
   352     CleanupClosePushL(thread);
       
   353 
       
   354     //TInt tmp;
       
   355 
       
   356 #ifndef __WINS__
       
   357 // For some reason cannot access heap in target
       
   358 //   thread.GetRamSizes(aSize,tmp);
       
   359 //   aUsed = aSize;
       
   360     
       
   361     // FrBo Commented since not supported on ARMV5 (on SDK Series60_30_2005_wk02_incl_LBSpre4)
       
   362     //thread.GetRamSizes(aSize, aUsed);
       
   363     // Use below for now
       
   364     aSize=-1;aUsed=-1;
       
   365     
       
   366     TThreadStackInfo stackInfo;
       
   367     thread.StackInfo(stackInfo);
       
   368     
       
   369     aSize = stackInfo.iBase - stackInfo.iLimit;
       
   370     aUsed = aSize;
       
   371 
       
   372 #else
       
   373 
       
   374     // FrBo Commented since not supported on WINSCW (on SDK Series60_30_2005_wk04_Mars)
       
   375     // Use below for now
       
   376     aSize=-1;aUsed=-1;
       
   377     
       
   378     TThreadStackInfo stackInfo;
       
   379     thread.StackInfo(stackInfo);
       
   380     
       
   381     aSize = stackInfo.iBase - stackInfo.iLimit;
       
   382     aUsed = aSize;
       
   383     
       
   384     /*
       
   385     // In WINS we can get both the size of the heap, and amount
       
   386     // of bytes actually used. This method Panics on ARMI
       
   387     RHeap* heap = NULL;
       
   388     heap = thread.Heap();
       
   389     if (heap == NULL)
       
   390         {
       
   391         iLog->Put(_L("Could not open heap."));
       
   392         aSize=-1;aUsed=-1;
       
   393         return KErrGeneral;
       
   394         LogErrorAndLeave(_L("Could not open heap."));
       
   395         }
       
   396     TInt tmp;
       
   397     aSize = heap->Size();
       
   398     aUsed = heap->Size() - (heap->Available(tmp));
       
   399     */
       
   400 
       
   401 #endif
       
   402     CleanupStack::PopAndDestroy(&thread); // thread
       
   403     return KErrNone;
       
   404     }
       
   405 
       
   406 
       
   407 void CPosTp33::ThreadTestL()
       
   408     {
       
   409     iLog -> Put(_L("ThreadTestL() -->"));
       
   410     StartMultipleClientsL(5);
       
   411     MonitorHeapL();
       
   412     User::After(4000000);
       
   413     TBool alive = ServerAlive(KPosLandmarksServerName);
       
   414     if(alive) LogErrorAndLeave(_L("Server alive"));
       
   415     iLog -> Put(_L("ThreadTestL() <--"));    
       
   416     }
       
   417 
       
   418 // ---------------------------------------------------------
       
   419 // ThreadFunction
       
   420 //
       
   421 // (other items were commented in a header).
       
   422 // ---------------------------------------------------------
       
   423 //
       
   424 LOCAL_C TInt ThreadFunction(TAny* aData)
       
   425     {
       
   426     CTrapCleanup* cleanup=CTrapCleanup::New(); 
       
   427 
       
   428     CActiveScheduler* actSch = new (ELeave) CActiveScheduler;
       
   429     CActiveScheduler::Install(actSch);
       
   430 
       
   431     TRAPD(err, CPosTp33::RunTestL(aData));
       
   432        
       
   433     delete actSch;
       
   434     delete cleanup;
       
   435     return err;
       
   436     }
       
   437 
       
   438 void CPosTp33::StartMultipleClientsL(const TUint aNoClients)
       
   439     {
       
   440     _LIT(KMultipleErr, "Error %d from client %d when multiple clients access the landmarks db");
       
   441     _LIT(KPanicErr, "Thread exit with incorrect code");
       
   442 
       
   443     CreateThreadsL(aNoClients);
       
   444 
       
   445     RArray<TRequestStatus> statuses;
       
   446     CleanupClosePushL(statuses);
       
   447    
       
   448     for (TUint j=0; j<aNoClients; j++)
       
   449         {
       
   450         TRequestStatus status;
       
   451         statuses.Append(status);
       
   452         }
       
   453 
       
   454     TInt i=0;
       
   455     TInt errrorsFound = KErrNone;
       
   456     for (i=0; i<iThreads.Count(); i++)
       
   457         { 
       
   458         iThreads[i].Logon(statuses[i]);
       
   459         iThreads[i].Resume();
       
   460         // This prevents some weird CentralRepository alloc to accur, which 
       
   461         // caused the test to fail
       
   462         User::After(2000000);
       
   463         }
       
   464     
       
   465     for (i=0; i<iThreads.Count(); i++)
       
   466         {
       
   467         User::WaitForRequest(statuses[i]);
       
   468         }
       
   469     
       
   470     for (i=0; i<iThreads.Count(); i++)
       
   471         {
       
   472 
       
   473         iLog -> Put(_L("Checking Thread"));
       
   474         TInt exitReason = iThreads[i].ExitReason();
       
   475         
       
   476         //AssertTrueSecL(exitReason == KErrNone || exitReason == KErrNotFound, KMultipleErr, exitReason);
       
   477         if (exitReason != KErrNone && exitReason != KErrNotFound)
       
   478 	        {
       
   479 	        TBuf<100> buf;
       
   480 	        buf.Format(KMultipleErr, exitReason, i);
       
   481 	        iLog->Put(buf);
       
   482 	        errrorsFound++;
       
   483 	        }
       
   484 
       
   485 #ifndef __WINS__ 
       
   486         AssertTrueSecL(iThreads[i].ExitType() == EExitPanic, KPanicErr); //does not currently work use exit on wins instead
       
   487 #else
       
   488         AssertTrueSecL(iThreads[i].ExitType() == EExitKill, KPanicErr); 
       
   489 #endif
       
   490         }
       
   491     for (i=0; i<iThreads.Count(); i++)
       
   492         {
       
   493         iThreads[i].Close();
       
   494         }
       
   495     iThreadIndex=0;
       
   496     iThreads.Close();
       
   497     CleanupStack::PopAndDestroy(&statuses);
       
   498     
       
   499     if (errrorsFound != KErrNone) LogErrorAndLeave(_L("Error from thread client"));
       
   500     }
       
   501 
       
   502 // ---------------------------------------------------------
       
   503 // CPosTp33::CreateThreadsL
       
   504 //
       
   505 // (other items were commented in a header).
       
   506 // ---------------------------------------------------------
       
   507 //
       
   508 void CPosTp33::CreateThreadsL(const TUint aNoThreads)
       
   509     {
       
   510     _LIT(KThreadName, "LM TP33 test thread%d");
       
   511     _LIT(KCreateThreadErr, "Create thread failed with %d");
       
   512 
       
   513     for (TUint i=0; i<aNoThreads; i++)
       
   514         {
       
   515         RThread thread;
       
   516         TBuf<32> name;
       
   517 	    name.Format(KThreadName, 1+iThreads.Count());
       
   518                 
       
   519         TInt err;
       
   520         err = thread.Create(name, ThreadFunction, KDefaultStackSize, KMinHeapSize, KMaxHeapSize, reinterpret_cast<TAny*>(this));
       
   521 
       
   522         AssertTrueSecL(err == KErrNone, KCreateThreadErr, err);
       
   523         
       
   524         iThreads.Append(thread);
       
   525         }
       
   526     }
       
   527 
       
   528 
       
   529 // ---------------------------------------------------------
       
   530 // CPosTp30::RunTestL
       
   531 //
       
   532 // (other items were commented in a header).
       
   533 // ---------------------------------------------------------
       
   534 //
       
   535 void CPosTp33::RunTestL(TAny* /*aData*/)
       
   536     {
       
   537     CPosLandmarkDatabase* lmd = CPosLandmarkDatabase::OpenL();
       
   538     CleanupStack::PushL(lmd);
       
   539     
       
   540     if (lmd->IsInitializingNeeded())
       
   541        {
       
   542        ExecuteAndDeleteLD(lmd->InitializeL()); 
       
   543        }
       
   544 
       
   545     CPosLmCategoryManager* cm = CPosLmCategoryManager::NewL(*lmd); 
       
   546     CleanupStack::PushL(cm);
       
   547 
       
   548     TRequestStatus status;
       
   549     TPosLmEvent event;
       
   550     lmd->NotifyDatabaseEvent(event, status); 
       
   551 
       
   552     //XXX fix this when dialogs works again
       
   553 #ifndef __WINS__ 
       
   554     User::Panic(_L("Panic before cleanup"),-1); //does not currently work use exit on wins instead
       
   555 #else
       
   556     User::Exit(-1);
       
   557 #endif
       
   558 
       
   559     lmd->CancelNotifyDatabaseEvent();
       
   560     CleanupStack::PopAndDestroy(2, lmd);
       
   561     
       
   562     }
       
   563 
       
   564 //  End of File