| zeshuai007 | 58d385e | 2020-08-29 11:13:41 +0800 | [diff] [blame] | 1 | #!/usr/bin/env ruby |
| 2 | # |
| 3 | # Licensed to the Apache Software Foundation (ASF) under one |
| 4 | # or more contributor license agreements. See the NOTICE file |
| 5 | # distributed with this work for additional information |
| 6 | # regarding copyright ownership. The ASF licenses this file |
| 7 | # to you under the Apache License, Version 2.0 (the |
| 8 | # "License"); you may not use this file except in compliance |
| 9 | # with the License. You may obtain a copy of the License at |
| 10 | # |
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | # |
| 13 | # Unless required by applicable law or agreed to in writing, |
| 14 | # software distributed under the License is distributed on an |
| 15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | # KIND, either express or implied. See the License for the |
| 17 | # specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | # |
| 20 | |
| 21 | require 'thrift' |
| 22 | require 'test/unit' |
| 23 | |
| 24 | class TestBinaryProtocol < Test::Unit::TestCase |
| 25 | def test_defferent_data_types |
| 26 | begin |
| 27 | port = ARGV[0] || 9090 |
| 28 | pid = Process.fork |
| 29 | if pid.nil?then |
| 30 | buf = String.new('1234567') |
| 31 | socket = Thrift::ServerSocket.new(port) |
| 32 | socket.listen() |
| 33 | client = socket.accept() |
| 34 | transport = Thrift::BufferedTransport.new(client) |
| 35 | protocol = Thrift::BinaryProtocol.new(transport) |
| 36 | |
| 37 | acc_bool = protocol.read_bool() |
| 38 | assert_equal(true,acc_bool) |
| 39 | |
| 40 | acc_boolf = protocol.read_bool() |
| 41 | assert_equal(false,acc_boolf) |
| 42 | |
| 43 | acc_byte = protocol.read_byte() |
| 44 | assert_equal(123,acc_byte) |
| 45 | |
| 46 | acc_i16 = protocol.read_i16() |
| 47 | assert_equal(4203,acc_i16) |
| 48 | |
| 49 | acc_i32 = protocol.read_i32() |
| 50 | assert_equal(2000000032,acc_i32) |
| 51 | |
| 52 | acc_i64 = protocol.read_i64() |
| 53 | assert_equal(1844674407370955161,acc_i64) |
| 54 | |
| 55 | acc_double = protocol.read_double() |
| 56 | assert_equal(3.1415926,acc_double) |
| 57 | assert_kind_of(Float,acc_double) |
| 58 | |
| 59 | acc_string = protocol.read_string() |
| 60 | assert_equal("hello_world123456789!@#$%&",acc_string) |
| 61 | |
| 62 | acc_binary = protocol.read_binary() |
| 63 | ret = acc_binary.bytes.to_a |
| 64 | assert_equal((0...256).reverse_each.to_a,ret) |
| 65 | |
| 66 | acc_message = protocol.read_message_begin() |
| 67 | protocol.read_message_end() |
| 68 | assert_equal(["hello_world",123,455536],acc_message) |
| 69 | |
| 70 | acc_field = protocol.read_field_begin() |
| 71 | protocol.read_field_end() |
| 72 | assert_equal([nil,123,25536],acc_field) |
| 73 | |
| 74 | acc_map = protocol.read_map_begin() |
| 75 | protocol.read_map_end() |
| 76 | assert_equal([56,123,2000000032],acc_map) |
| 77 | |
| 78 | acc_list = protocol.read_list_begin() |
| 79 | protocol.read_list_end() |
| 80 | assert_equal([125,2000000032],acc_list) |
| 81 | |
| 82 | acc_set = protocol.read_set_begin() |
| 83 | protocol.read_set_end() |
| 84 | assert_equal([-124,2000000022],acc_set) |
| 85 | |
| 86 | exit(0) |
| 87 | else |
| 88 | sleep(2) |
| 89 | socket = Thrift::Socket.new('localhost',port) |
| 90 | transport = Thrift::BufferedTransport.new(socket) |
| 91 | transport.open() |
| 92 | protocol = Thrift::BinaryProtocol.new(transport) |
| 93 | |
| 94 | protocol.write_bool(true) |
| 95 | transport.flush() |
| 96 | |
| 97 | protocol.write_bool(false) |
| 98 | transport.flush() |
| 99 | |
| 100 | protocol.write_byte(123) |
| 101 | transport.flush() |
| 102 | |
| 103 | protocol.write_i16(4203) |
| 104 | transport.flush() |
| 105 | |
| 106 | protocol.write_i32(2000000032) |
| 107 | transport.flush() |
| 108 | |
| 109 | protocol.write_i64(1844674407370955161) |
| 110 | transport.flush() |
| 111 | |
| 112 | protocol.write_double(3.1415926) |
| 113 | transport.flush() |
| 114 | |
| 115 | protocol.write_string("hello_world123456789!@#$%&") |
| 116 | transport.flush() |
| 117 | |
| 118 | val = (0...256).reverse_each.to_a |
| 119 | protocol.write_binary(val.pack('C*')) |
| 120 | transport.flush() |
| 121 | |
| 122 | protocol.write_message_begin("hello_world",123,455536) |
| 123 | protocol.write_message_end() |
| 124 | transport.flush() |
| 125 | |
| 126 | protocol.write_field_begin("hello_world",123,25536) |
| 127 | protocol.write_field_end() |
| 128 | transport.flush() |
| 129 | |
| 130 | protocol.write_map_begin(56,123,2000000032) |
| 131 | protocol.write_map_end() |
| 132 | transport.flush() |
| 133 | |
| 134 | protocol.write_list_begin(125,2000000032) |
| 135 | protocol.write_list_end() |
| 136 | transport.flush() |
| 137 | |
| 138 | protocol.write_set_begin(-124,2000000022) |
| 139 | protocol.write_set_end() |
| 140 | transport.flush() |
| 141 | Process.wait(pid) |
| 142 | end |
| 143 | end |
| 144 | end |
| 145 | end |