sysstatemgmt/systemstatemgr/test/tsus/src/tsus_step_adaptionserver.cpp
changeset 0 4e1aa6a622a0
child 3 a811597961f0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 // Copyright (c) 2008-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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @test
       
    19  @internalComponent - Internal Symbian test code 
       
    20 */
       
    21 
       
    22 #include "susadaptionserver.h"
       
    23 
       
    24 #include "tsus_step_adaptionserver.h"
       
    25 
       
    26 /**
       
    27  * Client interface to simulate Heap failure at server side
       
    28  */
       
    29 TInt RTestSusAdaptationCli::SetHeapFailure(TInt aFailureRate)
       
    30     {
       
    31     if(Handle())
       
    32         {
       
    33         return SendReceive(EDebugSetHeapFailure, TIpcArgs(aFailureRate));
       
    34         }
       
    35     return KErrDisconnected;
       
    36     }
       
    37 
       
    38 /**
       
    39  * Client interface to restore Heap failure at server side
       
    40  */
       
    41 TInt RTestSusAdaptationCli::UnSetHeapFailure()
       
    42     {
       
    43     if(Handle())
       
    44         {
       
    45         return SendReceive(EDebugUnSetHeapFailure);
       
    46         }
       
    47     return KErrDisconnected;
       
    48     }
       
    49  
       
    50 CSusAdaptionServerTest::~CSusAdaptionServerTest()
       
    51 	{
       
    52 	}
       
    53 
       
    54 CSusAdaptionServerTest::CSusAdaptionServerTest()
       
    55 	{
       
    56 	SetTestStepName(KTSusAdaptionServerStep);
       
    57 	}
       
    58 
       
    59 /** */
       
    60 TVerdict CSusAdaptionServerTest::doTestStepPreambleL()
       
    61 	{
       
    62 	return CTestStep::doTestStepPreambleL();
       
    63 	}
       
    64 
       
    65 /** 
       
    66  Old Test CaseID 		APPFWK-SUS-0011
       
    67  New Test CaseID 		DEVSRVS-SSMA-SUS-0011
       
    68  */
       
    69 
       
    70 TVerdict CSusAdaptionServerTest::doTestStepL()
       
    71 	{
       
    72 	INFO_PRINTF1(_L("CSusAdaptionServerTest tests started...."));
       
    73 
       
    74 	__UHEAP_MARK;		
       
    75 	TRAPD(err, doTestForMemoryLeaksL());
       
    76 	TEST(err == KErrNone);
       
    77 
       
    78 	TRAP(err, doTestProcessCriticalL());
       
    79 	TEST(KErrNone == err || KErrAlreadyExists == err);
       
    80 	TRAP(err, doTestOOML());
       
    81 	TEST(err == KErrNone);	
       
    82 	
       
    83 	doTestForNormalEmergencyCallinOOM();
       
    84 	doTestForEmergencyCallOOM();
       
    85 	doTestForSettingPriorityClient();
       
    86 	
       
    87 	__UHEAP_MARKEND;
       
    88 	
       
    89 	INFO_PRINTF1(_L("....CSusAdaptionServerTest tests completed!"));
       
    90 	return TestStepResult();	
       
    91 	}
       
    92 
       
    93 /** */
       
    94 TVerdict CSusAdaptionServerTest::doTestStepPostambleL()
       
    95 	{
       
    96 	return CTestStep::doTestStepPostambleL();
       
    97 	}
       
    98 
       
    99 
       
   100 void CSusAdaptionServerTest::doTestForMemoryLeaksL()
       
   101 	{
       
   102 	INFO_PRINTF1(_L("doTestForMemoryLeaksL tests started...."));
       
   103 	
       
   104 	INFO_PRINTF1(_L("Checking for memoryleaks in SusAdaptionServer destructor"));
       
   105 
       
   106 	INFO_PRINTF1(_L("Create active scheduler."));
       
   107 	CActiveScheduler* sched = new(ELeave) CActiveScheduler;
       
   108 	CleanupStack::PushL (sched );
       
   109 	CActiveScheduler::Install (sched );
       
   110 	
       
   111 	__UHEAP_MARK;
       
   112 	StartAndDestroyServerL();
       
   113 	__UHEAP_MARKEND;
       
   114 	
       
   115 	INFO_PRINTF1(_L("Destroy active scheduler."));
       
   116 	CleanupStack::PopAndDestroy(sched);
       
   117 
       
   118 	INFO_PRINTF1(_L("doTestForMemoryLeaksL tests completed."));
       
   119 	}
       
   120 
       
   121 
       
   122 	
       
   123 void CSusAdaptionServerTest::doTestProcessCriticalL()
       
   124 	{
       
   125 	INFO_PRINTF1(_L("doTestProcessCriticalL tests started...."));
       
   126 
       
   127 	INFO_PRINTF1(_L("Checking that SusAdaptionServer sets it thread to critical"));
       
   128 	
       
   129 	INFO_PRINTF1(_L("Get the critical state of the current thread"));
       
   130 	User::TCritical before = User::Critical();
       
   131 	INFO_PRINTF3(_L("The critical state of the current thread: expected %d, actual %d"), User::ENotCritical, before);
       
   132 	TESTL(before == User::ENotCritical);
       
   133 	
       
   134 	CActiveScheduler* sched = new(ELeave) CActiveScheduler;
       
   135 	CleanupStack::PushL (sched );
       
   136 	CActiveScheduler::Install (sched );
       
   137 	INFO_PRINTF1(_L("Create SSM adaptation server."));
       
   138 	CSsmAdaptationServer* server = CSsmAdaptationServer::NewLC();
       
   139 	
       
   140 	INFO_PRINTF1(_L("Get the critical state of the current thread"));
       
   141 	User::TCritical critical = User::Critical();
       
   142 	INFO_PRINTF3(_L("The critical state of the current thread: expected %d, actual %d"), User::ESystemCritical, critical);
       
   143 	TEST(critical == User::ESystemCritical);
       
   144 
       
   145 	INFO_PRINTF1(_L("Destroy SSM adaptation server."));
       
   146 	CleanupStack::PopAndDestroy(server);
       
   147 	CleanupStack::PopAndDestroy(sched);
       
   148 	
       
   149 	User::TCritical after = User::Critical();
       
   150 	INFO_PRINTF3(_L("The critical state of the current thread: expected %d, actual %d"), User::ENotCritical, after);
       
   151 	TESTL(after == User::ENotCritical);
       
   152 
       
   153 	INFO_PRINTF1(_L("doTestProcessCriticalL tests completed."));
       
   154 	}
       
   155 	
       
   156 void CSusAdaptionServerTest::doTestOOML()
       
   157 	{
       
   158 	INFO_PRINTF1(_L("doTestOOML tests started...."));
       
   159 
       
   160 	INFO_PRINTF1(_L("Create active scheduler."));
       
   161 	CActiveScheduler* sched = new(ELeave) CActiveScheduler;
       
   162 	CleanupStack::PushL (sched );
       
   163 	CActiveScheduler::Install (sched );
       
   164 	
       
   165 	TInt ret = KErrNoMemory;
       
   166 	for(TInt fail=0; ret == KErrNoMemory; fail++)
       
   167 		{
       
   168 		__UHEAP_FAILNEXT(fail);
       
   169 		__UHEAP_MARK;
       
   170 		INFO_PRINTF2(_L("Iteration #: %d"), fail);
       
   171 		TRAP(ret, StartAndDestroyServerL());
       
   172 		INFO_PRINTF2(_L("StartAndDestroyServerL() returned: %d"), ret);
       
   173 		__UHEAP_SETFAIL(RHeap::ENone,0);// turn failure off
       
   174 		__UHEAP_MARKEND;
       
   175 		TEST((ret==KErrNoMemory || ret==KErrNone));
       
   176 		}
       
   177 
       
   178 	INFO_PRINTF1(_L("Destroy active scheduler."));
       
   179 	CleanupStack::PopAndDestroy(sched);
       
   180 	INFO_PRINTF1(_L("doTestOOML tests completed."));
       
   181 	}
       
   182 
       
   183 void CSusAdaptionServerTest::StartAndDestroyServerL()
       
   184 	{
       
   185 	INFO_PRINTF1(_L("Create SSM adaptation server."));
       
   186 	CSsmAdaptationServer* server = NULL;
       
   187 	TRAPD(err, server = CSsmAdaptationServer::NewLC(); CleanupStack::Pop(server));
       
   188 	TEST(err == KErrAlreadyExists || err == KErrNone);
       
   189 	if(err != KErrNone && err != KErrAlreadyExists)
       
   190 		User::LeaveIfError(err);
       
   191 	INFO_PRINTF1(_L("Destroy SSM adaptation server."));
       
   192 	delete server;
       
   193 	}
       
   194 
       
   195 /**
       
   196  * Tests for client calling Activate/Deactivate Rf without setting as priorityclient.
       
   197  */
       
   198 void CSusAdaptionServerTest::doTestForNormalEmergencyCallinOOM()
       
   199     {
       
   200     INFO_PRINTF1(_L("doTestForNormalEmergencyCallinOOM started."));
       
   201     __UHEAP_MARK;
       
   202     RTestSusAdaptationCli adaptationclitest;    
       
   203     TInt err = adaptationclitest.Connect();
       
   204     INFO_PRINTF2(_L("Connect() returned Error %d : Expected Error is KErrNone."),err);
       
   205     TEST(err == KErrNone);
       
   206     TRequestStatus status;
       
   207     // Simulate OOM condition
       
   208     User::__DbgSetAllocFail(EFalse, RAllocator::EDeterministic, 1);
       
   209     User::__DbgSetAllocFail(ETrue, RAllocator::EDeterministic, 1);
       
   210     //Simulate OOM at Serverside
       
   211     adaptationclitest.SetHeapFailure(1);
       
   212     //Normal client calling ActivateRfForEmergencyCall Rf in OOM condition. This call must fail with KErrNoMemory 
       
   213     //as there is no memory reserved.
       
   214     adaptationclitest.ActivateRfForEmergencyCall(status);
       
   215     // Wait for completion of requests
       
   216     User::WaitForRequest(status);    
       
   217     //Restore OOM condition
       
   218     adaptationclitest.UnSetHeapFailure();
       
   219     adaptationclitest.Close();
       
   220     User::__DbgSetAllocFail(EFalse, RAllocator::ENone, 1);
       
   221     User::__DbgSetAllocFail(ETrue, RAllocator::ENone, 1);
       
   222     TEST(status.Int() == KErrNoMemory);
       
   223     __UHEAP_MARKEND;
       
   224     INFO_PRINTF1(_L("doTestForNormalEmergencyCallinOOM completed.")); 
       
   225     }
       
   226 
       
   227 
       
   228 /**
       
   229  * Tests for performing ActivateRfForEmergencyCall + DeactivateRfForEmergencyCall 
       
   230  *  during OOM condition by priority clients.
       
   231  */
       
   232 void CSusAdaptionServerTest::doTestForEmergencyCallOOM()
       
   233     {
       
   234     INFO_PRINTF1(_L("doTestForEmergencyCallOOM started."));
       
   235     __UHEAP_MARK;
       
   236     RTestSusAdaptationCli adaptationclitest;    
       
   237     TInt err = adaptationclitest.Connect();
       
   238     INFO_PRINTF2(_L("Connect() returned Error %d : Expected Error is KErrNone."),err);
       
   239     TEST(err == KErrNone);
       
   240     err = adaptationclitest.SetAsPriorityClient();
       
   241     INFO_PRINTF2(_L("SetAsPriorityClient() returned Error %d : Expected Error is KErrNone."),err);
       
   242     TEST(err == KErrNone);
       
   243     //Activate Rf + Deactivate Rf during non OOM situation.
       
   244     //Calls will be queued and processed one after the other. After 3 consecutive requests for 
       
   245     //ActivateRfForEmergencyCall(), first call will be submitted directly to process and other two calls 
       
   246     // will get queued. By end of this process will be having (count + 2) reserved memory in queue.i.e 4 reserved slots
       
   247     TRequestStatus status1,status2,status3,status4,status5,status6; 
       
   248     adaptationclitest.ActivateRfForEmergencyCall(status1); 
       
   249     adaptationclitest.ActivateRfForEmergencyCall(status2);
       
   250     adaptationclitest.ActivateRfForEmergencyCall(status3);
       
   251     //Wait for request to complete
       
   252     User::WaitForRequest(status1);
       
   253     User::WaitForRequest(status2);
       
   254     User::WaitForRequest(status3);
       
   255     TEST(status1.Int() == KErrNone);
       
   256     TEST(status2.Int() == KErrNone);
       
   257     TEST(status3.Int() == KErrNone);
       
   258     // Simulate OOM condition
       
   259     User::__DbgSetAllocFail(EFalse, RAllocator::EDeterministic, 1);
       
   260     User::__DbgSetAllocFail(ETrue, RAllocator::EDeterministic, 1);
       
   261     //Simulate OOM at Serverside
       
   262     adaptationclitest.SetHeapFailure(1);
       
   263  
       
   264     //After the above 2 calls to Activate the Rfs, will be left with 4 reserved memory in queue, so in OOM
       
   265     //condition client can queue 4 activate/deactivate requests.The 5th request will fail with KErrNoMemory
       
   266     adaptationclitest.ActivateRfForEmergencyCall(status1); 
       
   267     adaptationclitest.ActivateRfForEmergencyCall(status2);
       
   268     adaptationclitest.DeactivateRfForEmergencyCall(status3);
       
   269     adaptationclitest.DeactivateRfForEmergencyCall(status4);
       
   270     adaptationclitest.DeactivateRfForEmergencyCall(status5);
       
   271     adaptationclitest.DeactivateRfForEmergencyCall(status6);
       
   272     // Wait for completion of requests
       
   273     User::WaitForRequest(status1);
       
   274     User::WaitForRequest(status2);
       
   275     User::WaitForRequest(status3);
       
   276     User::WaitForRequest(status4);
       
   277     User::WaitForRequest(status5);
       
   278     User::WaitForRequest(status6);    
       
   279     
       
   280     //Restore OOM condition
       
   281     adaptationclitest.UnSetHeapFailure();
       
   282     User::__DbgSetAllocFail(EFalse, RAllocator::ENone, 1);
       
   283     User::__DbgSetAllocFail(ETrue, RAllocator::ENone, 1);
       
   284     TEST(status1.Int() == KErrNone);
       
   285     TEST(status2.Int() == KErrNone);
       
   286     TEST(status3.Int() == KErrNone);
       
   287     TEST(status4.Int() == KErrNone);
       
   288     TEST(status5.Int() == KErrNone);
       
   289     //6th request will fail with KErrNoMemory if, above 5 requests are still queued and there is no reserved slot for
       
   290     //6th one..otherwise, 6th request will be queued and processed
       
   291     TEST(status6.Int() == KErrNoMemory || status6.Int() == KErrNone);
       
   292     
       
   293     adaptationclitest.Close();
       
   294     __UHEAP_MARKEND;
       
   295     INFO_PRINTF1(_L("doTestForEmergencyCallOOM completed."));        
       
   296     }
       
   297 
       
   298 /**
       
   299  * Tests to Set PriorityClient from two different clients.
       
   300  */
       
   301 void CSusAdaptionServerTest::doTestForSettingPriorityClient()
       
   302     {
       
   303     INFO_PRINTF1(_L("doTestForSettingPriorityClient started."));
       
   304     __UHEAP_MARK;
       
   305     RTestSusAdaptationCli adaptationclitest1;
       
   306     TInt err = adaptationclitest1.Connect();
       
   307     INFO_PRINTF2(_L("adaptationclitest1.Connect() returned Error %d : Expected Error is KErrNone."),err);   
       
   308     TEST(err == KErrNone);
       
   309     
       
   310     RTestSusAdaptationCli adaptationclitest2;
       
   311     err = adaptationclitest2.Connect();
       
   312     INFO_PRINTF2(_L("adaptationclitest2.Connect() returned Error %d : Expected Error is KErrNone."),err);   
       
   313     TEST(err == KErrNone);
       
   314        
       
   315     err = adaptationclitest1.SetAsPriorityClient();
       
   316     INFO_PRINTF2(_L("adaptationclitest1.SetAsPriorityClient() returned Error %d : Expected Error is KErrNone."),err);
       
   317     TEST(err == KErrNone);
       
   318     
       
   319     err = adaptationclitest2.SetAsPriorityClient();
       
   320     INFO_PRINTF2(_L("adaptationclitest2.SetAsPriorityClient() returned Error %d : Expected Error is KErrAlreadyExists."),err);
       
   321     TEST(err == KErrAlreadyExists);
       
   322     
       
   323     // Restore OOM condition
       
   324     adaptationclitest1.Close();
       
   325     adaptationclitest2.Close();
       
   326     __UHEAP_MARKEND;
       
   327     INFO_PRINTF1(_L("doTestForSettingPriorityClient completed."));
       
   328     }
       
   329