00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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;
00082 const TInt KInitHeapSize=0x1000;
00083 const TInt KHeapSize=0x1000000;
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();
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);
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();
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();
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
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