diff options
author | Kevin Clark <kclark@apache.org> | 2008-07-08 00:47:49 +0000 |
---|---|---|
committer | Kevin Clark <kclark@apache.org> | 2008-07-08 00:47:49 +0000 |
commit | 4bd8916b1257378f8173c4ada41b4606e9c0226b (patch) | |
tree | a43f051132e33d3ed07e3412b8777c3c08a75e6d /test/rb | |
parent | af78778acd7fb551a84aaf63e604d969123bf85d (diff) | |
download | thrift-4bd8916b1257378f8173c4ada41b4606e9c0226b.tar.gz |
Merge branch 'fastbinary'
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@674688 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test/rb')
-rw-r--r-- | test/rb/Makefile | 22 | ||||
-rw-r--r-- | test/rb/Makefile.am | 9 | ||||
-rw-r--r-- | test/rb/benchmarks/protocol_benchmark.rb | 158 | ||||
-rw-r--r-- | test/rb/core/test_binary_protocol_accelerated.rb | 275 | ||||
-rw-r--r-- | test/rb/fixtures/structs.rb | 225 | ||||
-rw-r--r-- | test/rb/generation/test_struct.rb | 3 | ||||
-rw-r--r-- | test/rb/integration/accelerated_buffered_client.rb | 145 | ||||
-rw-r--r-- | test/rb/integration/accelerated_buffered_server.rb | 47 | ||||
-rw-r--r-- | test/rb/integration/buffered_client.rb | 145 | ||||
-rw-r--r-- | test/rb/integration/simple_client.rb | 145 | ||||
-rw-r--r-- | test/rb/integration/simple_server.rb | 46 | ||||
-rw-r--r-- | test/rb/test_helper.rb | 12 | ||||
-rw-r--r-- | test/rb/test_suite.rb | 2 |
13 files changed, 1211 insertions, 23 deletions
diff --git a/test/rb/Makefile b/test/rb/Makefile deleted file mode 100644 index 5c1e61a86..000000000 --- a/test/rb/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -# Makefile for Thrift test project. -# -# Author: -# Mark Slee <mcslee@facebook.com> - -# Default target is everything -target: all - -# Tools -THRIFT = ../../compiler/cpp/thrift - -all: stubs check - -stubs: ../ThriftTest.thrift ../SmallTest.thrift - $(THRIFT) --gen rb ../ThriftTest.thrift - $(THRIFT) --gen rb ../SmallTest.thrift - -check: stubs - ruby test_suite.rb - -clean: - $(RM) -r gen-rb diff --git a/test/rb/Makefile.am b/test/rb/Makefile.am new file mode 100644 index 000000000..b19fcde95 --- /dev/null +++ b/test/rb/Makefile.am @@ -0,0 +1,9 @@ +THRIFT = $(top_srcdir)/compiler/cpp/thrift + +stubs: ../ThriftTest.thrift ../SmallTest.thrift + $(THRIFT) --gen rb ../ThriftTest.thrift + $(THRIFT) --gen rb ../SmallTest.thrift + +check: stubs + $(RUBY) test_suite.rb + diff --git a/test/rb/benchmarks/protocol_benchmark.rb b/test/rb/benchmarks/protocol_benchmark.rb new file mode 100644 index 000000000..99f07601c --- /dev/null +++ b/test/rb/benchmarks/protocol_benchmark.rb @@ -0,0 +1,158 @@ +$LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. .. .. lib rb lib]) +$LOAD_PATH.unshift File.join(File.dirname(__FILE__), *%w[.. .. .. lib rb ext]) + +require 'thrift' +require 'thrift/transport' +require 'thrift/protocol/binaryprotocol' +require 'thrift/protocol/binaryprotocolaccelerated' + +require 'benchmark' +require 'rubygems' +require 'set' +require 'pp' + +# require 'ruby-debug' +# require 'ruby-prof' + +require File.join(File.dirname(__FILE__), '../fixtures/structs') + +transport1 = Thrift::MemoryBuffer.new +ruby_binary_protocol = Thrift::BinaryProtocol.new(transport1) + +transport2 = Thrift::MemoryBuffer.new +c_fast_binary_protocol = Thrift::BinaryProtocolAccelerated.new(transport2) + + +ooe = Fixtures::Structs::OneOfEach.new +ooe.im_true = true +ooe.im_false = false +ooe.a_bite = -42 +ooe.integer16 = 27000 +ooe.integer32 = 1<<24 +ooe.integer64 = 6000 * 1000 * 1000 +ooe.double_precision = Math::PI +ooe.some_characters = "Debug THIS!" +ooe.zomg_unicode = "\xd7\n\a\t" + +n1 = Fixtures::Structs::Nested1.new +n1.a_list = [] +n1.a_list << ooe << ooe << ooe << ooe +n1.i32_map = {} +n1.i32_map[1234] = ooe +n1.i32_map[46345] = ooe +n1.i32_map[-34264] = ooe +n1.i64_map = {} +n1.i64_map[43534986783945] = ooe +n1.i64_map[-32434639875122] = ooe +n1.dbl_map = {} +n1.dbl_map[324.65469834] = ooe +n1.dbl_map[-9458672340.4986798345112] = ooe +n1.str_map = {} +n1.str_map['sdoperuix'] = ooe +n1.str_map['pwoerxclmn'] = ooe + +n2 = Fixtures::Structs::Nested2.new +n2.a_list = [] +n2.a_list << n1 << n1 << n1 << n1 << n1 +n2.i32_map = {} +n2.i32_map[398345] = n1 +n2.i32_map[-2345] = n1 +n2.i32_map[12312] = n1 +n2.i64_map = {} +n2.i64_map[2349843765934] = n1 +n2.i64_map[-123234985495] = n1 +n2.i64_map[0] = n1 +n2.dbl_map = {} +n2.dbl_map[23345345.38927834] = n1 +n2.dbl_map[-1232349.5489345] = n1 +n2.dbl_map[-234984574.23498725] = n1 +n2.str_map = {} +n2.str_map[''] = n1 +n2.str_map['sdflkertpioux'] = n1 +n2.str_map['sdfwepwdcjpoi'] = n1 + +n3 = Fixtures::Structs::Nested3.new +n3.a_list = [] +n3.a_list << n2 << n2 << n2 << n2 << n2 +n3.i32_map = {} +n3.i32_map[398345] = n2 +n3.i32_map[-2345] = n2 +n3.i32_map[12312] = n2 +n3.i64_map = {} +n3.i64_map[2349843765934] = n2 +n3.i64_map[-123234985495] = n2 +n3.i64_map[0] = n2 +n3.dbl_map = {} +n3.dbl_map[23345345.38927834] = n2 +n3.dbl_map[-1232349.5489345] = n2 +n3.dbl_map[-234984574.23498725] = n2 +n3.str_map = {} +n3.str_map[''] = n2 +n3.str_map['sdflkertpioux'] = n2 +n3.str_map['sdfwepwdcjpoi'] = n2 + +n4 = Fixtures::Structs::Nested4.new +n4.a_list = [] +n4.a_list << n3 +n4.i32_map = {} +n4.i32_map[-2345] = n3 +n4.i64_map = {} +n4.i64_map[2349843765934] = n3 +n4.dbl_map = {} +n4.dbl_map[-1232349.5489345] = n3 +n4.str_map = {} +n4.str_map[''] = n3 + + +# prof = RubyProf.profile do +# n4.write(c_fast_binary_protocol) +# Fixtures::Structs::Nested4.new.read(c_fast_binary_protocol) +# end +# +# printer = RubyProf::GraphHtmlPrinter.new(prof) +# printer.print(STDOUT, :min_percent=>0) + +Benchmark.bmbm do |x| + x.report("ruby write large (1MB) structure once") do + n4.write(ruby_binary_protocol) + end + + x.report("ruby read large (1MB) structure once") do + Fixtures::Structs::Nested4.new.read(ruby_binary_protocol) + end + + x.report("c write large (1MB) structure once") do + n4.write(c_fast_binary_protocol) + end + + x.report("c read large (1MB) structure once") do + Fixtures::Structs::Nested4.new.read(c_fast_binary_protocol) + end + + + + x.report("ruby write 10_000 small structures") do + 10_000.times do + ooe.write(ruby_binary_protocol) + end + end + + x.report("ruby read 10_000 small structures") do + 10_000.times do + Fixtures::Structs::OneOfEach.new.read(ruby_binary_protocol) + end + end + + x.report("c write 10_000 small structures") do + 10_000.times do + ooe.write(c_fast_binary_protocol) + end + end + + x.report("c read 10_000 small structures") do + 10_000.times do + Fixtures::Structs::OneOfEach.new.read(c_fast_binary_protocol) + end + end + +end diff --git a/test/rb/core/test_binary_protocol_accelerated.rb b/test/rb/core/test_binary_protocol_accelerated.rb new file mode 100644 index 000000000..15c033e19 --- /dev/null +++ b/test/rb/core/test_binary_protocol_accelerated.rb @@ -0,0 +1,275 @@ +require File.join(File.dirname(__FILE__), '../test_helper') +require File.join(File.dirname(__FILE__), '../fixtures/structs') + +require 'thrift' +require 'thrift/transport' +require 'thrift/protocol/binaryprotocol' +require 'thrift/protocol/binaryprotocolaccelerated' + +class BinaryProtocolAcceleratedTest < Test::Unit::TestCase + I8_MIN = -128 + I8_MAX = 127 + I16_MIN = -32768 + I16_MAX = 32767 + I32_MIN = -2147483648 + I32_MAX = 2147483647 + I64_MIN = -9223372036854775808 + I64_MAX = 9223372036854775807 + DBL_MIN = Float::MIN + DBL_MAX = Float::MAX + + # booleans might be read back differently, so we supply a list [write_value, read_value] + BOOL_VALUES = [[0,true], [14,true], [-14,true], [true,true], [false,false], ["",true]] + BYTE_VALUES = [14, -14, I8_MIN, I8_MAX] + I16_VALUES = [400, 0, -234, I16_MIN, I16_MAX] + I32_VALUES = [325, 0, -1, -1073741825, -278, -4352388, I32_MIN, I32_MAX] + I64_VALUES = [15, 0, -33, I64_MIN, I64_MAX] + DBL_VALUES = [DBL_MIN, -33.8755, 0, 3658.1279, DBL_MAX] + STR_VALUES = ["", "welcome to my test"] + + def setup + @trans = Thrift::MemoryBuffer.new + @fast_proto = Thrift::BinaryProtocolAccelerated.new(@trans) + @slow_proto = Thrift::BinaryProtocol.new(@trans) + end + + def assert_encodes_struct(obj) + obj.write(@slow_proto) + expected = @trans.read(@trans.available) # read it all baby + assert_equal expected, @fast_proto.encode_binary(obj) + end + + # Assumes encode works + def assert_decodes_struct(obj, eql_obj = nil) + data = @fast_proto.encode_binary(obj) + @trans.write data + assert_equal (eql_obj || obj), @fast_proto.decode_binary(obj.class.new, @trans) + end + + def test_encodes_and_decodes_bools + BOOL_VALUES.each do |(write_val, read_val)| + obj = Fixtures::Structs::OneBool.new(:bool => write_val) + assert_encodes_struct obj + assert_decodes_struct obj, Fixtures::Structs::OneBool.new(:bool => read_val) + end + end + + def test_encodes_and_decodes_bytes + BYTE_VALUES.each do |val| + obj = Fixtures::Structs::OneByte.new(:byte => val) + assert_encodes_struct obj + assert_decodes_struct obj + end + end + + def test_encodes_and_decodes_i16 + I16_VALUES.each do |val| + obj = Fixtures::Structs::OneI16.new(:i16 => val) + assert_encodes_struct obj + assert_decodes_struct obj + end + end + + def test_encodes_and_decodes_i32 + I32_VALUES.each do |val| + obj = Fixtures::Structs::OneI32.new(:i32 => val) + assert_encodes_struct obj + assert_decodes_struct obj + end + end + + def test_encodes_and_decodes_i64 + I64_VALUES.each do |val| + obj = Fixtures::Structs::OneI64.new(:i64 => val) + assert_encodes_struct obj + assert_decodes_struct obj + end + end + + def test_encodes_and_decodes_double + DBL_VALUES.each do |val| + obj = Fixtures::Structs::OneDouble.new(:double => val) + assert_encodes_struct obj + assert_decodes_struct obj + end + end + + def test_encodes_strings + STR_VALUES.each do |val| + obj = Fixtures::Structs::OneString.new(:string => val) + assert_encodes_struct obj + assert_decodes_struct obj + end + end + + def test_encodes_maps + obj = Fixtures::Structs::OneMap.new(:map => {"a" => "b", "c" => "d"}) + assert_encodes_struct obj + assert_decodes_struct obj + end + + def test_encodes_lists + obj = Fixtures::Structs::OneList.new(:list => ["a", "b", "c", "d"]) + assert_encodes_struct obj + assert_decodes_struct obj + end + + def test_encodes_sets + expected_return = Fixtures::Structs::OneSet.new(:set => Set.new(["a", "b", "c"])) + # support hashes + obj = Fixtures::Structs::OneSet.new(:set => {"a" => true, "b" => true, "c" => true}) + assert_encodes_struct obj + assert_decodes_struct obj, expected_return + + # We also support arrays as compatability bug from TBinaryProtocol.... + obj = Fixtures::Structs::OneSet.new(:set => ["a", "b", "c"]) + assert_encodes_struct obj + assert_decodes_struct obj, expected_return + + # We also support sets + obj = Fixtures::Structs::OneSet.new(:set => Set.new(["a", "b", "c"])) + assert_encodes_struct obj + assert_decodes_struct obj, expected_return + end + + def test_encodes_maps_of_maps + obj = Fixtures::Structs::NestedMap.new(:map => { 1 => { 2 => 3 }}) + assert_encodes_struct obj + assert_decodes_struct obj + end + + def test_encodes_list_of_lists + obj = Fixtures::Structs::NestedList.new(:list => [[1,2,3], [4,5,6]]) + assert_encodes_struct obj + assert_decodes_struct obj + end + + def test_encodes_set_of_sets + obj = Fixtures::Structs::NestedSet.new(:set => Set.new([Set.new('a')])) + assert_encodes_struct obj + + # Nested hashes won't work with ==, so we do it by hand + data = @fast_proto.encode_binary(obj) + @trans.write data + decoded = @fast_proto.decode_binary(obj.class.new, @trans) + assert_equal decoded.set.entries, Set.new([Set.new('a')]).entries + end + + if ENV['MEM_TEST'] + def test_for_memory_leaks_on_exceptions + ooe = Fixtures::Structs::OneOfEach.new + ooe.im_true = true + ooe.im_false = false + ooe.a_bite = -42 + ooe.integer16 = 27000 + ooe.integer32 = 1<<24 + ooe.integer64 = 6000 * 1000 * 1000 + ooe.double_precision = Math::PI + ooe.some_characters = "Debug THIS!" + ooe.zomg_unicode = "\xd7\n\a\t" + + 10_000.times do + data = @fast_proto.encode_binary(ooe) + bytes = [] + data.each_byte do |b| + bytes << b + transport = TMemoryBuffer.new + transport.write(bytes.pack("c#{bytes.length}")) + + begin + @fast_proto.decode_binary(Fixtures::Structs::OneOfEach.new, transport) + rescue Exception + end + end + end + + end + end + + unless ENV['FAST_TEST'] + def test_encodes_and_decodes_struct_of_structs + ooe = Fixtures::Structs::OneOfEach.new + ooe.im_true = true + ooe.im_false = false + ooe.a_bite = -42 + ooe.integer16 = 27000 + ooe.integer32 = 1<<24 + ooe.integer64 = 6000 * 1000 * 1000 + ooe.double_precision = Math::PI + ooe.some_characters = "Debug THIS!" + ooe.zomg_unicode = "\xd7\n\a\t" + + n1 = Fixtures::Structs::Nested1.new + n1.a_list = [] + n1.a_list << ooe << ooe << ooe << ooe + n1.i32_map = {} + n1.i32_map[1234] = ooe + n1.i32_map[46345] = ooe + n1.i32_map[-34264] = ooe + n1.i64_map = {} + n1.i64_map[43534986783945] = ooe + n1.i64_map[-32434639875122] = ooe + n1.dbl_map = {} + n1.dbl_map[324.65469834] = ooe + n1.dbl_map[-9458672340.4986798345112] = ooe + n1.str_map = {} + n1.str_map['sdoperuix'] = ooe + n1.str_map['pwoerxclmn'] = ooe + + n2 = Fixtures::Structs::Nested2.new + n2.a_list = [] + n2.a_list << n1 << n1 << n1 << n1 << n1 + n2.i32_map = {} + n2.i32_map[398345] = n1 + n2.i32_map[-2345] = n1 + n2.i32_map[12312] = n1 + n2.i64_map = {} + n2.i64_map[2349843765934] = n1 + n2.i64_map[-123234985495] = n1 + n2.i64_map[0] = n1 + n2.dbl_map = {} + n2.dbl_map[23345345.38927834] = n1 + n2.dbl_map[-1232349.5489345] = n1 + n2.dbl_map[-234984574.23498725] = n1 + n2.str_map = {} + n2.str_map[''] = n1 + n2.str_map['sdflkertpioux'] = n1 + n2.str_map['sdfwepwdcjpoi'] = n1 + + n3 = Fixtures::Structs::Nested3.new + n3.a_list = [] + n3.a_list << n2 << n2 << n2 << n2 << n2 + n3.i32_map = {} + n3.i32_map[398345] = n2 + n3.i32_map[-2345] = n2 + n3.i32_map[12312] = n2 + n3.i64_map = {} + n3.i64_map[2349843765934] = n2 + n3.i64_map[-123234985495] = n2 + n3.i64_map[0] = n2 + n3.dbl_map = {} + n3.dbl_map[23345345.38927834] = n2 + n3.dbl_map[-1232349.5489345] = n2 + n3.dbl_map[-234984574.23498725] = n2 + n3.str_map = {} + n3.str_map[''] = n2 + n3.str_map['sdflkertpioux'] = n2 + n3.str_map['sdfwepwdcjpoi'] = n2 + + n4 = Fixtures::Structs::Nested4.new + n4.a_list = [] + n4.a_list << n3 + n4.i32_map = {} + n4.i32_map[-2345] = n3 + n4.i64_map = {} + n4.i64_map[2349843765934] = n3 + n4.dbl_map = {} + n4.dbl_map[-1232349.5489345] = n3 + n4.str_map = {} + n4.str_map[''] = n3 + + assert_encodes_struct n4 + assert_decodes_struct n4 + end + end +end diff --git a/test/rb/fixtures/structs.rb b/test/rb/fixtures/structs.rb new file mode 100644 index 000000000..82c291eca --- /dev/null +++ b/test/rb/fixtures/structs.rb @@ -0,0 +1,225 @@ +require 'thrift' + +module Fixtures + module Structs + class OneBool + include Thrift::Struct + attr_accessor :bool + FIELDS = { + 1 => {:type => Thrift::Types::BOOL, :name => 'bool'} + } + end + + class OneByte + include Thrift::Struct + attr_accessor :byte + FIELDS = { + 1 => {:type => Thrift::Types::BYTE, :name => 'byte'} + } + end + + class OneI16 + include Thrift::Struct + attr_accessor :i16 + FIELDS = { + 1 => {:type => Thrift::Types::I16, :name => 'i16'} + } + end + + class OneI32 + include Thrift::Struct + attr_accessor :i32 + FIELDS = { + 1 => {:type => Thrift::Types::I32, :name => 'i32'} + } + end + + class OneI64 + include Thrift::Struct + attr_accessor :i64 + FIELDS = { + 1 => {:type => Thrift::Types::I64, :name => 'i64'} + } + end + + class OneDouble + include Thrift::Struct + attr_accessor :double + FIELDS = { + 1 => {:type => Thrift::Types::DOUBLE, :name => 'double'} + } + end + + class OneString + include Thrift::Struct + attr_accessor :string + FIELDS = { + 1 => {:type => Thrift::Types::STRING, :name => 'string'} + } + end + + class OneMap + include Thrift::Struct + attr_accessor :map + FIELDS = { + 1 => {:type => Thrift::Types::MAP, :name => 'map', :key => {:type => Thrift::Types::STRING}, :value => {:type => Thrift::Types::STRING}} + } + end + + class NestedMap + include Thrift::Struct + attr_accessor :map + FIELDS = { + 0 => {:type => Thrift::Types::MAP, :name => 'map', :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::MAP, :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::I32}}} + } + end + + class OneList + include Thrift::Struct + attr_accessor :list + FIELDS = { + 1 => {:type => Thrift::Types::LIST, :name => 'list', :element => {:type => Thrift::Types::STRING}} + } + end + + class NestedList + include Thrift::Struct + attr_accessor :list + FIELDS = { + 0 => {:type => Thrift::Types::LIST, :name => 'list', :element => {:type => Thrift::Types::LIST, :element => { :type => Thrift::Types::I32 } } } + } + end + + class OneSet + include Thrift::Struct + attr_accessor :set + FIELDS = { + 1 => {:type => Thrift::Types::SET, :name => 'set', :element => {:type => Thrift::Types::STRING}} + } + end + + class NestedSet + include Thrift::Struct + attr_accessor :set + FIELDS = { + 1 => {:type => Thrift::Types::SET, :name => 'set', :element => {:type => Thrift::Types::SET, :element => { :type => Thrift::Types::STRING } }} + } + end + + # struct OneOfEach { + # 1: bool im_true, + # 2: bool im_false, + # 3: byte a_bite, + # 4: i16 integer16, + # 5: i32 integer32, + # 6: i64 integer64, + # 7: double double_precision, + # 8: string some_characters, + # 9: string zomg_unicode, + # 10: bool what_who, + # 11: binary base64, + # } + class OneOfEach + include Thrift::Struct + attr_accessor :im_true, :im_false, :a_bite, :integer16, :integer32, :integer64, :double_precision, :some_characters, :zomg_unicode, :what_who, :base64 + FIELDS = { + 1 => {:type => Thrift::Types::BOOL, :name => 'im_true'}, + 2 => {:type => Thrift::Types::BOOL, :name => 'im_false'}, + 3 => {:type => Thrift::Types::BYTE, :name => 'a_bite'}, + 4 => {:type => Thrift::Types::I16, :name => 'integer16'}, + 5 => {:type => Thrift::Types::I32, :name => 'integer32'}, + 6 => {:type => Thrift::Types::I64, :name => 'integer64'}, + 7 => {:type => Thrift::Types::DOUBLE, :name => 'double_precision'}, + 8 => {:type => Thrift::Types::STRING, :name => 'some_characters'}, + 9 => {:type => Thrift::Types::STRING, :name => 'zomg_unicode'}, + 10 => {:type => Thrift::Types::BOOL, :name => 'what_who'}, + 11 => {:type => Thrift::Types::STRING, :name => 'base64'} + } + + # Added for assert_equal + def ==(other) + [:im_true, :im_false, :a_bite, :integer16, :integer32, :integer64, :double_precision, :some_characters, :zomg_unicode, :what_who, :base64].each do |f| + var = "@#{f}" + return false if instance_variable_get(var) != other.instance_variable_get(var) + end + true + end + end + + # struct Nested1 { + # 1: list<OneOfEach> a_list + # 2: map<i32, OneOfEach> i32_map + # 3: map<i64, OneOfEach> i64_map + # 4: map<double, OneOfEach> dbl_map + # 5: map<string, OneOfEach> str_map + # } + class Nested1 + include Thrift::Struct + attr_accessor :a_list, :i32_map, :i64_map, :dbl_map, :str_map + FIELDS = { + 1 => {:type => Thrift::Types::LIST, :name => 'a_list', :element => {:type => Thrift::Types::STRUCT, :class => OneOfEach}}, + 2 => {:type => Thrift::Types::MAP, :name => 'i32_map', :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::STRUCT, :class => OneOfEach}}, + 3 => {:type => Thrift::Types::MAP, :name => 'i64_map', :key => {:type => Thrift::Types::I64}, :value => {:type => Thrift::Types::STRUCT, :class => OneOfEach}}, + 4 => {:type => Thrift::Types::MAP, :name => 'dbl_map', :key => {:type => Thrift::Types::DOUBLE}, :value => {:type => Thrift::Types::STRUCT, :class => OneOfEach}}, + 5 => {:type => Thrift::Types::MAP, :name => 'str_map', :key => {:type => Thrift::Types::STRING}, :value => {:type => Thrift::Types::STRUCT, :class => OneOfEach}} + } + end + + # struct Nested2 { + # 1: list<Nested1> a_list + # 2: map<i32, Nested1> i32_map + # 3: map<i64, Nested1> i64_map + # 4: map<double, Nested1> dbl_map + # 5: map<string, Nested1> str_map + # } + class Nested2 + include Thrift::Struct + attr_accessor :a_list, :i32_map, :i64_map, :dbl_map, :str_map + FIELDS = { + 1 => {:type => Thrift::Types::LIST, :name => 'a_list', :element => {:type => Thrift::Types::STRUCT, :class => Nested1}}, + 2 => {:type => Thrift::Types::MAP, :name => 'i32_map', :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::STRUCT, :class => Nested1}}, + 3 => {:type => Thrift::Types::MAP, :name => 'i64_map', :key => {:type => Thrift::Types::I64}, :value => {:type => Thrift::Types::STRUCT, :class => Nested1}}, + 4 => {:type => Thrift::Types::MAP, :name => 'dbl_map', :key => {:type => Thrift::Types::DOUBLE}, :value => {:type => Thrift::Types::STRUCT, :class => Nested1}}, + 5 => {:type => Thrift::Types::MAP, :name => 'str_map', :key => {:type => Thrift::Types::STRING}, :value => {:type => Thrift::Types::STRUCT, :class => Nested1}} + } + end + + # struct Nested3 { + # 1: list<Nested2> a_list + # 2: map<i32, Nested2> i32_map + # 3: map<i64, Nested2> i64_map + # 4: map<double, Nested2> dbl_map + # 5: map<string, Nested2> str_map + # } + class Nested3 + include Thrift::Struct + attr_accessor :a_list, :i32_map, :i64_map, :dbl_map, :str_map + FIELDS = { + 1 => {:type => Thrift::Types::LIST, :name => 'a_list', :element => {:type => Thrift::Types::STRUCT, :class => Nested2}}, + 2 => {:type => Thrift::Types::MAP, :name => 'i32_map', :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::STRUCT, :class => Nested2}}, + 3 => {:type => Thrift::Types::MAP, :name => 'i64_map', :key => {:type => Thrift::Types::I64}, :value => {:type => Thrift::Types::STRUCT, :class => Nested2}}, + 4 => {:type => Thrift::Types::MAP, :name => 'dbl_map', :key => {:type => Thrift::Types::DOUBLE}, :value => {:type => Thrift::Types::STRUCT, :class => Nested2}}, + 5 => {:type => Thrift::Types::MAP, :name => 'str_map', :key => {:type => Thrift::Types::STRING}, :value => {:type => Thrift::Types::STRUCT, :class => Nested2}} + } + end + + # struct Nested4 { + # 1: list<Nested3> a_list + # 2: map<i32, Nested3> i32_map + # 3: map<i64, Nested3> i64_map + # 4: map<double, Nested3> dbl_map + # 5: map<string, Nested3> str_map + # } + class Nested4 + include Thrift::Struct + attr_accessor :a_list, :i32_map, :i64_map, :dbl_map, :str_map + FIELDS = { + 1 => {:type => Thrift::Types::LIST, :name => 'a_list', :element => {:type => Thrift::Types::STRUCT, :class => Nested3}}, + 2 => {:type => Thrift::Types::MAP, :name => 'i32_map', :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::STRUCT, :class => Nested3}}, + 3 => {:type => Thrift::Types::MAP, :name => 'i64_map', :key => {:type => Thrift::Types::I64}, :value => {:type => Thrift::Types::STRUCT, :class => Nested3}}, + 4 => {:type => Thrift::Types::MAP, :name => 'dbl_map', :key => {:type => Thrift::Types::DOUBLE}, :value => {:type => Thrift::Types::STRUCT, :class => Nested3}}, + 5 => {:type => Thrift::Types::MAP, :name => 'str_map', :key => {:type => Thrift::Types::STRING}, :value => {:type => Thrift::Types::STRUCT, :class => Nested3}} + } + end + end +end diff --git a/test/rb/generation/test_struct.rb b/test/rb/generation/test_struct.rb index 3af0df848..b4526ab22 100644 --- a/test/rb/generation/test_struct.rb +++ b/test/rb/generation/test_struct.rb @@ -17,6 +17,9 @@ class TestStructGeneration < Test::Unit::TestCase assert_kind_of(Hash, hello.complex) assert_equal(hello.complex, { 6243 => 632, 2355 => 532, 23 => 532}) + + bool_passer = TestNamespace::BoolPasser.new(:value => false) + assert_equal false, bool_passer.value end def test_goodbyez diff --git a/test/rb/integration/accelerated_buffered_client.rb b/test/rb/integration/accelerated_buffered_client.rb new file mode 100644 index 000000000..a27787cef --- /dev/null +++ b/test/rb/integration/accelerated_buffered_client.rb @@ -0,0 +1,145 @@ +require File.join(File.dirname(__FILE__), '../test_helper') + +require 'thrift' +require 'thrift/protocol/binaryprotocolaccelerated' +require 'ThriftTest' + +class AcceleratedBufferedClientTest < Test::Unit::TestCase + def setup + unless @socket + @socket = Thrift::Socket.new('localhost', 9090) + @protocol = Thrift::BinaryProtocolAccelerated.new(Thrift::BufferedTransport.new(@socket)) + @client = Thrift::Test::ThriftTest::Client.new(@protocol) + @socket.open + end + end + + def test_string + assert_equal(@client.testString('string'), 'string') + end + + def test_byte + val = 8 + assert_equal(@client.testByte(val), val) + assert_equal(@client.testByte(-val), -val) + end + + def test_i32 + val = 32 + assert_equal(@client.testI32(val), val) + assert_equal(@client.testI32(-val), -val) + end + + def test_i64 + val = 64 + assert_equal(@client.testI64(val), val) + assert_equal(@client.testI64(-val), -val) + end + + def test_double + val = 3.14 + assert_equal(@client.testDouble(val), val) + assert_equal(@client.testDouble(-val), -val) + assert_kind_of(Float, @client.testDouble(val)) + end + + def test_map + val = {1 => 1, 2 => 2, 3 => 3} + assert_equal(@client.testMap(val), val) + assert_kind_of(Hash, @client.testMap(val)) + end + + def test_list + val = [1,2,3,4,5] + assert_equal(@client.testList(val), val) + assert_kind_of(Array, @client.testList(val)) + end + + def test_enum + val = Thrift::Test::Numberz::SIX + ret = @client.testEnum(val) + + assert_equal(ret, 6) + assert_kind_of(Fixnum, ret) + end + + def test_typedef + #UserId testTypedef(1: UserId thing), + true + end + + def test_set + val = Set.new([1,2,3]) + assert_equal(@client.testSet(val), val) + assert_kind_of(Set, @client.testSet(val)) + end + + def get_struct + Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 }) + end + + def test_struct + ret = @client.testStruct(get_struct) + + assert_nil(ret.byte_thing, nil) + assert_nil(ret.i64_thing, nil) + assert_equal(ret.string_thing, 'hi!') + assert_equal(ret.i32_thing, 4) + assert_kind_of(Thrift::Test::Xtruct, ret) + end + + def test_nest + struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10}) + + ret = @client.testNest(struct2) + + assert_nil(ret.struct_thing.byte_thing, nil) + assert_nil(ret.struct_thing.i64_thing, nil) + assert_equal(ret.struct_thing.string_thing, 'hi!') + assert_equal(ret.struct_thing.i32_thing, 4) + assert_equal(ret.i32_thing, 10) + + assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing) + assert_kind_of(Thrift::Test::Xtruct2, ret) + end + + def test_insane + insane = Thrift::Test::Insanity.new({ + 'userMap' => { Thrift::Test::Numberz::ONE => 44 }, + 'xtructs' => [get_struct, + Thrift::Test::Xtruct.new({ + 'string_thing' => 'hi again', + 'i32_thing' => 12 + }) + ] + }) + + ret = @client.testInsanity(insane) + + assert_not_nil(ret[44]) + assert_not_nil(ret[44][1]) + + struct = ret[44][1] + + assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44) + assert_equal(struct.xtructs[1].string_thing, 'hi again') + assert_equal(struct.xtructs[1].i32_thing, 12) + + assert_kind_of(Hash, struct.userMap) + assert_kind_of(Array, struct.xtructs) + assert_kind_of(Thrift::Test::Insanity, struct) + end + + def test_map_map + ret = @client.testMapMap(4) + assert_kind_of(Hash, ret) + assert_equal(ret, { 4 => { 4 => 4}}) + end + + def test_exception + assert_raise Thrift::Test::Xception do + @client.testException('foo') + end + end +end + diff --git a/test/rb/integration/accelerated_buffered_server.rb b/test/rb/integration/accelerated_buffered_server.rb new file mode 100644 index 000000000..d505e9f01 --- /dev/null +++ b/test/rb/integration/accelerated_buffered_server.rb @@ -0,0 +1,47 @@ +$:.push File.dirname(__FILE__) + '/../gen-rb' +$:.push File.join(File.dirname(__FILE__), '../../../lib/rb/lib') +$:.push File.join(File.dirname(__FILE__), '../../../lib/rb/ext') + +require 'thrift' +require 'thrift/protocol/binaryprotocolaccelerated' +require 'ThriftTest' + +class SimpleHandler + [:testString, :testByte, :testI32, :testI64, :testDouble, + :testStruct, :testMap, :testSet, :testList, :testNest, + :testEnum, :testTypedef].each do |meth| + + define_method(meth) do |thing| + thing + end + + end + + def testInsanity(thing) + num, uid = thing.userMap.find { true } + return {uid => {num => thing}} + end + + def testMapMap(thing) + return {thing => {thing => thing}} + end + + def testEnum(thing) + return thing + end + + def testTypedef(thing) + return thing + end + + def testException(thing) + raise Thrift::Test::Xception, :message => 'error' + end +end + +@handler = SimpleHandler.new +@processor = Thrift::Test::ThriftTest::Processor.new(@handler) +@transport = Thrift::ServerSocket.new(9090) +@server = Thrift::ThreadedServer.new(@processor, @transport, Thrift::BufferedTransportFactory.new, Thrift::BinaryProtocolAcceleratedFactory.new) + +@server.serve diff --git a/test/rb/integration/buffered_client.rb b/test/rb/integration/buffered_client.rb new file mode 100644 index 000000000..3548b18e4 --- /dev/null +++ b/test/rb/integration/buffered_client.rb @@ -0,0 +1,145 @@ +require File.join(File.dirname(__FILE__), '../test_helper') + +require 'thrift' +require 'thrift/protocol/binaryprotocol' +require 'ThriftTest' + +class BufferedClientTest < Test::Unit::TestCase + def setup + unless @socket + @socket = Thrift::Socket.new('localhost', 9090) + @protocol = Thrift::BinaryProtocol.new(Thrift::BufferedTransport.new(@socket)) + @client = Thrift::Test::ThriftTest::Client.new(@protocol) + @socket.open + end + end + + def test_string + assert_equal(@client.testString('string'), 'string') + end + + def test_byte + val = 8 + assert_equal(@client.testByte(val), val) + assert_equal(@client.testByte(-val), -val) + end + + def test_i32 + val = 32 + assert_equal(@client.testI32(val), val) + assert_equal(@client.testI32(-val), -val) + end + + def test_i64 + val = 64 + assert_equal(@client.testI64(val), val) + assert_equal(@client.testI64(-val), -val) + end + + def test_double + val = 3.14 + assert_equal(@client.testDouble(val), val) + assert_equal(@client.testDouble(-val), -val) + assert_kind_of(Float, @client.testDouble(val)) + end + + def test_map + val = {1 => 1, 2 => 2, 3 => 3} + assert_equal(@client.testMap(val), val) + assert_kind_of(Hash, @client.testMap(val)) + end + + def test_list + val = [1,2,3,4,5] + assert_equal(@client.testList(val), val) + assert_kind_of(Array, @client.testList(val)) + end + + def test_enum + val = Thrift::Test::Numberz::SIX + ret = @client.testEnum(val) + + assert_equal(ret, 6) + assert_kind_of(Fixnum, ret) + end + + def test_typedef + #UserId testTypedef(1: UserId thing), + true + end + + def test_set + val = Set.new([1,2,3]) + assert_equal(@client.testSet(val), val) + assert_kind_of(Set, @client.testSet(val)) + end + + def get_struct + Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 }) + end + + def test_struct + ret = @client.testStruct(get_struct) + + assert_nil(ret.byte_thing, nil) + assert_nil(ret.i64_thing, nil) + assert_equal(ret.string_thing, 'hi!') + assert_equal(ret.i32_thing, 4) + assert_kind_of(Thrift::Test::Xtruct, ret) + end + + def test_nest + struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10}) + + ret = @client.testNest(struct2) + + assert_nil(ret.struct_thing.byte_thing, nil) + assert_nil(ret.struct_thing.i64_thing, nil) + assert_equal(ret.struct_thing.string_thing, 'hi!') + assert_equal(ret.struct_thing.i32_thing, 4) + assert_equal(ret.i32_thing, 10) + + assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing) + assert_kind_of(Thrift::Test::Xtruct2, ret) + end + + def test_insane + insane = Thrift::Test::Insanity.new({ + 'userMap' => { Thrift::Test::Numberz::ONE => 44 }, + 'xtructs' => [get_struct, + Thrift::Test::Xtruct.new({ + 'string_thing' => 'hi again', + 'i32_thing' => 12 + }) + ] + }) + + ret = @client.testInsanity(insane) + + assert_not_nil(ret[44]) + assert_not_nil(ret[44][1]) + + struct = ret[44][1] + + assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44) + assert_equal(struct.xtructs[1].string_thing, 'hi again') + assert_equal(struct.xtructs[1].i32_thing, 12) + + assert_kind_of(Hash, struct.userMap) + assert_kind_of(Array, struct.xtructs) + assert_kind_of(Thrift::Test::Insanity, struct) + end + + def test_map_map + ret = @client.testMapMap(4) + assert_kind_of(Hash, ret) + assert_equal(ret, { 4 => { 4 => 4}}) + end + + def test_exception + assert_raise Thrift::Test::Xception do + @client.testException('foo') + end + end +end + diff --git a/test/rb/integration/simple_client.rb b/test/rb/integration/simple_client.rb new file mode 100644 index 000000000..fe2ed8219 --- /dev/null +++ b/test/rb/integration/simple_client.rb @@ -0,0 +1,145 @@ +require File.join(File.dirname(__FILE__), '../test_helper') + +require 'thrift' +require 'thrift/protocol/binaryprotocol' +require 'ThriftTest' + +class SimpleClientTest < Test::Unit::TestCase + def setup + unless @socket + @socket = Thrift::Socket.new('localhost', 9090) + @protocol = Thrift::BinaryProtocol.new(@socket) + @client = Thrift::Test::ThriftTest::Client.new(@protocol) + @socket.open + end + end + + def test_string + assert_equal(@client.testString('string'), 'string') + end + + def test_byte + val = 8 + assert_equal(@client.testByte(val), val) + assert_equal(@client.testByte(-val), -val) + end + + def test_i32 + val = 32 + assert_equal(@client.testI32(val), val) + assert_equal(@client.testI32(-val), -val) + end + + def test_i64 + val = 64 + assert_equal(@client.testI64(val), val) + assert_equal(@client.testI64(-val), -val) + end + + def test_double + val = 3.14 + assert_equal(@client.testDouble(val), val) + assert_equal(@client.testDouble(-val), -val) + assert_kind_of(Float, @client.testDouble(val)) + end + + def test_map + val = {1 => 1, 2 => 2, 3 => 3} + assert_equal(@client.testMap(val), val) + assert_kind_of(Hash, @client.testMap(val)) + end + + def test_list + val = [1,2,3,4,5] + assert_equal(@client.testList(val), val) + assert_kind_of(Array, @client.testList(val)) + end + + def test_enum + val = Thrift::Test::Numberz::SIX + ret = @client.testEnum(val) + + assert_equal(ret, 6) + assert_kind_of(Fixnum, ret) + end + + def test_typedef + #UserId testTypedef(1: UserId thing), + true + end + + def test_set + val = Set.new([1,2,3]) + assert_equal(@client.testSet(val), val) + assert_kind_of(Set, @client.testSet(val)) + end + + def get_struct + Thrift::Test::Xtruct.new({'string_thing' => 'hi!', 'i32_thing' => 4 }) + end + + def test_struct + ret = @client.testStruct(get_struct) + + assert_nil(ret.byte_thing, nil) + assert_nil(ret.i64_thing, nil) + assert_equal(ret.string_thing, 'hi!') + assert_equal(ret.i32_thing, 4) + assert_kind_of(Thrift::Test::Xtruct, ret) + end + + def test_nest + struct2 = Thrift::Test::Xtruct2.new({'struct_thing' => get_struct, 'i32_thing' => 10}) + + ret = @client.testNest(struct2) + + assert_nil(ret.struct_thing.byte_thing, nil) + assert_nil(ret.struct_thing.i64_thing, nil) + assert_equal(ret.struct_thing.string_thing, 'hi!') + assert_equal(ret.struct_thing.i32_thing, 4) + assert_equal(ret.i32_thing, 10) + + assert_kind_of(Thrift::Test::Xtruct, ret.struct_thing) + assert_kind_of(Thrift::Test::Xtruct2, ret) + end + + def test_insane + insane = Thrift::Test::Insanity.new({ + 'userMap' => { Thrift::Test::Numberz::ONE => 44 }, + 'xtructs' => [get_struct, + Thrift::Test::Xtruct.new({ + 'string_thing' => 'hi again', + 'i32_thing' => 12 + }) + ] + }) + + ret = @client.testInsanity(insane) + + assert_not_nil(ret[44]) + assert_not_nil(ret[44][1]) + + struct = ret[44][1] + + assert_equal(struct.userMap[Thrift::Test::Numberz::ONE], 44) + assert_equal(struct.xtructs[1].string_thing, 'hi again') + assert_equal(struct.xtructs[1].i32_thing, 12) + + assert_kind_of(Hash, struct.userMap) + assert_kind_of(Array, struct.xtructs) + assert_kind_of(Thrift::Test::Insanity, struct) + end + + def test_map_map + ret = @client.testMapMap(4) + assert_kind_of(Hash, ret) + assert_equal(ret, { 4 => { 4 => 4}}) + end + + def test_exception + assert_raise Thrift::Test::Xception do + @client.testException('foo') + end + end +end + diff --git a/test/rb/integration/simple_server.rb b/test/rb/integration/simple_server.rb new file mode 100644 index 000000000..6b029ba3e --- /dev/null +++ b/test/rb/integration/simple_server.rb @@ -0,0 +1,46 @@ +$:.push File.dirname(__FILE__) + '/../gen-rb' +$:.push File.join(File.dirname(__FILE__), '../../../lib/rb/lib') + +require 'thrift' +require 'thrift/protocol/binaryprotocol' +require 'ThriftTest' + +class SimpleHandler + [:testString, :testByte, :testI32, :testI64, :testDouble, + :testStruct, :testMap, :testSet, :testList, :testNest, + :testEnum, :testTypedef].each do |meth| + + define_method(meth) do |thing| + thing + end + + end + + def testInsanity(thing) + num, uid = thing.userMap.find { true } + return {uid => {num => thing}} + end + + def testMapMap(thing) + return {thing => {thing => thing}} + end + + def testEnum(thing) + return thing + end + + def testTypedef(thing) + return thing + end + + def testException(thing) + raise Thrift::Test::Xception, :message => 'error' + end +end + +@handler = SimpleHandler.new +@processor = Thrift::Test::ThriftTest::Processor.new(@handler) +@transport = Thrift::ServerSocket.new(9090) +@server = Thrift::ThreadedServer.new(@processor, @transport) + +@server.serve diff --git a/test/rb/test_helper.rb b/test/rb/test_helper.rb index 125f52f8d..51f72b162 100644 --- a/test/rb/test_helper.rb +++ b/test/rb/test_helper.rb @@ -1,4 +1,16 @@ $:.unshift File.dirname(__FILE__) + '/gen-rb' $:.unshift File.join(File.dirname(__FILE__), '../../lib/rb/lib') +$:.unshift File.join(File.dirname(__FILE__), '../../lib/rb/ext') require 'test/unit' + +module Thrift + module Struct + def ==(other) + return false unless other.is_a? self.class + self.class.const_get(:FIELDS).collect {|fid, data| data[:name] }.all? do |field| + send(field) == other.send(field) + end + end + end +end diff --git a/test/rb/test_suite.rb b/test/rb/test_suite.rb index af686a82d..361196337 100644 --- a/test/rb/test_suite.rb +++ b/test/rb/test_suite.rb @@ -1 +1 @@ -Dir["{core,generation,integration}/**/*.rb"].each {|f| require f }
\ No newline at end of file +Dir["{core,generation}/**/*.rb"].each {|f| require f }
\ No newline at end of file |