examples/Base/IPC/ClientServer/Gettingstarted/transient/test/test.cpp

00001 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // Transient server example - test program
00015 //
00016 
00017 #include <e32base.h>
00018 #include <e32test.h>
00019 #include <f32file.h>
00020 #include "testclient.h"
00021 
00022 _LIT(KTest,"Transient server test");
00023 static RTest test(KTest);
00024 
00025 const TInt KMaxMessage=100;
00026 typedef TBuf<KMaxMessage> TMessage;
00027 
00028 _LIT(KMessage1,"message 1");
00029 _LIT(KMessage2,"message 2");
00030 _LIT(KMessage3,"message 3");
00031 
00032 _LIT(KThread1,"Thread 1");
00033 _LIT(KThread2,"Thread 2");
00034 
00035 static CClient* NewClientL()
00036         {
00037         _LIT(KClientDll,"t-testc");
00038         return CClient::NewL(KClientDll);
00039         }
00040 
00041 static CClient* NewClientLC()
00042         {
00043         CClient* c=NewClientL();
00044         CleanupClosePushL(*c);
00045         return c;
00046         }
00047 
00048 typedef void (*ThreadTestL)(void);
00049 
00050 static TInt ThreadFuncL(TAny* aFuncL)
00051         {
00052         ThreadTestL f=ThreadTestL(aFuncL);
00053         CTrapCleanup* c=CTrapCleanup::New();
00054         ASSERT(c!=0);
00055 #ifdef _DEBUG
00056         TRAPD(r,f());
00057         ASSERT(r==0);
00058 #else
00059         TRAP_IGNORE(f());
00060 #endif
00061         delete c;
00062         return KErrNone;
00063         }
00064 
00065 static RThread TestThreadL(const TDesC& aName,ThreadTestL aTestL,TThreadPriority aPriority=EPriorityNormal)
00066         {
00067         const TInt KStackSize=0x2000;                   //  8KB
00068         const TInt KInitHeapSize=0x1000;                //  4KB
00069         const TInt KHeapSize=0x1000000;                 // 16MB
00070 
00071         RThread t;
00072         test (t.Create(aName,&ThreadFuncL,KStackSize,KInitHeapSize,KHeapSize,(TAny*)aTestL)==KErrNone);
00073         t.SetPriority(aPriority);
00074         t.Resume();
00075         return t;
00076         }
00077 
00078 static RThread FindServer()
00079         {
00080         RThread t;
00081         t.SetHandle(0);
00082         _LIT(KServerName,"*t-server");
00083         TFindThread find(KServerName);
00084         TFullName n;
00085         if (find.Next(n)==KErrNone)
00086                 t.Open(find);
00087         return t;
00088         }
00089 
00090 static void WaitForClose()
00091         {
00092         RThread t(FindServer());
00093         if (t.Handle()!=0)
00094                 {
00095                 TRequestStatus s;
00096                 t.Logon(s);
00097                 if (t.ExitType()==EExitPending || s!=KRequestPending)
00098                         User::WaitForRequest(s);
00099                 t.Close();
00100                 }
00101         }
00102 
00103 static void CheckInterfaceL()
00104         {
00105         test.Start(_L("Single session"));
00106         CClient* c=NewClientLC();
00107         test (c->Send(KMessage1)==KErrNone);
00108         TRequestStatus s;
00109         TMessage msg;
00110         c->Receive(s,msg);
00111         test (s==KRequestPending);
00112         test (c->Send(KMessage2)==KErrNone);
00113         test (s==KErrNone);
00114         test (msg==KMessage2);
00115         test (c->Send(KMessage1)==KErrNone);
00116         test (s==KErrNone);
00117         test (msg==KMessage2);
00118         User::WaitForRequest(s);
00119         c->CancelReceive();
00120         test (s==KErrNone);
00121         test (msg==KMessage2);
00122         c->Receive(s,msg);
00123         test (s==KRequestPending);
00124         c->CancelReceive();
00125         test (s==KErrCancel);
00126         test (c->Send(KMessage1)==KErrNone);
00127         test (s==KErrCancel);
00128         User::WaitForRequest(s);
00129         c->CancelReceive();
00130         CleanupStack::PopAndDestroy(); // c
00131 //
00132         test.Next(_L("Second session"));
00133         c=NewClientLC();
00134         CClient* c2=NewClientLC();
00135         c->Receive(s,msg);
00136         test (s==KRequestPending);
00137         test (c2->Send(KMessage3)==KErrNone);
00138         test (s==KErrNone);
00139         test (msg==KMessage3);
00140         TRequestStatus s2;
00141         TMessage msg2;
00142         c->Receive(s,msg);
00143         test (s==KRequestPending);
00144         c2->Receive(s2,msg2);
00145         test (s2==KRequestPending);
00146         test (c->Send(KMessage1)==KErrNone);
00147         test (s==KErrNone);
00148         test (msg==KMessage1);
00149         test (s2==KErrNone);
00150         test (msg2==KMessage1);
00151 //
00152         CleanupStack::PopAndDestroy(2); // c2, c
00153         WaitForClose();
00154         test.End();
00155         }
00156 
00157 static RSemaphore StartSem;
00158 static RSemaphore GoSem;
00159 
00160 static void Start1L()
00161         {
00162         StartSem.Wait();
00163         CClient* c=NewClientLC();
00164         GoSem.Wait();
00165         c->Send(KMessage1);
00166         TMessage m;
00167         TRequestStatus s;
00168         c->Receive(s,m);
00169         GoSem.Signal();
00170         User::WaitForRequest(s);
00171         ASSERT(m==KMessage2);
00172         CleanupStack::PopAndDestroy();  // c
00173         }
00174 
00175 static void Start2L()
00176         {
00177         StartSem.Wait();
00178         CClient* c=NewClientLC();
00179         TMessage m;
00180         TRequestStatus s;
00181         c->Receive(s,m);
00182         GoSem.Signal();
00183         User::WaitForRequest(s);
00184         ASSERT(m==KMessage1);
00185         GoSem.Wait();
00186         c->Send(KMessage2);
00187         CleanupStack::PopAndDestroy();  // c
00188         }
00189 
00190 static void SimultaneousStartL()
00191         {
00192         test.Start(_L("Set up threads"));
00193         test (StartSem.CreateLocal(0,EOwnerProcess)==KErrNone);
00194         test (GoSem.CreateLocal(0,EOwnerProcess)==KErrNone);
00195         RThread t=TestThreadL(KThread1,&Start1L,EPriorityMore);
00196         TRequestStatus s1;
00197         t.Logon(s1);
00198         t.Close();
00199         t=TestThreadL(KThread2,&Start2L,EPriorityMore);
00200         TRequestStatus s2;
00201         t.Logon(s2);
00202         t.Close();
00203 //
00204         test.Next(_L("Set them off"));
00205         StartSem.Signal(2);
00206         User::WaitForRequest(s1);
00207         test (s1==KErrNone);
00208         User::WaitForRequest(s2);
00209         test (s2==KErrNone);
00210 //
00211         GoSem.Close();
00212         StartSem.Close();
00213         WaitForClose();
00214         test.End();
00215         }
00216 
00217 static void StartWhileStoppingL()
00218         {
00219         test.Start(_L("Start & stop server and wait for exit"));
00220         NewClientL()->Close();
00221         RThread t(FindServer());
00222         test (t.Handle()!=0);
00223         TRequestStatus s;
00224         t.Logon(s);
00225         if (t.ExitType()==EExitPending || s!=KRequestPending)
00226                 User::WaitForRequest(s);
00227         test.Next(_L("attempt to restart with dead thread/server"));
00228         TRAPD(r,NewClientL()->Close());
00229         test (r==KErrNone);
00230         test.Next(_L("attempt to restart after cleanup"));
00231         t.Close();
00232         WaitForClose();
00233         TRAP(r,NewClientL()->Close());
00234         test (r==KErrNone);
00235         WaitForClose();
00236         test.End();
00237         }
00238 
00239 static void MainL()
00240         {
00241         test.Next(_L("Validate interface"));
00242         CheckInterfaceL();
00243         test.Next(_L("Simultaneous Start"));
00244         SimultaneousStartL();
00245         test.Next(_L("Start while stopping"));
00246         StartWhileStoppingL();
00247         }
00248 
00249 TInt E32Main()
00250         {
00251         test.Title();
00252         test.Start(_L("initialising"));
00253         // start the loader
00254         RFs fs;
00255         test (fs.Connect()==KErrNone);
00256         fs.Close();
00257         CTrapCleanup* c=CTrapCleanup::New();
00258         test (c!=0);
00259         TRAPD(r,MainL());
00260         test (r==0);
00261         delete c;
00262         test.End();
00263         test.Close();
00264         return KErrNone;
00265         }
00266 

Generated by  doxygen 1.6.2