phonebookengines_old/contactsmodel/tsrc/T_Concurrent.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2000-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 //  Include Files  
       
    17 #include "T_Concurrent.h"
       
    18 #include <e32base.h>
       
    19 #include <e32std.h>
       
    20 #include <e32cons.h>            
       
    21 #include <cntdb.h>
       
    22 #include <cntdbobs.h>
       
    23 #include <cntitem.h>
       
    24 #include <cntfield.h>
       
    25 #include <cntfldst.h>
       
    26 
       
    27 
       
    28 //  Constants
       
    29 _LIT(KThreadTitle, "Thread2");
       
    30 _LIT(KTextFailed, " failed, leave code = %d");
       
    31 
       
    32 // Global Variables
       
    33 RTest MainTest(_L("Concurrent test"));	// Main RTest
       
    34 CContactDatabase * database = NULL;		// Shared database.
       
    35 RSemaphore sem;							// Synchronising Semaphore
       
    36 TInt t1MachineId = 0;					// Thread1 machineId from db.
       
    37 TInt currentStep = 0;					// Current test step.
       
    38 
       
    39 //
       
    40 //                  CHILD THREAD FUNCTIONS.
       
    41 //
       
    42 
       
    43 /**
       
    44 * The main function called when the child thread is invoked.
       
    45 */
       
    46 TInt RunChildThread(TAny * /* ptr */)
       
    47 	{
       
    48     // Create cleanup stack
       
    49     __UHEAP_MARK;
       
    50     CTrapCleanup* cleanup = CTrapCleanup::New();
       
    51     
       
    52     // Create active scheduler (to run active objects)
       
    53     CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
       
    54     CActiveScheduler::Install(scheduler);
       
    55     CChildThread* childThread = NULL;
       
    56     
       
    57     TInt err;
       
    58     TRAP(err, childThread = CChildThread::NewL());
       
    59 	if (err == KErrNone)
       
    60 		{
       
    61 		TRAP(err, childThread->AccessDBTestL() );
       
    62 		}
       
    63     
       
    64 	if (err != KErrNone)
       
    65     	MainTest.Printf( _L(" Child thread test returned with error = %d"),  err);
       
    66 
       
    67 	delete childThread;
       
    68     delete cleanup;
       
    69     delete scheduler;
       
    70 	__UHEAP_MARKEND;
       
    71     return KErrNone;	
       
    72 	}
       
    73 
       
    74 /**
       
    75 * The child thread accesses the shared db session.
       
    76 * A basic operation like getting the count of contacts is performed.
       
    77 */
       
    78 CChildThread * CChildThread::NewL()
       
    79 	{
       
    80 	CChildThread * self = new(ELeave) CChildThread;
       
    81 	CleanupStack::PushL(self);
       
    82 	self->ConstructL();
       
    83 	CleanupStack::Pop(self);
       
    84 	return self;
       
    85 	}
       
    86 
       
    87 void CChildThread::ConstructL()
       
    88 	{
       
    89 	TBuf<100> threadName;
       
    90 	threadName.Format(_L("Child Thread"));
       
    91 	iTest = new(ELeave) RTest(threadName);
       
    92 	}
       
    93 
       
    94 CChildThread::~CChildThread()
       
    95 	{
       
    96 	iTest->Close();
       
    97 	delete iTest;
       
    98 	iTest = NULL;
       
    99 	}
       
   100 	
       
   101 
       
   102 /**
       
   103 * This function tries to access the db and check the count, or perform some other action
       
   104 * on the db. The session object is shared with the main thread .
       
   105 */
       
   106 void CChildThread::AccessDBTestL()
       
   107 	{
       
   108     
       
   109     while(currentStep != ETestsEnd)
       
   110     	{
       
   111 	    TInt t2MachineId = database->MachineId();
       
   112 	    TInt count = database->CountL();
       
   113 	    iTest->Printf( _L("Thread2: Contact Count = %d"), count  );
       
   114 	    
       
   115 	    // Check that the child thread is accessing the shared db session correctly.
       
   116 	    MainTest(count==1);
       
   117 	    MainTest( t2MachineId == t1MachineId );
       
   118 	    
       
   119 	    sem.Signal();
       
   120 	    RThread().Suspend();
       
   121 	    }
       
   122 	   
       
   123 	// Child thread is now over.  
       
   124 	sem.Signal();  
       
   125 	    
       
   126 	}
       
   127 
       
   128 
       
   129 //
       
   130 //                  MAIN THREAD FUNCTIONS.
       
   131 //
       
   132 /**
       
   133 * The main thread object which owns the session with the database in thread shared mode.
       
   134 */
       
   135 CMainThread * CMainThread::NewL()
       
   136 	{
       
   137 	CMainThread * self = new (ELeave) CMainThread;
       
   138 	CleanupStack::PushL(self);
       
   139 	self->ConstructL();
       
   140 	CleanupStack::Pop();
       
   141 	return self;
       
   142 	}
       
   143 	
       
   144 void CMainThread::ConstructL()
       
   145 	{
       
   146 	TBuf<100> threadName;
       
   147 	threadName.Format(_L("Main Thread"));
       
   148 	iTest = new(ELeave) RTest(threadName);
       
   149 	}
       
   150 	
       
   151 CMainThread::~CMainThread()
       
   152 	{
       
   153 	TRAP_IGNORE(CloseDatabaseSessionL() );
       
   154 	iTest->Close();
       
   155 	delete iTest;
       
   156 	iTest = NULL;
       
   157 	iChildThread.Close();
       
   158 	}
       
   159 	
       
   160 /** 
       
   161 * The function to empty the database and close the database session.
       
   162 */
       
   163 void CMainThread::CloseDatabaseSessionL()
       
   164 	{
       
   165 		TestDeleteContactL();
       
   166 		delete database;
       
   167 		database = NULL;
       
   168 	}
       
   169 
       
   170 /**
       
   171 * Create the database in shared mode.
       
   172 */
       
   173 void CMainThread::TestCreateDatabaseL()
       
   174 	{
       
   175 	// Creat the database in the first thread.
       
   176     iTest->Next( _L("CreateDatabaseL :Test creating the db in multi - thread mode.") );
       
   177     TRAP_IGNORE(CContactDatabase::DeleteDefaultFileL());
       
   178     database = CContactDatabase::CreateL( CContactDatabase::EMultiThread );
       
   179     t1MachineId = database->MachineId();
       
   180 	}
       
   181 
       
   182 /**
       
   183 * Open the database in shared mode.
       
   184 */
       
   185 void CMainThread::TestOpenDatabaseL()
       
   186 	{
       
   187 	// Open the database in the first thread.
       
   188     iTest->Next( _L("OpenDatabaseL :Test Opening the db in multi - thread mode.") );
       
   189     database = CContactDatabase::OpenL( CContactDatabase::EMultiThread );
       
   190     t1MachineId = database->MachineId();
       
   191 	}
       
   192 
       
   193 /**
       
   194 * Replace the database in shared mode.
       
   195 */
       
   196 void CMainThread::TestReplaceDatabaseL()
       
   197 	{
       
   198     // Replace the database in the first thread, in mutithread mode.
       
   199     iTest->Next( _L("ReplaceDatabaseL :Test Replacing the db in multi - thread mode.") );
       
   200     database = CContactDatabase::ReplaceL( CContactDatabase::EMultiThread );
       
   201     t1MachineId = database->MachineId();
       
   202 	}
       
   203 
       
   204 /**
       
   205 * Add one contact to the database.
       
   206 */
       
   207 void CMainThread::TestAddContactL()
       
   208 	{
       
   209     
       
   210     // Add a contact 
       
   211 	_LIT(KForename,"Jo"); 
       
   212 	_LIT(KSurname,"Stichbury"); 
       
   213 
       
   214 	// Create a  contact card to contain the data
       
   215 	CContactCard* newCard = CContactCard::NewLC();
       
   216     
       
   217 	// Create the firstName field and add the data to it
       
   218 	CContactItemField* firstName = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
       
   219 	firstName->TextStorage()->SetTextL(KForename);
       
   220 	newCard->AddFieldL(*firstName);
       
   221   	CleanupStack::Pop(firstName);
       
   222   	
       
   223 	// Create the lastName field and add the data to it
       
   224    	CContactItemField* lastName= CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);
       
   225   	lastName ->TextStorage()->SetTextL(KSurname);
       
   226   	newCard->AddFieldL(*lastName);
       
   227    	CleanupStack::Pop(lastName);
       
   228    		    
       
   229 	// Add newCard to the database
       
   230 	iContactId = database->AddNewContactL(*newCard);
       
   231 	CleanupStack::PopAndDestroy(newCard);
       
   232 	
       
   233 	iTest->Printf( _L("Thread1: Added 1 contact to the database."));
       
   234 	}
       
   235 
       
   236 /**
       
   237 * Delete the contact from the database.
       
   238 */
       
   239 void CMainThread::TestDeleteContactL()
       
   240 	{
       
   241 	// Check if a contact has been stored in the db and delete it.
       
   242 	if (iContactId)
       
   243 		{
       
   244 		database->DeleteContactL(iContactId);
       
   245 		iContactId = 0;
       
   246 		}
       
   247 	}
       
   248 
       
   249 /**
       
   250 * The main thread controls the child thread
       
   251 */
       
   252 void CMainThread::LaunchChildThreadL()
       
   253 	{
       
   254 	
       
   255     TRAPD(err,iChildThread.Create(KThreadTitle, RunChildThread, KDefaultStackSize, NULL, NULL, EOwnerProcess));
       
   256     
       
   257     if(err != KErrNone)
       
   258 		{
       
   259 		iTest->Printf(_L("Failed to create the child thread, error code=%d"), err);
       
   260 		User::Leave(err);
       
   261 		}
       
   262 	}
       
   263 
       
   264 /**
       
   265 * Resume the child thread.
       
   266 */	
       
   267 
       
   268 void CMainThread::ResumeChildThreadL()
       
   269 	{
       
   270     iChildThread.Resume();
       
   271 	}
       
   272 
       
   273 
       
   274 //
       
   275 //                  MAIN FUNCTIONS.
       
   276 //
       
   277 
       
   278 /**
       
   279 * The main function which creates the main thread controller object 
       
   280 * and invokes all the tests.
       
   281 */
       
   282 
       
   283 /**
       
   284 
       
   285 @SYMTestCaseID     PIM-T-CONCURRENT-0001
       
   286 
       
   287 */
       
   288 
       
   289 LOCAL_C void DoTestsL()
       
   290     {
       
   291     
       
   292     // Initialisation
       
   293     MainTest.Start(_L("@SYMTESTCaseID:PIM-T-CONCURRENT-0001 T_Concurrent"));
       
   294 
       
   295     
       
   296     // Create active scheduler (to run active objects)
       
   297     CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
       
   298     CleanupStack::PushL(scheduler);
       
   299     CActiveScheduler::Install(scheduler);
       
   300 
       
   301     // Create main thread object, child thread and synchnising semaphore.
       
   302     sem.CreateLocal(0);
       
   303     CMainThread * ptr = CMainThread::NewL();
       
   304     CleanupStack::PushL(ptr);
       
   305     ptr->LaunchChildThreadL();
       
   306 	
       
   307 	// Run all the tests
       
   308 	while (currentStep <= ETestsEnd)
       
   309 		{
       
   310 		switch(currentStep)
       
   311 			{
       
   312 			case ETestCreate:
       
   313 				// create the db in shared mode and add a contact. 
       
   314 			    ptr->TestCreateDatabaseL();
       
   315 			    ptr->TestAddContactL();
       
   316 				break;
       
   317 			case ETestOpen:
       
   318 			 	// create the db in shared mode 
       
   319 			    ptr->TestOpenDatabaseL();
       
   320 			    ptr->TestAddContactL();
       
   321 				break;
       
   322 			case ETestReplace:
       
   323 				// create the db in shared mode 
       
   324 			    ptr->TestReplaceDatabaseL();
       
   325 				ptr->TestAddContactL();
       
   326 				break;
       
   327 			case ETestsEnd:
       
   328 				break;
       
   329 			default:
       
   330 				break;
       
   331 			} // end of switch
       
   332 			
       
   333 			// Run the child thread and let it access the shared session.
       
   334 		    ptr->ResumeChildThreadL();
       
   335 		    sem.Wait();
       
   336 		    ptr->CloseDatabaseSessionL();
       
   337 		    currentStep++ ;
       
   338 		} // end of while
       
   339 
       
   340     // Cleanup and close.
       
   341     sem.Close();
       
   342     CleanupStack::PopAndDestroy(ptr);
       
   343     CleanupStack::PopAndDestroy(scheduler);
       
   344     }
       
   345 
       
   346 
       
   347 /**
       
   348 *  Entry point function.
       
   349 */
       
   350 GLDEF_C TInt E32Main()
       
   351     {
       
   352     // Create cleanup stack
       
   353     __UHEAP_MARK;
       
   354     CTrapCleanup* cleanup = CTrapCleanup::New();
       
   355 	
       
   356     TRAPD(mainError, DoTestsL());
       
   357     if (mainError)
       
   358         MainTest.Printf(KTextFailed, mainError);
       
   359     
       
   360     MainTest.End();
       
   361     MainTest.Close();
       
   362     delete cleanup;
       
   363     
       
   364     __UHEAP_MARKEND;
       
   365     return KErrNone;
       
   366     }
       
   367 
       
   368 	
       
   369 
       
   370 
       
   371 
       
   372 
       
   373