diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 79e6e6034ba..8e05de1fc60 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -320,21 +320,21 @@ jobs: - name: Set up .NET SDK (via install script) run: | # remove any existing install - sudo apt remove dotnet* + sudo apt remove dotnet* # install key sudo apt install gpg wget https://dot.net/v1/dotnet-install.asc - gpg --import dotnet-install.asc + gpg --import dotnet-install.asc # download and verify wget https://dot.net/v1/dotnet-install.sh wget https://dot.net/v1/dotnet-install.sig - gpg --verify dotnet-install.sig dotnet-install.sh + gpg --verify dotnet-install.sig dotnet-install.sh # run install script chmod +x dotnet-install.sh - ./dotnet-install.sh --channel 10.0 + ./dotnet-install.sh --channel 10.0 # export env vars export DOTNET_ROOT=$HOME/.dotnet - export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools + export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools dotnet --list-sdks # the sdk is installed by default, but keep this step for reference @@ -646,6 +646,54 @@ jobs: path: lib/cpp/test/*.xml retention-days: 3 + lib-ruby: + needs: compiler + runs-on: ubuntu-24.04 + name: lib-ruby (${{ matrix.ruby-version }}) ${{ matrix.skip-build-ext && 'noext' || '' }} + strategy: + matrix: + ruby-version: ["2.7", "3.0", "3.1", "3.2", "3.3", "3.4", "4.0.0-preview2"] + skip-build-ext: [false] + include: + - ruby-version: "2.7" + skip-build-ext: true + + fail-fast: false + env: + SKIP_BUILD_EXT: ${{ matrix.skip-build-ext && '1' || '' }} + steps: + - uses: actions/checkout@v5 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} + bundler-cache: true + working-directory: lib/rb + + - name: Run bootstrap + run: ./bootstrap.sh + + - name: Run configure + run: | + ./configure $(echo $CONFIG_ARGS_FOR_LIBS | sed 's/without-ruby/with-ruby/') + + - uses: actions/download-artifact@v6 + with: + name: thrift-compiler + path: compiler/cpp + + - name: Run thrift-compiler + run: | + chmod a+x compiler/cpp/thrift + compiler/cpp/thrift -version + + - name: Run make for ruby + run: make -C lib/rb + + - name: Run make check for lib/rb + run: make -C lib/rb check + cross-test: needs: - lib-java-kotlin @@ -755,4 +803,3 @@ jobs: name: cross-test-log_${{ matrix.server_lang }}-${{ matrix.client_lang }} path: test/log/ retention-days: 3 - diff --git a/.gitignore b/.gitignore index 9fc033fd149..0fd47642dab 100644 --- a/.gitignore +++ b/.gitignore @@ -260,7 +260,7 @@ project.lock.json /lib/rb/ext/mkmf.log /lib/rb/ext/thrift_native.bundle /lib/rb/ext/thrift_native.so -#/lib/rb/test/ +/lib/rb/test/ /lib/rb/thrift-*.gem /lib/php/src/ext/thrift_protocol/Makefile.* /lib/php/src/ext/thrift_protocol/build/ diff --git a/LANGUAGES.md b/LANGUAGES.md index e70b0657ee9..21e60eb67b0 100644 --- a/LANGUAGES.md +++ b/LANGUAGES.md @@ -312,7 +312,7 @@ Thrift's core protocol is TBinary, supported by all languages except for JavaScr Ruby 0.2.0 Yes -2.4.02.5.1p57 +2.7.84.0.0 YesYesYesYes YesYes diff --git a/lib/rb/Gemfile b/lib/rb/Gemfile index b4e2a20bb60..83ff86730aa 100644 --- a/lib/rb/Gemfile +++ b/lib/rb/Gemfile @@ -1,3 +1,7 @@ source "https://rubygems.org" gemspec + +# Only required for tests with Rack 2.x on Ruby 4.0+ +gem 'cgi' +gem 'ostruct' diff --git a/lib/rb/Gemfile.lock b/lib/rb/Gemfile.lock index 796f2991992..e3d0d17a32f 100644 --- a/lib/rb/Gemfile.lock +++ b/lib/rb/Gemfile.lock @@ -2,6 +2,7 @@ PATH remote: . specs: thrift (0.23.0) + logger GEM remote: https://rubygems.org/ @@ -9,12 +10,15 @@ GEM binding_of_caller (1.0.1) debug_inspector (>= 1.2.0) byebug (11.1.3) + cgi (0.5.0) coderay (1.1.3) daemons (1.4.1) debug_inspector (1.2.0) diff-lcs (1.6.2) eventmachine (1.2.7) + logger (1.7.0) method_source (0.9.2) + ostruct (0.6.3) pry (0.11.3) coderay (~> 1.1.0) method_source (~> 0.9.0) @@ -27,7 +31,7 @@ GEM rack (2.2.21) rack-test (0.8.3) rack (>= 1.0, < 3) - rake (12.3.3) + rake (13.3.1) rspec (3.13.2) rspec-core (~> 3.13.0) rspec-expectations (~> 3.13.0) @@ -57,12 +61,14 @@ PLATFORMS DEPENDENCIES bundler (~> 2.2.34) + cgi + ostruct pry (~> 0.11.3) pry-byebug (~> 3.6) pry-stack_explorer (~> 0.4.9.2) rack (>= 2.2.20) rack-test (~> 0.8.3) - rake (~> 12.3) + rake (~> 13.3) rspec (~> 3.7) srv (~> 1.0) thin (~> 1.7) diff --git a/lib/rb/Makefile.am b/lib/rb/Makefile.am index 1088bcbdd18..3967b5475e5 100644 --- a/lib/rb/Makefile.am +++ b/lib/rb/Makefile.am @@ -36,9 +36,6 @@ clean-local: check-local: all $(BUNDLER) install $(BUNDLER) exec rake - $(RUBY) test/TestBinaryProtocol.rb - $(RUBY) test/TestCompactProtocol.rb - $(RUBY) test/TestJsonProtocol.rb endif diff --git a/lib/rb/Rakefile b/lib/rb/Rakefile index c6b650eae2a..35819d8edb2 100644 --- a/lib/rb/Rakefile +++ b/lib/rb/Rakefile @@ -62,7 +62,7 @@ namespace :'gen-rb' do dir = File.dirname(__FILE__) + '/benchmark' sh THRIFT, '--gen', 'rb', '-o', dir, "#{dir}/Benchmark.thrift" end - + task :'debug_proto' do sh "mkdir", "-p", "test/debug_proto" sh THRIFT, '--gen', 'rb', "-o", "test/debug_proto", "../../test/v0.16/DebugProtoTest.thrift" @@ -72,6 +72,7 @@ end desc "Build the native library" task :build_ext => :'gen-rb' do next if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/ + next if ENV['SKIP_BUILD_EXT'] == '1' Dir::chdir(File::dirname('ext/extconf.rb')) do unless sh "ruby #{File::basename('ext/extconf.rb')}" $stderr.puts "Failed to run extconf" @@ -115,6 +116,6 @@ end CLEAN.include [ '.bundle', 'benchmark/gen-rb', 'coverage', 'ext/*.{o,bundle,so,dll}', 'ext/mkmf.log', - 'ext/Makefile', 'ext/conftest.dSYM', 'Gemfile.lock', 'mkmf.log', 'pkg', 'spec/gen-rb', - 'test', 'thrift-*.gem' + 'ext/Makefile', 'ext/conftest.dSYM', 'ext/thrift_native.bundle.dSYM', 'mkmf.log', + 'pkg', 'spec/gen-rb', 'test/debug_proto/gen-rb', 'thrift-*.gem' ] diff --git a/lib/rb/lib/thrift/bytes.rb b/lib/rb/lib/thrift/bytes.rb index efd4f6440cc..169a9502c3f 100644 --- a/lib/rb/lib/thrift/bytes.rb +++ b/lib/rb/lib/thrift/bytes.rb @@ -26,13 +26,13 @@ module Bytes # # size - The Integer size of the buffer (default: nil) to create # - # Returns a String with BINARY encoding, filled with null characters + # Returns a String with BINARY encoding, filled with null characters # if size is greater than zero def self.empty_byte_buffer(size = nil) if (size && size > 0) - "\0".force_encoding(Encoding::BINARY) * size + "\0".b * size else - ''.force_encoding(Encoding::BINARY) + "".b end end diff --git a/lib/rb/lib/thrift/protocol/compact_protocol.rb b/lib/rb/lib/thrift/protocol/compact_protocol.rb index 1f9bd30608f..52117eb33f7 100644 --- a/lib/rb/lib/thrift/protocol/compact_protocol.rb +++ b/lib/rb/lib/thrift/protocol/compact_protocol.rb @@ -276,7 +276,7 @@ def read_field_begin # push the new field onto the field stack so we can keep the deltas going. @last_field.push(field_id) - ["", CompactTypes.get_ttype(type & 0x0f), field_id] + [nil, CompactTypes.get_ttype(type & 0x0f), field_id] end end diff --git a/lib/rb/lib/thrift/union.rb b/lib/rb/lib/thrift/union.rb index 490c55c406b..88c4285550f 100644 --- a/lib/rb/lib/thrift/union.rb +++ b/lib/rb/lib/thrift/union.rb @@ -73,6 +73,8 @@ def write(oprot) fid = self.name_to_id(@setfield.to_s) field_info = struct_fields[fid] + raise "set_field is not valid for this union!" unless field_info + type = field_info[:type] if is_container? type oprot.write_field_begin(@setfield, type, fid) diff --git a/lib/rb/test/TestBinaryProtocol.rb b/lib/rb/test/TestBinaryProtocol.rb deleted file mode 100644 index 39f50739db8..00000000000 --- a/lib/rb/test/TestBinaryProtocol.rb +++ /dev/null @@ -1,145 +0,0 @@ -#!/usr/bin/env ruby -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# - -require 'thrift' -require 'test/unit' - -class TestBinaryProtocol < Test::Unit::TestCase - def test_defferent_data_types - begin - port = ARGV[0] || 9090 - pid = Process.fork - if pid.nil?then - buf = String.new('1234567') - socket = Thrift::ServerSocket.new(port) - socket.listen() - client = socket.accept() - transport = Thrift::BufferedTransport.new(client) - protocol = Thrift::BinaryProtocol.new(transport) - - acc_bool = protocol.read_bool() - assert_equal(true,acc_bool) - - acc_boolf = protocol.read_bool() - assert_equal(false,acc_boolf) - - acc_byte = protocol.read_byte() - assert_equal(123,acc_byte) - - acc_i16 = protocol.read_i16() - assert_equal(4203,acc_i16) - - acc_i32 = protocol.read_i32() - assert_equal(2000000032,acc_i32) - - acc_i64 = protocol.read_i64() - assert_equal(1844674407370955161,acc_i64) - - acc_double = protocol.read_double() - assert_equal(3.1415926,acc_double) - assert_kind_of(Float,acc_double) - - acc_string = protocol.read_string() - assert_equal("hello_world123456789!@#$%&",acc_string) - - acc_binary = protocol.read_binary() - ret = acc_binary.bytes.to_a - assert_equal((0...256).reverse_each.to_a,ret) - - acc_message = protocol.read_message_begin() - protocol.read_message_end() - assert_equal(["hello_world",123,455536],acc_message) - - acc_field = protocol.read_field_begin() - protocol.read_field_end() - assert_equal([nil,123,25536],acc_field) - - acc_map = protocol.read_map_begin() - protocol.read_map_end() - assert_equal([56,123,2000000032],acc_map) - - acc_list = protocol.read_list_begin() - protocol.read_list_end() - assert_equal([125,2000000032],acc_list) - - acc_set = protocol.read_set_begin() - protocol.read_set_end() - assert_equal([-124,2000000022],acc_set) - - exit(0) - else - sleep(2) - socket = Thrift::Socket.new('localhost',port) - transport = Thrift::BufferedTransport.new(socket) - transport.open() - protocol = Thrift::BinaryProtocol.new(transport) - - protocol.write_bool(true) - transport.flush() - - protocol.write_bool(false) - transport.flush() - - protocol.write_byte(123) - transport.flush() - - protocol.write_i16(4203) - transport.flush() - - protocol.write_i32(2000000032) - transport.flush() - - protocol.write_i64(1844674407370955161) - transport.flush() - - protocol.write_double(3.1415926) - transport.flush() - - protocol.write_string("hello_world123456789!@#$%&") - transport.flush() - - val = (0...256).reverse_each.to_a - protocol.write_binary(val.pack('C*')) - transport.flush() - - protocol.write_message_begin("hello_world",123,455536) - protocol.write_message_end() - transport.flush() - - protocol.write_field_begin("hello_world",123,25536) - protocol.write_field_end() - transport.flush() - - protocol.write_map_begin(56,123,2000000032) - protocol.write_map_end() - transport.flush() - - protocol.write_list_begin(125,2000000032) - protocol.write_list_end() - transport.flush() - - protocol.write_set_begin(-124,2000000022) - protocol.write_set_end() - transport.flush() - Process.wait(pid) - end - end - end -end diff --git a/lib/rb/test/TestCompactProtocol.rb b/lib/rb/test/TestCompactProtocol.rb deleted file mode 100644 index 02711ba7ec9..00000000000 --- a/lib/rb/test/TestCompactProtocol.rb +++ /dev/null @@ -1,164 +0,0 @@ -#!/usr/bin/env ruby -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# - -require 'thrift' -require 'test/unit' - -include Thrift - -class TestCompactProtocol < Test::Unit::TestCase - def test_defferent_data_types - begin - port = ARGV[0] || 9090 - pid = Process.fork - if pid.nil?then - buf = String.new('1234567') - socket = Thrift::ServerSocket.new(port) - socket.listen() - client = socket.accept() - transport = Thrift::BufferedTransport.new(client) - protocol = Thrift::CompactProtocol.new(transport) - acc_bool = protocol.read_bool() - assert_equal(true,acc_bool) - - acc_boolf = protocol.read_bool() - assert_equal(false,acc_boolf) - - acc_byte = protocol.read_byte() - assert_equal(123,acc_byte) - - acc_i16 = protocol.read_i16() - assert_equal(4203,acc_i16) - - acc_i32 = protocol.read_i32() - assert_equal(2000000032,acc_i32) - - acc_i64 = protocol.read_i64() - assert_equal(1844674407370955161,acc_i64) - - acc_double = protocol.read_double() - assert_equal(3.1415926,acc_double) - - assert_kind_of(Float,acc_double) - acc_string = protocol.read_string() - assert_equal("hello_world123456789!@#$%&",acc_string) - - acc_binary = protocol.read_binary() - ret = acc_binary.bytes.to_a - assert_equal((0...256).reverse_each.to_a,ret) - - acc_message = protocol.read_message_begin() - protocol.read_message_end() - assert_equal(["hello_world",4,455536],acc_message) - - acc_list = protocol.read_list_begin() - protocol.read_list_end() - assert_equal("[8, 12]", "#{acc_list}") - - acc_map1 = protocol.read_map_begin() - protocol.read_map_end() - assert_equal("[6, 8, 12]", "#{acc_map1}") - - acc_map2 = protocol.read_map_begin() - protocol.read_map_end() - assert_equal("[0, 0, 0]", "#{acc_map2}") - - acc_set = protocol.read_set_begin() - protocol.read_set_end() - assert_equal("[8, 5]", "#{acc_set}") - - acc_field1 = protocol.read_field_begin() - protocol.read_field_end() - assert_equal("[nil, 6, 5]","#{acc_field1}") - - acc_field2 = protocol.read_field_begin() - protocol.read_field_end() - assert_equal("[nil, 0, 0]","#{acc_field2}") - - - exit(0) - else - sleep(2) - socket = Thrift::Socket.new('localhost',port) - transport = Thrift::BufferedTransport.new(socket) - transport.open() - protocol = Thrift::CompactProtocol.new(transport) - - protocol.write_bool(true) - transport.flush() - - protocol.write_bool(false) - transport.flush() - - protocol.write_byte(123) - transport.flush() - - protocol.write_i16(4203) - transport.flush() - - protocol.write_i32(2000000032) - transport.flush() - - protocol.write_i64(1844674407370955161) - transport.flush() - - protocol.write_double(3.1415926) - transport.flush() - - protocol.write_string("hello_world123456789!@#$%&") - transport.flush() - - val = (0...256).reverse_each.to_a - protocol.write_binary(val.pack('C*')) - transport.flush() - - protocol.write_message_begin("hello_world",140,455536) - protocol.write_message_end() - transport.flush() - - protocol.write_list_begin(Thrift::Types::I32,12) - protocol.write_list_end() - transport.flush() - - protocol.write_map_begin(Thrift::Types::I16,Thrift::Types::I32,12) - protocol.write_map_end() - transport.flush() - - protocol.write_map_begin(Thrift::Types::I16,Thrift::Types::I32,0) - protocol.write_map_end() - transport.flush() - - protocol.write_set_begin(Thrift::Types::I32,5) - protocol.write_set_end() - transport.flush() - - protocol.write_field_begin("hello_world",Thrift::Types::I16,5) - protocol.write_field_stop() - transport.flush() - - protocol.write_field_begin("hello_world",Thrift::Types::BOOL,5) - protocol.write_field_stop() - transport.flush() - - Process.wait(pid) - end - end - end -end diff --git a/lib/rb/test/TestJsonProtocol.rb b/lib/rb/test/TestJsonProtocol.rb deleted file mode 100644 index 562aad50e45..00000000000 --- a/lib/rb/test/TestJsonProtocol.rb +++ /dev/null @@ -1,208 +0,0 @@ -#!/usr/bin/env ruby -# -# Licensed to the Apache Software Foundation (ASF) under one -# or more contributor license agreements. See the NOTICE file -# distributed with this work for additional information -# regarding copyright ownership. The ASF licenses this file -# to you under the Apache License, Version 2.0 (the -# "License"); you may not use this file except in compliance -# with the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. -# - -require 'thrift' -require 'test/unit' - -class TestJsonProtocol < Test::Unit::TestCase - def test_defferent_data_types - begin - port = ARGV[0] || 7070 - pid = Process.fork - if pid.nil?then - buf = String.new('1234567') - socket = Thrift::ServerSocket.new(port) - socket.listen() - client = socket.accept() - transport = Thrift::BufferedTransport.new(client) - protocol = Thrift::JsonProtocol.new(transport) - - acc_json_string = protocol.read_json_string() - assert_equal('hello_world123!@#$%',acc_json_string) - - acc_json_base64 = protocol.read_json_base64() - assert_equal('hello_world12233!@#$%',acc_json_base64) - - protocol.read_json_array_start() - acc_json_integer = protocol.read_json_integer() - protocol.read_json_array_end() - assert_equal(2553369689,acc_json_integer) - - - protocol.read_json_array_start() - acc_json_double = protocol.read_json_double() - protocol.read_json_array_end() - assert_equal(3.1415926,acc_json_double) - - acc_message = protocol.read_message_begin() - protocol.read_message_end() - assert_equal("[\"hello_world\", 155510, 102020]","#{acc_message}") - - acc_field = protocol.read_field_begin() - protocol.read_field_end() - assert_equal([nil ,6 ,12],acc_field) - - acc_map = protocol.read_map_begin() - protocol.read_map_end() - assert_equal([10, 8, 20],acc_map) - - acc_list = protocol.read_list_begin() - protocol.read_list_end() - assert_equal([6, 10],acc_list) - - acc_set = protocol.read_set_begin() - protocol.read_set_end() - assert_equal([8, 20],acc_set) - - protocol.read_json_object_start() - acc_bool = protocol.read_bool() - protocol.read_json_object_end() - assert_equal(true,acc_bool) - - protocol.read_json_object_start() - acc_byte = protocol.read_byte() - protocol.read_json_object_end() - assert_equal(123,acc_byte) - - protocol.read_json_object_start() - acc_i16 = protocol.read_i16() - protocol.read_json_object_end() - assert_equal(4203,acc_i16) - - - protocol.read_json_object_start() - acc_i32 = protocol.read_i32() - protocol.read_json_object_end() - assert_equal(2000000032,acc_i32) - - protocol.read_json_object_start() - acc_i64 = protocol.read_i64() - protocol.read_json_object_end() - assert_equal(1844674407370955161,acc_i64) - - protocol.read_json_object_start() - acc_double = protocol.read_double() - protocol.read_json_object_end() - assert_equal(3.1415926,acc_double) - - protocol.read_json_object_start() - acc_string = protocol.read_string() - protocol.read_json_object_end() - assert_equal("sello_world123456789!@#$%",acc_string) - - protocol.read_json_object_start() - acc_binary = protocol.read_binary() - ret =acc_binary.bytes.to_a - protocol.read_json_object_end() - assert_equal((0...256).reverse_each.to_a,ret) - - exit(0) - else - sleep(2) - socket = Thrift::Socket.new('localhost',port) - transport = Thrift::BufferedTransport.new(socket) - transport.open() - protocol = Thrift::JsonProtocol.new(transport) - - protocol.write_json_string('hello_world123!@#$%') - transport.flush() - - protocol.write_json_base64('hello_world12233!@#$%') - transport.flush() - - protocol.write_json_array_start() - protocol.write_json_integer(2553369689) - protocol.write_json_array_end() - transport.flush() - - protocol.write_json_array_start() - protocol.write_json_double(3.1415926) - protocol.write_json_array_end() - transport.flush() - - protocol.write_message_begin('hello_world',155510,102020) - protocol.write_message_end() - transport.flush() - - protocol.write_field_begin("hello_world",Thrift::Types::I16,12) - protocol.write_field_end() - transport.flush() - - protocol.write_map_begin(Thrift::Types::I64,Thrift::Types::I32,20) - protocol.write_map_end() - transport.flush() - - protocol.write_list_begin(Thrift::Types::I16,10) - protocol.write_list_end() - transport.flush() - - protocol.write_set_begin(Thrift::Types::I32,20) - protocol.write_set_end() - transport.flush() - - protocol.write_json_object_start() - protocol.write_bool(true) - protocol.write_json_object_end() - transport.flush() - - protocol.write_json_object_start() - protocol.write_byte(123) - protocol.write_json_object_end() - transport.flush() - - - protocol.write_json_object_start() - protocol.write_i16(4203) - protocol.write_json_object_end() - transport.flush() - - protocol.write_json_object_start() - protocol.write_i32(2000000032) - protocol.write_json_object_end() - transport.flush() - - protocol.write_json_object_start() - protocol.write_i64(1844674407370955161) - protocol.write_json_object_end() - transport.flush() - - protocol.write_json_object_start() - protocol.write_double(3.1415926) - protocol.write_json_object_end() - transport.flush() - - protocol.write_json_object_start() - protocol.write_string("sello_world123456789!@#$%") - protocol.write_json_object_end() - transport.flush() - - protocol.write_json_object_start() - val = (0...256).reverse_each.to_a - protocol.write_binary(val.pack('C*')) - protocol.write_json_object_end() - transport.flush() - - - transport.close() - Process.wait(pid) - end - end - end -end diff --git a/test/rb/core/protocol/test_binary_protocol.rb b/test/rb/core/protocol/test_binary_protocol.rb new file mode 100644 index 00000000000..4681636150a --- /dev/null +++ b/test/rb/core/protocol/test_binary_protocol.rb @@ -0,0 +1,162 @@ +#!/usr/bin/env ruby +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require File.expand_path('../../test_helper', __dir__) +require 'thrift' +require 'thread' + +class TestBinaryProtocol < Test::Unit::TestCase + def test_different_data_types + port = (ARGV[0] || 9090).to_i + server_ready = Queue.new + + server_thread = Thread.new do + socket = Thrift::ServerSocket.new(port) + transport = nil + + socket.listen + server_ready << true + client = socket.accept + transport = Thrift::BufferedTransport.new(client) + protocol = Thrift::BinaryProtocol.new(transport) + + results = {} + results[:acc_bool] = protocol.read_bool + results[:acc_boolf] = protocol.read_bool + results[:acc_byte] = protocol.read_byte + results[:acc_i16] = protocol.read_i16 + results[:acc_i32] = protocol.read_i32 + results[:acc_i64] = protocol.read_i64 + results[:acc_double] = protocol.read_double + results[:acc_string] = protocol.read_string + + acc_binary = protocol.read_binary + results[:acc_binary] = acc_binary.bytes.to_a + + results[:acc_message] = protocol.read_message_begin + protocol.read_message_end + + results[:acc_field] = protocol.read_field_begin + protocol.read_field_end + + results[:acc_map] = protocol.read_map_begin + protocol.read_map_end + + results[:acc_list] = protocol.read_list_begin + protocol.read_list_end + + results[:acc_set] = protocol.read_set_begin + protocol.read_set_end + + results + ensure + transport&.close + socket&.close + end + + server_ready.pop + + socket = Thrift::Socket.new('localhost', port) + transport = Thrift::BufferedTransport.new(socket) + transport.open + protocol = Thrift::BinaryProtocol.new(transport) + + # acc_bool + protocol.write_bool(true) + transport.flush + + # acc_boolf + protocol.write_bool(false) + transport.flush + + # acc_byte + protocol.write_byte(123) + transport.flush + + # acc_i16 + protocol.write_i16(4203) + transport.flush + + # acc_i32 + protocol.write_i32(2000000032) + transport.flush + + # acc_i64 + protocol.write_i64(1844674407370955161) + transport.flush + + # acc_double + protocol.write_double(3.1415926) + transport.flush + + # acc_string + protocol.write_string("hello_world123456789!@#$%&") + transport.flush + + val = (0...256).reverse_each.to_a + # acc_binary + protocol.write_binary(val.pack('C*')) + transport.flush + + # acc_message + protocol.write_message_begin("hello_world", 123, 455536) + protocol.write_message_end + transport.flush + + # acc_field + protocol.write_field_begin("hello_world", 123, 25536) + protocol.write_field_end + transport.flush + + # acc_map + protocol.write_map_begin(56, 123, 2000000032) + protocol.write_map_end + transport.flush + + # acc_list + protocol.write_list_begin(125, 2000000032) + protocol.write_list_end + transport.flush + + # acc_set + protocol.write_set_begin(-124, 2000000022) + protocol.write_set_end + transport.flush + + transport.close + + server_results = server_thread.value + assert_equal(true, server_results[:acc_bool]) + assert_equal(false, server_results[:acc_boolf]) + assert_equal(123, server_results[:acc_byte]) + assert_equal(4203, server_results[:acc_i16]) + assert_equal(2000000032, server_results[:acc_i32]) + assert_equal(1844674407370955161, server_results[:acc_i64]) + assert_equal(3.1415926, server_results[:acc_double]) + assert_kind_of(Float, server_results[:acc_double]) + assert_equal("hello_world123456789!@#$%&", server_results[:acc_string]) + assert_equal((0...256).reverse_each.to_a, server_results[:acc_binary]) + assert_equal(["hello_world", 123, 455536], server_results[:acc_message]) + assert_equal([nil, 123, 25536], server_results[:acc_field]) + assert_equal([56, 123, 2000000032], server_results[:acc_map]) + assert_equal([125, 2000000032], server_results[:acc_list]) + assert_equal([-124, 2000000022], server_results[:acc_set]) + end +end diff --git a/test/rb/core/protocol/test_compact_protocol.rb b/test/rb/core/protocol/test_compact_protocol.rb new file mode 100644 index 00000000000..53387036222 --- /dev/null +++ b/test/rb/core/protocol/test_compact_protocol.rb @@ -0,0 +1,180 @@ +#!/usr/bin/env ruby +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# + +require File.expand_path('../../test_helper', __dir__) +require 'thrift' +require 'thread' + +class TestCompactProtocol < Test::Unit::TestCase + def test_different_data_types + port = (ARGV[0] || 9090).to_i + server_ready = Queue.new + + server_thread = Thread.new do + socket = Thrift::ServerSocket.new(port) + transport = nil + + socket.listen + server_ready << true + client = socket.accept + transport = Thrift::BufferedTransport.new(client) + protocol = Thrift::CompactProtocol.new(transport) + + results = {} + results[:acc_bool] = protocol.read_bool + results[:acc_boolf] = protocol.read_bool + results[:acc_byte] = protocol.read_byte + results[:acc_i16] = protocol.read_i16 + results[:acc_i32] = protocol.read_i32 + results[:acc_i64] = protocol.read_i64 + results[:acc_double] = protocol.read_double + results[:acc_string] = protocol.read_string + + acc_binary = protocol.read_binary + results[:acc_binary] = acc_binary.bytes.to_a + + results[:acc_message] = protocol.read_message_begin + protocol.read_message_end + + results[:acc_list] = protocol.read_list_begin + protocol.read_list_end + + results[:acc_map1] = protocol.read_map_begin + protocol.read_map_end + + results[:acc_map2] = protocol.read_map_begin + protocol.read_map_end + + results[:acc_set] = protocol.read_set_begin + protocol.read_set_end + + results[:acc_field1] = protocol.read_field_begin + protocol.read_field_end + + results[:acc_field2] = protocol.read_field_begin + protocol.read_field_end + + results + ensure + transport&.close + socket&.close + end + + server_ready.pop + + socket = Thrift::Socket.new('localhost', port) + transport = Thrift::BufferedTransport.new(socket) + transport.open + protocol = Thrift::CompactProtocol.new(transport) + + # acc_bool + protocol.write_bool(true) + transport.flush + + # acc_boolf + protocol.write_bool(false) + transport.flush + + # acc_byte + protocol.write_byte(123) + transport.flush + + # acc_i16 + protocol.write_i16(4203) + transport.flush + + # acc_i32 + protocol.write_i32(2000000032) + transport.flush + + # acc_i64 + protocol.write_i64(1844674407370955161) + transport.flush + + # acc_double + protocol.write_double(3.1415926) + transport.flush + + # acc_string + protocol.write_string("hello_world123456789!@#$%&") + transport.flush + + val = (0...256).reverse_each.to_a + # acc_binary + protocol.write_binary(val.pack('C*')) + transport.flush + + # acc_message + protocol.write_message_begin("hello_world", 140, 455536) + protocol.write_message_end + transport.flush + + # acc_list + protocol.write_list_begin(Thrift::Types::I32, 12) + protocol.write_list_end + transport.flush + + # acc_map1 + protocol.write_map_begin(Thrift::Types::I16, Thrift::Types::I32, 12) + protocol.write_map_end + transport.flush + + # acc_map2 + protocol.write_map_begin(Thrift::Types::I16, Thrift::Types::I32, 0) + protocol.write_map_end + transport.flush + + # acc_set + protocol.write_set_begin(Thrift::Types::I32, 5) + protocol.write_set_end + transport.flush + + # acc_field1 + protocol.write_field_begin("hello_world", Thrift::Types::I16, 5) + protocol.write_field_stop + transport.flush + + # acc_field2 + protocol.write_field_begin("hello_world", Thrift::Types::BOOL, 5) + protocol.write_field_stop + transport.flush + + transport.close + + server_results = server_thread.value + assert_equal(true, server_results[:acc_bool]) + assert_equal(false, server_results[:acc_boolf]) + assert_equal(123, server_results[:acc_byte]) + assert_equal(4203, server_results[:acc_i16]) + assert_equal(2000000032, server_results[:acc_i32]) + assert_equal(1844674407370955161, server_results[:acc_i64]) + assert_equal(3.1415926, server_results[:acc_double]) + assert_kind_of(Float, server_results[:acc_double]) + assert_equal("hello_world123456789!@#$%&", server_results[:acc_string]) + assert_equal((0...256).reverse_each.to_a, server_results[:acc_binary]) + assert_equal(["hello_world", 4, 455536], server_results[:acc_message]) + assert_equal("[8, 12]", "#{server_results[:acc_list]}") + assert_equal("[6, 8, 12]", "#{server_results[:acc_map1]}") + assert_equal("[0, 0, 0]", "#{server_results[:acc_map2]}") + assert_equal("[8, 5]", "#{server_results[:acc_set]}") + assert_equal("[nil, 6, 5]", "#{server_results[:acc_field1]}") + assert_equal("[nil, 0, 0]", "#{server_results[:acc_field2]}") + end +end diff --git a/test/rb/core/protocol/test_json_protocol.rb b/test/rb/core/protocol/test_json_protocol.rb new file mode 100644 index 00000000000..a9c71e0b595 --- /dev/null +++ b/test/rb/core/protocol/test_json_protocol.rb @@ -0,0 +1,227 @@ +#!/usr/bin/env ruby +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +require File.expand_path('../../test_helper', __dir__) +require 'thrift' +require 'thread' + +class TestJsonProtocol < Test::Unit::TestCase + def test_different_data_types + port = (ARGV[0] || 7070).to_i + server_ready = Queue.new + + server_thread = Thread.new do + socket = Thrift::ServerSocket.new(port) + transport = nil + + socket.listen + server_ready << true + client = socket.accept + transport = Thrift::BufferedTransport.new(client) + protocol = Thrift::JsonProtocol.new(transport) + + results = {} + results[:acc_json_string] = protocol.read_json_string + results[:acc_json_base64] = protocol.read_json_base64 + + protocol.read_json_array_start + results[:acc_json_integer] = protocol.read_json_integer + protocol.read_json_array_end + + protocol.read_json_array_start + results[:acc_json_double] = protocol.read_json_double + protocol.read_json_array_end + + results[:acc_message] = protocol.read_message_begin + protocol.read_message_end + + results[:acc_field] = protocol.read_field_begin + protocol.read_field_end + + results[:acc_map] = protocol.read_map_begin + protocol.read_map_end + + results[:acc_list] = protocol.read_list_begin + protocol.read_list_end + + results[:acc_set] = protocol.read_set_begin + protocol.read_set_end + + protocol.read_json_object_start + results[:acc_bool] = protocol.read_bool + protocol.read_json_object_end + + protocol.read_json_object_start + results[:acc_byte] = protocol.read_byte + protocol.read_json_object_end + + protocol.read_json_object_start + results[:acc_i16] = protocol.read_i16 + protocol.read_json_object_end + + protocol.read_json_object_start + results[:acc_i32] = protocol.read_i32 + protocol.read_json_object_end + + protocol.read_json_object_start + results[:acc_i64] = protocol.read_i64 + protocol.read_json_object_end + + protocol.read_json_object_start + results[:acc_double] = protocol.read_double + protocol.read_json_object_end + + protocol.read_json_object_start + results[:acc_string] = protocol.read_string + protocol.read_json_object_end + + protocol.read_json_object_start + acc_binary = protocol.read_binary + results[:acc_binary] = acc_binary.bytes.to_a + protocol.read_json_object_end + + results + ensure + transport&.close + socket&.close + end + + server_ready.pop + + socket = Thrift::Socket.new('localhost', port) + transport = Thrift::BufferedTransport.new(socket) + transport.open + protocol = Thrift::JsonProtocol.new(transport) + + # acc_json_string + protocol.write_json_string('hello_world123!@#$%') + transport.flush + + # acc_json_base64 + protocol.write_json_base64('hello_world12233!@#$%') + transport.flush + + protocol.write_json_array_start + # acc_json_integer + protocol.write_json_integer(2553369689) + protocol.write_json_array_end + transport.flush + + protocol.write_json_array_start + # acc_json_double + protocol.write_json_double(3.1415926) + protocol.write_json_array_end + transport.flush + + # acc_message + protocol.write_message_begin('hello_world', 155510, 102020) + protocol.write_message_end + transport.flush + + # acc_field + protocol.write_field_begin("hello_world", Thrift::Types::I16, 12) + protocol.write_field_end + transport.flush + + # acc_map + protocol.write_map_begin(Thrift::Types::I64, Thrift::Types::I32, 20) + protocol.write_map_end + transport.flush + + # acc_list + protocol.write_list_begin(Thrift::Types::I16, 10) + protocol.write_list_end + transport.flush + + # acc_set + protocol.write_set_begin(Thrift::Types::I32, 20) + protocol.write_set_end + transport.flush + + protocol.write_json_object_start + # acc_bool + protocol.write_bool(true) + protocol.write_json_object_end + transport.flush + + protocol.write_json_object_start + # acc_byte + protocol.write_byte(123) + protocol.write_json_object_end + transport.flush + + protocol.write_json_object_start + # acc_i16 + protocol.write_i16(4203) + protocol.write_json_object_end + transport.flush + + protocol.write_json_object_start + # acc_i32 + protocol.write_i32(2000000032) + protocol.write_json_object_end + transport.flush + + protocol.write_json_object_start + # acc_i64 + protocol.write_i64(1844674407370955161) + protocol.write_json_object_end + transport.flush + + protocol.write_json_object_start + # acc_double + protocol.write_double(3.1415926) + protocol.write_json_object_end + transport.flush + + protocol.write_json_object_start + # acc_string + protocol.write_string("sello_world123456789!@#$%") + protocol.write_json_object_end + transport.flush + + protocol.write_json_object_start + val = (0...256).reverse_each.to_a + # acc_binary + protocol.write_binary(val.pack('C*')) + protocol.write_json_object_end + transport.flush + + transport.close + + server_results = server_thread.value + assert_equal('hello_world123!@#$%', server_results[:acc_json_string]) + assert_equal('hello_world12233!@#$%', server_results[:acc_json_base64]) + assert_equal(2553369689, server_results[:acc_json_integer]) + assert_equal(3.1415926, server_results[:acc_json_double]) + assert_equal("[\"hello_world\", 155510, 102020]", "#{server_results[:acc_message]}") + assert_equal([nil, 6, 12], server_results[:acc_field]) + assert_equal([10, 8, 20], server_results[:acc_map]) + assert_equal([6, 10], server_results[:acc_list]) + assert_equal([8, 20], server_results[:acc_set]) + assert_equal(true, server_results[:acc_bool]) + assert_equal(123, server_results[:acc_byte]) + assert_equal(4203, server_results[:acc_i16]) + assert_equal(2000000032, server_results[:acc_i32]) + assert_equal(1844674407370955161, server_results[:acc_i64]) + assert_equal(3.1415926, server_results[:acc_double]) + assert_equal("sello_world123456789!@#$%", server_results[:acc_string]) + assert_equal((0...256).reverse_each.to_a, server_results[:acc_binary]) + end +end