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