kerneltest/e32test/property/t_propclose.cpp
changeset 293 0659d0e1a03c
equal deleted inserted replaced
291:206a6eaaeb71 293:0659d0e1a03c
       
     1 // Copyright (c) 2010-2010 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 the License "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 // e32test\property\t_propclose
       
    15 //This test case was written to exercise the use-case 
       
    16 //before DPropertyRef::Close() was introduced in sproperty.cpp
       
    17 //Kernel used to crash when a Close() was issued on a property handle by some thread
       
    18 //when other thread had issued a Cancel() on it.
       
    19 //
       
    20 
       
    21 #define __E32TEST_EXTENSION__
       
    22 #include <e32test.h>
       
    23 #include <e32property.h>
       
    24 
       
    25 RTest test(_L("T_PROPCLOSE"));
       
    26 
       
    27 const TInt32 KUidTestPropertyCategoryValue = 0xbaadbeef;
       
    28 const TUid KUidTestPropertyCategory = {KUidTestPropertyCategoryValue};
       
    29 const TUint KUidTestPropertyKey = 0xfeedfaceu;
       
    30 const TInt KHeapSize=0x200;
       
    31 
       
    32 #define TEST_RUNS 1000
       
    33 
       
    34 class CPropertyTest
       
    35 	{
       
    36 public:
       
    37 	CPropertyTest();
       
    38 	~CPropertyTest();
       
    39 
       
    40 	TInt DefineProperty();
       
    41 
       
    42 	TInt CreateThreadOneAndResume();
       
    43 	TInt CreateThreadTwoAndResume();
       
    44 	void CloseAndWait();
       
    45 
       
    46 	static TInt ThreadFuncOne(TAny* aThreadOnePtr);
       
    47 	TInt RunThreadFuncOne();
       
    48 
       
    49 	static TInt ThreadFuncTwo(TAny* aThreadTwoPtr);
       
    50 	TInt RunThreadFuncTwo();
       
    51 
       
    52 private:	
       
    53 	RProperty iP;
       
    54 	RTest iTest;
       
    55 
       
    56 	RThread iThreadOne;
       
    57 	TRequestStatus iThreadOneStatus;
       
    58 
       
    59 	RThread iThreadTwo;
       
    60 	TRequestStatus iThreadTwoStatus;
       
    61 
       
    62 	TInt iLoopCount;
       
    63 	};
       
    64 
       
    65 
       
    66 CPropertyTest::CPropertyTest():iTest(_L("PropertyClose Test"))
       
    67 	{
       
    68 	iLoopCount=0;
       
    69 	}
       
    70 
       
    71 
       
    72 CPropertyTest::~CPropertyTest()
       
    73 	{
       
    74 	iTest.End();
       
    75 	iTest.Close();	
       
    76 	}
       
    77 
       
    78 
       
    79 TInt CPropertyTest::DefineProperty()
       
    80 	{
       
    81 	iTest.Title();
       
    82 	iTest.Start(_L("Start PropertyClose Test"));
       
    83 	iTest.Next(_L("Define Property"));
       
    84 	TInt r = RProperty::Define(KUidTestPropertyCategory, KUidTestPropertyKey, RProperty::EInt);
       
    85 	test(r==KErrAlreadyExists || r==KErrNone);
       
    86 	return r;
       
    87 	}
       
    88 
       
    89 TInt CPropertyTest::CreateThreadOneAndResume()
       
    90 	{
       
    91 	TInt r=KErrNone;
       
    92 
       
    93 	r = iThreadOne.Create(KNullDesC,ThreadFuncOne,KDefaultStackSize,KHeapSize,KHeapSize,this);
       
    94 	test_KErrNone(r);
       
    95 	iThreadOne.SetPriority(EPriorityAbsoluteHigh); //value 23
       
    96 	iThreadOne.Logon(iThreadOneStatus);
       
    97 
       
    98 	iThreadOne.Resume();
       
    99 	return r;
       
   100 	}
       
   101 
       
   102 
       
   103 TInt CPropertyTest::CreateThreadTwoAndResume()
       
   104 	{
       
   105 	TInt r=KErrNone;
       
   106 
       
   107 	r = iThreadTwo.Create(KNullDesC,ThreadFuncTwo,KDefaultStackSize,KHeapSize,KHeapSize,this);
       
   108 	test_KErrNone(r);
       
   109 	iThreadTwo.SetPriority(EPriorityMore); //value 23
       
   110 	iThreadTwo.Logon(iThreadTwoStatus);
       
   111 
       
   112 	iThreadTwo.Resume();
       
   113 	return r;
       
   114 	}
       
   115 
       
   116 void CPropertyTest::CloseAndWait()
       
   117 	{
       
   118 	User::WaitForAnyRequest();
       
   119 	if( iThreadOne.ExitType() == EExitPanic)
       
   120 		{
       
   121 		iThreadOne.Close();
       
   122 		CreateThreadOneAndResume();
       
   123 		CloseAndWait();
       
   124 		}
       
   125 	else if(iThreadOneStatus == KErrNone)
       
   126 			{
       
   127 			iThreadOne.Close();
       
   128 			}
       
   129 		else if(iThreadTwoStatus == KErrNone)
       
   130 					{
       
   131 					iThreadTwo.Close();
       
   132 					}
       
   133 	}
       
   134 
       
   135 
       
   136 TInt CPropertyTest::ThreadFuncOne(TAny* aThreadOnePtr)
       
   137 	{
       
   138 	return ((CPropertyTest*)aThreadOnePtr)->RunThreadFuncOne();
       
   139 	}
       
   140 
       
   141 
       
   142 TInt CPropertyTest::ThreadFuncTwo(TAny* aThreadTwoPtr)
       
   143 	{
       
   144 	return ((CPropertyTest*)aThreadTwoPtr)->RunThreadFuncTwo();
       
   145 	}
       
   146 
       
   147 
       
   148 TInt CPropertyTest::RunThreadFuncOne()
       
   149 	{
       
   150 	TInt r=KErrNone;
       
   151 	TInt attach;
       
   152 	TRequestStatus sp1;
       
   153 	while(iLoopCount <= TEST_RUNS)
       
   154 		{
       
   155 		attach = iP.Attach(KUidTestPropertyCategory, KUidTestPropertyKey);
       
   156 		test_KErrNone(attach);
       
   157 		iP.Subscribe(sp1);
       
   158 		iP.Cancel();  //should initiate TProperty::CompleteCancellationQDfc()
       
   159 		iLoopCount++;
       
   160 		}
       
   161 	return r;
       
   162 	}
       
   163 
       
   164 
       
   165 TInt CPropertyTest::RunThreadFuncTwo()
       
   166 	{
       
   167 	while(iLoopCount <= TEST_RUNS)
       
   168 		{
       
   169 		//loop adds delay to simulate crash on Naviengine
       
   170 		for(TInt i=0;i<10000;i++){};
       
   171 		iP.Close();
       
   172 		iLoopCount++;
       
   173 		}
       
   174 	return KErrNone;
       
   175 	}
       
   176 
       
   177 
       
   178 GLDEF_C TInt E32Main()
       
   179 	{
       
   180 	//Need to ensure ThreadOne,ThreadTwo and Main are of higher priorities than SVR thread
       
   181 	RProcess().SetPriority(EPriorityHigh); 
       
   182 	RThread().SetPriority(EPriorityMore); //value 20
       
   183 
       
   184 	CPropertyTest propertyTest;
       
   185 	propertyTest.DefineProperty();
       
   186 	propertyTest.CreateThreadOneAndResume();
       
   187 	propertyTest.CreateThreadTwoAndResume();
       
   188 	propertyTest.CloseAndWait();
       
   189 
       
   190 	return KErrNone;
       
   191 	}