os/kernelhwsrv/kerneltest/e32test/property/t_stress_property.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/property/t_stress_property.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,262 @@
     1.4 +// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +//
    1.18 +
    1.19 +#include <e32test.h>
    1.20 +#include "t_property.h"
    1.21 +
    1.22 +_LIT(KTestName,"t_stress_property");
    1.23 +
    1.24 +RTest test(KTestName);
    1.25 +
    1.26 +const TInt32 KUidPropTestCategoryValue = 0x101f75b8;
    1.27 +const TUid KPropTestCategory = { KUidPropTestCategoryValue };
    1.28 +
    1.29 +#define TEST_TIME 36000			//10 hours
    1.30 +
    1.31 +#define TEST_ERROR(rl,rr) { if((TInt)rl!=(TInt)rr) { ExitThread(rl, rr, __LINE__); return KErrGeneral; } }
    1.32 +
    1.33 +TBool volatile StopAndExit = EFalse;
    1.34 +TTime startTime;
    1.35 +
    1.36 +LOCAL_D void ExitThread(TInt rl, TInt rr, TInt aLine)
    1.37 +	{
    1.38 +	test.Printf(_L("Test '%S' failed at line %d; Expected value=%d Actual value=%d; \n"), &KTestName, aLine, rr, rl);
    1.39 +	StopAndExit = ETrue;
    1.40 +	//delete if it's not deleted, to wake up subscribing threads waiting for events on this property
    1.41 +	RProperty::Delete(KPropTestCategory,0);
    1.42 +	}
    1.43 +
    1.44 +LOCAL_D TInt LowPriorityThread1(TAny* /*aParameter*/)
    1.45 +	{
    1.46 +	RProperty prop;
    1.47 +	TBuf8<512> buffer;
    1.48 +	TInt length = 2;
    1.49 +
    1.50 +	TInt r=prop.Attach(KPropTestCategory,0);
    1.51 +	TEST_ERROR(r,KErrNone);
    1.52 +
    1.53 +	while(!StopAndExit)
    1.54 +		{
    1.55 +		buffer.SetLength(length);
    1.56 +		buffer[0]=(TUint8)(length%256);
    1.57 +		buffer[length-1]=(TUint8)((length-1)%256);
    1.58 +		++length;
    1.59 +		if(length>512)
    1.60 +			length=2;
    1.61 +		r=prop.Set(buffer);
    1.62 +		if(r!=KErrArgument && r!=KErrNotFound)
    1.63 +			{
    1.64 +			//if it's not of type EInt and defined
    1.65 +			TEST_ERROR(r,KErrNone);
    1.66 +			}
    1.67 +		User::AfterHighRes(0);
    1.68 +		}
    1.69 +
    1.70 +	return KErrNone;
    1.71 +	}
    1.72 +
    1.73 +LOCAL_D TInt LowPriorityThread2(TAny* /*aParameter*/)
    1.74 +	{
    1.75 +	RProperty prop;
    1.76 +	TBuf8<512> buffer;
    1.77 +
    1.78 +	TInt r=prop.Attach(KPropTestCategory,0);
    1.79 +	TEST_ERROR(r,KErrNone);
    1.80 +
    1.81 +	while(!StopAndExit)
    1.82 +		{
    1.83 +		r=prop.Get(buffer);
    1.84 +		if(r!=KErrArgument && r!=KErrNotFound)
    1.85 +			{
    1.86 +			//if it's not of type EInt and defined
    1.87 +			TEST_ERROR(r,KErrNone);
    1.88 +			TInt length=buffer.Length();
    1.89 +			if(length>0)
    1.90 +				{
    1.91 +				TEST_ERROR(buffer[0],length%256);
    1.92 +				TEST_ERROR(buffer[length-1],(length-1)%256);
    1.93 +				}
    1.94 +			}
    1.95 +		}
    1.96 +	return KErrNone;
    1.97 +	}
    1.98 +
    1.99 +LOCAL_D TInt MediumPriorityThread(TAny* /*aParameter*/)
   1.100 +	{
   1.101 +	RProperty prop;
   1.102 +	TBuf8<512> buffer;
   1.103 +
   1.104 +	TInt r=prop.Attach(KPropTestCategory,0);
   1.105 +	TEST_ERROR(r,KErrNone);
   1.106 +
   1.107 +	TRequestStatus status;
   1.108 +	
   1.109 +	while(!StopAndExit)
   1.110 +		{
   1.111 +		prop.Subscribe(status);
   1.112 +
   1.113 +		User::WaitForRequest(status);
   1.114 +		if(StopAndExit)
   1.115 +			break;
   1.116 +		if(status.Int() != KErrNotFound)
   1.117 +			{
   1.118 +			//property is defined
   1.119 +			TEST_ERROR(status.Int(),KErrNone);
   1.120 +
   1.121 +			r=prop.Get(buffer);
   1.122 +			if(r!=KErrArgument)
   1.123 +				{
   1.124 +				TEST_ERROR(r,KErrNone);
   1.125 +				TInt length=buffer.Length();
   1.126 +				if(length>0)
   1.127 +					{
   1.128 +					TEST_ERROR(buffer[0],length%256);
   1.129 +					TEST_ERROR(buffer[length-1],(length-1)%256);
   1.130 +					}
   1.131 +				}
   1.132 +			}
   1.133 +		}
   1.134 +
   1.135 +	return KErrNone;
   1.136 +	}
   1.137 +
   1.138 +LOCAL_D TInt HighPriorityThread(TAny* /*aParameter*/)
   1.139 +	{
   1.140 +
   1.141 +	TInt type=RProperty::EInt;
   1.142 +	TInt iteration=0;
   1.143 +	TInt r;
   1.144 +
   1.145 +	while(!StopAndExit)
   1.146 +		{
   1.147 +		User::AfterHighRes(1000); //wait for 1ms
   1.148 +		
   1.149 +//		test.Printf(_L("Deleting property\r\n"));
   1.150 +		r=RProperty::Delete(KPropTestCategory,0);
   1.151 +		TEST_ERROR(r,KErrNone);
   1.152 +
   1.153 +//		test.Printf(_L("Defining property\r\n"));
   1.154 +		r=RProperty::Define(KPropTestCategory,0,type, KPassPolicy, KPassPolicy);
   1.155 +		TEST_ERROR(r,KErrNone);
   1.156 +
   1.157 +		type=(type+1)%RProperty::ETypeLimit;
   1.158 +
   1.159 +		if(1000 == ++iteration)
   1.160 +			{
   1.161 +			//check if we should exit
   1.162 +			TTimeIntervalSeconds timeTaken;
   1.163 +			TTime time;
   1.164 +			time.HomeTime();
   1.165 +			TInt r = time.SecondsFrom(startTime, timeTaken);
   1.166 +			TEST_ERROR(r,KErrNone);
   1.167 +
   1.168 +			if(timeTaken.Int() >= TEST_TIME)
   1.169 +				{
   1.170 +				//we should exit
   1.171 +
   1.172 +				StopAndExit=ETrue;
   1.173 +				//delete if it's not deleted, to wake up subscribing threads waiting for events on this property
   1.174 +				RProperty::Delete(KPropTestCategory,0);
   1.175 +				break;
   1.176 +				}
   1.177 +			iteration=0;
   1.178 +			}
   1.179 +		}
   1.180 +	return KErrNone;
   1.181 +	}
   1.182 +
   1.183 +
   1.184 +
   1.185 +GLDEF_C TInt E32Main()
   1.186 +	{
   1.187 +
   1.188 +	test.Start(_L("Stress test using multiple threads accessing the same property"));
   1.189 +
   1.190 +    startTime.HomeTime();
   1.191 +	TInt r=RProperty::Define(KPropTestCategory,0,RProperty::EInt, KPassPolicy, KPassPolicy);
   1.192 +	test(r==KErrNone);
   1.193 +
   1.194 +	TRequestStatus status1;
   1.195 +	TRequestStatus status2;
   1.196 +	TRequestStatus status3;
   1.197 +	TRequestStatus status4;
   1.198 +	RThread t1;
   1.199 +	RThread t2;
   1.200 +	RThread t3;
   1.201 +	RThread t4;
   1.202 +
   1.203 +	r = t1.Create(KNullDesC, LowPriorityThread1, 0x2000, NULL, 0);
   1.204 +	test(r == KErrNone);
   1.205 +	t1.SetPriority(EPriorityLess);
   1.206 +	t1.Logon(status1);
   1.207 +
   1.208 +	r = t2.Create(KNullDesC, LowPriorityThread2, 0x2000, NULL, 0);
   1.209 +	test(r == KErrNone);
   1.210 +	t2.SetPriority(EPriorityLess);
   1.211 +	t2.Logon(status2);
   1.212 +
   1.213 +	r = t3.Create(KNullDesC, MediumPriorityThread, 0x2000, NULL, 0);
   1.214 +	test(r == KErrNone);
   1.215 +	t3.SetPriority(EPriorityNormal);
   1.216 +	t3.Logon(status3);
   1.217 +	
   1.218 +	r = t4.Create(KNullDesC, HighPriorityThread, 0x2000, NULL, 0);
   1.219 +	test(r == KErrNone);
   1.220 +	t4.SetPriority(EPriorityMore);
   1.221 +	t4.Logon(status4);
   1.222 +	
   1.223 +	TBool jit = User::JustInTime();
   1.224 +	User::SetJustInTime(EFalse);
   1.225 +
   1.226 +	t1.Resume();
   1.227 +	t2.Resume();
   1.228 +	t3.Resume();
   1.229 +	t4.Resume();
   1.230 +
   1.231 +	User::WaitForRequest(status1);
   1.232 +	User::WaitForRequest(status2);
   1.233 +	User::WaitForRequest(status3);
   1.234 +	User::WaitForRequest(status4);
   1.235 +
   1.236 +	User::SetJustInTime(jit);
   1.237 +
   1.238 +	test(status1 == KErrNone);
   1.239 +	test(status2 == KErrNone);
   1.240 +	test(status3 == KErrNone);
   1.241 +	test(status4 == KErrNone);
   1.242 +
   1.243 +	TTimeIntervalSeconds timeTaken;
   1.244 +	TTime time;
   1.245 +	time.HomeTime();
   1.246 +	r = time.SecondsFrom(startTime, timeTaken);
   1.247 +	test(r==KErrNone);
   1.248 +	TInt totalTime = timeTaken.Int();
   1.249 +	
   1.250 +	TInt seconds = totalTime % 60;
   1.251 +    TInt minutes = (totalTime / 60) % 60;
   1.252 +    TInt hours   = totalTime / 3600;
   1.253 +
   1.254 +    test.Printf(_L("Time taken since test started: %d:%d:%d\r\n"), 
   1.255 +                   hours, minutes, seconds);
   1.256 +
   1.257 +	CLOSE_AND_WAIT(t1);
   1.258 +	CLOSE_AND_WAIT(t2);
   1.259 +	CLOSE_AND_WAIT(t3);
   1.260 +	CLOSE_AND_WAIT(t4);
   1.261 +
   1.262 +	test.End();
   1.263 +
   1.264 +	return KErrNone;
   1.265 +	}