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_version1; |
| 21 | |
| 22 | {$APPTYPE CONSOLE} |
| 23 | |
| 24 | uses |
| 25 | Classes, Windows, SysUtils, |
Jens Geyer | 66f9536 | 2021-03-29 20:35:41 +0200 | [diff] [blame] | 26 | Skiptest.One in 'gen-delphi\Skiptest.One.pas', |
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 | |
| 46 | |
| 47 | function CreatePing : IPing; |
| 48 | begin |
| 49 | result := TPingImpl.Create; |
Jens Geyer | 66f9536 | 2021-03-29 20:35:41 +0200 | [diff] [blame] | 50 | result.Version1 := TConstants.SKIPTESTSERVICE_VERSION; |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 51 | result.EnumTest := TPingPongEnum.PingOne; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 52 | end; |
| 53 | |
| 54 | |
| 55 | type |
| 56 | TDummyServer = class( TInterfacedObject, TSkipTestService.Iface) |
| 57 | protected |
| 58 | // TSkipTestService.Iface |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 59 | function PingPong(const ping: IPing): IPing; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 60 | end; |
| 61 | |
| 62 | |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 63 | function TDummyServer.PingPong(const ping: IPing): IPing; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 64 | // TSkipTestService.Iface |
| 65 | begin |
| 66 | Writeln('- performing request from version '+IntToStr(ping.Version1)+' client'); |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 67 | Writeln( ping.ToString); |
| 68 | result := CreatePing; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 69 | end; |
| 70 | |
| 71 | |
| 72 | function CreateProtocol( protfact : IProtocolFactory; stm : TStream; aForInput : Boolean) : IProtocol; |
| 73 | var adapt : IThriftStream; |
| 74 | trans : ITransport; |
| 75 | begin |
| 76 | adapt := TThriftStreamAdapterDelphi.Create( stm, FALSE); |
| 77 | if aForInput |
Jens Geyer | a019cda | 2019-11-09 23:24:52 +0100 | [diff] [blame] | 78 | then trans := TStreamTransportImpl.Create( adapt, nil, TThriftConfigurationImpl.Create) |
| 79 | else trans := TStreamTransportImpl.Create( nil, adapt, TThriftConfigurationImpl.Create); |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 80 | result := protfact.GetProtocol( trans); |
| 81 | end; |
| 82 | |
| 83 | |
| 84 | procedure CreateRequest( protfact : IProtocolFactory; fname : string); |
| 85 | var stm : TFileStream; |
| 86 | ping : IPing; |
| 87 | proto : IProtocol; |
| 88 | client : TSkipTestService.TClient; // we need access to send/recv_pingpong() |
| 89 | cliRef : IUnknown; // holds the refcount |
| 90 | begin |
| 91 | Writeln('- creating new request'); |
| 92 | stm := TFileStream.Create( fname+REQUEST_EXT+'.tmp', fmCreate); |
| 93 | try |
| 94 | ping := CreatePing; |
| 95 | |
| 96 | // save request data |
| 97 | proto := CreateProtocol( protfact, stm, FALSE); |
| 98 | client := TSkipTestService.TClient.Create( nil, proto); |
| 99 | cliRef := client as IUnknown; |
| 100 | client.send_PingPong( ping); |
| 101 | |
| 102 | finally |
| 103 | client := nil; // not Free! |
| 104 | cliRef := nil; |
| 105 | stm.Free; |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 106 | if client = nil then {warning suppressed}; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 107 | end; |
| 108 | |
| 109 | DeleteFile( fname+REQUEST_EXT); |
| 110 | RenameFile( fname+REQUEST_EXT+'.tmp', fname+REQUEST_EXT); |
| 111 | end; |
| 112 | |
| 113 | |
| 114 | procedure ReadResponse( protfact : IProtocolFactory; fname : string); |
| 115 | var stm : TFileStream; |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 116 | ping : IPing; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 117 | proto : IProtocol; |
| 118 | client : TSkipTestService.TClient; // we need access to send/recv_pingpong() |
| 119 | cliRef : IUnknown; // holds the refcount |
| 120 | begin |
| 121 | Writeln('- reading response'); |
| 122 | stm := TFileStream.Create( fname+RESPONSE_EXT, fmOpenRead); |
| 123 | try |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 124 | // load request data |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 125 | proto := CreateProtocol( protfact, stm, TRUE); |
| 126 | client := TSkipTestService.TClient.Create( proto, nil); |
| 127 | cliRef := client as IUnknown; |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 128 | ping := client.recv_PingPong; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 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 | end; |
| 137 | |
| 138 | |
| 139 | procedure ProcessFile( protfact : IProtocolFactory; fname : string); |
| 140 | var stmIn, stmOut : TFileStream; |
| 141 | protIn, protOut : IProtocol; |
| 142 | server : IProcessor; |
| 143 | begin |
| 144 | Writeln('- processing request'); |
| 145 | stmOut := nil; |
| 146 | stmIn := TFileStream.Create( fname+REQUEST_EXT, fmOpenRead); |
| 147 | try |
| 148 | stmOut := TFileStream.Create( fname+RESPONSE_EXT+'.tmp', fmCreate); |
| 149 | |
| 150 | // process request and write response data |
| 151 | protIn := CreateProtocol( protfact, stmIn, TRUE); |
| 152 | protOut := CreateProtocol( protfact, stmOut, FALSE); |
| 153 | |
| 154 | server := TSkipTestService.TProcessorImpl.Create( TDummyServer.Create); |
| 155 | server.Process( protIn, protOut); |
| 156 | |
| 157 | finally |
| 158 | server := nil; // not Free! |
| 159 | stmIn.Free; |
| 160 | stmOut.Free; |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 161 | if server = nil then {warning suppressed}; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 162 | end; |
| 163 | |
| 164 | DeleteFile( fname+RESPONSE_EXT); |
| 165 | RenameFile( fname+RESPONSE_EXT+'.tmp', fname+RESPONSE_EXT); |
| 166 | end; |
| 167 | |
| 168 | |
| 169 | procedure Test( protfact : IProtocolFactory; fname : string); |
| 170 | begin |
| 171 | // try to read an existing request |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 172 | Writeln('Reading data file '+fname); |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 173 | if FileExists( fname + REQUEST_EXT) then begin |
| 174 | ProcessFile( protfact, fname); |
| 175 | ReadResponse( protfact, fname); |
| 176 | end; |
| 177 | |
| 178 | // create a new request and try to process |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 179 | Writeln('Writing data file '+fname); |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 180 | CreateRequest( protfact, fname); |
| 181 | ProcessFile( protfact, fname); |
| 182 | ReadResponse( protfact, fname); |
| 183 | end; |
| 184 | |
| 185 | |
| 186 | const |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 187 | FILE_BINARY = 'pingpong.bin'; |
| 188 | FILE_JSON = 'pingpong.json'; |
| 189 | FILE_COMPACT = 'pingpong.compact'; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 190 | begin |
| 191 | try |
Jens Geyer | 66f9536 | 2021-03-29 20:35:41 +0200 | [diff] [blame] | 192 | Writeln( 'Delphi SkipTest '+IntToStr(TConstants.SKIPTESTSERVICE_VERSION)+' using '+Thrift.Version); |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 193 | |
| 194 | Writeln; |
| 195 | Writeln('Binary protocol'); |
| 196 | Test( TBinaryProtocolImpl.TFactory.Create, FILE_BINARY); |
| 197 | |
| 198 | Writeln; |
| 199 | Writeln('JSON protocol'); |
| 200 | Test( TJSONProtocolImpl.TFactory.Create, FILE_JSON); |
| 201 | |
| 202 | Writeln; |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 203 | Writeln('Compact protocol'); |
| 204 | Test( TCompactProtocolImpl.TFactory.Create, FILE_COMPACT); |
| 205 | |
| 206 | Writeln; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 207 | Writeln('Test completed without errors.'); |
| 208 | Writeln; |
| 209 | Write('Press ENTER to close ...'); Readln; |
| 210 | except |
| 211 | on E: Exception do |
| 212 | Writeln(E.ClassName, ': ', E.Message); |
| 213 | end; |
| 214 | end. |
| 215 | |