kerneltest/e32test/property/t_stress_property.cpp
changeset 0 a41df078684a
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2002-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 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 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include "t_property.h"
       
    18 
       
    19 _LIT(KTestName,"t_stress_property");
       
    20 
       
    21 RTest test(KTestName);
       
    22 
       
    23 const TInt32 KUidPropTestCategoryValue = 0x101f75b8;
       
    24 const TUid KPropTestCategory = { KUidPropTestCategoryValue };
       
    25 
       
    26 #define TEST_TIME 36000			//10 hours
       
    27 
       
    28 #define TEST_ERROR(rl,rr) { if((TInt)rl!=(TInt)rr) { ExitThread(rl, rr, __LINE__); return KErrGeneral; } }
       
    29 
       
    30 TBool volatile StopAndExit = EFalse;
       
    31 TTime startTime;
       
    32 
       
    33 LOCAL_D void ExitThread(TInt rl, TInt rr, TInt aLine)
       
    34 	{
       
    35 	test.Printf(_L("Test '%S' failed at line %d; Expected value=%d Actual value=%d; \n"), &KTestName, aLine, rr, rl);
       
    36 	StopAndExit = ETrue;
       
    37 	//delete if it's not deleted, to wake up subscribing threads waiting for events on this property
       
    38 	RProperty::Delete(KPropTestCategory,0);
       
    39 	}
       
    40 
       
    41 LOCAL_D TInt LowPriorityThread1(TAny* /*aParameter*/)
       
    42 	{
       
    43 	RProperty prop;
       
    44 	TBuf8<512> buffer;
       
    45 	TInt length = 2;
       
    46 
       
    47 	TInt r=prop.Attach(KPropTestCategory,0);
       
    48 	TEST_ERROR(r,KErrNone);
       
    49 
       
    50 	while(!StopAndExit)
       
    51 		{
       
    52 		buffer.SetLength(length);
       
    53 		buffer[0]=(TUint8)(length%256);
       
    54 		buffer[length-1]=(TUint8)((length-1)%256);
       
    55 		++length;
       
    56 		if(length>512)
       
    57 			length=2;
       
    58 		r=prop.Set(buffer);
       
    59 		if(r!=KErrArgument && r!=KErrNotFound)
       
    60 			{
       
    61 			//if it's not of type EInt and defined
       
    62 			TEST_ERROR(r,KErrNone);
       
    63 			}
       
    64 		User::AfterHighRes(0);
       
    65 		}
       
    66 
       
    67 	return KErrNone;
       
    68 	}
       
    69 
       
    70 LOCAL_D TInt LowPriorityThread2(TAny* /*aParameter*/)
       
    71 	{
       
    72 	RProperty prop;
       
    73 	TBuf8<512> buffer;
       
    74 
       
    75 	TInt r=prop.Attach(KPropTestCategory,0);
       
    76 	TEST_ERROR(r,KErrNone);
       
    77 
       
    78 	while(!StopAndExit)
       
    79 		{
       
    80 		r=prop.Get(buffer);
       
    81 		if(r!=KErrArgument && r!=KErrNotFound)
       
    82 			{
       
    83 			//if it's not of type EInt and defined
       
    84 			TEST_ERROR(r,KErrNone);
       
    85 			TInt length=buffer.Length();
       
    86 			if(length>0)
       
    87 				{
       
    88 				TEST_ERROR(buffer[0],length%256);
       
    89 				TEST_ERROR(buffer[length-1],(length-1)%256);
       
    90 				}
       
    91 			}
       
    92 		}
       
    93 	return KErrNone;
       
    94 	}
       
    95 
       
    96 LOCAL_D TInt MediumPriorityThread(TAny* /*aParameter*/)
       
    97 	{
       
    98 	RProperty prop;
       
    99 	TBuf8<512> buffer;
       
   100 
       
   101 	TInt r=prop.Attach(KPropTestCategory,0);
       
   102 	TEST_ERROR(r,KErrNone);
       
   103 
       
   104 	TRequestStatus status;
       
   105 	
       
   106 	while(!StopAndExit)
       
   107 		{
       
   108 		prop.Subscribe(status);
       
   109 
       
   110 		User::WaitForRequest(status);
       
   111 		if(StopAndExit)
       
   112 			break;
       
   113 		if(status.Int() != KErrNotFound)
       
   114 			{
       
   115 			//property is defined
       
   116 			TEST_ERROR(status.Int(),KErrNone);
       
   117 
       
   118 			r=prop.Get(buffer);
       
   119 			if(r!=KErrArgument)
       
   120 				{
       
   121 				TEST_ERROR(r,KErrNone);
       
   122 				TInt length=buffer.Length();
       
   123 				if(length>0)
       
   124 					{
       
   125 					TEST_ERROR(buffer[0],length%256);
       
   126 					TEST_ERROR(buffer[length-1],(length-1)%256);
       
   127 					}
       
   128 				}
       
   129 			}
       
   130 		}
       
   131 
       
   132 	return KErrNone;
       
   133 	}
       
   134 
       
   135 LOCAL_D TInt HighPriorityThread(TAny* /*aParameter*/)
       
   136 	{
       
   137 
       
   138 	TInt type=RProperty::EInt;
       
   139 	TInt iteration=0;
       
   140 	TInt r;
       
   141 
       
   142 	while(!StopAndExit)
       
   143 		{
       
   144 		User::AfterHighRes(1000); //wait for 1ms
       
   145 		
       
   146 //		test.Printf(_L("Deleting property\r\n"));
       
   147 		r=RProperty::Delete(KPropTestCategory,0);
       
   148 		TEST_ERROR(r,KErrNone);
       
   149 
       
   150 //		test.Printf(_L("Defining property\r\n"));
       
   151 		r=RProperty::Define(KPropTestCategory,0,type, KPassPolicy, KPassPolicy);
       
   152 		TEST_ERROR(r,KErrNone);
       
   153 
       
   154 		type=(type+1)%RProperty::ETypeLimit;
       
   155 
       
   156 		if(1000 == ++iteration)
       
   157 			{
       
   158 			//check if we should exit
       
   159 			TTimeIntervalSeconds timeTaken;
       
   160 			TTime time;
       
   161 			time.HomeTime();
       
   162 			TInt r = time.SecondsFrom(startTime, timeTaken);
       
   163 			TEST_ERROR(r,KErrNone);
       
   164 
       
   165 			if(timeTaken.Int() >= TEST_TIME)
       
   166 				{
       
   167 				//we should exit
       
   168 
       
   169 				StopAndExit=ETrue;
       
   170 				//delete if it's not deleted, to wake up subscribing threads waiting for events on this property
       
   171 				RProperty::Delete(KPropTestCategory,0);
       
   172 				break;
       
   173 				}
       
   174 			iteration=0;
       
   175 			}
       
   176 		}
       
   177 	return KErrNone;
       
   178 	}
       
   179 
       
   180 
       
   181 
       
   182 GLDEF_C TInt E32Main()
       
   183 	{
       
   184 
       
   185 	test.Start(_L("Stress test using multiple threads accessing the same property"));
       
   186 
       
   187     startTime.HomeTime();
       
   188 	TInt r=RProperty::Define(KPropTestCategory,0,RProperty::EInt, KPassPolicy, KPassPolicy);
       
   189 	test(r==KErrNone);
       
   190 
       
   191 	TRequestStatus status1;
       
   192 	TRequestStatus status2;
       
   193 	TRequestStatus status3;
       
   194 	TRequestStatus status4;
       
   195 	RThread t1;
       
   196 	RThread t2;
       
   197 	RThread t3;
       
   198 	RThread t4;
       
   199 
       
   200 	r = t1.Create(KNullDesC, LowPriorityThread1, 0x2000, NULL, 0);
       
   201 	test(r == KErrNone);
       
   202 	t1.SetPriority(EPriorityLess);
       
   203 	t1.Logon(status1);
       
   204 
       
   205 	r = t2.Create(KNullDesC, LowPriorityThread2, 0x2000, NULL, 0);
       
   206 	test(r == KErrNone);
       
   207 	t2.SetPriority(EPriorityLess);
       
   208 	t2.Logon(status2);
       
   209 
       
   210 	r = t3.Create(KNullDesC, MediumPriorityThread, 0x2000, NULL, 0);
       
   211 	test(r == KErrNone);
       
   212 	t3.SetPriority(EPriorityNormal);
       
   213 	t3.Logon(status3);
       
   214 	
       
   215 	r = t4.Create(KNullDesC, HighPriorityThread, 0x2000, NULL, 0);
       
   216 	test(r == KErrNone);
       
   217 	t4.SetPriority(EPriorityMore);
       
   218 	t4.Logon(status4);
       
   219 	
       
   220 	TBool jit = User::JustInTime();
       
   221 	User::SetJustInTime(EFalse);
       
   222 
       
   223 	t1.Resume();
       
   224 	t2.Resume();
       
   225 	t3.Resume();
       
   226 	t4.Resume();
       
   227 
       
   228 	User::WaitForRequest(status1);
       
   229 	User::WaitForRequest(status2);
       
   230 	User::WaitForRequest(status3);
       
   231 	User::WaitForRequest(status4);
       
   232 
       
   233 	User::SetJustInTime(jit);
       
   234 
       
   235 	test(status1 == KErrNone);
       
   236 	test(status2 == KErrNone);
       
   237 	test(status3 == KErrNone);
       
   238 	test(status4 == KErrNone);
       
   239 
       
   240 	TTimeIntervalSeconds timeTaken;
       
   241 	TTime time;
       
   242 	time.HomeTime();
       
   243 	r = time.SecondsFrom(startTime, timeTaken);
       
   244 	test(r==KErrNone);
       
   245 	TInt totalTime = timeTaken.Int();
       
   246 	
       
   247 	TInt seconds = totalTime % 60;
       
   248     TInt minutes = (totalTime / 60) % 60;
       
   249     TInt hours   = totalTime / 3600;
       
   250 
       
   251     test.Printf(_L("Time taken since test started: %d:%d:%d\r\n"), 
       
   252                    hours, minutes, seconds);
       
   253 
       
   254 	CLOSE_AND_WAIT(t1);
       
   255 	CLOSE_AND_WAIT(t2);
       
   256 	CLOSE_AND_WAIT(t3);
       
   257 	CLOSE_AND_WAIT(t4);
       
   258 
       
   259 	test.End();
       
   260 
       
   261 	return KErrNone;
       
   262 	}