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