blob: 07c2c9af4751b0b96a5e6c6400d016c7fd2dc7dc [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',
33 Thrift.Collections in '..\..\src\Thrift.Collections.pas',
Jens Geyera019cda2019-11-09 23:24:52 +010034 Thrift.Configuration in '..\..\src\Thrift.Configuration.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000035 Thrift.Server in '..\..\src\Thrift.Server.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000036 Thrift.Utils in '..\..\src\Thrift.Utils.pas',
Jens Geyer02230912019-04-03 01:12:51 +020037 Thrift.WinHTTP in '..\..\src\Thrift.WinHTTP.pas',
Jens Geyereab29a02014-11-09 23:32:50 +010038 Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas',
Jake Farrellf6e8b0d2012-10-05 00:41:59 +000039 Thrift.Stream in '..\..\src\Thrift.Stream.pas';
Jake Farrell6cd63ec2012-08-29 02:04:35 +000040
41const
42 REQUEST_EXT = '.request';
43 RESPONSE_EXT = '.response';
44
45function CreatePing : IPing;
46var list : IThriftList<IPong>;
47 set_ : IHashSet<string>;
48begin
49 result := TPingImpl.Create;
Jens Geyer17c3ad92017-09-05 20:31:27 +020050 result.Version1 := Tskiptest_version_2Constants.SKIPTESTSERVICE_VERSION;
Jake Farrell6cd63ec2012-08-29 02:04:35 +000051 result.BoolVal := TRUE;
52 result.ByteVal := 2;
53 result.DbVal := 3;
54 result.I16Val := 4;
55 result.I32Val := 5;
56 result.I64Val := 6;
57 result.StrVal := 'seven';
58
59 result.StructVal := TPongImpl.Create;
60 result.StructVal.Version1 := -1;
61 result.StructVal.Version2 := -2;
62
63 list := TThriftListImpl<IPong>.Create;
64 list.Add( result.StructVal);
65 list.Add( result.StructVal);
66
67 set_ := THashSetImpl<string>.Create;
68 set_.Add( 'one');
69 set_.Add( 'uno');
70 set_.Add( 'eins');
71 set_.Add( 'een');
72
73 result.MapVal := TThriftDictionaryImpl< IThriftList<IPong>, IHashSet<string>>.Create;
74 result.MapVal.Add( list, set_);
75end;
76
77
78type
79 TDummyServer = class( TInterfacedObject, TSkipTestService.Iface)
80 protected
81 // TSkipTestService.Iface
82 function PingPong(const ping: IPing; const pong: IPong): IPing;
83 end;
84
85
86function TDummyServer.PingPong(const ping: IPing; const pong: IPong): IPing;
87// TSkipTestService.Iface
88begin
89 Writeln('- performing request from version '+IntToStr(ping.Version1)+' client');
90 result := CreatePing;
91end;
92
93
94function CreateProtocol( protfact : IProtocolFactory; stm : TStream; aForInput : Boolean) : IProtocol;
95var adapt : IThriftStream;
96 trans : ITransport;
97begin
98 adapt := TThriftStreamAdapterDelphi.Create( stm, FALSE);
99 if aForInput
Jens Geyera019cda2019-11-09 23:24:52 +0100100 then trans := TStreamTransportImpl.Create( adapt, nil, TThriftConfigurationImpl.Create)
101 else trans := TStreamTransportImpl.Create( nil, adapt, TThriftConfigurationImpl.Create);
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000102 result := protfact.GetProtocol( trans);
103end;
104
105
106procedure CreateRequest( protfact : IProtocolFactory; fname : string);
107var stm : TFileStream;
108 ping : IPing;
109 proto : IProtocol;
110 client : TSkipTestService.TClient; // we need access to send/recv_pingpong()
111 cliRef : IUnknown; // holds the refcount
112begin
113 Writeln('- creating new request');
114 stm := TFileStream.Create( fname+REQUEST_EXT+'.tmp', fmCreate);
115 try
116 ping := CreatePing;
117
118 // save request data
119 proto := CreateProtocol( protfact, stm, FALSE);
120 client := TSkipTestService.TClient.Create( nil, proto);
121 cliRef := client as IUnknown;
122 client.send_PingPong( ping, ping.StructVal);
123
124 finally
125 client := nil; // not Free!
126 cliRef := nil;
127 stm.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100128 if client = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000129 end;
130
131 DeleteFile( fname+REQUEST_EXT);
132 RenameFile( fname+REQUEST_EXT+'.tmp', fname+REQUEST_EXT);
133end;
134
135
136procedure ReadResponse( protfact : IProtocolFactory; fname : string);
137var stm : TFileStream;
138 ping : IPing;
139 proto : IProtocol;
140 client : TSkipTestService.TClient; // we need access to send/recv_pingpong()
141 cliRef : IUnknown; // holds the refcount
142begin
143 Writeln('- reading response');
144 stm := TFileStream.Create( fname+RESPONSE_EXT, fmOpenRead);
145 try
146 // save request data
147 proto := CreateProtocol( protfact, stm, TRUE);
148 client := TSkipTestService.TClient.Create( proto, nil);
149 cliRef := client as IUnknown;
150 ping := client.recv_PingPong;
151
152 finally
153 client := nil; // not Free!
154 cliRef := nil;
155 stm.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100156 if client = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000157 end;
158end;
159
160
161procedure ProcessFile( protfact : IProtocolFactory; fname : string);
162var stmIn, stmOut : TFileStream;
163 protIn, protOut : IProtocol;
164 server : IProcessor;
165begin
166 Writeln('- processing request');
167 stmOut := nil;
168 stmIn := TFileStream.Create( fname+REQUEST_EXT, fmOpenRead);
169 try
170 stmOut := TFileStream.Create( fname+RESPONSE_EXT+'.tmp', fmCreate);
171
172 // process request and write response data
173 protIn := CreateProtocol( protfact, stmIn, TRUE);
174 protOut := CreateProtocol( protfact, stmOut, FALSE);
175
176 server := TSkipTestService.TProcessorImpl.Create( TDummyServer.Create);
177 server.Process( protIn, protOut);
178
179 finally
180 server := nil; // not Free!
181 stmIn.Free;
182 stmOut.Free;
Konrad Grochowski3b5dacb2014-11-24 10:55:31 +0100183 if server = nil then {warning suppressed};
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000184 end;
185
186 DeleteFile( fname+RESPONSE_EXT);
187 RenameFile( fname+RESPONSE_EXT+'.tmp', fname+RESPONSE_EXT);
188end;
189
190
191procedure Test( protfact : IProtocolFactory; fname : string);
192begin
193 // try to read an existing request
194 if FileExists( fname + REQUEST_EXT) then begin
195 ProcessFile( protfact, fname);
196 ReadResponse( protfact, fname);
197 end;
198
199 // create a new request and try to process
200 CreateRequest( protfact, fname);
201 ProcessFile( protfact, fname);
202 ReadResponse( protfact, fname);
203end;
204
205
206const
207 FILE_BINARY = 'pingpong.bin';
208 FILE_JSON = 'pingpong.json';
209begin
210 try
Jens Geyer17c3ad92017-09-05 20:31:27 +0200211 Writeln( 'Delphi SkipTest '+IntToStr(Tskiptest_version_2Constants.SKIPTESTSERVICE_VERSION)+' using '+Thrift.Version);
Jake Farrell6cd63ec2012-08-29 02:04:35 +0000212
213 Writeln;
214 Writeln('Binary protocol');
215 Test( TBinaryProtocolImpl.TFactory.Create, FILE_BINARY);
216
217 Writeln;
218 Writeln('JSON protocol');
219 Test( TJSONProtocolImpl.TFactory.Create, FILE_JSON);
220
221 Writeln;
222 Writeln('Test completed without errors.');
223 Writeln;
224 Write('Press ENTER to close ...'); Readln;
225 except
226 on E: Exception do
227 Writeln(E.ClassName, ': ', E.Message);
228 end;
229end.
230