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