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