authenticationservices/authenticationserver/test/tAuthSvr/src/step_multithreaded.cpp
changeset 29 ece3df019add
equal deleted inserted replaced
19:cd501b96611d 29:ece3df019add
       
     1 /*
       
     2 * Copyright (c) 2009 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 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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Multi threaded tests on AuthServer.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "tAuthSvrStep.h"
       
    20 #include "authserver/authclient.h"
       
    21 #include "authserver/authmgrclient.h"
       
    22 
       
    23 
       
    24 
       
    25 using namespace AuthServer;
       
    26 
       
    27 //Active object for registration
       
    28 class CMultiThreadedRegisterActive : public CActive
       
    29     {
       
    30     public:
       
    31      CMultiThreadedRegisterActive(RAuthMgrClient& aClient, HBufC* aIdentityString, CIdentity*& aResult) : 
       
    32          CActive(EPriorityNormal),
       
    33          iFirstTime(true),
       
    34          iClient(aClient), 
       
    35          iResult(aResult),
       
    36          iIdentityString(aIdentityString),
       
    37          iErr(KErrNone)
       
    38         {
       
    39         CActiveScheduler::Add(this);
       
    40         }
       
    41     ~CMultiThreadedRegisterActive()
       
    42         {
       
    43         delete iIdentityString; 
       
    44         }
       
    45     void doReg()
       
    46         {
       
    47         SetActive();        
       
    48         TRequestStatus* status = &iStatus;
       
    49         User::RequestComplete(status, KErrNone);
       
    50         iFirstTime = ETrue;
       
    51         }
       
    52         void DoCancel() 
       
    53         {
       
    54         }
       
    55      void RunL() 
       
    56         {
       
    57         iErr = iStatus.Int();
       
    58         if (iFirstTime)
       
    59             {
       
    60             SetActive();
       
    61             iStatus = KRequestPending;
       
    62             iClient.RegisterIdentityL(iResult, *iIdentityString, iStatus);
       
    63             iFirstTime = false;
       
    64             }
       
    65         else
       
    66             {
       
    67             iErr = iStatus.Int();
       
    68             RDebug::Printf("iErr = %d", iErr);
       
    69             CActiveScheduler::Stop();
       
    70             }
       
    71         }
       
    72         
       
    73     TBool iFirstTime;
       
    74     RAuthMgrClient& iClient;
       
    75     CIdentity*& iResult;
       
    76     HBufC* iIdentityString;
       
    77     TInt iErr;
       
    78     };
       
    79 
       
    80 CTMultiThreaded::~CTMultiThreaded()
       
    81 	{}
       
    82 
       
    83 CTMultiThreaded::CTMultiThreaded(CTAuthSvrServer& aParent): iParent(aParent)
       
    84 
       
    85 	{
       
    86 	
       
    87 	SetTestStepName(KTMultiThreadedTest);
       
    88 	}
       
    89 
       
    90 TVerdict CTMultiThreaded::doTestStepPreambleL()
       
    91 
       
    92 	{
       
    93 	CTStepActSch::doTestStepPreambleL();	
       
    94 	return TestStepResult();
       
    95 	}
       
    96 
       
    97 TVerdict CTMultiThreaded::doTestStepPostambleL()
       
    98 
       
    99     {
       
   100     
       
   101     CTStepActSch::doTestStepPostambleL();
       
   102     return TestStepResult();
       
   103     }
       
   104 
       
   105 TVerdict CTMultiThreaded::doTestStepL()
       
   106 	{
       
   107 	SetTestStepResult(EPass);
       
   108 	Logger().ShareAuto();
       
   109 	//Initialize AuthServer.
       
   110 	InitAuthServerFromFileL();  
       
   111 	
       
   112 	
       
   113 	//Setup two threads which request for registration concurrently.    
       
   114 	//Create first thread.
       
   115 	RThread firstThread;
       
   116 	TInt err(0);
       
   117 	TThreadParams firstThreadParams;
       
   118 	err = firstThread.Create( _L("First Thread"), (TThreadFunction)runMultiThreadedTest, 
       
   119                                     KDefaultStackSize, KMinHeapSize, 1024*1024 /*Max heap size*/, &firstThreadParams);
       
   120 	if(err != KErrNone)
       
   121 	    {
       
   122 	    ERR_PRINTF2(_L("Couldn't start first thread. Error = %d"), err);
       
   123 	    SetTestStepResult(EFail);
       
   124 	    return TestStepResult();
       
   125 	    }
       
   126 	    
       
   127 	// Setup the requeststatus for firstThread completion notification.
       
   128 	TRequestStatus firstThreadFinishStatus;
       
   129 	firstThread.Logon(firstThreadFinishStatus);
       
   130 	
       
   131 	
       
   132 	
       
   133 	//Setup the second thread similarly.
       
   134 	RThread secondThread;
       
   135 	TThreadParams secondThreadParams;
       
   136     err = secondThread.Create( _L("Second Thread"), (TThreadFunction)runMultiThreadedTest, 
       
   137 									KDefaultStackSize, KMinHeapSize, 1024*1024 /*Max heap size*/, &secondThreadParams);
       
   138     if(err != KErrNone)
       
   139         {
       
   140         ERR_PRINTF2(_L("Couldn't start second thread. Error = %d"), err);
       
   141         SetTestStepResult(EFail);
       
   142         return TestStepResult();
       
   143         }
       
   144         
       
   145     // Setup the requeststatus for secondThread completion notification.
       
   146     TRequestStatus secondThreadFinishStatus;
       
   147     secondThread.Logon(secondThreadFinishStatus);
       
   148     
       
   149     //Resume both threads.
       
   150 	firstThread.Resume();   
       
   151 	secondThread.Resume();   
       
   152 	
       
   153 	//Wait for either request to complete.
       
   154 	User::WaitForRequest(firstThreadFinishStatus, secondThreadFinishStatus );     
       
   155 	
       
   156 	//Now, wait for the unfinished thread.
       
   157 	User::WaitForRequest(firstThreadFinishStatus == KRequestPending? firstThreadFinishStatus:secondThreadFinishStatus);
       
   158 	
       
   159 	INFO_PRINTF1(_L("Both Threads completed."));
       
   160 	INFO_PRINTF2(_L("First thread completed with %d."), firstThreadParams.result);
       
   161 	INFO_PRINTF2(_L("Second thread completed with %d."), secondThreadParams.result);
       
   162 	
       
   163 	if((firstThreadParams.result != KErrServerBusy && secondThreadParams.result != KErrNone) &&
       
   164 	        (firstThreadParams.result != KErrNone && secondThreadParams.result != KErrServerBusy))
       
   165 	    {
       
   166 	    SetTestStepResult(EFail);
       
   167 	    }
       
   168 	firstThread.Close();
       
   169 	secondThread.Close();
       
   170 	return TestStepResult();
       
   171 	}
       
   172 
       
   173 
       
   174 
       
   175 void CTMultiThreaded::runMultiThreadedTest(TThreadParams* aParams)
       
   176     {
       
   177     CTrapCleanup* cleanup = CTrapCleanup::New();
       
   178     CActiveScheduler* sched(0);
       
   179     
       
   180     TRAPD(err, sched = new (ELeave) CActiveScheduler);
       
   181     if(err != KErrNone)
       
   182         {
       
   183         delete cleanup;
       
   184         User::Exit(err);
       
   185         }
       
   186     CActiveScheduler::Install(sched); 
       
   187    
       
   188     
       
   189     /* Switch case can be added here to generalize this function to run any method.
       
   190      * For now, only registration is called.
       
   191      */
       
   192     
       
   193     TRAPD(ret,doRegisterL(*aParams));
       
   194 	
       
   195 	RDebug::Printf("doRegisterL returned %d", ret);
       
   196     
       
   197     delete cleanup;
       
   198     delete sched;
       
   199     }
       
   200 
       
   201 
       
   202 void CTMultiThreaded::doRegisterL(TThreadParams& aParams)
       
   203     {
       
   204     _LIT(KIdString, "User1");
       
   205     
       
   206     RAuthMgrClient client;
       
   207     User::LeaveIfError(client.Connect());
       
   208     CleanupClosePushL(client);
       
   209     
       
   210     HBufC* identityString = KIdString().AllocLC();
       
   211     CIdentity* identity1 = 0;
       
   212     
       
   213     CMultiThreadedRegisterActive* active = new (ELeave)CMultiThreadedRegisterActive(client, identityString, identity1);
       
   214     CleanupStack::PushL(active);
       
   215     active->doReg();
       
   216     CActiveScheduler::Start();
       
   217     
       
   218     //Store the error code in aParams.
       
   219     aParams.result = active->iErr;
       
   220     if(aParams.result == KErrNone)
       
   221         {
       
   222         RDebug::Printf("Registered new Identity! Id = %x", identity1->Id());
       
   223         }
       
   224     else
       
   225         {
       
   226         RDebug::Printf("Registration Failed !");
       
   227         }
       
   228     
       
   229     delete identity1;
       
   230     
       
   231     CleanupStack::Pop(3, &client);
       
   232     }
       
   233