| 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 TestJsonProtocol < Test::Unit::TestCase |
| 25 | def test_defferent_data_types |
| 26 | begin |
| 27 | port = ARGV[0] || 7070 |
| 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::JsonProtocol.new(transport) |
| 36 | |
| 37 | acc_json_string = protocol.read_json_string() |
| 38 | assert_equal('hello_world123!@#$%',acc_json_string) |
| 39 | |
| 40 | acc_json_base64 = protocol.read_json_base64() |
| 41 | assert_equal('hello_world12233!@#$%',acc_json_base64) |
| 42 | |
| 43 | protocol.read_json_array_start() |
| 44 | acc_json_integer = protocol.read_json_integer() |
| 45 | protocol.read_json_array_end() |
| 46 | assert_equal(2553369689,acc_json_integer) |
| 47 | |
| 48 | |
| 49 | protocol.read_json_array_start() |
| 50 | acc_json_double = protocol.read_json_double() |
| 51 | protocol.read_json_array_end() |
| 52 | assert_equal(3.1415926,acc_json_double) |
| 53 | |
| 54 | acc_message = protocol.read_message_begin() |
| 55 | protocol.read_message_end() |
| 56 | assert_equal("[\"hello_world\", 155510, 102020]","#{acc_message}") |
| 57 | |
| 58 | acc_field = protocol.read_field_begin() |
| 59 | protocol.read_field_end() |
| 60 | assert_equal([nil ,6 ,12],acc_field) |
| 61 | |
| 62 | acc_map = protocol.read_map_begin() |
| 63 | protocol.read_map_end() |
| 64 | assert_equal([10, 8, 20],acc_map) |
| 65 | |
| 66 | acc_list = protocol.read_list_begin() |
| 67 | protocol.read_list_end() |
| 68 | assert_equal([6, 10],acc_list) |
| 69 | |
| 70 | acc_set = protocol.read_set_begin() |
| 71 | protocol.read_set_end() |
| 72 | assert_equal([8, 20],acc_set) |
| 73 | |
| 74 | protocol.read_json_object_start() |
| 75 | acc_bool = protocol.read_bool() |
| 76 | protocol.read_json_object_end() |
| 77 | assert_equal(true,acc_bool) |
| 78 | |
| 79 | protocol.read_json_object_start() |
| 80 | acc_byte = protocol.read_byte() |
| 81 | protocol.read_json_object_end() |
| 82 | assert_equal(123,acc_byte) |
| 83 | |
| 84 | protocol.read_json_object_start() |
| 85 | acc_i16 = protocol.read_i16() |
| 86 | protocol.read_json_object_end() |
| 87 | assert_equal(4203,acc_i16) |
| 88 | |
| 89 | |
| 90 | protocol.read_json_object_start() |
| 91 | acc_i32 = protocol.read_i32() |
| 92 | protocol.read_json_object_end() |
| 93 | assert_equal(2000000032,acc_i32) |
| 94 | |
| 95 | protocol.read_json_object_start() |
| 96 | acc_i64 = protocol.read_i64() |
| 97 | protocol.read_json_object_end() |
| 98 | assert_equal(1844674407370955161,acc_i64) |
| 99 | |
| 100 | protocol.read_json_object_start() |
| 101 | acc_double = protocol.read_double() |
| 102 | protocol.read_json_object_end() |
| 103 | assert_equal(3.1415926,acc_double) |
| 104 | |
| 105 | protocol.read_json_object_start() |
| 106 | acc_string = protocol.read_string() |
| 107 | protocol.read_json_object_end() |
| 108 | assert_equal("sello_world123456789!@#$%",acc_string) |
| 109 | |
| 110 | protocol.read_json_object_start() |
| 111 | acc_binary = protocol.read_binary() |
| 112 | ret =acc_binary.bytes.to_a |
| 113 | protocol.read_json_object_end() |
| 114 | assert_equal((0...256).reverse_each.to_a,ret) |
| 115 | |
| 116 | exit(0) |
| 117 | else |
| 118 | sleep(2) |
| 119 | socket = Thrift::Socket.new('localhost',port) |
| 120 | transport = Thrift::BufferedTransport.new(socket) |
| 121 | transport.open() |
| 122 | protocol = Thrift::JsonProtocol.new(transport) |
| 123 | |
| 124 | protocol.write_json_string('hello_world123!@#$%') |
| 125 | transport.flush() |
| 126 | |
| 127 | protocol.write_json_base64('hello_world12233!@#$%') |
| 128 | transport.flush() |
| 129 | |
| 130 | protocol.write_json_array_start() |
| 131 | protocol.write_json_integer(2553369689) |
| 132 | protocol.write_json_array_end() |
| 133 | transport.flush() |
| 134 | |
| 135 | protocol.write_json_array_start() |
| 136 | protocol.write_json_double(3.1415926) |
| 137 | protocol.write_json_array_end() |
| 138 | transport.flush() |
| 139 | |
| 140 | protocol.write_message_begin('hello_world',155510,102020) |
| 141 | protocol.write_message_end() |
| 142 | transport.flush() |
| 143 | |
| 144 | protocol.write_field_begin("hello_world",Thrift::Types::I16,12) |
| 145 | protocol.write_field_end() |
| 146 | transport.flush() |
| 147 | |
| 148 | protocol.write_map_begin(Thrift::Types::I64,Thrift::Types::I32,20) |
| 149 | protocol.write_map_end() |
| 150 | transport.flush() |
| 151 | |
| 152 | protocol.write_list_begin(Thrift::Types::I16,10) |
| 153 | protocol.write_list_end() |
| 154 | transport.flush() |
| 155 | |
| 156 | protocol.write_set_begin(Thrift::Types::I32,20) |
| 157 | protocol.write_set_end() |
| 158 | transport.flush() |
| 159 | |
| 160 | protocol.write_json_object_start() |
| 161 | protocol.write_bool(true) |
| 162 | protocol.write_json_object_end() |
| 163 | transport.flush() |
| 164 | |
| 165 | protocol.write_json_object_start() |
| 166 | protocol.write_byte(123) |
| 167 | protocol.write_json_object_end() |
| 168 | transport.flush() |
| 169 | |
| 170 | |
| 171 | protocol.write_json_object_start() |
| 172 | protocol.write_i16(4203) |
| 173 | protocol.write_json_object_end() |
| 174 | transport.flush() |
| 175 | |
| 176 | protocol.write_json_object_start() |
| 177 | protocol.write_i32(2000000032) |
| 178 | protocol.write_json_object_end() |
| 179 | transport.flush() |
| 180 | |
| 181 | protocol.write_json_object_start() |
| 182 | protocol.write_i64(1844674407370955161) |
| 183 | protocol.write_json_object_end() |
| 184 | transport.flush() |
| 185 | |
| 186 | protocol.write_json_object_start() |
| 187 | protocol.write_double(3.1415926) |
| 188 | protocol.write_json_object_end() |
| 189 | transport.flush() |
| 190 | |
| 191 | protocol.write_json_object_start() |
| 192 | protocol.write_string("sello_world123456789!@#$%") |
| 193 | protocol.write_json_object_end() |
| 194 | transport.flush() |
| 195 | |
| 196 | protocol.write_json_object_start() |
| 197 | val = (0...256).reverse_each.to_a |
| 198 | protocol.write_binary(val.pack('C*')) |
| 199 | protocol.write_json_object_end() |
| 200 | transport.flush() |
| 201 | |
| 202 | |
| 203 | transport.close() |
| 204 | Process.wait(pid) |
| 205 | end |
| 206 | end |
| 207 | end |
| 208 | end |