blob: 6d877de0b3660a98e3be5c6af7e7d5d1e1571646 [file] [log] [blame]
Jake Farrell6cd63ec2012-08-29 02:04:35 +00001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19
20program skiptest_version1;
21
22{$APPTYPE CONSOLE}
23
24uses
25 Classes, Windows, SysUtils,
Jens Geyer66f95362021-03-29 20:35:41 +020026 Skiptest.One in 'gen-delphi\Skiptest.One.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000027 Thrift in '..\..\src\Thrift.pas',
Jens Geyer606f1ef2018-04-09 23:09:41 +020028 Thrift.Exception in '..\..\src\Thrift.Exception.pas',
Jens Geyerbea9bbe2016-04-20 00:02:40 +020029 Thrift.Socket in '..\..\src\Thrift.Socket.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000030 Thrift.Transport in '..\..\src\Thrift.Transport.pas',
31 Thrift.Protocol in '..\..\src\Thrift.Protocol.pas',
32 Thrift.Protocol.JSON in '..\..\src\Thrift.Protocol.JSON.pas',
Jens Geyer6e2a6982019-12-12 23:07:49 +010033 Thrift.Protocol.Compact in '..\..\src\Thrift.Protocol.Compact.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000034 Thrift.Collections in '..\..\src\Thrift.Collections.pas',
Jens Geyera019cda2019-11-09 23:24:52 +010035 Thrift.Configuration in '..\..\src\Thrift.Configuration.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000036 Thrift.Server in '..\..\src\Thrift.Server.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000037 Thrift.Utils in '..\..\src\Thrift.Utils.pas',
Jens Geyer02230912019-04-03 01:12:51 +020038 Thrift.WinHTTP in '..\..\src\Thrift.WinHTTP.pas',
Jens Geyereab29a02014-11-09 23:32:50 +010039 Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000040 Thrift.Stream in '..\..\src\Thrift.Stream.pas';
Jake Farrell6cd63ec2012-08-29 02:04:35 +000041
42const
43 REQUEST_EXT = '.request';
44 RESPONSE_EXT = '.response';
45
46
47function CreatePing : IPing;
48begin
49 result := TPingImpl.Create;
Jens Geyer66f95362021-03-29 20:35:41 +020050 result.Version1 := TConstants.SKIPTESTSERVICE_VERSION;
Jens Geyer6e2a6982019-12-12 23:07:49 +010051 result.EnumTest := TPingPongEnum.PingOne;
Jake Farrell6cd63ec2012-08-29 02:04:35 +000052end;
53
54
55type
56 TDummyServer = class( TInterfacedObject, TSkipTestService.Iface)
57 protected
58 // TSkipTestService.Iface
Jens Geyer6e2a6982019-12-12 23:07:49 +010059 function PingPong(const ping: IPing): IPing;
Jake Farrell6cd63ec2012-08-29 02:04:35 +000060 end;
61
62
Jens Geyer6e2a6982019-12-12 23:07:49 +010063function TDummyServer.PingPong(const ping: IPing): IPing;
Jake Farrell6cd63ec2012-08-29 02:04:35 +000064// TSkipTestService.Iface
65begin
66 Writeln('- performing request from version '+IntToStr(ping.Version1)+' client');
Jens Geyer6e2a6982019-12-12 23:07:49 +010067 Writeln( ping.ToString);
68 result := CreatePing;
Jake Farrell6cd63ec2012-08-29 02:04:35 +000069end;
70
71
72function CreateProtocol( protfact : IProtocolFactory; stm : TStream; aForInput : Boolean) : IProtocol;
73var adapt : IThriftStream;
74 trans : ITransport;
75begin
76 adapt := TThriftStreamAdapterDelphi.Create( stm, FALSE);
77 if aForInput
Jens Geyera019cda2019-11-09 23:24:52 +010078 then trans := TStreamTransportImpl.Create( adapt, nil, TThriftConfigurationImpl.Create)
79 else trans := TStreamTransportImpl.Create( nil, adapt, TThriftConfigurationImpl.Create);
Jake Farrell6cd63ec2012-08-29 02:04:35 +000080 result := protfact.GetProtocol( trans);
81end;
82
83
84procedure CreateRequest( protfact : IProtocolFactory; fname : string);
85var stm : TFileStream;
86 ping : IPing;
87 proto : IProtocol;
88 client : TSkipTestService.TClient; // we need access to send/recv_pingpong()
89 cliRef : IUnknown; // holds the refcount
90begin
91 Writeln('- creating new request');
92 stm := TFileStream.Create( fname+REQUEST_EXT+'.tmp', fmCreate);
93 try
94 ping := CreatePing;
95
96 // save request data
97 proto := CreateProtocol( protfact, stm, FALSE);
98 client := TSkipTestService.TClient.Create( nil, proto);
99 cliRef := client as IUnknown;
100 client.send_PingPong( ping);
101
102 finally
103 client := nil; // not Free!
104 cliRef := nil;
105 stm.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100106 if client = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000107 end;
108
109 DeleteFile( fname+REQUEST_EXT);
110 RenameFile( fname+REQUEST_EXT+'.tmp', fname+REQUEST_EXT);
111end;
112
113
114procedure ReadResponse( protfact : IProtocolFactory; fname : string);
115var stm : TFileStream;
Jens Geyer6e2a6982019-12-12 23:07:49 +0100116 ping : IPing;
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000117 proto : IProtocol;
118 client : TSkipTestService.TClient; // we need access to send/recv_pingpong()
119 cliRef : IUnknown; // holds the refcount
120begin
121 Writeln('- reading response');
122 stm := TFileStream.Create( fname+RESPONSE_EXT, fmOpenRead);
123 try
Jens Geyer6e2a6982019-12-12 23:07:49 +0100124 // load request data
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000125 proto := CreateProtocol( protfact, stm, TRUE);
126 client := TSkipTestService.TClient.Create( proto, nil);
127 cliRef := client as IUnknown;
Jens Geyer6e2a6982019-12-12 23:07:49 +0100128 ping := client.recv_PingPong;
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000129
130 finally
131 client := nil; // not Free!
132 cliRef := nil;
133 stm.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100134 if client = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000135 end;
136end;
137
138
139procedure ProcessFile( protfact : IProtocolFactory; fname : string);
140var stmIn, stmOut : TFileStream;
141 protIn, protOut : IProtocol;
142 server : IProcessor;
143begin
144 Writeln('- processing request');
145 stmOut := nil;
146 stmIn := TFileStream.Create( fname+REQUEST_EXT, fmOpenRead);
147 try
148 stmOut := TFileStream.Create( fname+RESPONSE_EXT+'.tmp', fmCreate);
149
150 // process request and write response data
151 protIn := CreateProtocol( protfact, stmIn, TRUE);
152 protOut := CreateProtocol( protfact, stmOut, FALSE);
153
154 server := TSkipTestService.TProcessorImpl.Create( TDummyServer.Create);
155 server.Process( protIn, protOut);
156
157 finally
158 server := nil; // not Free!
159 stmIn.Free;
160 stmOut.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100161 if server = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000162 end;
163
164 DeleteFile( fname+RESPONSE_EXT);
165 RenameFile( fname+RESPONSE_EXT+'.tmp', fname+RESPONSE_EXT);
166end;
167
168
169procedure Test( protfact : IProtocolFactory; fname : string);
170begin
171 // try to read an existing request
Jens Geyer6e2a6982019-12-12 23:07:49 +0100172 Writeln('Reading data file '+fname);
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000173 if FileExists( fname + REQUEST_EXT) then begin
174 ProcessFile( protfact, fname);
175 ReadResponse( protfact, fname);
176 end;
177
178 // create a new request and try to process
Jens Geyer6e2a6982019-12-12 23:07:49 +0100179 Writeln('Writing data file '+fname);
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000180 CreateRequest( protfact, fname);
181 ProcessFile( protfact, fname);
182 ReadResponse( protfact, fname);
183end;
184
185
186const
Jens Geyer6e2a6982019-12-12 23:07:49 +0100187 FILE_BINARY = 'pingpong.bin';
188 FILE_JSON = 'pingpong.json';
189 FILE_COMPACT = 'pingpong.compact';
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000190begin
191 try
Jens Geyer66f95362021-03-29 20:35:41 +0200192 Writeln( 'Delphi SkipTest '+IntToStr(TConstants.SKIPTESTSERVICE_VERSION)+' using '+Thrift.Version);
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000193
194 Writeln;
195 Writeln('Binary protocol');
196 Test( TBinaryProtocolImpl.TFactory.Create, FILE_BINARY);
197
198 Writeln;
199 Writeln('JSON protocol');
200 Test( TJSONProtocolImpl.TFactory.Create, FILE_JSON);
201
202 Writeln;
Jens Geyer6e2a6982019-12-12 23:07:49 +0100203 Writeln('Compact protocol');
204 Test( TCompactProtocolImpl.TFactory.Create, FILE_COMPACT);
205
206 Writeln;
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000207 Writeln('Test completed without errors.');
208 Writeln;
209 Write('Press ENTER to close ...'); Readln;
210 except
211 on E: Exception do
212 Writeln(E.ClassName, ': ', E.Message);
213 end;
214end.
215