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