Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame^] | 1 | require File.dirname(__FILE__) + '/spec_helper' |
| 2 | require "thrift/protocol/compact_protocol" |
| 3 | |
| 4 | describe Thrift::CompactProtocol do |
| 5 | TESTS = { |
| 6 | :byte => (-127..127).to_a, |
| 7 | :i16 => (0..14).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort, |
| 8 | :i32 => (0..30).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort, |
| 9 | :i64 => (0..62).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort, |
| 10 | :string => ["", "1", "short", "fourteen123456", "fifteen12345678", "1" * 127, "1" * 3000], |
| 11 | :binary => ["", "\001", "\001" * 5, "\001" * 14, "\001" * 15, "\001" * 127, "\001" * 3000], |
| 12 | :double => [0.0, 1.0, -1.0, 1.1, -1.1, 10000000.1, 1.0/0.0, -1.0/0.0], |
| 13 | :bool => [true, false] |
| 14 | } |
| 15 | |
| 16 | it "should encode and decode naked primitives correctly" do |
| 17 | TESTS.each_pair do |primitive_type, test_values| |
| 18 | test_values.each do |value| |
| 19 | # puts "testing #{value}" if primitive_type == :i64 |
| 20 | trans = Thrift::MemoryBuffer.new |
| 21 | proto = Thrift::CompactProtocol.new(trans) |
| 22 | |
| 23 | proto.send(writer(primitive_type), value) |
| 24 | # puts "buf: #{trans.inspect_buffer}" if primitive_type == :i64 |
| 25 | read_back = proto.send(reader(primitive_type)) |
| 26 | read_back.should == value |
| 27 | end |
| 28 | end |
| 29 | end |
| 30 | |
| 31 | it "should encode and decode primitives in fields correctly" do |
| 32 | TESTS.each_pair do |primitive_type, test_values| |
| 33 | final_primitive_type = primitive_type == :binary ? :string : primitive_type |
| 34 | thrift_type = Thrift::Types.const_get(final_primitive_type.to_s.upcase) |
| 35 | # puts primitive_type |
| 36 | test_values.each do |value| |
| 37 | trans = Thrift::MemoryBuffer.new |
| 38 | proto = Thrift::CompactProtocol.new(trans) |
| 39 | |
| 40 | proto.write_field_begin(nil, thrift_type, 15) |
| 41 | proto.send(writer(primitive_type), value) |
| 42 | proto.write_field_end |
| 43 | |
| 44 | proto = Thrift::CompactProtocol.new(trans) |
| 45 | name, type, id = proto.read_field_begin |
| 46 | type.should == thrift_type |
| 47 | id.should == 15 |
| 48 | read_back = proto.send(reader(primitive_type)) |
| 49 | read_back.should == value |
| 50 | proto.read_field_end |
| 51 | end |
| 52 | end |
| 53 | end |
| 54 | |
| 55 | it "should encode and decode a monster struct correctly" do |
| 56 | trans = Thrift::MemoryBuffer.new |
| 57 | proto = Thrift::CompactProtocol.new(trans) |
| 58 | |
| 59 | struct = CompactProtoTestStruct.new |
| 60 | # sets and maps don't hash well... not sure what to do here. |
| 61 | struct.set_byte_map = nil |
| 62 | struct.map_byte_map = nil |
| 63 | struct.write(proto) |
| 64 | |
| 65 | # puts trans.inspect |
| 66 | |
| 67 | struct2 = CompactProtoTestStruct.new |
| 68 | struct2.instance_variables.each do |ivar| |
| 69 | struct2.instance_variable_set(ivar, nil) |
| 70 | end |
| 71 | |
| 72 | struct2.should_not == struct |
| 73 | |
| 74 | struct2.read(proto) |
| 75 | |
| 76 | struct2.should == struct |
| 77 | end |
| 78 | |
| 79 | it "should make method calls correctly" do |
| 80 | client_out_trans = Thrift::MemoryBuffer.new |
| 81 | client_out_proto = Thrift::CompactProtocol.new(client_out_trans) |
| 82 | |
| 83 | client_in_trans = Thrift::MemoryBuffer.new |
| 84 | client_in_proto = Thrift::CompactProtocol.new(client_in_trans) |
| 85 | |
| 86 | processor = Srv::Processor.new(JankyHandler.new) |
| 87 | |
| 88 | client = Srv::Client.new(client_in_proto, client_out_proto) |
| 89 | client.send_Janky(1) |
| 90 | # puts client_out_trans.inspect_buffer |
| 91 | processor.process(client_out_proto, client_in_proto) |
| 92 | client.recv_Janky.should == 2 |
| 93 | end |
| 94 | |
| 95 | class JankyHandler |
| 96 | def Janky(i32arg) |
| 97 | i32arg * 2 |
| 98 | end |
| 99 | end |
| 100 | |
| 101 | def writer(sym) |
| 102 | sym = sym == :binary ? :string : sym |
| 103 | "write_#{sym.to_s}" |
| 104 | end |
| 105 | |
| 106 | def reader(sym) |
| 107 | sym = sym == :binary ? :string : sym |
| 108 | "read_#{sym.to_s}" |
| 109 | end |
| 110 | end |