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