Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 1 | (* |
| 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 | |
| 20 | program skiptest_version2; |
| 21 | |
| 22 | {$APPTYPE CONSOLE} |
| 23 | |
| 24 | uses |
| 25 | Classes, Windows, SysUtils, |
| 26 | Skiptest.Two, |
Jake Farrell | f6e8b0d | 2012-10-05 00:41:59 +0000 | [diff] [blame] | 27 | Thrift in '..\..\src\Thrift.pas', |
Jens Geyer | 606f1ef | 2018-04-09 23:09:41 +0200 | [diff] [blame] | 28 | Thrift.Exception in '..\..\src\Thrift.Exception.pas', |
Jens Geyer | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame] | 29 | Thrift.Socket in '..\..\src\Thrift.Socket.pas', |
Jake Farrell | f6e8b0d | 2012-10-05 00:41:59 +0000 | [diff] [blame] | 30 | 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', |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 33 | Thrift.Protocol.Compact in '..\..\src\Thrift.Protocol.Compact.pas', |
Jake Farrell | f6e8b0d | 2012-10-05 00:41:59 +0000 | [diff] [blame] | 34 | Thrift.Collections in '..\..\src\Thrift.Collections.pas', |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 35 | Thrift.Configuration in '..\..\src\Thrift.Configuration.pas', |
Jake Farrell | f6e8b0d | 2012-10-05 00:41:59 +0000 | [diff] [blame] | 36 | Thrift.Server in '..\..\src\Thrift.Server.pas', |
Jake Farrell | f6e8b0d | 2012-10-05 00:41:59 +0000 | [diff] [blame] | 37 | Thrift.Utils in '..\..\src\Thrift.Utils.pas', |
Jens Geyer | 0223091 | 2019-04-03 01:12:51 +0200 | [diff] [blame] | 38 | Thrift.WinHTTP in '..\..\src\Thrift.WinHTTP.pas', |
Jens Geyer | eab29a0 | 2014-11-09 23:32:50 +0100 | [diff] [blame] | 39 | Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas', |
Jake Farrell | f6e8b0d | 2012-10-05 00:41:59 +0000 | [diff] [blame] | 40 | Thrift.Stream in '..\..\src\Thrift.Stream.pas'; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 41 | |
| 42 | const |
| 43 | REQUEST_EXT = '.request'; |
| 44 | RESPONSE_EXT = '.response'; |
| 45 | |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 46 | |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 47 | function CreatePing : IPing; |
| 48 | var list : IThriftList<IPong>; |
| 49 | set_ : IHashSet<string>; |
| 50 | begin |
| 51 | result := TPingImpl.Create; |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 52 | result.Version1 := Tskiptest_version_2Constants.SKIPTESTSERVICE_VERSION; |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 53 | result.EnumTest := TPingPongEnum.PingTwo; |
| 54 | |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 55 | result.BoolVal := TRUE; |
| 56 | result.ByteVal := 2; |
| 57 | result.DbVal := 3; |
| 58 | result.I16Val := 4; |
| 59 | result.I32Val := 5; |
| 60 | result.I64Val := 6; |
| 61 | result.StrVal := 'seven'; |
| 62 | |
| 63 | result.StructVal := TPongImpl.Create; |
| 64 | result.StructVal.Version1 := -1; |
| 65 | result.StructVal.Version2 := -2; |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 66 | result.StructVal.EnumTest := TPingPongEnum.PongTwo; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 67 | |
| 68 | list := TThriftListImpl<IPong>.Create; |
| 69 | list.Add( result.StructVal); |
| 70 | list.Add( result.StructVal); |
| 71 | |
| 72 | set_ := THashSetImpl<string>.Create; |
| 73 | set_.Add( 'one'); |
| 74 | set_.Add( 'uno'); |
| 75 | set_.Add( 'eins'); |
| 76 | set_.Add( 'een'); |
| 77 | |
| 78 | result.MapVal := TThriftDictionaryImpl< IThriftList<IPong>, IHashSet<string>>.Create; |
| 79 | result.MapVal.Add( list, set_); |
| 80 | end; |
| 81 | |
| 82 | |
| 83 | type |
| 84 | TDummyServer = class( TInterfacedObject, TSkipTestService.Iface) |
| 85 | protected |
| 86 | // TSkipTestService.Iface |
| 87 | function PingPong(const ping: IPing; const pong: IPong): IPing; |
| 88 | end; |
| 89 | |
| 90 | |
| 91 | function TDummyServer.PingPong(const ping: IPing; const pong: IPong): IPing; |
| 92 | // TSkipTestService.Iface |
| 93 | begin |
| 94 | Writeln('- performing request from version '+IntToStr(ping.Version1)+' client'); |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 95 | Writeln( ping.ToString); |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 96 | result := CreatePing; |
| 97 | end; |
| 98 | |
| 99 | |
| 100 | function CreateProtocol( protfact : IProtocolFactory; stm : TStream; aForInput : Boolean) : IProtocol; |
| 101 | var adapt : IThriftStream; |
| 102 | trans : ITransport; |
| 103 | begin |
| 104 | adapt := TThriftStreamAdapterDelphi.Create( stm, FALSE); |
| 105 | if aForInput |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 106 | then trans := TStreamTransportImpl.Create( adapt, nil, TThriftConfigurationImpl.Create) |
| 107 | else trans := TStreamTransportImpl.Create( nil, adapt, TThriftConfigurationImpl.Create); |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 108 | result := protfact.GetProtocol( trans); |
| 109 | end; |
| 110 | |
| 111 | |
| 112 | procedure CreateRequest( protfact : IProtocolFactory; fname : string); |
| 113 | var stm : TFileStream; |
| 114 | ping : IPing; |
| 115 | proto : IProtocol; |
| 116 | client : TSkipTestService.TClient; // we need access to send/recv_pingpong() |
| 117 | cliRef : IUnknown; // holds the refcount |
| 118 | begin |
| 119 | Writeln('- creating new request'); |
| 120 | stm := TFileStream.Create( fname+REQUEST_EXT+'.tmp', fmCreate); |
| 121 | try |
| 122 | ping := CreatePing; |
| 123 | |
| 124 | // save request data |
| 125 | proto := CreateProtocol( protfact, stm, FALSE); |
| 126 | client := TSkipTestService.TClient.Create( nil, proto); |
| 127 | cliRef := client as IUnknown; |
| 128 | client.send_PingPong( ping, ping.StructVal); |
| 129 | |
| 130 | finally |
| 131 | client := nil; // not Free! |
| 132 | cliRef := nil; |
| 133 | stm.Free; |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 134 | if client = nil then {warning suppressed}; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 135 | end; |
| 136 | |
| 137 | DeleteFile( fname+REQUEST_EXT); |
| 138 | RenameFile( fname+REQUEST_EXT+'.tmp', fname+REQUEST_EXT); |
| 139 | end; |
| 140 | |
| 141 | |
| 142 | procedure ReadResponse( protfact : IProtocolFactory; fname : string); |
| 143 | var stm : TFileStream; |
| 144 | ping : IPing; |
| 145 | proto : IProtocol; |
| 146 | client : TSkipTestService.TClient; // we need access to send/recv_pingpong() |
| 147 | cliRef : IUnknown; // holds the refcount |
| 148 | begin |
| 149 | Writeln('- reading response'); |
| 150 | stm := TFileStream.Create( fname+RESPONSE_EXT, fmOpenRead); |
| 151 | try |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 152 | // load request data |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 153 | proto := CreateProtocol( protfact, stm, TRUE); |
| 154 | client := TSkipTestService.TClient.Create( proto, nil); |
| 155 | cliRef := client as IUnknown; |
| 156 | ping := client.recv_PingPong; |
| 157 | |
| 158 | finally |
| 159 | client := nil; // not Free! |
| 160 | cliRef := nil; |
| 161 | stm.Free; |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 162 | if client = nil then {warning suppressed}; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 163 | end; |
| 164 | end; |
| 165 | |
| 166 | |
| 167 | procedure ProcessFile( protfact : IProtocolFactory; fname : string); |
| 168 | var stmIn, stmOut : TFileStream; |
| 169 | protIn, protOut : IProtocol; |
| 170 | server : IProcessor; |
| 171 | begin |
| 172 | Writeln('- processing request'); |
| 173 | stmOut := nil; |
| 174 | stmIn := TFileStream.Create( fname+REQUEST_EXT, fmOpenRead); |
| 175 | try |
| 176 | stmOut := TFileStream.Create( fname+RESPONSE_EXT+'.tmp', fmCreate); |
| 177 | |
| 178 | // process request and write response data |
| 179 | protIn := CreateProtocol( protfact, stmIn, TRUE); |
| 180 | protOut := CreateProtocol( protfact, stmOut, FALSE); |
| 181 | |
| 182 | server := TSkipTestService.TProcessorImpl.Create( TDummyServer.Create); |
| 183 | server.Process( protIn, protOut); |
| 184 | |
| 185 | finally |
| 186 | server := nil; // not Free! |
| 187 | stmIn.Free; |
| 188 | stmOut.Free; |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 189 | if server = nil then {warning suppressed}; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 190 | end; |
| 191 | |
| 192 | DeleteFile( fname+RESPONSE_EXT); |
| 193 | RenameFile( fname+RESPONSE_EXT+'.tmp', fname+RESPONSE_EXT); |
| 194 | end; |
| 195 | |
| 196 | |
| 197 | procedure Test( protfact : IProtocolFactory; fname : string); |
| 198 | begin |
| 199 | // try to read an existing request |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 200 | Writeln; |
| 201 | Writeln('Reading data file '+fname); |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 202 | if FileExists( fname + REQUEST_EXT) then begin |
| 203 | ProcessFile( protfact, fname); |
| 204 | ReadResponse( protfact, fname); |
| 205 | end; |
| 206 | |
| 207 | // create a new request and try to process |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 208 | Writeln; |
| 209 | Writeln('Writing data file '+fname); |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 210 | CreateRequest( protfact, fname); |
| 211 | ProcessFile( protfact, fname); |
| 212 | ReadResponse( protfact, fname); |
| 213 | end; |
| 214 | |
| 215 | |
| 216 | const |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 217 | FILE_BINARY = 'pingpong.bin'; |
| 218 | FILE_JSON = 'pingpong.json'; |
| 219 | FILE_COMPACT = 'pingpong.compact'; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 220 | begin |
| 221 | try |
Jens Geyer | 17c3ad9 | 2017-09-05 20:31:27 +0200 | [diff] [blame] | 222 | Writeln( 'Delphi SkipTest '+IntToStr(Tskiptest_version_2Constants.SKIPTESTSERVICE_VERSION)+' using '+Thrift.Version); |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 223 | |
| 224 | Writeln; |
| 225 | Writeln('Binary protocol'); |
| 226 | Test( TBinaryProtocolImpl.TFactory.Create, FILE_BINARY); |
| 227 | |
| 228 | Writeln; |
| 229 | Writeln('JSON protocol'); |
| 230 | Test( TJSONProtocolImpl.TFactory.Create, FILE_JSON); |
| 231 | |
| 232 | Writeln; |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 233 | Writeln('Compact protocol'); |
| 234 | Test( TCompactProtocolImpl.TFactory.Create, FILE_COMPACT); |
| 235 | |
| 236 | Writeln; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 237 | Writeln('Test completed without errors.'); |
| 238 | Writeln; |
| 239 | Write('Press ENTER to close ...'); Readln; |
| 240 | except |
| 241 | on E: Exception do |
| 242 | Writeln(E.ClassName, ': ', E.Message); |
| 243 | end; |
| 244 | end. |
| 245 | |