| 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 | include Thrift |
| 25 | |
| 26 | class TestCompactProtocol < Test::Unit::TestCase |
| 27 | def test_defferent_data_types |
| 28 | begin |
| 29 | port = ARGV[0] || 9090 |
| 30 | pid = Process.fork |
| 31 | if pid.nil?then |
| 32 | buf = String.new('1234567') |
| 33 | socket = Thrift::ServerSocket.new(port) |
| 34 | socket.listen() |
| 35 | client = socket.accept() |
| 36 | transport = Thrift::BufferedTransport.new(client) |
| 37 | protocol = Thrift::CompactProtocol.new(transport) |
| 38 | acc_bool = protocol.read_bool() |
| 39 | assert_equal(true,acc_bool) |
| 40 | |
| 41 | acc_boolf = protocol.read_bool() |
| 42 | assert_equal(false,acc_boolf) |
| 43 | |
| 44 | acc_byte = protocol.read_byte() |
| 45 | assert_equal(123,acc_byte) |
| 46 | |
| 47 | acc_i16 = protocol.read_i16() |
| 48 | assert_equal(4203,acc_i16) |
| 49 | |
| 50 | acc_i32 = protocol.read_i32() |
| 51 | assert_equal(2000000032,acc_i32) |
| 52 | |
| 53 | acc_i64 = protocol.read_i64() |
| 54 | assert_equal(1844674407370955161,acc_i64) |
| 55 | |
| 56 | acc_double = protocol.read_double() |
| 57 | assert_equal(3.1415926,acc_double) |
| 58 | |
| 59 | assert_kind_of(Float,acc_double) |
| 60 | acc_string = protocol.read_string() |
| 61 | assert_equal("hello_world123456789!@#$%&",acc_string) |
| 62 | |
| 63 | acc_binary = protocol.read_binary() |
| 64 | ret = acc_binary.bytes.to_a |
| 65 | assert_equal((0...256).reverse_each.to_a,ret) |
| 66 | |
| 67 | acc_message = protocol.read_message_begin() |
| 68 | protocol.read_message_end() |
| 69 | assert_equal(["hello_world",4,455536],acc_message) |
| 70 | |
| 71 | acc_list = protocol.read_list_begin() |
| 72 | protocol.read_list_end() |
| 73 | assert_equal("[8, 12]", "#{acc_list}") |
| 74 | |
| 75 | acc_map1 = protocol.read_map_begin() |
| 76 | protocol.read_map_end() |
| 77 | assert_equal("[6, 8, 12]", "#{acc_map1}") |
| 78 | |
| 79 | acc_map2 = protocol.read_map_begin() |
| 80 | protocol.read_map_end() |
| 81 | assert_equal("[0, 0, 0]", "#{acc_map2}") |
| 82 | |
| 83 | acc_set = protocol.read_set_begin() |
| 84 | protocol.read_set_end() |
| 85 | assert_equal("[8, 5]", "#{acc_set}") |
| 86 | |
| 87 | acc_field1 = protocol.read_field_begin() |
| 88 | protocol.read_field_end() |
| 89 | assert_equal("[nil, 6, 5]","#{acc_field1}") |
| 90 | |
| 91 | acc_field2 = protocol.read_field_begin() |
| 92 | protocol.read_field_end() |
| 93 | assert_equal("[nil, 0, 0]","#{acc_field2}") |
| 94 | |
| 95 | |
| 96 | exit(0) |
| 97 | else |
| 98 | sleep(2) |
| 99 | socket = Thrift::Socket.new('localhost',port) |
| 100 | transport = Thrift::BufferedTransport.new(socket) |
| 101 | transport.open() |
| 102 | protocol = Thrift::CompactProtocol.new(transport) |
| 103 | |
| 104 | protocol.write_bool(true) |
| 105 | transport.flush() |
| 106 | |
| 107 | protocol.write_bool(false) |
| 108 | transport.flush() |
| 109 | |
| 110 | protocol.write_byte(123) |
| 111 | transport.flush() |
| 112 | |
| 113 | protocol.write_i16(4203) |
| 114 | transport.flush() |
| 115 | |
| 116 | protocol.write_i32(2000000032) |
| 117 | transport.flush() |
| 118 | |
| 119 | protocol.write_i64(1844674407370955161) |
| 120 | transport.flush() |
| 121 | |
| 122 | protocol.write_double(3.1415926) |
| 123 | transport.flush() |
| 124 | |
| 125 | protocol.write_string("hello_world123456789!@#$%&") |
| 126 | transport.flush() |
| 127 | |
| 128 | val = (0...256).reverse_each.to_a |
| 129 | protocol.write_binary(val.pack('C*')) |
| 130 | transport.flush() |
| 131 | |
| 132 | protocol.write_message_begin("hello_world",140,455536) |
| 133 | protocol.write_message_end() |
| 134 | transport.flush() |
| 135 | |
| 136 | protocol.write_list_begin(Thrift::Types::I32,12) |
| 137 | protocol.write_list_end() |
| 138 | transport.flush() |
| 139 | |
| 140 | protocol.write_map_begin(Thrift::Types::I16,Thrift::Types::I32,12) |
| 141 | protocol.write_map_end() |
| 142 | transport.flush() |
| 143 | |
| 144 | protocol.write_map_begin(Thrift::Types::I16,Thrift::Types::I32,0) |
| 145 | protocol.write_map_end() |
| 146 | transport.flush() |
| 147 | |
| 148 | protocol.write_set_begin(Thrift::Types::I32,5) |
| 149 | protocol.write_set_end() |
| 150 | transport.flush() |
| 151 | |
| 152 | protocol.write_field_begin("hello_world",Thrift::Types::I16,5) |
| 153 | protocol.write_field_stop() |
| 154 | transport.flush() |
| 155 | |
| 156 | protocol.write_field_begin("hello_world",Thrift::Types::BOOL,5) |
| 157 | protocol.write_field_stop() |
| 158 | transport.flush() |
| 159 | |
| 160 | Process.wait(pid) |
| 161 | end |
| 162 | end |
| 163 | end |
| 164 | end |