David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 20 | require File.dirname(__FILE__) + '/spec_helper' |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 21 | |
| 22 | describe Thrift::CompactProtocol do |
| 23 | TESTS = { |
| 24 | :byte => (-127..127).to_a, |
| 25 | :i16 => (0..14).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort, |
| 26 | :i32 => (0..30).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort, |
| 27 | :i64 => (0..62).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort, |
| 28 | :string => ["", "1", "short", "fourteen123456", "fifteen12345678", "1" * 127, "1" * 3000], |
| 29 | :binary => ["", "\001", "\001" * 5, "\001" * 14, "\001" * 15, "\001" * 127, "\001" * 3000], |
| 30 | :double => [0.0, 1.0, -1.0, 1.1, -1.1, 10000000.1, 1.0/0.0, -1.0/0.0], |
| 31 | :bool => [true, false] |
| 32 | } |
| 33 | |
| 34 | it "should encode and decode naked primitives correctly" do |
| 35 | TESTS.each_pair do |primitive_type, test_values| |
| 36 | test_values.each do |value| |
| 37 | # puts "testing #{value}" if primitive_type == :i64 |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 38 | trans = Thrift::MemoryBufferTransport.new |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 39 | proto = Thrift::CompactProtocol.new(trans) |
| 40 | |
| 41 | proto.send(writer(primitive_type), value) |
| 42 | # puts "buf: #{trans.inspect_buffer}" if primitive_type == :i64 |
| 43 | read_back = proto.send(reader(primitive_type)) |
| 44 | read_back.should == value |
| 45 | end |
| 46 | end |
| 47 | end |
| 48 | |
| 49 | it "should encode and decode primitives in fields correctly" do |
| 50 | TESTS.each_pair do |primitive_type, test_values| |
| 51 | final_primitive_type = primitive_type == :binary ? :string : primitive_type |
| 52 | thrift_type = Thrift::Types.const_get(final_primitive_type.to_s.upcase) |
| 53 | # puts primitive_type |
| 54 | test_values.each do |value| |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 55 | trans = Thrift::MemoryBufferTransport.new |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 56 | proto = Thrift::CompactProtocol.new(trans) |
| 57 | |
| 58 | proto.write_field_begin(nil, thrift_type, 15) |
| 59 | proto.send(writer(primitive_type), value) |
| 60 | proto.write_field_end |
| 61 | |
| 62 | proto = Thrift::CompactProtocol.new(trans) |
| 63 | name, type, id = proto.read_field_begin |
| 64 | type.should == thrift_type |
| 65 | id.should == 15 |
| 66 | read_back = proto.send(reader(primitive_type)) |
| 67 | read_back.should == value |
| 68 | proto.read_field_end |
| 69 | end |
| 70 | end |
| 71 | end |
| 72 | |
| 73 | it "should encode and decode a monster struct correctly" do |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 74 | trans = Thrift::MemoryBufferTransport.new |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 75 | proto = Thrift::CompactProtocol.new(trans) |
| 76 | |
| 77 | struct = CompactProtoTestStruct.new |
| 78 | # sets and maps don't hash well... not sure what to do here. |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 79 | struct.write(proto) |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 80 | |
Bryan Duxbury | 9e4c041 | 2009-04-07 23:45:15 +0000 | [diff] [blame] | 81 | struct2 = CompactProtoTestStruct.new |
| 82 | struct2.read(proto) |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 83 | struct2.should == struct |
| 84 | end |
| 85 | |
| 86 | it "should make method calls correctly" do |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 87 | client_out_trans = Thrift::MemoryBufferTransport.new |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 88 | client_out_proto = Thrift::CompactProtocol.new(client_out_trans) |
Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 89 | |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 90 | client_in_trans = Thrift::MemoryBufferTransport.new |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 91 | client_in_proto = Thrift::CompactProtocol.new(client_in_trans) |
Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 92 | |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 93 | processor = Srv::Processor.new(JankyHandler.new) |
Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 94 | |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 95 | client = Srv::Client.new(client_in_proto, client_out_proto) |
| 96 | client.send_Janky(1) |
| 97 | # puts client_out_trans.inspect_buffer |
| 98 | processor.process(client_out_proto, client_in_proto) |
| 99 | client.recv_Janky.should == 2 |
| 100 | end |
| 101 | |
Bryan Duxbury | 35565a4 | 2010-01-06 23:12:09 +0000 | [diff] [blame] | 102 | it "should deal with fields following fields that have non-delta ids" do |
| 103 | brcp = BreaksRubyCompactProtocol.new( |
| 104 | :field1 => "blah", |
| 105 | :field2 => BigFieldIdStruct.new( |
| 106 | :field1 => "string1", |
| 107 | :field2 => "string2"), |
| 108 | :field3 => 3) |
| 109 | ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new) |
| 110 | bytes = ser.serialize(brcp) |
| 111 | |
| 112 | deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new) |
| 113 | brcp2 = BreaksRubyCompactProtocol.new |
| 114 | deser.deserialize(brcp2, bytes) |
| 115 | brcp2.should == brcp |
| 116 | end |
| 117 | |
Bryan Duxbury | d815c21 | 2009-03-19 18:57:43 +0000 | [diff] [blame] | 118 | class JankyHandler |
| 119 | def Janky(i32arg) |
| 120 | i32arg * 2 |
| 121 | end |
| 122 | end |
| 123 | |
| 124 | def writer(sym) |
| 125 | sym = sym == :binary ? :string : sym |
| 126 | "write_#{sym.to_s}" |
| 127 | end |
| 128 | |
| 129 | def reader(sym) |
| 130 | sym = sym == :binary ? :string : sym |
| 131 | "read_#{sym.to_s}" |
| 132 | end |
| 133 | end |