Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [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 | package; |
| 21 | |
| 22 | import haxe.Int64; |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 23 | import sys.FileSystem; |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 24 | |
| 25 | import org.apache.thrift.*; |
| 26 | import org.apache.thrift.protocol.*; |
| 27 | import org.apache.thrift.transport.*; |
| 28 | import org.apache.thrift.server.*; |
| 29 | import org.apache.thrift.meta_data.*; |
| 30 | |
| 31 | import thrift.test.*; // generated code |
| 32 | |
| 33 | |
| 34 | class StreamTest extends TestBase { |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 35 | |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 36 | |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 37 | private inline static var tmpfile : String = "data.tmp"; |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 38 | |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 39 | |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 40 | private static function MakeTestData() : Xtruct { |
| 41 | var data : Xtruct = new Xtruct(); |
| 42 | data.string_thing = "Streamtest"; |
| 43 | data.byte_thing = -128; |
| 44 | data.i32_thing = 4711; |
| 45 | data.i64_thing = Int64.make(0x12345678,0x9ABCDEF0); |
| 46 | return data; |
| 47 | } |
| 48 | |
| 49 | public static function WriteData() : Xtruct |
| 50 | { |
Jens Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 51 | var config : TConfiguration = new TConfiguration(); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 52 | var stream : TStream = new TFileStream( tmpfile, CreateNew); |
Jens Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 53 | var trans : TTransport = new TStreamTransport( null, stream, config); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 54 | var prot = new TJSONProtocol( trans); |
| 55 | |
| 56 | var data = MakeTestData(); |
| 57 | data.write(prot); |
| 58 | trans.close(); |
| 59 | |
| 60 | return data; |
| 61 | } |
| 62 | |
| 63 | public static function ReadData() : Xtruct |
| 64 | { |
Jens Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 65 | var config : TConfiguration = new TConfiguration(); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 66 | var stream : TStream = new TFileStream( tmpfile, Read); |
Jens Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 67 | var trans : TTransport = new TStreamTransport( stream, null, config); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 68 | var prot = new TJSONProtocol( trans); |
| 69 | |
| 70 | var data : Xtruct = new Xtruct(); |
| 71 | data.read(prot); |
| 72 | trans.close(); |
| 73 | |
| 74 | return data; |
| 75 | } |
| 76 | |
Jens Geyer | ce1d314 | 2022-06-06 14:29:38 +0200 | [diff] [blame] | 77 | public static function Run(server : Bool) : Void |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 78 | { |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 79 | try { |
| 80 | var written = WriteData(); |
| 81 | var read = ReadData(); |
| 82 | FileSystem.deleteFile(tmpfile); |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 83 | |
Jens Geyer | e5ff9a8 | 2014-11-11 01:39:38 +0100 | [diff] [blame] | 84 | TestBase.Expect( read.string_thing == written.string_thing, "string data"); |
| 85 | TestBase.Expect( read.byte_thing == written.byte_thing, "byte data"); |
| 86 | TestBase.Expect( read.i32_thing == written.i32_thing, "i32 data"); |
| 87 | TestBase.Expect( Int64.compare( read.i64_thing, written.i64_thing) == 0, "i64 data"); |
| 88 | |
| 89 | } catch(e:Dynamic) { |
| 90 | FileSystem.deleteFile(tmpfile); |
| 91 | throw e; |
| 92 | } |
Jens Geyer | b502832 | 2014-11-09 02:38:11 +0100 | [diff] [blame] | 93 | } |
Jens Geyer | fea00ac | 2014-10-01 02:22:48 +0200 | [diff] [blame] | 94 | |
| 95 | } |
| 96 | |
| 97 | |