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 | bea9bbe | 2016-04-20 00:02:40 +0200 | [diff] [blame^] | 28 | Thrift.Socket in '..\..\src\Thrift.Socket.pas', |
Jake Farrell | f6e8b0d | 2012-10-05 00:41:59 +0000 | [diff] [blame] | 29 | 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 Geyer | eab29a0 | 2014-11-09 23:32:50 +0100 | [diff] [blame] | 36 | Thrift.TypeRegistry in '..\..\src\Thrift.TypeRegistry.pas', |
Jake Farrell | f6e8b0d | 2012-10-05 00:41:59 +0000 | [diff] [blame] | 37 | Thrift.Stream in '..\..\src\Thrift.Stream.pas'; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 38 | |
| 39 | const |
| 40 | REQUEST_EXT = '.request'; |
| 41 | RESPONSE_EXT = '.response'; |
| 42 | |
| 43 | function CreatePing : IPing; |
| 44 | var list : IThriftList<IPong>; |
| 45 | set_ : IHashSet<string>; |
| 46 | begin |
| 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_); |
| 73 | end; |
| 74 | |
| 75 | |
| 76 | type |
| 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 | |
| 84 | function TDummyServer.PingPong(const ping: IPing; const pong: IPong): IPing; |
| 85 | // TSkipTestService.Iface |
| 86 | begin |
| 87 | Writeln('- performing request from version '+IntToStr(ping.Version1)+' client'); |
| 88 | result := CreatePing; |
| 89 | end; |
| 90 | |
| 91 | |
| 92 | function CreateProtocol( protfact : IProtocolFactory; stm : TStream; aForInput : Boolean) : IProtocol; |
| 93 | var adapt : IThriftStream; |
| 94 | trans : ITransport; |
| 95 | begin |
| 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); |
| 101 | end; |
| 102 | |
| 103 | |
| 104 | procedure CreateRequest( protfact : IProtocolFactory; fname : string); |
| 105 | var 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 |
| 110 | begin |
| 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 Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 126 | if client = nil then {warning suppressed}; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 127 | end; |
| 128 | |
| 129 | DeleteFile( fname+REQUEST_EXT); |
| 130 | RenameFile( fname+REQUEST_EXT+'.tmp', fname+REQUEST_EXT); |
| 131 | end; |
| 132 | |
| 133 | |
| 134 | procedure ReadResponse( protfact : IProtocolFactory; fname : string); |
| 135 | var 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 |
| 140 | begin |
| 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 Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 154 | if client = nil then {warning suppressed}; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 155 | end; |
| 156 | end; |
| 157 | |
| 158 | |
| 159 | procedure ProcessFile( protfact : IProtocolFactory; fname : string); |
| 160 | var stmIn, stmOut : TFileStream; |
| 161 | protIn, protOut : IProtocol; |
| 162 | server : IProcessor; |
| 163 | begin |
| 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 Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 181 | if server = nil then {warning suppressed}; |
Jake Farrell | 6cd63ec | 2012-08-29 02:04:35 +0000 | [diff] [blame] | 182 | end; |
| 183 | |
| 184 | DeleteFile( fname+RESPONSE_EXT); |
| 185 | RenameFile( fname+RESPONSE_EXT+'.tmp', fname+RESPONSE_EXT); |
| 186 | end; |
| 187 | |
| 188 | |
| 189 | procedure Test( protfact : IProtocolFactory; fname : string); |
| 190 | begin |
| 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); |
| 201 | end; |
| 202 | |
| 203 | |
| 204 | const |
| 205 | FILE_BINARY = 'pingpong.bin'; |
| 206 | FILE_JSON = 'pingpong.json'; |
| 207 | begin |
| 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; |
| 227 | end. |
| 228 | |