ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/ibx/trunk/testsuite/Test14.pas
Revision: 323
Committed: Thu Feb 25 12:14:35 2021 UTC (3 years, 6 months ago) by tony
Content type: text/x-pascal
File size: 7587 byte(s)
Log Message:
Fixed Merged

File Contents

# User Rev Content
1 tony 323 (*
2     * IBX Test suite. This program is used to test the IBX non-visual
3     * components and provides a semi-automated pass/fail check for each test.
4     *
5     * The contents of this file are subject to the Initial Developer's
6     * Public License Version 1.0 (the "License"); you may not use this
7     * file except in compliance with the License. You may obtain a copy
8     * of the License here:
9     *
10     * http://www.firebirdsql.org/index.php?op=doc&id=idpl
11     *
12     * Software distributed under the License is distributed on an "AS
13     * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14     * implied. See the License for the specific language governing rights
15     * and limitations under the License.
16     *
17     * The Initial Developer of the Original Code is Tony Whyman.
18     *
19     * The Original Code is (C) 2021 Tony Whyman, MWA Software
20     * (http://www.mwasoftware.co.uk).
21     *
22     * All Rights Reserved.
23     *
24     * Contributor(s): ______________________________________.
25     *
26     *)
27 tony 315 unit Test14;
28    
29     {$mode objfpc}{$H+}
30    
31     {Test 14: Open and read from Employee Database with ISQLMonitor}
32    
33     { This is a simple use of IBX to access the employee database in console mode.
34     The program opens the database, runs a query and writes the result to stdout.
35    
36     ISQLMonitor is used to trace the activity.
37    
38     The Monitor is in a separate process
39    
40     }
41    
42     interface
43    
44     uses
45     Classes, SysUtils, TestApplication, IBXTestBase, IB, IBCustomDataSet, IBDatabase,
46     IBQuery, IBInternals, IBSQLMonitor, Process {$IFDEF UNIX}, BaseUnix {$ENDIF};
47    
48     const
49     aTestID = '14';
50     aTestTitle = 'Open and read from Employee Database with ISQLMonitor and external monitor';
51    
52     type
53    
54     { TTest14 }
55    
56     TTest14 = class(TIBXTestBase)
57     private
58     class var FTerminated: boolean;
59     private
60     FIBSQLMonitor: TIBSQLMonitor;
61     FLog: TStringList;
62     FProcess: TProcess;
63     FIsChild: boolean;
64     FLogFile: Text;
65     {$IFDEF UNIX}
66     Foa,Fna : PSigActionRec;
67     procedure SetupSignalHandler;
68     {$ENDIF}
69     procedure HandleOnSQL(EventText: String; EventTime : TDateTime);
70 tony 319 procedure ShowStatistics(Sender: TObject);
71 tony 315 protected
72     procedure CreateObjects(Application: TTestApplication); override;
73     function GetTestID: AnsiString; override;
74     function GetTestTitle: AnsiString; override;
75     procedure InitTest; override;
76     procedure ProcessResults; override;
77     public
78     destructor Destroy; override;
79     function ChildProcess: boolean; override;
80     procedure RunTest(CharSet: AnsiString; SQLDialect: integer); override;
81     end;
82    
83    
84     implementation
85    
86     const
87     sqlExample =
88     'with recursive Depts As ( '+
89     'Select DEPT_NO, DEPARTMENT, HEAD_DEPT, cast(DEPARTMENT as VarChar(256)) as DEPT_PATH,'+
90     'cast(DEPT_NO as VarChar(64)) as DEPT_KEY_PATH '+
91     'From DEPARTMENT Where HEAD_DEPT is NULL '+
92     'UNION ALL '+
93     'Select D.DEPT_NO, D.DEPARTMENT, D.HEAD_DEPT, Depts.DEPT_PATH || '' / '' || D.DEPARTMENT as DEPT_PATH,'+
94     'Depts.DEPT_KEY_PATH || '';'' || D.DEPT_NO as DEPT_KEY_PATH '+
95     'From DEPARTMENT D '+
96     'JOIN Depts On D.HEAD_DEPT = Depts.DEPT_NO '+
97     ')'+
98    
99     'Select First 2 A.EMP_NO, A.FIRST_NAME, A.LAST_NAME, A.PHONE_EXT, A.HIRE_DATE, A.DEPT_NO, A.JOB_CODE,'+
100     'A.JOB_GRADE, A.JOB_COUNTRY, A.SALARY, A.FULL_NAME, D.DEPT_PATH, D.DEPT_KEY_PATH '+
101     'From EMPLOYEE A '+
102     'JOIN Depts D On D.DEPT_NO = A.DEPT_NO';
103    
104     LogFileName = 'Test_ISQLMonitor' + '.log';
105    
106     { TTest14 }
107    
108     procedure TTest14.HandleOnSQL(EventText: String; EventTime: TDateTime);
109     begin
110     if FIsChild then
111     begin
112 tony 319 writeln(FLogFile,'*Monitor* '{+DateTimeToStr(EventTime)}+' '+EventText);
113 tony 315 Flush(FLogFile);
114     end;
115     end;
116    
117 tony 319 procedure TTest14.ShowStatistics(Sender: TObject);
118     begin
119     if ChildProcess then
120     begin
121     writeln(FLogFile,FIBSQLMonitor.ReadCount,' ISQL Monitor Messages Received');
122     Flush(FLogFile);
123     Close(FLogFile);
124     end;
125     end;
126    
127 tony 315 {$IFDEF UNIX}
128     procedure DoSigTerm(sig : cint);cdecl;
129     begin
130     TTest14.FTerminated := true;
131     end;
132    
133     procedure TTest14.SetupSignalHandler;
134     begin
135     FTerminated := false;
136     new(Fna);
137     new(Foa);
138     Fna^.sa_Handler:=SigActionHandler(@DoSigTerm);
139     fillchar(Fna^.Sa_Mask,sizeof(Fna^.sa_mask),#0);
140     Fna^.Sa_Flags:=0;
141     {$ifdef Linux} // Linux specific
142     Fna^.Sa_Restorer:=Nil;
143     {$endif}
144     if fpSigAction(SigTerm,Fna,Foa)<>0 then
145     begin
146 tony 319 writeln('Error setting signal handler: ',fpgeterrno,'.');
147 tony 315 halt(1);
148     end;
149     end;
150     {$ENDIF}
151    
152     procedure TTest14.CreateObjects(Application: TTestApplication);
153     begin
154     inherited CreateObjects(Application);
155 tony 319 if ChildProcess then
156     begin
157     FIBSQLMonitor := TIBSQLMonitor.Create(Application);
158     FIBSQLMonitor.TraceFlags := [tfQPrepare, tfQExecute, tfQFetch, tfError, tfStmt, tfConnect,
159     tfTransact, tfBlob, tfService, tfMisc];
160     FIBSQLMonitor.OnSQL := @HandleOnSQL;
161     FIBSQLMonitor.OnMonitoringDisabled := @ShowStatistics;
162     end
163     else
164     begin
165     IBDatabase.TraceFlags := [tfQPrepare, tfQExecute, tfQFetch, tfError, tfStmt, tfConnect,
166     tfTransact, tfBlob, tfService, tfMisc];
167     FLog := TStringList.Create;
168     FProcess := TProcess.Create(Application);
169     end;
170 tony 315 end;
171    
172     function TTest14.GetTestID: AnsiString;
173     begin
174     Result := aTestID;
175     end;
176    
177     function TTest14.GetTestTitle: AnsiString;
178     begin
179     Result := aTestTitle;
180     end;
181    
182     procedure TTest14.InitTest;
183     begin
184     inherited InitTest;
185     IBDatabase.DatabaseName := Owner.GetEmployeeDatabaseName;
186     ReadOnlyTransaction;
187     FIsChild := Owner.TestOption <> '';
188     if FIsChild then
189     begin
190     {$IFDEF UNIX}
191     SetupSignalHandler;
192     {$ENDIF}
193     FIBSQLMonitor.Enabled := true;
194     assignFile(FLogFile,Owner.TestOption);
195     Rewrite(FLogFile);
196     end
197     else
198     begin
199     FProcess.Executable := ParamStr(0);
200     FProcess.Parameters.Clear;
201     FProcess.Parameters.Add('-q');
202     FProcess.Parameters.Add('-t');
203     FProcess.Parameters.Add(GetTestID);
204     FProcess.Parameters.Add('-O');
205     FProcess.Parameters.Add(LogFileName);
206     FProcess.Options := [];
207     end;
208     end;
209    
210     procedure TTest14.ProcessResults;
211     begin
212     inherited ProcessResults;
213     if assigned(FLog) then
214     FreeAndNil(FLog);
215     if assigned(FProcess) then
216     FreeAndNil(FProcess);
217     end;
218    
219     destructor TTest14.Destroy;
220     begin
221     if assigned(FLog) then
222     FreeAndNil(FLog);
223     if assigned(FProcess) then
224     FreeAndNil(FProcess);
225     inherited Destroy;
226     end;
227    
228     function TTest14.ChildProcess: boolean;
229     begin
230     Result := Owner.TestOption <> '';
231     end;
232    
233     procedure TTest14.RunTest(CharSet: AnsiString; SQLDialect: integer);
234     var stats: TPerfCounters;
235     i: integer;
236     aLogFile: Text;
237     Line: string;
238     begin
239     writeln(OutFile);
240     EnableMonitoring;
241     if not FIsChild then
242     FProcess.Execute
243     else
244     begin
245     while not FTerminated do
246     CheckSynchronize(1); //loop until terminated
247     Exit;
248     end;
249    
250 tony 319 Sleep(1000); {wait for child to become ready}
251 tony 315 CheckSynchronize(1);
252     with IBQuery do
253     begin
254     AllowAutoActivateTransaction := true;
255     Unidirectional := true;
256     SQL.Text := sqlExample;
257     EnableStatistics := true;
258     Active := true;
259     PrintDataSet(IBQuery);
260    
261     if GetPerfStatistics(stats) then
262     WritePerfStats(stats);
263     PrintAffectedRows(IBQuery);
264     writeln(OutFile);
265     writeln(OutFile,'Reconnect');
266     IBDatabase.ReConnect;
267     Unidirectional := false;
268     Active := true;
269     PrintDataSet(IBQuery);
270     end;
271     IBDatabase.Connected := false;
272 tony 319 Sleep(1000);
273 tony 315 CheckSynchronize(1);
274 tony 319 Sleep(1000);
275 tony 315 DisableMonitoring;
276 tony 319 writeln(Outfile,MonitorHook.GetWriteCount,' ISQL Monitor Messages written');
277     Sleep(1000);
278 tony 315 FProcess.Terminate(0);
279     Sleep(1000);
280     assignFile(aLogFile,LogFileName);
281     Reset(aLogFile);
282     while not EOF(aLogFile) do
283     begin
284     readln(aLogFile,Line);
285     writeln(OutFile,Line);
286     end;
287     end;
288    
289     initialization
290     RegisterTest(TTest14);
291    
292     end.
293