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