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

00001 /*
00002 Copyright (c) 2000-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description: Transient server example - test program  
00028 */
00029 
00030 
00031 #include <e32base.h>
00032 #include <e32test.h>
00033 #include <f32file.h>
00034 #include "testclient.h"
00035 
00036 _LIT(KTest,"Transient server test");
00037 static RTest test(KTest);
00038 
00039 const TInt KMaxMessage=100;
00040 typedef TBuf<KMaxMessage> TMessage;
00041 
00042 _LIT(KMessage1,"message 1");
00043 _LIT(KMessage2,"message 2");
00044 _LIT(KMessage3,"message 3");
00045 
00046 _LIT(KThread1,"Thread 1");
00047 _LIT(KThread2,"Thread 2");
00048 
00049 static CClient* NewClientL()
00050         {
00051         _LIT(KClientDll,"t-testc");
00052         return CClient::NewL(KClientDll);
00053         }
00054 
00055 static CClient* NewClientLC()
00056         {
00057         CClient* c=NewClientL();
00058         CleanupClosePushL(*c);
00059         return c;
00060         }
00061 
00062 typedef void (*ThreadTestL)(void);
00063 
00064 static TInt ThreadFuncL(TAny* aFuncL)
00065         {
00066         ThreadTestL f=ThreadTestL(aFuncL);
00067         CTrapCleanup* c=CTrapCleanup::New();
00068         ASSERT(c!=0);
00069 #ifdef _DEBUG
00070         TRAPD(r,f());
00071         ASSERT(r==0);
00072 #else
00073         TRAP_IGNORE(f());
00074 #endif
00075         delete c;
00076         return KErrNone;
00077         }
00078 
00079 static RThread TestThreadL(const TDesC& aName,ThreadTestL aTestL,TThreadPriority aPriority=EPriorityNormal)
00080         {
00081         const TInt KStackSize=0x2000;                   //  8KB
00082         const TInt KInitHeapSize=0x1000;                //  4KB
00083         const TInt KHeapSize=0x1000000;                 // 16MB
00084 
00085         RThread t;
00086         test (t.Create(aName,&ThreadFuncL,KStackSize,KInitHeapSize,KHeapSize,(TAny*)aTestL)==KErrNone);
00087         t.SetPriority(aPriority);
00088         t.Resume();
00089         return t;
00090         }
00091 
00092 static RThread FindServer()
00093         {
00094         RThread t;
00095         t.SetHandle(0);
00096         _LIT(KServerName,"*t-server");
00097         TFindThread find(KServerName);
00098         TFullName n;
00099         if (find.Next(n)==KErrNone)
00100                 t.Open(find);
00101         return t;
00102         }
00103 
00104 static void WaitForClose()
00105         {
00106         RThread t(FindServer());
00107         if (t.Handle()!=0)
00108                 {
00109                 TRequestStatus s;
00110                 t.Logon(s);
00111                 if (t.ExitType()==EExitPending || s!=KRequestPending)
00112                         User::WaitForRequest(s);
00113                 t.Close();
00114                 }
00115         }
00116 
00117 static void CheckInterfaceL()
00118         {
00119         test.Start(_L("Single session"));
00120         CClient* c=NewClientLC();
00121         test (c->Send(KMessage1)==KErrNone);
00122         TRequestStatus s;
00123         TMessage msg;
00124         c->Receive(s,msg);
00125         test (s==KRequestPending);
00126         test (c->Send(KMessage2)==KErrNone);
00127         test (s==KErrNone);
00128         test (msg==KMessage2);
00129         test (c->Send(KMessage1)==KErrNone);
00130         test (s==KErrNone);
00131         test (msg==KMessage2);
00132         User::WaitForRequest(s);
00133         c->CancelReceive();
00134         test (s==KErrNone);
00135         test (msg==KMessage2);
00136         c->Receive(s,msg);
00137         test (s==KRequestPending);
00138         c->CancelReceive();
00139         test (s==KErrCancel);
00140         test (c->Send(KMessage1)==KErrNone);
00141         test (s==KErrCancel);
00142         User::WaitForRequest(s);
00143         c->CancelReceive();
00144         CleanupStack::PopAndDestroy(); // c
00145 //
00146         test.Next(_L("Second session"));
00147         c=NewClientLC();
00148         CClient* c2=NewClientLC();
00149         c->Receive(s,msg);
00150         test (s==KRequestPending);
00151         test (c2->Send(KMessage3)==KErrNone);
00152         test (s==KErrNone);
00153         test (msg==KMessage3);
00154         TRequestStatus s2;
00155         TMessage msg2;
00156         c->Receive(s,msg);
00157         test (s==KRequestPending);
00158         c2->Receive(s2,msg2);
00159         test (s2==KRequestPending);
00160         test (c->Send(KMessage1)==KErrNone);
00161         test (s==KErrNone);
00162         test (msg==KMessage1);
00163         test (s2==KErrNone);
00164         test (msg2==KMessage1);
00165 //
00166         CleanupStack::PopAndDestroy(2); // c2, c
00167         WaitForClose();
00168         test.End();
00169         }
00170 
00171 static RSemaphore StartSem;
00172 static RSemaphore GoSem;
00173 
00174 static void Start1L()
00175         {
00176         StartSem.Wait();
00177         CClient* c=NewClientLC();
00178         GoSem.Wait();
00179         c->Send(KMessage1);
00180         TMessage m;
00181         TRequestStatus s;
00182         c->Receive(s,m);
00183         GoSem.Signal();
00184         User::WaitForRequest(s);
00185         ASSERT(m==KMessage2);
00186         CleanupStack::PopAndDestroy();  // c
00187         }
00188 
00189 static void Start2L()
00190         {
00191         StartSem.Wait();
00192         CClient* c=NewClientLC();
00193         TMessage m;
00194         TRequestStatus s;
00195         c->Receive(s,m);
00196         GoSem.Signal();
00197         User::WaitForRequest(s);
00198         ASSERT(m==KMessage1);
00199         GoSem.Wait();
00200         c->Send(KMessage2);
00201         CleanupStack::PopAndDestroy();  // c
00202         }
00203 
00204 static void SimultaneousStartL()
00205         {
00206         test.Start(_L("Set up threads"));
00207         test (StartSem.CreateLocal(0,EOwnerProcess)==KErrNone);
00208         test (GoSem.CreateLocal(0,EOwnerProcess)==KErrNone);
00209         RThread t=TestThreadL(KThread1,&Start1L,EPriorityMore);
00210         TRequestStatus s1;
00211         t.Logon(s1);
00212         t.Close();
00213         t=TestThreadL(KThread2,&Start2L,EPriorityMore);
00214         TRequestStatus s2;
00215         t.Logon(s2);
00216         t.Close();
00217 //
00218         test.Next(_L("Set them off"));
00219         StartSem.Signal(2);
00220         User::WaitForRequest(s1);
00221         test (s1==KErrNone);
00222         User::WaitForRequest(s2);
00223         test (s2==KErrNone);
00224 //
00225         GoSem.Close();
00226         StartSem.Close();
00227         WaitForClose();
00228         test.End();
00229         }
00230 
00231 static void StartWhileStoppingL()
00232         {
00233         test.Start(_L("Start & stop server and wait for exit"));
00234         NewClientL()->Close();
00235         RThread t(FindServer());
00236         test (t.Handle()!=0);
00237         TRequestStatus s;
00238         t.Logon(s);
00239         if (t.ExitType()==EExitPending || s!=KRequestPending)
00240                 User::WaitForRequest(s);
00241         test.Next(_L("attempt to restart with dead thread/server"));
00242         TRAPD(r,NewClientL()->Close());
00243         test (r==KErrNone);
00244         test.Next(_L("attempt to restart after cleanup"));
00245         t.Close();
00246         WaitForClose();
00247         TRAP(r,NewClientL()->Close());
00248         test (r==KErrNone);
00249         WaitForClose();
00250         test.End();
00251         }
00252 
00253 static void MainL()
00254         {
00255         test.Next(_L("Validate interface"));
00256         CheckInterfaceL();
00257         test.Next(_L("Simultaneous Start"));
00258         SimultaneousStartL();
00259         test.Next(_L("Start while stopping"));
00260         StartWhileStoppingL();
00261         }
00262 
00263 TInt E32Main()
00264         {
00265         test.Title();
00266         test.Start(_L("initialising"));
00267         // start the loader
00268         RFs fs;
00269         test (fs.Connect()==KErrNone);
00270         fs.Close();
00271         CTrapCleanup* c=CTrapCleanup::New();
00272         test (c!=0);
00273         TRAPD(r,MainL());
00274         test (r==0);
00275         delete c;
00276         test.End();
00277         test.Close();
00278         return KErrNone;
00279         }
00280 

Generated by  doxygen 1.6.2