blob: 478ea7c7ad0195649d7c43e9883245f15454342e [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_version2;
21
22{$APPTYPE CONSOLE}
23
24uses
25 Classes, Windows, SysUtils,
26 Skiptest.Two,
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
Jens Geyer6e2a6982019-12-12 23:07:49 +010046
Jake Farrell6cd63ec2012-08-29 02:04:35 +000047function CreatePing : IPing;
48var list : IThriftList<IPong>;
49 set_ : IHashSet<string>;
50begin
51 result := TPingImpl.Create;
Jens Geyer17c3ad92017-09-05 20:31:27 +020052 result.Version1 := Tskiptest_version_2Constants.SKIPTESTSERVICE_VERSION;
Jens Geyer6e2a6982019-12-12 23:07:49 +010053 result.EnumTest := TPingPongEnum.PingTwo;
54
Jake Farrell6cd63ec2012-08-29 02:04:35 +000055 result.BoolVal := TRUE;
56 result.ByteVal := 2;
57 result.DbVal := 3;
58 result.I16Val := 4;
59 result.I32Val := 5;
60 result.I64Val := 6;
61 result.StrVal := 'seven';
62
63 result.StructVal := TPongImpl.Create;
64 result.StructVal.Version1 := -1;
65 result.StructVal.Version2 := -2;
Jens Geyer6e2a6982019-12-12 23:07:49 +010066 result.StructVal.EnumTest := TPingPongEnum.PongTwo;
Jake Farrell6cd63ec2012-08-29 02:04:35 +000067
68 list := TThriftListImpl<IPong>.Create;
69 list.Add( result.StructVal);
70 list.Add( result.StructVal);
71
72 set_ := THashSetImpl<string>.Create;
73 set_.Add( 'one');
74 set_.Add( 'uno');
75 set_.Add( 'eins');
76 set_.Add( 'een');
77
78 result.MapVal := TThriftDictionaryImpl< IThriftList<IPong>, IHashSet<string>>.Create;
79 result.MapVal.Add( list, set_);
80end;
81
82
83type
84 TDummyServer = class( TInterfacedObject, TSkipTestService.Iface)
85 protected
86 // TSkipTestService.Iface
87 function PingPong(const ping: IPing; const pong: IPong): IPing;
88 end;
89
90
91function TDummyServer.PingPong(const ping: IPing; const pong: IPong): IPing;
92// TSkipTestService.Iface
93begin
94 Writeln('- performing request from version '+IntToStr(ping.Version1)+' client');
Jens Geyer6e2a6982019-12-12 23:07:49 +010095 Writeln( ping.ToString);
Jake Farrell6cd63ec2012-08-29 02:04:35 +000096 result := CreatePing;
97end;
98
99
100function CreateProtocol( protfact : IProtocolFactory; stm : TStream; aForInput : Boolean) : IProtocol;
101var adapt : IThriftStream;
102 trans : ITransport;
103begin
104 adapt := TThriftStreamAdapterDelphi.Create( stm, FALSE);
105 if aForInput
Jens Geyera019cda2019-11-09 23:24:52 +0100106 then trans := TStreamTransportImpl.Create( adapt, nil, TThriftConfigurationImpl.Create)
107 else trans := TStreamTransportImpl.Create( nil, adapt, TThriftConfigurationImpl.Create);
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000108 result := protfact.GetProtocol( trans);
109end;
110
111
112procedure CreateRequest( protfact : IProtocolFactory; fname : string);
113var stm : TFileStream;
114 ping : IPing;
115 proto : IProtocol;
116 client : TSkipTestService.TClient; // we need access to send/recv_pingpong()
117 cliRef : IUnknown; // holds the refcount
118begin
119 Writeln('- creating new request');
120 stm := TFileStream.Create( fname+REQUEST_EXT+'.tmp', fmCreate);
121 try
122 ping := CreatePing;
123
124 // save request data
125 proto := CreateProtocol( protfact, stm, FALSE);
126 client := TSkipTestService.TClient.Create( nil, proto);
127 cliRef := client as IUnknown;
128 client.send_PingPong( ping, ping.StructVal);
129
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;
136
137 DeleteFile( fname+REQUEST_EXT);
138 RenameFile( fname+REQUEST_EXT+'.tmp', fname+REQUEST_EXT);
139end;
140
141
142procedure ReadResponse( protfact : IProtocolFactory; fname : string);
143var stm : TFileStream;
144 ping : IPing;
145 proto : IProtocol;
146 client : TSkipTestService.TClient; // we need access to send/recv_pingpong()
147 cliRef : IUnknown; // holds the refcount
148begin
149 Writeln('- reading response');
150 stm := TFileStream.Create( fname+RESPONSE_EXT, fmOpenRead);
151 try
Jens Geyer6e2a6982019-12-12 23:07:49 +0100152 // load request data
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000153 proto := CreateProtocol( protfact, stm, TRUE);
154 client := TSkipTestService.TClient.Create( proto, nil);
155 cliRef := client as IUnknown;
156 ping := client.recv_PingPong;
157
158 finally
159 client := nil; // not Free!
160 cliRef := nil;
161 stm.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100162 if client = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000163 end;
164end;
165
166
167procedure ProcessFile( protfact : IProtocolFactory; fname : string);
168var stmIn, stmOut : TFileStream;
169 protIn, protOut : IProtocol;
170 server : IProcessor;
171begin
172 Writeln('- processing request');
173 stmOut := nil;
174 stmIn := TFileStream.Create( fname+REQUEST_EXT, fmOpenRead);
175 try
176 stmOut := TFileStream.Create( fname+RESPONSE_EXT+'.tmp', fmCreate);
177
178 // process request and write response data
179 protIn := CreateProtocol( protfact, stmIn, TRUE);
180 protOut := CreateProtocol( protfact, stmOut, FALSE);
181
182 server := TSkipTestService.TProcessorImpl.Create( TDummyServer.Create);
183 server.Process( protIn, protOut);
184
185 finally
186 server := nil; // not Free!
187 stmIn.Free;
188 stmOut.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100189 if server = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000190 end;
191
192 DeleteFile( fname+RESPONSE_EXT);
193 RenameFile( fname+RESPONSE_EXT+'.tmp', fname+RESPONSE_EXT);
194end;
195
196
197procedure Test( protfact : IProtocolFactory; fname : string);
198begin
199 // try to read an existing request
Jens Geyer6e2a6982019-12-12 23:07:49 +0100200 Writeln;
201 Writeln('Reading data file '+fname);
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000202 if FileExists( fname + REQUEST_EXT) then begin
203 ProcessFile( protfact, fname);
204 ReadResponse( protfact, fname);
205 end;
206
207 // create a new request and try to process
Jens Geyer6e2a6982019-12-12 23:07:49 +0100208 Writeln;
209 Writeln('Writing data file '+fname);
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000210 CreateRequest( protfact, fname);
211 ProcessFile( protfact, fname);
212 ReadResponse( protfact, fname);
213end;
214
215
216const
Jens Geyer6e2a6982019-12-12 23:07:49 +0100217 FILE_BINARY = 'pingpong.bin';
218 FILE_JSON = 'pingpong.json';
219 FILE_COMPACT = 'pingpong.compact';
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000220begin
221 try
Jens Geyer17c3ad92017-09-05 20:31:27 +0200222 Writeln( 'Delphi SkipTest '+IntToStr(Tskiptest_version_2Constants.SKIPTESTSERVICE_VERSION)+' using '+Thrift.Version);
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000223
224 Writeln;
225 Writeln('Binary protocol');
226 Test( TBinaryProtocolImpl.TFactory.Create, FILE_BINARY);
227
228 Writeln;
229 Writeln('JSON protocol');
230 Test( TJSONProtocolImpl.TFactory.Create, FILE_JSON);
231
232 Writeln;
Jens Geyer6e2a6982019-12-12 23:07:49 +0100233 Writeln('Compact protocol');
234 Test( TCompactProtocolImpl.TFactory.Create, FILE_COMPACT);
235
236 Writeln;
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000237 Writeln('Test completed without errors.');
238 Writeln;
239 Write('Press ENTER to close ...'); Readln;
240 except
241 on E: Exception do
242 Writeln(E.ClassName, ': ', E.Message);
243 end;
244end.
245