sysresmonitoring/oommonitor/tsrc/ut_oomwatcher/src/ut_oomwatchercases.cpp
changeset 77 b01c07dfcf84
equal deleted inserted replaced
74:1505405bc645 77:b01c07dfcf84
       
     1 /*
       
     2 * Copyright (c) 2010 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: This file contains STIFUnit implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 /**
       
    19  * STIF_UNIT_INCLUDE SECTION - put all #includes between STIF_UNIT_INCLUDE_SECTION
       
    20  *                             and STIF_UNIT_INCLUDE_SECTION_END
       
    21  */
       
    22 #ifdef STIF_UNIT_INCLUDE_SECTION
       
    23 #include "ut_oomwatchercommon.h"
       
    24 #include "oomoutofmemorywatcher.h"
       
    25 #include "oommemorymonitor.h"
       
    26 #include <oommonitorsession.h>
       
    27 #include "oompanic.h"
       
    28 
       
    29 #include <u32hal.h>
       
    30 #include <hal.h>
       
    31 #include <e32property.h>
       
    32 
       
    33 #endif //STIF_UNIT_INCLUDE_SECTION_END
       
    34 
       
    35 /**
       
    36  * GLOBAL VARIABLES SECTION
       
    37  */
       
    38 #ifdef TEST_VAR_DECLARATIONS
       
    39 CActiveScheduler* scheduler;
       
    40 	 
       
    41 #endif
       
    42 /**
       
    43  * END OF GLOBAL VARIABLES SECTION
       
    44  */
       
    45 
       
    46 
       
    47 /**
       
    48  * TEST CASES SECTION
       
    49  */
       
    50 #ifdef TEST_CASES
       
    51 /**
       
    52  * STIF_SETUP defines activities needed before every test case.
       
    53  */
       
    54 STIF_SETUP
       
    55 {
       
    56     // Install active scheduler at start up
       
    57     scheduler = new (ELeave) CActiveScheduler();
       
    58     CleanupStack::PushL(scheduler);
       
    59     CActiveScheduler::Install(scheduler);
       
    60 }
       
    61 
       
    62 /**
       
    63  * STIF_TEARDOWN defines activities needed after every test case
       
    64  */
       
    65 STIF_TEARDOWN
       
    66 {
       
    67     // Delete active scheduler at last
       
    68     CActiveScheduler::Install(NULL);
       
    69     CleanupStack::PopAndDestroy(scheduler);
       
    70 }
       
    71 
       
    72 /**
       
    73  * @SYMTestCaseID              GAPS-OOM-WATCHER-001
       
    74  * @SYMTestCaseDesc            Watch when memory is lower then threshold
       
    75  * @SYMTestPriority            Medium
       
    76  * @SYMTestActions             1) Create COutOfMemoryWatcher object, set memory threshold in constructor
       
    77  *                             2) Occupy memory so that free memory is below memory threshold
       
    78  * @SYMTestExpectedResults     1) COutOfMemoryWatcher object created
       
    79  *                             2) FreeMemThresholdCrossedL() is called       
       
    80  * @SYMTestType                Unit Test
       
    81  * @SYMCreationDate            20-07-2010
       
    82  */
       
    83 STIF_TESTDEFINE(GAPS-OOM-WATCHER-001)
       
    84 {
       
    85     const TInt KMemThresholdLow = 26214400;
       
    86     const TInt KMemThresholdGood = 31457280; // low=25M good=30M
       
    87     CMemoryMonitor* monitor = CMemoryMonitor::NewL();
       
    88     CleanupStack::PushL(monitor);
       
    89     COutOfMemoryWatcher* watcher = COutOfMemoryWatcher::NewL(*monitor, KMemThresholdLow, KMemThresholdGood, 
       
    90                                                               ETrue, KMemThresholdLow, KMemThresholdGood);// low=25M good=30M
       
    91     CleanupStack::PushL(watcher);
       
    92     // Verify objects created
       
    93     STIF_ASSERT_NOT_NULL(monitor);
       
    94     STIF_ASSERT_NOT_NULL(watcher);
       
    95     STIF_LOG("CMemoryMonitor and COutOfMemoryWatcher has been created successfully");
       
    96     
       
    97     // Start the watcher
       
    98     watcher->Start();
       
    99     
       
   100     // Create property for getting whether watcher has called correct method
       
   101     RProperty testWatcherProperty;
       
   102     TInt error;
       
   103     error = RProperty::Define(KOomWatcherTestProperty, KOomWatcherTestKey, RProperty::EInt, 
       
   104                               KAllowAllPolicy, KAllowAllPolicy);
       
   105     STIF_ASSERT_EQUALS(error, KErrNone);
       
   106     error = testWatcherProperty.Attach(KOomWatcherTestProperty, KOomWatcherTestKey, EOwnerThread);
       
   107     STIF_ASSERT_EQUALS(error, KErrNone);
       
   108     CleanupClosePushL(testWatcherProperty);
       
   109     STIF_LOG("Communication property created");
       
   110     
       
   111     // Occupy memory so that low mem is triggered
       
   112     UserSvr::HalFunction(EHalGroupKernel, EKernelHalSupervisorBarrier, 0, 0);
       
   113     TMemoryInfoV1Buf meminfo;
       
   114     UserHal::MemoryInfo(meminfo);
       
   115     TInt free = meminfo().iFreeRamInBytes;
       
   116     TInt totalRam;
       
   117     STIF_ASSERT_EQUALS(HAL::Get(HAL::EMemoryRAM, totalRam), KErrNone);
       
   118     TChunkCreateInfo createInfo;
       
   119     const TInt KChunkMaximumBelowFree = 20480*1024; // Chunk maximum = free - 20M
       
   120     createInfo.SetNormal(0, free - KChunkMaximumBelowFree);
       
   121     createInfo.SetPaging(TChunkCreateInfo::EUnpaged);
       
   122     RChunk c;
       
   123     error = c.Create(createInfo);
       
   124     STIF_ASSERT_EQUALS(error, KErrNone);
       
   125     CleanupClosePushL(c);
       
   126     const TInt KBufferSpace = 22528*1024; // 22M buffer
       
   127     STIF_LOG("Leave 22M RAM");
       
   128     error = c.Adjust(free - KBufferSpace);  // leave 22M
       
   129     STIF_ASSERT_EQUALS(error, KErrNone);
       
   130     STIF_LOG("Memory occupied");
       
   131     
       
   132     CActiveScheduler::Start();
       
   133     TInt propValue;
       
   134     error = testWatcherProperty.Get(propValue);
       
   135     STIF_ASSERT_EQUALS(error, KErrNone);
       
   136     STIF_ASSERT_EQUALS(propValue, KOomTestWatcherPropertyValue); // Same with set in ut_mockoommonitor.cpp
       
   137     STIF_LOG("Memory low handling function has been called correctly");
       
   138 
       
   139     // Cleanup
       
   140     CleanupStack::PopAndDestroy(4);
       
   141     error = RProperty::Delete(KOomWatcherTestProperty, KOomWatcherTestKey);
       
   142     STIF_ASSERT_EQUALS(error, KErrNone);
       
   143 }
       
   144 
       
   145 /**
       
   146  * @SYMTestCaseID              GAPS-OOM-WATCHER-002
       
   147  * @SYMTestCaseDesc            Watch when swap memory is lower then threshold
       
   148  * @SYMTestPriority            Medium
       
   149  * @SYMTestActions             1) Create COutOfMemoryWatcher object
       
   150  *                             2) Set swap memory threshold with UpdateThresholds, 
       
   151  *                                the value is larger than maximum swap memory space
       
   152  * @SYMTestExpectedResults     1) COutOfMemoryWatcher object created
       
   153  *                             2) FreeMemThresholdCrossedL() is called                      
       
   154  * @SYMTestType                Unit Test
       
   155  * @SYMCreationDate            20-07-2010
       
   156  */
       
   157 STIF_TESTDEFINE(GAPS-OOM-WATCHER-002)
       
   158 {
       
   159     const TInt KMemThresholdLow = 26214400;
       
   160     const TInt KMemThresholdGood = 31457280; // low=25M good=30M
       
   161     CMemoryMonitor* monitor = CMemoryMonitor::NewL();
       
   162     CleanupStack::PushL(monitor);
       
   163     COutOfMemoryWatcher* watcher = COutOfMemoryWatcher::NewL(*monitor, KMemThresholdLow, KMemThresholdGood, ETrue, KMemThresholdLow, KMemThresholdGood);// low=25M good=30M
       
   164     CleanupStack::PushL(watcher);
       
   165     // Verify objects created
       
   166     STIF_ASSERT_NOT_NULL(monitor);
       
   167     STIF_ASSERT_NOT_NULL(watcher);
       
   168     STIF_LOG("CMemoryMonitor and COutOfMemoryWatcher has been created successfully");
       
   169     
       
   170     // Start the watcher
       
   171     watcher->Start();
       
   172     
       
   173     // Create property for getting whether watcher has called correct method
       
   174     RProperty testWatcherProperty;
       
   175     TInt error;
       
   176     error = RProperty::Define(KOomWatcherTestProperty, KOomWatcherTestKey, RProperty::EInt, 
       
   177                               KAllowAllPolicy, KAllowAllPolicy);
       
   178     STIF_ASSERT_EQUALS(error, KErrNone);
       
   179     error = testWatcherProperty.Attach(KOomWatcherTestProperty, KOomWatcherTestKey, EOwnerThread);
       
   180     STIF_ASSERT_EQUALS(error, KErrNone);
       
   181     CleanupClosePushL(testWatcherProperty);
       
   182     STIF_LOG("Communication property created");
       
   183     
       
   184     // Change swap memory target to lower then swap memory, so that low mem is triggered
       
   185     // Get swap info
       
   186     SVMSwapInfo swapInfo;
       
   187     error = UserSvr::HalFunction(EHalGroupVM, EVMHalGetSwapInfo, &swapInfo, 0);
       
   188     STIF_ASSERT_EQUALS(error, KErrNone);
       
   189     // Set swap threshold
       
   190     watcher->UpdateThresholds(KMemThresholdLow, KMemThresholdGood, swapInfo.iSwapSize, swapInfo.iSwapSize);
       
   191     STIF_LOG("Swap memory threshold changed");
       
   192     
       
   193     CActiveScheduler::Start();
       
   194     TInt propValue;
       
   195     error = testWatcherProperty.Get(propValue);
       
   196     STIF_ASSERT_EQUALS(error, KErrNone);
       
   197     STIF_ASSERT_EQUALS(propValue, KOomTestWatcherPropertyValue); // Same with set in ut_mockoommonitor.cpp
       
   198     STIF_LOG("Swap memory low handling function has been called correctly");
       
   199 
       
   200     // Cleanup
       
   201     CleanupStack::PopAndDestroy(3);
       
   202     error = RProperty::Delete(KOomWatcherTestProperty, KOomWatcherTestKey);
       
   203     STIF_ASSERT_EQUALS(error, KErrNone);
       
   204 }
       
   205 
       
   206 /**
       
   207  * @SYMTestCaseID              GAPS-OOM-SERVER-001
       
   208  * @SYMTestCaseDesc            Test calling functions via client-server
       
   209  * @SYMTestPriority            Medium
       
   210  * @SYMTestActions             1) Start server in another process
       
   211  *                             2) Create and connect to ROomMonitorSession
       
   212  *                             3) Call ROomMonitorSession::RequestFreeMemory() function
       
   213  *                             4) Call ROomMonitorSession::RequestOptionalRam() function
       
   214  *                             5) Call ROomMonitorSession::CancelRequestFreeMemory() function
       
   215  *                             6) Call ROomMonitorSession::ThisAppIsNotExiting() function
       
   216  * @SYMTestExpectedResults     1) Server started
       
   217  *                             2) ROomMonitorSession connects to server successfully
       
   218  *                             3) RequestFreeMemoryL() function at server side is called
       
   219  *                             4) RequestOptionalRamL() function at server side is called
       
   220  *                             5) The cancel request is handled by ServiceL() function at server side
       
   221  *                             6) AppNotExiting() function at server side is called
       
   222  * @SYMTestExpectedResults     
       
   223  *                             
       
   224  * @SYMTestType                Unit Test
       
   225  * @SYMCreationDate            20-07-2010
       
   226  */
       
   227 STIF_TESTDEFINE(GAPS-OOM-SERVER-001)
       
   228 {
       
   229     // Start the mock server
       
   230     _LIT(KServerName, "ut_mockoomserver.exe");
       
   231     RProcess process;
       
   232     TInt error = process.Create(KServerName, KNullDesC);
       
   233     STIF_ASSERT_EQUALS(error, KErrNone);
       
   234     CleanupClosePushL(process);
       
   235     TRequestStatus status;
       
   236     process.Rendezvous(status);
       
   237     process.Resume();
       
   238     User::WaitForRequest(status);
       
   239     STIF_ASSERT_EQUALS(status.Int(), KErrNone);
       
   240     STIF_LOG("Mock test server has been started");
       
   241 
       
   242     // Create property for getting whether server has called correct method
       
   243     RProperty testServerProperty;
       
   244     error = RProperty::Define(KOomServerTestProperty, KOomServerTestKey, RProperty::EInt, 
       
   245                               KAllowAllPolicy, KAllowAllPolicy);
       
   246     STIF_ASSERT_EQUALS(error, KErrNone);
       
   247     error = testServerProperty.Attach(KOomServerTestProperty, KOomServerTestKey, EOwnerThread);
       
   248     STIF_ASSERT_EQUALS(error, KErrNone);
       
   249     CleanupClosePushL(testServerProperty);
       
   250     STIF_LOG("Communication property created");
       
   251 
       
   252     // Connect to ROomMonitorSession
       
   253     ROomMonitorSession monitorSession;
       
   254     error = monitorSession.Connect();
       
   255     STIF_ASSERT_EQUALS(error, KErrNone);
       
   256     CleanupClosePushL(monitorSession);
       
   257     STIF_LOG("ROomMonitorSession has connected to server successfully");
       
   258     
       
   259     // Test ROomMonitorSession::RequestFreeMemory() function (2 overloaded versions)
       
   260     monitorSession.RequestFreeMemory(0);
       
   261     // The function shall already be called at this time, and the property shall already get set
       
   262     TInt propValue = KOomTestInitialValue;
       
   263     error = testServerProperty.Get(propValue);
       
   264     STIF_ASSERT_EQUALS(error, KErrNone);
       
   265     STIF_ASSERT_EQUALS(propValue, KOomTestClientQueueFreeMemoryCalled); // Same with set in ut_mockoomclientrequestqueue.cpp
       
   266     STIF_LOG("RequestFreeMemory() sync version has been called correctly");
       
   267     // Set the property to initial value before next call
       
   268     error = testServerProperty.Set(KOomTestInitialValue);
       
   269     STIF_ASSERT_EQUALS(error, KErrNone);
       
   270     propValue = KOomTestInitialValue;
       
   271     
       
   272     monitorSession.RequestFreeMemory(0, status);
       
   273     User::WaitForRequest(status);
       
   274     // The function shall already be called at this time, and the property shall already get set
       
   275     error = testServerProperty.Get(propValue);
       
   276     STIF_ASSERT_EQUALS(error, KErrNone);
       
   277     STIF_ASSERT_EQUALS(propValue, KOomTestClientQueueFreeMemoryCalled); // Same with set in ut_mockoomclientrequestqueue.cpp
       
   278     STIF_LOG("RequestFreeMemory() async version has been called correctly"); 
       
   279     // Set the property to initial value before next call
       
   280     error = testServerProperty.Set(KOomTestInitialValue);
       
   281     STIF_ASSERT_EQUALS(error, KErrNone);
       
   282     propValue = KOomTestInitialValue;
       
   283 
       
   284     // Test ROomMonitorSession::RequestOptionalRam() function (2 overloaded versions)
       
   285     TInt bytesAvailable;
       
   286     monitorSession.RequestOptionalRam(0, 0, 0, bytesAvailable);
       
   287     // The function shall already be called at this time, and the property shall already get set
       
   288     error = testServerProperty.Get(propValue);
       
   289     STIF_ASSERT_EQUALS(error, KErrNone);
       
   290     STIF_ASSERT_EQUALS(propValue, KOomTestClientQueueOptionalRamCalled); // Same with set in ut_mockoomclientrequestqueue.cpp
       
   291     STIF_LOG("RequestOptionalRam() sync version has been called correctly"); 
       
   292     // Set the property to initial value before next call
       
   293     error = testServerProperty.Set(KOomTestInitialValue);
       
   294     STIF_ASSERT_EQUALS(error, KErrNone);
       
   295     propValue = KOomTestInitialValue;
       
   296     
       
   297     monitorSession.RequestOptionalRam(0, 0, 0, status);
       
   298     User::WaitForRequest(status);
       
   299     // The function shall already be called at this time, and the property shall already get set
       
   300     error = testServerProperty.Get(propValue);
       
   301     STIF_ASSERT_EQUALS(error, KErrNone);
       
   302     STIF_ASSERT_EQUALS(propValue, KOomTestClientQueueOptionalRamCalled); // Same with set in ut_mockoomclientrequestqueue.cpp
       
   303     STIF_LOG("RequestOptionalRam() async version has been called correctly"); 
       
   304     // Set the property to initial value before next call
       
   305     error = testServerProperty.Set(KOomTestInitialValue);
       
   306     STIF_ASSERT_EQUALS(error, KErrNone);
       
   307     propValue = KOomTestInitialValue;
       
   308 
       
   309     // Test ROomMonitorSession::CancelRequestFreeMemory()
       
   310     monitorSession.CancelRequestFreeMemory();
       
   311     // No verification is needed as this function only returns success
       
   312     STIF_LOG("CancelRequestFreeMemory() has been called correctly"); 
       
   313     
       
   314     // Test ROomMonitorSession::ThisAppIsNotExiting() function 
       
   315     monitorSession.ThisAppIsNotExiting(0);
       
   316     // The function shall already be called at this time, and the property shall already get set
       
   317     error = testServerProperty.Get(propValue);
       
   318     STIF_ASSERT_EQUALS(error, KErrNone);
       
   319     STIF_ASSERT_EQUALS(propValue, KOomTestMonitorAppNotExitingCalled); // Same with set in ut_mockoommonitor.cpp
       
   320     STIF_LOG("ThisAppIsNotExiting() has been called correctly"); 
       
   321     // Set the property to initial value before next call
       
   322     error = testServerProperty.Set(KOomTestInitialValue);
       
   323     STIF_ASSERT_EQUALS(error, KErrNone);
       
   324     propValue = KOomTestInitialValue;
       
   325 
       
   326     // The test is done, close the app
       
   327     process.Kill(KErrNone);
       
   328     // Cleanup
       
   329     CleanupStack::PopAndDestroy(3);
       
   330     error = RProperty::Delete(KOomServerTestProperty, KOomServerTestKey);
       
   331     STIF_ASSERT_EQUALS(error, KErrNone);
       
   332 }
       
   333 
       
   334 /**
       
   335  * @SYMTestCaseID              GAPS-OOM-SERVER-002
       
   336  * @SYMTestCaseDesc            Test calling SetOomPriority functions via client-server
       
   337  * @SYMTestPriority            Medium
       
   338  * @SYMTestActions             1) Start server in another process
       
   339  *                             2) Create and connect to ROomMonitorSession
       
   340  *                             3) Call ROomMonitorSession::SetOomPriority() function
       
   341  * @SYMTestExpectedResults     1) Server started
       
   342  *                             2) ROomMonitorSession connects to server successfully
       
   343  *                             3) The request is handled by ServiceL() function at server side
       
   344  * @SYMTestType                Unit Test
       
   345  * @SYMCreationDate            20-07-2010
       
   346  */
       
   347 STIF_TESTDEFINE(GAPS-OOM-SERVER-002)
       
   348 {
       
   349     // Start the mock server
       
   350     _LIT(KServerName, "ut_mockoomserver.exe");
       
   351     RProcess process;
       
   352     TInt error = process.Create(KServerName, KNullDesC);
       
   353     STIF_ASSERT_EQUALS(error, KErrNone);
       
   354     CleanupClosePushL(process);
       
   355     TRequestStatus status;
       
   356     process.Rendezvous(status);
       
   357     process.Resume();
       
   358     User::WaitForRequest(status);
       
   359     STIF_ASSERT_EQUALS(status.Int(), KErrNone);
       
   360     STIF_LOG("Mock test server has been started");
       
   361 
       
   362     // Connect to ROomMonitorSession
       
   363     ROomMonitorSession monitorSession;
       
   364     error = monitorSession.Connect();
       
   365     STIF_ASSERT_EQUALS(error, KErrNone);
       
   366     CleanupClosePushL(monitorSession);
       
   367     STIF_LOG("ROomMonitorSession has connected to server successfully");
       
   368 
       
   369 #ifdef _DEBUG
       
   370     TestModuleIf().SetExitReason(CTestModuleIf::EPanic, KNoCoeEnvFound);
       
   371 #endif
       
   372     // Test ROomMonitorSession::SetOomPriority() function 
       
   373     monitorSession.SetOomPriority(ROomMonitorSession::EOomPriorityNormal);
       
   374     // No need to verify
       
   375     STIF_LOG("SetOomPriority() has been called correctly"); 
       
   376 
       
   377     // The test is done, close the app
       
   378     process.Kill(KErrNone);
       
   379     // Cleanup
       
   380     CleanupStack::PopAndDestroy(2);
       
   381     error = RProperty::Delete(KOomServerTestProperty, KOomServerTestKey);
       
   382     STIF_ASSERT_EQUALS(error, KErrNone);
       
   383 }
       
   384 
       
   385 #endif
       
   386 /**
       
   387  * END OF TEST CASES SECTION
       
   388  */
       
   389 
       
   390 // End of File