1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/misc/t_destruct.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,158 @@
1.4 +// Copyright (c) 2007-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 +// e32test\misc\t_destruct.cpp
1.18 +// Test that global objects are destroyed correctly
1.19 +//
1.20 +//
1.21 +
1.22 +#define __E32TEST_EXTENSION__
1.23 +
1.24 +#include <e32std.h>
1.25 +#include <e32std_private.h>
1.26 +#include <e32ldr.h>
1.27 +#include <e32ldr_private.h>
1.28 +#include <f32file.h>
1.29 +#include <e32test.h>
1.30 +#include <e32msgqueue.h>
1.31 +
1.32 +#include "t_destruct.h"
1.33 +
1.34 +_LIT(KDestructSlave, "t_destruct_slave");
1.35 +
1.36 +const char* KMessageNames[] =
1.37 + {
1.38 + "Construct",
1.39 + "ConstructStatic",
1.40 + "ConstructDynamic",
1.41 + "ConstructStatic3",
1.42 + "PreDestruct",
1.43 + "Destruct",
1.44 + "DestructStatic",
1.45 + "DestructDynamic",
1.46 + "DestructStatic3"
1.47 + };
1.48 +
1.49 +RTest test(_L("t_destruct"));
1.50 +RMsgQueue<TMessage> MessageQueue;
1.51 +
1.52 +void TestNextMessage(TMessage aExpected, TBool& ok)
1.53 + {
1.54 + TMessage message;
1.55 + TInt r = MessageQueue.Receive(message);
1.56 + if (r == KErrUnderflow)
1.57 + {
1.58 + RDebug::Printf(" * expected message %s but got underflow", KMessageNames[aExpected]);
1.59 + ok = EFalse;
1.60 + return;
1.61 + }
1.62 + test_KErrNone(r);
1.63 + test(message < ENumMessges);
1.64 + test(aExpected < ENumMessges);
1.65 + if (message == aExpected)
1.66 + RDebug::Printf(" - received message %s", KMessageNames[message]);
1.67 + else
1.68 + {
1.69 + RDebug::Printf(" * expected message %s but got %s", KMessageNames[aExpected], KMessageNames[message]);
1.70 + ok = EFalse;
1.71 + }
1.72 + }
1.73 +
1.74 +void TestDestruction(TTestType aTestType)
1.75 + {
1.76 + TBuf<4> cmd;
1.77 + cmd.AppendFormat(_L("%d"), aTestType);
1.78 +
1.79 + RProcess p;
1.80 + test_KErrNone(p.Create(KDestructSlave, cmd));
1.81 +
1.82 + TRequestStatus status;
1.83 + p.Logon(status);
1.84 + p.Resume();
1.85 + User::WaitForRequest(status);
1.86 +
1.87 + TExitType expectedExit = aTestType != ETestLastThreadPanic ? EExitKill : EExitPanic;
1.88 + test_Equal(expectedExit, p.ExitType());
1.89 + test_Equal(aTestType, p.ExitReason());
1.90 +
1.91 + CLOSE_AND_WAIT(p);
1.92 +
1.93 + TMessage message;
1.94 + TBool ok = ETrue;
1.95 +
1.96 + TestNextMessage(EMessageConstructStatic3, ok);
1.97 + TestNextMessage(EMessageConstructStatic, ok);
1.98 + TestNextMessage(EMessageConstruct, ok);
1.99 + TestNextMessage(EMessageConstructDynamic, ok);
1.100 + TestNextMessage(EMessagePreDestruct, ok);
1.101 +
1.102 + if (aTestType != ETestLastThreadPanic && aTestType != ETestDestructorExits)
1.103 + {
1.104 + TestNextMessage(EMessageDestruct, ok);
1.105 + TestNextMessage(EMessageDestructStatic, ok);
1.106 + TestNextMessage(EMessageDestructStatic3, ok);
1.107 + }
1.108 +
1.109 + if (aTestType != ETestLastThreadPanic)
1.110 + TestNextMessage(EMessageDestructDynamic, ok);
1.111 +
1.112 + test(ok);
1.113 + test_Equal(KErrUnderflow, MessageQueue.Receive(message));
1.114 + }
1.115 +
1.116 +TInt E32Main()
1.117 + {
1.118 + test.Title();
1.119 + test.Start(_L("t_destruct"));
1.120 +
1.121 + // Turn off evil lazy dll unloading
1.122 + RLoader l;
1.123 + test_KErrNone(l.Connect());
1.124 + test_KErrNone(l.CancelLazyDllUnload());
1.125 + l.Close();
1.126 +
1.127 + test_KErrNone(MessageQueue.CreateGlobal(KMessageQueueName, 10));
1.128 +
1.129 + test.Next(_L("Test global object destruction when main thread returns"));
1.130 + TestDestruction(ETestMainThreadReturn);
1.131 +
1.132 + test.Next(_L("Test global object destruction when main thread exits"));
1.133 + TestDestruction(ETestMainThreadExit);
1.134 +
1.135 + test.Next(_L("Test global object destruction when child thread exits"));
1.136 + TestDestruction(ETestChildThreadReturn);
1.137 +
1.138 + test.Next(_L("Test global object destruction when other thread has exited"));
1.139 + TestDestruction(ETestOtherThreadExit);
1.140 +
1.141 + test.Next(_L("Test global object destruction when other thread has panicked"));
1.142 + TestDestruction(ETestOtherThreadPanic);
1.143 +
1.144 + test.Next(_L("Test global object destruction when other thread killed by critial thread exit"));
1.145 + TestDestruction(ETestOtherThreadRunning);
1.146 +
1.147 + test.Next(_L("Test global object destruction when permanent thread exits"));
1.148 + TestDestruction(ETestPermanentThreadExit);
1.149 +
1.150 + test.Next(_L("Test global object destruction only happens once when destrctor creates new thread"));
1.151 + TestDestruction(ETestRecursive);
1.152 +
1.153 + test.Next(_L("Test global object destruction only happens once when destrctor calls User::Exit"));
1.154 + TestDestruction(ETestDestructorExits);
1.155 +
1.156 + test.Next(_L("Test NO global object destruction when last thread panics"));
1.157 + TestDestruction(ETestLastThreadPanic);
1.158 +
1.159 + test.End();
1.160 + return KErrNone;
1.161 + }