os/kernelhwsrv/kerneltest/e32test/misc/t_destruct.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2007-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 // e32test\misc\t_destruct.cpp
    15 // Test that global objects are destroyed correctly
    16 // 
    17 //
    18 
    19 #define __E32TEST_EXTENSION__
    20 
    21 #include <e32std.h>
    22 #include <e32std_private.h>
    23 #include <e32ldr.h>
    24 #include <e32ldr_private.h>
    25 #include <f32file.h>
    26 #include <e32test.h>
    27 #include <e32msgqueue.h>
    28 
    29 #include "t_destruct.h"
    30 
    31 _LIT(KDestructSlave, "t_destruct_slave");
    32 
    33 const char* KMessageNames[] =
    34 	{
    35 	"Construct",
    36 	"ConstructStatic",
    37 	"ConstructDynamic",
    38 	"ConstructStatic3",
    39 	"PreDestruct",
    40 	"Destruct",
    41 	"DestructStatic",
    42 	"DestructDynamic",
    43 	"DestructStatic3"
    44 	};
    45 
    46 RTest test(_L("t_destruct"));
    47 RMsgQueue<TMessage> MessageQueue;
    48 
    49 void TestNextMessage(TMessage aExpected, TBool& ok)
    50 	{
    51 	TMessage message;
    52 	TInt r = MessageQueue.Receive(message);
    53 	if (r == KErrUnderflow)
    54 		{
    55 		RDebug::Printf(" * expected message %s but got underflow", KMessageNames[aExpected]);
    56 		ok = EFalse;
    57 		return;
    58 		}
    59 	test_KErrNone(r);
    60 	test(message < ENumMessges);
    61 	test(aExpected < ENumMessges);
    62 	if (message == aExpected)
    63 		RDebug::Printf(" - received message %s", KMessageNames[message]);
    64 	else
    65 		{
    66 		RDebug::Printf(" * expected message %s but got %s", KMessageNames[aExpected], KMessageNames[message]);
    67 		ok = EFalse;
    68 		}
    69 	}
    70 
    71 void TestDestruction(TTestType aTestType)
    72 	{
    73 	TBuf<4> cmd;
    74 	cmd.AppendFormat(_L("%d"), aTestType);
    75 
    76 	RProcess p;
    77 	test_KErrNone(p.Create(KDestructSlave, cmd));
    78 	
    79 	TRequestStatus status;
    80 	p.Logon(status);
    81 	p.Resume();
    82 	User::WaitForRequest(status);
    83 
    84 	TExitType expectedExit = aTestType != ETestLastThreadPanic ? EExitKill : EExitPanic;
    85 	test_Equal(expectedExit, p.ExitType());
    86 	test_Equal(aTestType, p.ExitReason());
    87 
    88 	CLOSE_AND_WAIT(p);
    89 	
    90 	TMessage message;
    91 	TBool ok = ETrue;
    92 
    93 	TestNextMessage(EMessageConstructStatic3, ok);
    94 	TestNextMessage(EMessageConstructStatic, ok);
    95 	TestNextMessage(EMessageConstruct, ok);
    96 	TestNextMessage(EMessageConstructDynamic, ok);
    97 	TestNextMessage(EMessagePreDestruct, ok);
    98 	
    99 	if (aTestType != ETestLastThreadPanic && aTestType != ETestDestructorExits)
   100 		{
   101 		TestNextMessage(EMessageDestruct, ok);
   102 		TestNextMessage(EMessageDestructStatic, ok);
   103 		TestNextMessage(EMessageDestructStatic3, ok);
   104 		}
   105 	
   106 	if (aTestType != ETestLastThreadPanic)
   107 		TestNextMessage(EMessageDestructDynamic, ok);
   108 	
   109 	test(ok);
   110 	test_Equal(KErrUnderflow, MessageQueue.Receive(message));
   111 	}
   112 
   113 TInt E32Main()
   114 	{
   115     test.Title();
   116     test.Start(_L("t_destruct"));
   117 
   118 	// Turn off evil lazy dll unloading
   119 	RLoader l;
   120 	test_KErrNone(l.Connect());
   121 	test_KErrNone(l.CancelLazyDllUnload());
   122 	l.Close();
   123 
   124 	test_KErrNone(MessageQueue.CreateGlobal(KMessageQueueName, 10));
   125 	
   126 	test.Next(_L("Test global object destruction when main thread returns"));
   127 	TestDestruction(ETestMainThreadReturn);
   128 	
   129 	test.Next(_L("Test global object destruction when main thread exits"));
   130 	TestDestruction(ETestMainThreadExit);
   131 	
   132 	test.Next(_L("Test global object destruction when child thread exits"));
   133 	TestDestruction(ETestChildThreadReturn);
   134 
   135 	test.Next(_L("Test global object destruction when other thread has exited"));
   136 	TestDestruction(ETestOtherThreadExit);
   137 
   138 	test.Next(_L("Test global object destruction when other thread has panicked"));
   139 	TestDestruction(ETestOtherThreadPanic);
   140 
   141 	test.Next(_L("Test global object destruction when other thread killed by critial thread exit"));
   142 	TestDestruction(ETestOtherThreadRunning);
   143 
   144 	test.Next(_L("Test global object destruction when permanent thread exits"));
   145 	TestDestruction(ETestPermanentThreadExit);
   146 
   147 	test.Next(_L("Test global object destruction only happens once when destrctor creates new thread"));
   148 	TestDestruction(ETestRecursive);
   149 
   150 	test.Next(_L("Test global object destruction only happens once when destrctor calls User::Exit"));
   151 	TestDestruction(ETestDestructorExits);
   152 
   153 	test.Next(_L("Test NO global object destruction when last thread panics"));
   154 	TestDestruction(ETestLastThreadPanic);
   155 	
   156     test.End();
   157 	return KErrNone;
   158 	}