thrift-0.14.0/ 0000755 0000041 0000041 00000000000 14020410666 013123 5 ustar www-data www-data thrift-0.14.0/README.md 0000644 0000041 0000041 00000002673 14020410666 014412 0 ustar www-data www-data Thrift Ruby Software Library
http://thrift.apache.org
== LICENSE:
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.
== DESCRIPTION:
Thrift is a strongly-typed language-agnostic RPC system.
This library is the ruby implementation for both clients and servers.
== INSTALL:
$ gem install thrift
== CAVEATS:
This library provides the client and server implementations of thrift.
It does not provide the compiler for the .thrift files. To compile
.thrift files into language-specific implementations, please download the full
thrift software package.
== USAGE:
This section should get written by someone with the time and inclination.
In the meantime, look at existing code, such as the benchmark or the tutorial
in the full thrift distribution.
thrift-0.14.0/spec/ 0000755 0000041 0000041 00000000000 14020410666 014055 5 ustar www-data www-data thrift-0.14.0/spec/ssl_server_socket_spec.rb 0000644 0000041 0000041 00000002175 14020410666 021160 0 ustar www-data www-data #
# 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 'spec_helper'
require File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared")
describe 'SSLServerSocket' do
describe Thrift::SSLServerSocket do
before(:each) do
@socket = Thrift::SSLServerSocket.new(1234)
end
it "should provide a reasonable to_s" do
expect(@socket.to_s).to eq("ssl(socket(:1234))")
end
end
end
thrift-0.14.0/spec/struct_spec.rb 0000644 0000041 0000041 00000032346 14020410666 016750 0 ustar www-data www-data #
# 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 'spec_helper'
describe 'Struct' do
describe Thrift::Struct do
it "should iterate over all fields properly" do
fields = {}
SpecNamespace::Foo.new.each_field { |fid,field_info| fields[fid] = field_info }
expect(fields).to eq(SpecNamespace::Foo::FIELDS)
end
it "should initialize all fields to defaults" do
validate_default_arguments(SpecNamespace::Foo.new)
end
it "should initialize all fields to defaults and accept a block argument" do
SpecNamespace::Foo.new do |f|
validate_default_arguments(f)
end
end
def validate_default_arguments(object)
expect(object.simple).to eq(53)
expect(object.words).to eq("words")
expect(object.hello).to eq(SpecNamespace::Hello.new(:greeting => 'hello, world!'))
expect(object.ints).to eq([1, 2, 2, 3])
expect(object.complex).to be_nil
expect(object.shorts).to eq(Set.new([5, 17, 239]))
end
it "should not share default values between instances" do
begin
struct = SpecNamespace::Foo.new
struct.ints << 17
expect(SpecNamespace::Foo.new.ints).to eq([1,2,2,3])
ensure
# ensure no leakage to other tests
SpecNamespace::Foo::FIELDS[4][:default] = [1,2,2,3]
end
end
it "should properly initialize boolean values" do
struct = SpecNamespace::BoolStruct.new(:yesno => false)
expect(struct.yesno).to be_falsey
end
it "should have proper == semantics" do
expect(SpecNamespace::Foo.new).not_to eq(SpecNamespace::Hello.new)
expect(SpecNamespace::Foo.new).to eq(SpecNamespace::Foo.new)
expect(SpecNamespace::Foo.new(:simple => 52)).not_to eq(SpecNamespace::Foo.new)
end
it "should print enum value names in inspect" do
expect(SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect).to eq("")
expect(SpecNamespace::StructWithEnumMap.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect).to eq("")
end
it "should pretty print binary fields" do
expect(SpecNamespace::Foo2.new(:my_binary => "\001\002\003").inspect).to eq("")
end
it "should offer field? methods" do
expect(SpecNamespace::Foo.new.opt_string?).to be_falsey
expect(SpecNamespace::Foo.new(:simple => 52).simple?).to be_truthy
expect(SpecNamespace::Foo.new(:my_bool => false).my_bool?).to be_truthy
expect(SpecNamespace::Foo.new(:my_bool => true).my_bool?).to be_truthy
end
it "should be comparable" do
s1 = SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::ONE)
s2 = SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::TWO)
expect(s1 <=> s2).to eq(-1)
expect(s2 <=> s1).to eq(1)
expect(s1 <=> s1).to eq(0)
expect(s1 <=> SpecNamespace::StructWithSomeEnum.new()).to eq(-1)
end
it "should read itself off the wire" do
struct = SpecNamespace::Foo.new
prot = Thrift::BaseProtocol.new(double("transport"))
expect(prot).to receive(:read_struct_begin).twice
expect(prot).to receive(:read_struct_end).twice
expect(prot).to receive(:read_field_begin).and_return(
['complex', Thrift::Types::MAP, 5], # Foo
['words', Thrift::Types::STRING, 2], # Foo
['hello', Thrift::Types::STRUCT, 3], # Foo
['greeting', Thrift::Types::STRING, 1], # Hello
[nil, Thrift::Types::STOP, 0], # Hello
['simple', Thrift::Types::I32, 1], # Foo
['ints', Thrift::Types::LIST, 4], # Foo
['shorts', Thrift::Types::SET, 6], # Foo
[nil, Thrift::Types::STOP, 0] # Hello
)
expect(prot).to receive(:read_field_end).exactly(7).times
expect(prot).to receive(:read_map_begin).and_return(
[Thrift::Types::I32, Thrift::Types::MAP, 2], # complex
[Thrift::Types::STRING, Thrift::Types::DOUBLE, 2], # complex/1/value
[Thrift::Types::STRING, Thrift::Types::DOUBLE, 1] # complex/2/value
)
expect(prot).to receive(:read_map_end).exactly(3).times
expect(prot).to receive(:read_list_begin).and_return([Thrift::Types::I32, 4])
expect(prot).to receive(:read_list_end)
expect(prot).to receive(:read_set_begin).and_return([Thrift::Types::I16, 2])
expect(prot).to receive(:read_set_end)
expect(prot).to receive(:read_i32).and_return(
1, 14, # complex keys
42, # simple
4, 23, 4, 29 # ints
)
expect(prot).to receive(:read_string).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
expect(prot).to receive(:read_double).and_return(Math::PI, Math::E, 4.669201609)
expect(prot).to receive(:read_i16).and_return(2, 3)
expect(prot).not_to receive(:skip)
struct.read(prot)
expect(struct.simple).to eq(42)
expect(struct.complex).to eq({1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}})
expect(struct.hello).to eq(SpecNamespace::Hello.new(:greeting => "what's up?"))
expect(struct.words).to eq("apple banana")
expect(struct.ints).to eq([4, 23, 4, 29])
expect(struct.shorts).to eq(Set.new([3, 2]))
end
it "should serialize false boolean fields correctly" do
b = SpecNamespace::BoolStruct.new(:yesno => false)
prot = Thrift::BinaryProtocol.new(Thrift::MemoryBufferTransport.new)
expect(prot).to receive(:write_bool).with(false)
b.write(prot)
end
it "should skip unexpected fields in structs and use default values" do
struct = SpecNamespace::Foo.new
prot = Thrift::BaseProtocol.new(double("transport"))
expect(prot).to receive(:read_struct_begin)
expect(prot).to receive(:read_struct_end)
expect(prot).to receive(:read_field_begin).and_return(
['simple', Thrift::Types::I32, 1],
['complex', Thrift::Types::STRUCT, 5],
['thinz', Thrift::Types::MAP, 7],
['foobar', Thrift::Types::I32, 3],
['words', Thrift::Types::STRING, 2],
[nil, Thrift::Types::STOP, 0]
)
expect(prot).to receive(:read_field_end).exactly(5).times
expect(prot).to receive(:read_i32).and_return(42)
expect(prot).to receive(:read_string).and_return("foobar")
expect(prot).to receive(:skip).with(Thrift::Types::STRUCT)
expect(prot).to receive(:skip).with(Thrift::Types::MAP)
# prot.should_receive(:read_map_begin).and_return([Thrift::Types::I32, Thrift::Types::I32, 0])
# prot.should_receive(:read_map_end)
expect(prot).to receive(:skip).with(Thrift::Types::I32)
struct.read(prot)
expect(struct.simple).to eq(42)
expect(struct.complex).to be_nil
expect(struct.words).to eq("foobar")
expect(struct.hello).to eq(SpecNamespace::Hello.new(:greeting => 'hello, world!'))
expect(struct.ints).to eq([1, 2, 2, 3])
expect(struct.shorts).to eq(Set.new([5, 17, 239]))
end
it "should write itself to the wire" do
prot = Thrift::BaseProtocol.new(double("transport")) #mock("Protocol")
expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Foo")
expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Hello")
expect(prot).to receive(:write_struct_end).twice
expect(prot).to receive(:write_field_begin).with('ints', Thrift::Types::LIST, 4)
expect(prot).to receive(:write_i32).with(1)
expect(prot).to receive(:write_i32).with(2).twice
expect(prot).to receive(:write_i32).with(3)
expect(prot).to receive(:write_field_begin).with('complex', Thrift::Types::MAP, 5)
expect(prot).to receive(:write_i32).with(5)
expect(prot).to receive(:write_string).with('foo')
expect(prot).to receive(:write_double).with(1.23)
expect(prot).to receive(:write_field_begin).with('shorts', Thrift::Types::SET, 6)
expect(prot).to receive(:write_i16).with(5)
expect(prot).to receive(:write_i16).with(17)
expect(prot).to receive(:write_i16).with(239)
expect(prot).to receive(:write_field_stop).twice
expect(prot).to receive(:write_field_end).exactly(6).times
expect(prot).to receive(:write_field_begin).with('simple', Thrift::Types::I32, 1)
expect(prot).to receive(:write_i32).with(53)
expect(prot).to receive(:write_field_begin).with('hello', Thrift::Types::STRUCT, 3)
expect(prot).to receive(:write_field_begin).with('greeting', Thrift::Types::STRING, 1)
expect(prot).to receive(:write_string).with('hello, world!')
expect(prot).to receive(:write_map_begin).with(Thrift::Types::I32, Thrift::Types::MAP, 1)
expect(prot).to receive(:write_map_begin).with(Thrift::Types::STRING, Thrift::Types::DOUBLE, 1)
expect(prot).to receive(:write_map_end).twice
expect(prot).to receive(:write_list_begin).with(Thrift::Types::I32, 4)
expect(prot).to receive(:write_list_end)
expect(prot).to receive(:write_set_begin).with(Thrift::Types::I16, 3)
expect(prot).to receive(:write_set_end)
struct = SpecNamespace::Foo.new
struct.words = nil
struct.complex = {5 => {"foo" => 1.23}}
struct.write(prot)
end
it "should raise an exception if presented with an unknown container" do
# yeah this is silly, but I'm going for code coverage here
struct = SpecNamespace::Foo.new
expect { struct.send :write_container, nil, nil, {:type => "foo"} }.to raise_error(StandardError, "Not a container type: foo")
end
it "should support optional type-checking in Thrift::Struct.new" do
Thrift.type_checking = true
begin
expect { SpecNamespace::Hello.new(:greeting => 3) }.to raise_error(Thrift::TypeError, /Expected Types::STRING, received (Integer|Fixnum) for field greeting/)
ensure
Thrift.type_checking = false
end
expect { SpecNamespace::Hello.new(:greeting => 3) }.not_to raise_error
end
it "should support optional type-checking in field accessors" do
Thrift.type_checking = true
begin
hello = SpecNamespace::Hello.new
expect { hello.greeting = 3 }.to raise_error(Thrift::TypeError, /Expected Types::STRING, received (Integer|Fixnum) for field greeting/)
ensure
Thrift.type_checking = false
end
expect { hello.greeting = 3 }.not_to raise_error
end
it "should raise an exception when unknown types are given to Thrift::Struct.new" do
expect { SpecNamespace::Hello.new(:fish => 'salmon') }.to raise_error(Exception, "Unknown key given to SpecNamespace::Hello.new: fish")
end
it "should support `raise Xception, 'message'` for Exception structs" do
begin
raise SpecNamespace::Xception, "something happened"
rescue Thrift::Exception => e
expect(e.message).to eq("something happened")
expect(e.code).to eq(1)
# ensure it gets serialized properly, this is the really important part
prot = Thrift::BaseProtocol.new(double("trans"))
expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Xception")
expect(prot).to receive(:write_struct_end)
expect(prot).to receive(:write_field_begin).with('message', Thrift::Types::STRING, 1)#, "something happened")
expect(prot).to receive(:write_string).with("something happened")
expect(prot).to receive(:write_field_begin).with('code', Thrift::Types::I32, 2)#, 1)
expect(prot).to receive(:write_i32).with(1)
expect(prot).to receive(:write_field_stop)
expect(prot).to receive(:write_field_end).twice
e.write(prot)
end
end
it "should support the regular initializer for exception structs" do
begin
raise SpecNamespace::Xception, :message => "something happened", :code => 5
rescue Thrift::Exception => e
expect(e.message).to eq("something happened")
expect(e.code).to eq(5)
prot = Thrift::BaseProtocol.new(double("trans"))
expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Xception")
expect(prot).to receive(:write_struct_end)
expect(prot).to receive(:write_field_begin).with('message', Thrift::Types::STRING, 1)
expect(prot).to receive(:write_string).with("something happened")
expect(prot).to receive(:write_field_begin).with('code', Thrift::Types::I32, 2)
expect(prot).to receive(:write_i32).with(5)
expect(prot).to receive(:write_field_stop)
expect(prot).to receive(:write_field_end).twice
e.write(prot)
end
end
end
end
thrift-0.14.0/spec/unix_socket_spec.rb 0000644 0000041 0000041 00000007736 14020410666 017764 0 ustar www-data www-data #
# 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 'spec_helper'
require File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared")
describe 'UNIXSocket' do
describe Thrift::UNIXSocket do
before(:each) do
@path = '/tmp/thrift_spec_socket'
@socket = Thrift::UNIXSocket.new(@path)
@handle = double("Handle", :closed? => false)
allow(@handle).to receive(:close)
allow(::UNIXSocket).to receive(:new).and_return(@handle)
end
it_should_behave_like "a socket"
it "should raise a TransportException when it cannot open a socket" do
expect(::UNIXSocket).to receive(:new).and_raise(StandardError)
expect { @socket.open }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
end
it "should accept an optional timeout" do
allow(::UNIXSocket).to receive(:new)
expect(Thrift::UNIXSocket.new(@path, 5).timeout).to eq(5)
end
it "should provide a reasonable to_s" do
allow(::UNIXSocket).to receive(:new)
expect(Thrift::UNIXSocket.new(@path).to_s).to eq("domain(#{@path})")
end
end
describe Thrift::UNIXServerSocket do
before(:each) do
@path = '/tmp/thrift_spec_socket'
@socket = Thrift::UNIXServerSocket.new(@path)
end
it "should create a handle when calling listen" do
expect(UNIXServer).to receive(:new).with(@path)
@socket.listen
end
it "should create a Thrift::UNIXSocket to wrap accepted sockets" do
handle = double("UNIXServer")
expect(UNIXServer).to receive(:new).with(@path).and_return(handle)
@socket.listen
sock = double("sock")
expect(handle).to receive(:accept).and_return(sock)
trans = double("UNIXSocket")
expect(Thrift::UNIXSocket).to receive(:new).and_return(trans)
expect(trans).to receive(:handle=).with(sock)
expect(@socket.accept).to eq(trans)
end
it "should close the handle when closed" do
handle = double("UNIXServer", :closed? => false)
expect(UNIXServer).to receive(:new).with(@path).and_return(handle)
@socket.listen
expect(handle).to receive(:close)
allow(File).to receive(:delete)
@socket.close
end
it "should delete the socket when closed" do
handle = double("UNIXServer", :closed? => false)
expect(UNIXServer).to receive(:new).with(@path).and_return(handle)
@socket.listen
allow(handle).to receive(:close)
expect(File).to receive(:delete).with(@path)
@socket.close
end
it "should return nil when accepting if there is no handle" do
expect(@socket.accept).to be_nil
end
it "should return true for closed? when appropriate" do
handle = double("UNIXServer", :closed? => false)
allow(UNIXServer).to receive(:new).and_return(handle)
allow(File).to receive(:delete)
@socket.listen
expect(@socket).not_to be_closed
allow(handle).to receive(:close)
@socket.close
expect(@socket).to be_closed
@socket.listen
expect(@socket).not_to be_closed
allow(handle).to receive(:closed?).and_return(true)
expect(@socket).to be_closed
end
it "should provide a reasonable to_s" do
expect(@socket.to_s).to eq("domain(#{@path})")
end
end
end
thrift-0.14.0/spec/union_spec.rb 0000644 0000041 0000041 00000017776 14020410666 016566 0 ustar www-data www-data #
# 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 'spec_helper'
describe 'Union' do
describe Thrift::Union do
it "should return nil value in unset union" do
union = SpecNamespace::My_union.new
expect(union.get_set_field).to eq(nil)
expect(union.get_value).to eq(nil)
end
it "should set a field and be accessible through get_value and the named field accessor" do
union = SpecNamespace::My_union.new
union.integer32 = 25
expect(union.get_set_field).to eq(:integer32)
expect(union.get_value).to eq(25)
expect(union.integer32).to eq(25)
end
it "should work correctly when instantiated with static field constructors" do
union = SpecNamespace::My_union.integer32(5)
expect(union.get_set_field).to eq(:integer32)
expect(union.integer32).to eq(5)
end
it "should raise for wrong set field" do
union = SpecNamespace::My_union.new
union.integer32 = 25
expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.")
end
it "should raise for wrong set field when hash initialized and type checking is off" do
Thrift.type_checking = false
union = SpecNamespace::My_union.new({incorrect_field: :incorrect})
expect { Thrift::Serializer.new.serialize(union) }.to raise_error(RuntimeError, "set_field is not valid for this union!")
end
it "should not be equal to nil" do
union = SpecNamespace::My_union.new
expect(union).not_to eq(nil)
end
it "should not be equal with an empty String" do
union = SpecNamespace::My_union.new
expect(union).not_to eq('')
end
it "should not equate two different unions, i32 vs. string" do
union = SpecNamespace::My_union.new(:integer32, 25)
other_union = SpecNamespace::My_union.new(:some_characters, "blah!")
expect(union).not_to eq(other_union)
end
it "should properly reset setfield and setvalue" do
union = SpecNamespace::My_union.new(:integer32, 25)
expect(union.get_set_field).to eq(:integer32)
union.some_characters = "blah!"
expect(union.get_set_field).to eq(:some_characters)
expect(union.get_value).to eq("blah!")
expect { union.integer32 }.to raise_error(RuntimeError, "integer32 is not union's set field.")
end
it "should not equate two different unions with different values" do
union = SpecNamespace::My_union.new(:integer32, 25)
other_union = SpecNamespace::My_union.new(:integer32, 400)
expect(union).not_to eq(other_union)
end
it "should not equate two different unions with different fields" do
union = SpecNamespace::My_union.new(:integer32, 25)
other_union = SpecNamespace::My_union.new(:other_i32, 25)
expect(union).not_to eq(other_union)
end
it "should inspect properly" do
union = SpecNamespace::My_union.new(:integer32, 25)
expect(union.inspect).to eq("")
end
it "should not allow setting with instance_variable_set" do
union = SpecNamespace::My_union.new(:integer32, 27)
union.instance_variable_set(:@some_characters, "hallo!")
expect(union.get_set_field).to eq(:integer32)
expect(union.get_value).to eq(27)
expect { union.some_characters }.to raise_error(RuntimeError, "some_characters is not union's set field.")
end
it "should serialize to binary correctly" do
trans = Thrift::MemoryBufferTransport.new
proto = Thrift::BinaryProtocol.new(trans)
union = SpecNamespace::My_union.new(:integer32, 25)
union.write(proto)
other_union = SpecNamespace::My_union.new(:integer32, 25)
other_union.read(proto)
expect(other_union).to eq(union)
end
it "should serialize to json correctly" do
trans = Thrift::MemoryBufferTransport.new
proto = Thrift::JsonProtocol.new(trans)
union = SpecNamespace::My_union.new(:integer32, 25)
union.write(proto)
other_union = SpecNamespace::My_union.new(:integer32, 25)
other_union.read(proto)
expect(other_union).to eq(union)
end
it "should raise when validating unset union" do
union = SpecNamespace::My_union.new
expect { union.validate }.to raise_error(StandardError, "Union fields are not set.")
other_union = SpecNamespace::My_union.new(:integer32, 1)
expect { other_union.validate }.not_to raise_error
end
it "should validate an enum field properly" do
union = SpecNamespace::TestUnion.new(:enum_field, 3)
expect(union.get_set_field).to eq(:enum_field)
expect { union.validate }.to raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!")
other_union = SpecNamespace::TestUnion.new(:enum_field, 1)
expect { other_union.validate }.not_to raise_error
end
it "should properly serialize and match structs with a union" do
union = SpecNamespace::My_union.new(:integer32, 26)
swu = SpecNamespace::Struct_with_union.new(:fun_union => union)
trans = Thrift::MemoryBufferTransport.new
proto = Thrift::CompactProtocol.new(trans)
swu.write(proto)
other_union = SpecNamespace::My_union.new(:some_characters, "hello there")
swu2 = SpecNamespace::Struct_with_union.new(:fun_union => other_union)
expect(swu2).not_to eq(swu)
swu2.read(proto)
expect(swu2).to eq(swu)
end
it "should support old style constructor" do
union = SpecNamespace::My_union.new(:integer32 => 26)
expect(union.get_set_field).to eq(:integer32)
expect(union.get_value).to eq(26)
end
it "should not throw an error when inspected and unset" do
expect{SpecNamespace::TestUnion.new().inspect}.not_to raise_error
end
it "should print enum value name when inspected" do
expect(SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect).to eq("")
expect(SpecNamespace::My_union.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect).to eq("")
end
it "should offer field? methods" do
expect(SpecNamespace::My_union.new.some_enum?).to be_falsey
expect(SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).some_enum?).to be_truthy
expect(SpecNamespace::My_union.new(:im_true => false).im_true?).to be_truthy
expect(SpecNamespace::My_union.new(:im_true => true).im_true?).to be_truthy
end
it "should pretty print binary fields" do
expect(SpecNamespace::TestUnion.new(:binary_field => "\001\002\003").inspect).to eq("")
end
it "should be comparable" do
relationships = [
[0, -1, -1, -1],
[1, 0, -1, -1],
[1, 1, 0, -1],
[1, 1, 1, 0]]
objs = [
SpecNamespace::TestUnion.new(:string_field, "blah"),
SpecNamespace::TestUnion.new(:string_field, "blahblah"),
SpecNamespace::TestUnion.new(:i32_field, 1),
SpecNamespace::TestUnion.new()]
for y in 0..3
for x in 0..3
# puts "#{objs[y].inspect} <=> #{objs[x].inspect} should == #{relationships[y][x]}"
expect(objs[y] <=> objs[x]).to eq(relationships[y][x])
end
end
end
end
end
thrift-0.14.0/spec/ssl_socket_spec.rb 0000644 0000041 0000041 00000006467 14020410666 017602 0 ustar www-data www-data #
# 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 'spec_helper'
require File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared")
describe 'SSLSocket' do
describe Thrift::SSLSocket do
before(:each) do
@context = OpenSSL::SSL::SSLContext.new
@socket = Thrift::SSLSocket.new
@simple_socket_handle = double("Handle", :closed? => false)
allow(@simple_socket_handle).to receive(:close)
allow(@simple_socket_handle).to receive(:connect_nonblock)
allow(@simple_socket_handle).to receive(:setsockopt)
@handle = double(double("SSLHandle", :connect_nonblock => true, :post_connection_check => true), :closed? => false)
allow(@handle).to receive(:connect_nonblock)
allow(@handle).to receive(:close)
allow(@handle).to receive(:post_connection_check)
allow(::Socket).to receive(:new).and_return(@simple_socket_handle)
allow(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(@handle)
end
it_should_behave_like "a socket"
it "should raise a TransportException when it cannot open a ssl socket" do
expect(::Socket).to receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]])
expect { @socket.open }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
end
it "should open a ::Socket with default args" do
expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(@simple_socket_handle, nil).and_return(@handle)
expect(@handle).to receive(:post_connection_check).with('localhost')
@socket.open
end
it "should accept host/port options" do
handle = double("Handle", :connect_nonblock => true, :setsockopt => nil)
allow(::Socket).to receive(:new).and_return(handle)
expect(::Socket).to receive(:getaddrinfo).with("my.domain", 1234, nil, ::Socket::SOCK_STREAM).and_return([[]])
expect(::Socket).to receive(:sockaddr_in)
expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(handle, nil).and_return(@handle)
expect(@handle).to receive(:post_connection_check).with('my.domain')
Thrift::SSLSocket.new('my.domain', 1234, 6000, nil).open
end
it "should accept an optional timeout" do
expect(Thrift::SSLSocket.new('localhost', 8080, 5).timeout).to eq(5)
end
it "should accept an optional context" do
expect(Thrift::SSLSocket.new('localhost', 8080, 5, @context).ssl_context).to eq(@context)
end
it "should provide a reasonable to_s" do
expect(Thrift::SSLSocket.new('myhost', 8090).to_s).to eq("ssl(socket(myhost:8090))")
end
end
end
thrift-0.14.0/spec/binary_protocol_accelerated_spec.rb 0000644 0000041 0000041 00000003320 14020410666 023133 0 ustar www-data www-data #
# 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 'spec_helper'
require File.expand_path("#{File.dirname(__FILE__)}/binary_protocol_spec_shared")
if defined? Thrift::BinaryProtocolAccelerated
describe 'BinaryProtocolAccelerated' do
# since BinaryProtocolAccelerated should be directly equivalent to
# BinaryProtocol, we don't need any custom specs!
it_should_behave_like 'a binary protocol'
def protocol_class
Thrift::BinaryProtocolAccelerated
end
describe Thrift::BinaryProtocolAcceleratedFactory do
it "should create a BinaryProtocolAccelerated" do
expect(Thrift::BinaryProtocolAcceleratedFactory.new.get_protocol(double("MockTransport"))).to be_instance_of(Thrift::BinaryProtocolAccelerated)
end
it "should provide a reasonable to_s" do
expect(Thrift::BinaryProtocolAcceleratedFactory.new.to_s).to eq("binary-accel")
end
end
end
else
puts "skipping BinaryProtocolAccelerated spec because it is not defined."
end
thrift-0.14.0/spec/socket_spec.rb 0000644 0000041 0000041 00000005410 14020410666 016704 0 ustar www-data www-data #
# 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 'spec_helper'
require File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared")
describe 'Socket' do
describe Thrift::Socket do
before(:each) do
@socket = Thrift::Socket.new
@handle = double("Handle", :closed? => false)
allow(@handle).to receive(:close)
allow(@handle).to receive(:connect_nonblock)
allow(@handle).to receive(:setsockopt)
allow(::Socket).to receive(:new).and_return(@handle)
end
it_should_behave_like "a socket"
it "should raise a TransportException when it cannot open a socket" do
expect(::Socket).to receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]])
expect { @socket.open }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
end
it "should open a ::Socket with default args" do
expect(::Socket).to receive(:new).and_return(double("Handle", :connect_nonblock => true, :setsockopt => nil))
expect(::Socket).to receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]])
expect(::Socket).to receive(:sockaddr_in)
@socket.to_s == "socket(localhost:9090)"
@socket.open
end
it "should accept host/port options" do
expect(::Socket).to receive(:new).and_return(double("Handle", :connect_nonblock => true, :setsockopt => nil))
expect(::Socket).to receive(:getaddrinfo).with("my.domain", 1234, nil, ::Socket::SOCK_STREAM).and_return([[]])
expect(::Socket).to receive(:sockaddr_in)
@socket = Thrift::Socket.new('my.domain', 1234).open
@socket.to_s == "socket(my.domain:1234)"
end
it "should accept an optional timeout" do
allow(::Socket).to receive(:new)
expect(Thrift::Socket.new('localhost', 8080, 5).timeout).to eq(5)
end
it "should provide a reasonable to_s" do
allow(::Socket).to receive(:new)
expect(Thrift::Socket.new('myhost', 8090).to_s).to eq("socket(myhost:8090)")
end
end
end
thrift-0.14.0/spec/BaseService.thrift 0000644 0000041 0000041 00000001623 14020410666 017474 0 ustar www-data www-data # 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.
#
namespace rb Base
struct Hello {
1: string greeting = "hello world"
}
service BaseService {
Hello greeting(1:bool english)
}
thrift-0.14.0/spec/json_protocol_spec.rb 0000644 0000041 0000041 00000044542 14020410666 020317 0 ustar www-data www-data # encoding: UTF-8
#
# 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 'spec_helper'
describe 'JsonProtocol' do
describe Thrift::JsonProtocol do
before(:each) do
@trans = Thrift::MemoryBufferTransport.new
@prot = Thrift::JsonProtocol.new(@trans)
end
it "should write json escaped char" do
@prot.write_json_escape_char("\n")
expect(@trans.read(@trans.available)).to eq('\u000a')
@prot.write_json_escape_char(" ")
expect(@trans.read(@trans.available)).to eq('\u0020')
end
it "should write json char" do
@prot.write_json_char("\n")
expect(@trans.read(@trans.available)).to eq('\\n')
@prot.write_json_char(" ")
expect(@trans.read(@trans.available)).to eq(' ')
@prot.write_json_char("\\")
expect(@trans.read(@trans.available)).to eq("\\\\")
@prot.write_json_char("@")
expect(@trans.read(@trans.available)).to eq('@')
end
it "should write json string" do
@prot.write_json_string("this is a \\ json\nstring")
expect(@trans.read(@trans.available)).to eq("\"this is a \\\\ json\\nstring\"")
end
it "should write json base64" do
@prot.write_json_base64("this is a base64 string")
expect(@trans.read(@trans.available)).to eq("\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\"")
end
it "should write json integer" do
@prot.write_json_integer(45)
expect(@trans.read(@trans.available)).to eq("45")
@prot.write_json_integer(33000)
expect(@trans.read(@trans.available)).to eq("33000")
@prot.write_json_integer(3000000000)
expect(@trans.read(@trans.available)).to eq("3000000000")
@prot.write_json_integer(6000000000)
expect(@trans.read(@trans.available)).to eq("6000000000")
end
it "should write json double" do
@prot.write_json_double(12.3)
expect(@trans.read(@trans.available)).to eq("12.3")
@prot.write_json_double(-3.21)
expect(@trans.read(@trans.available)).to eq("-3.21")
@prot.write_json_double(((+1.0/0.0)/(+1.0/0.0)))
expect(@trans.read(@trans.available)).to eq("\"NaN\"")
@prot.write_json_double((+1.0/0.0))
expect(@trans.read(@trans.available)).to eq("\"Infinity\"")
@prot.write_json_double((-1.0/0.0))
expect(@trans.read(@trans.available)).to eq("\"-Infinity\"")
end
it "should write json object start" do
@prot.write_json_object_start
expect(@trans.read(@trans.available)).to eq("{")
end
it "should write json object end" do
@prot.write_json_object_end
expect(@trans.read(@trans.available)).to eq("}")
end
it "should write json array start" do
@prot.write_json_array_start
expect(@trans.read(@trans.available)).to eq("[")
end
it "should write json array end" do
@prot.write_json_array_end
expect(@trans.read(@trans.available)).to eq("]")
end
it "should write message begin" do
@prot.write_message_begin("name", 12, 32)
expect(@trans.read(@trans.available)).to eq("[1,\"name\",12,32")
end
it "should write message end" do
@prot.write_message_end
expect(@trans.read(@trans.available)).to eq("]")
end
it "should write struct begin" do
@prot.write_struct_begin("name")
expect(@trans.read(@trans.available)).to eq("{")
end
it "should write struct end" do
@prot.write_struct_end
expect(@trans.read(@trans.available)).to eq("}")
end
it "should write field begin" do
@prot.write_field_begin("name", Thrift::Types::STRUCT, 32)
expect(@trans.read(@trans.available)).to eq("32{\"rec\"")
end
it "should write field end" do
@prot.write_field_end
expect(@trans.read(@trans.available)).to eq("}")
end
it "should write field stop" do
@prot.write_field_stop
expect(@trans.read(@trans.available)).to eq("")
end
it "should write map begin" do
@prot.write_map_begin(Thrift::Types::STRUCT, Thrift::Types::LIST, 32)
expect(@trans.read(@trans.available)).to eq("[\"rec\",\"lst\",32,{")
end
it "should write map end" do
@prot.write_map_end
expect(@trans.read(@trans.available)).to eq("}]")
end
it "should write list begin" do
@prot.write_list_begin(Thrift::Types::STRUCT, 32)
expect(@trans.read(@trans.available)).to eq("[\"rec\",32")
end
it "should write list end" do
@prot.write_list_end
expect(@trans.read(@trans.available)).to eq("]")
end
it "should write set begin" do
@prot.write_set_begin(Thrift::Types::STRUCT, 32)
expect(@trans.read(@trans.available)).to eq("[\"rec\",32")
end
it "should write set end" do
@prot.write_set_end
expect(@trans.read(@trans.available)).to eq("]")
end
it "should write bool" do
@prot.write_bool(true)
expect(@trans.read(@trans.available)).to eq("1")
@prot.write_bool(false)
expect(@trans.read(@trans.available)).to eq("0")
end
it "should write byte" do
@prot.write_byte(100)
expect(@trans.read(@trans.available)).to eq("100")
end
it "should write i16" do
@prot.write_i16(1000)
expect(@trans.read(@trans.available)).to eq("1000")
end
it "should write i32" do
@prot.write_i32(3000000000)
expect(@trans.read(@trans.available)).to eq("3000000000")
end
it "should write i64" do
@prot.write_i64(6000000000)
expect(@trans.read(@trans.available)).to eq("6000000000")
end
it "should write double" do
@prot.write_double(1.23)
expect(@trans.read(@trans.available)).to eq("1.23")
@prot.write_double(-32.1)
expect(@trans.read(@trans.available)).to eq("-32.1")
@prot.write_double(((+1.0/0.0)/(+1.0/0.0)))
expect(@trans.read(@trans.available)).to eq("\"NaN\"")
@prot.write_double((+1.0/0.0))
expect(@trans.read(@trans.available)).to eq("\"Infinity\"")
@prot.write_double((-1.0/0.0))
expect(@trans.read(@trans.available)).to eq("\"-Infinity\"")
end
if RUBY_VERSION >= '1.9'
it 'should write string' do
@prot.write_string('this is a test string')
a = @trans.read(@trans.available)
expect(a).to eq('"this is a test string"'.force_encoding(Encoding::BINARY))
expect(a.encoding).to eq(Encoding::BINARY)
end
it 'should write string with unicode characters' do
@prot.write_string("this is a test string with unicode characters: \u20AC \u20AD")
a = @trans.read(@trans.available)
expect(a).to eq("\"this is a test string with unicode characters: \u20AC \u20AD\"".force_encoding(Encoding::BINARY))
expect(a.encoding).to eq(Encoding::BINARY)
end
else
it 'should write string' do
@prot.write_string('this is a test string')
expect(@trans.read(@trans.available)).to eq('"this is a test string"')
end
end
it "should write binary" do
@prot.write_binary("this is a base64 string")
expect(@trans.read(@trans.available)).to eq("\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\"")
end
it "should write long binary" do
@prot.write_binary((0...256).to_a.pack('C*'))
expect(@trans.read(@trans.available)).to eq("\"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==\"")
end
it "should get type name for type id" do
expect {@prot.get_type_name_for_type_id(Thrift::Types::STOP)}.to raise_error(NotImplementedError)
expect {@prot.get_type_name_for_type_id(Thrift::Types::VOID)}.to raise_error(NotImplementedError)
expect(@prot.get_type_name_for_type_id(Thrift::Types::BOOL)).to eq("tf")
expect(@prot.get_type_name_for_type_id(Thrift::Types::BYTE)).to eq("i8")
expect(@prot.get_type_name_for_type_id(Thrift::Types::DOUBLE)).to eq("dbl")
expect(@prot.get_type_name_for_type_id(Thrift::Types::I16)).to eq("i16")
expect(@prot.get_type_name_for_type_id(Thrift::Types::I32)).to eq("i32")
expect(@prot.get_type_name_for_type_id(Thrift::Types::I64)).to eq("i64")
expect(@prot.get_type_name_for_type_id(Thrift::Types::STRING)).to eq("str")
expect(@prot.get_type_name_for_type_id(Thrift::Types::STRUCT)).to eq("rec")
expect(@prot.get_type_name_for_type_id(Thrift::Types::MAP)).to eq("map")
expect(@prot.get_type_name_for_type_id(Thrift::Types::SET)).to eq("set")
expect(@prot.get_type_name_for_type_id(Thrift::Types::LIST)).to eq("lst")
end
it "should get type id for type name" do
expect {@prot.get_type_id_for_type_name("pp")}.to raise_error(NotImplementedError)
expect(@prot.get_type_id_for_type_name("tf")).to eq(Thrift::Types::BOOL)
expect(@prot.get_type_id_for_type_name("i8")).to eq(Thrift::Types::BYTE)
expect(@prot.get_type_id_for_type_name("dbl")).to eq(Thrift::Types::DOUBLE)
expect(@prot.get_type_id_for_type_name("i16")).to eq(Thrift::Types::I16)
expect(@prot.get_type_id_for_type_name("i32")).to eq(Thrift::Types::I32)
expect(@prot.get_type_id_for_type_name("i64")).to eq(Thrift::Types::I64)
expect(@prot.get_type_id_for_type_name("str")).to eq(Thrift::Types::STRING)
expect(@prot.get_type_id_for_type_name("rec")).to eq(Thrift::Types::STRUCT)
expect(@prot.get_type_id_for_type_name("map")).to eq(Thrift::Types::MAP)
expect(@prot.get_type_id_for_type_name("set")).to eq(Thrift::Types::SET)
expect(@prot.get_type_id_for_type_name("lst")).to eq(Thrift::Types::LIST)
end
it "should read json syntax char" do
@trans.write('F')
expect {@prot.read_json_syntax_char('G')}.to raise_error(Thrift::ProtocolException)
@trans.write('H')
@prot.read_json_syntax_char('H')
end
it "should read json escape char" do
@trans.write('0054')
expect(@prot.read_json_escape_char).to eq('T')
@trans.write("\"\\\"\"")
expect(@prot.read_json_string(false)).to eq("\"")
@trans.write("\"\\\\\"")
expect(@prot.read_json_string(false)).to eq("\\")
@trans.write("\"\\/\"")
expect(@prot.read_json_string(false)).to eq("\/")
@trans.write("\"\\b\"")
expect(@prot.read_json_string(false)).to eq("\b")
@trans.write("\"\\f\"")
expect(@prot.read_json_string(false)).to eq("\f")
@trans.write("\"\\n\"")
expect(@prot.read_json_string(false)).to eq("\n")
@trans.write("\"\\r\"")
expect(@prot.read_json_string(false)).to eq("\r")
@trans.write("\"\\t\"")
expect(@prot.read_json_string(false)).to eq("\t")
end
it "should read json string" do
@trans.write("\"\\P")
expect {@prot.read_json_string(false)}.to raise_error(Thrift::ProtocolException)
@trans.write("\"this is a test string\"")
expect(@prot.read_json_string).to eq("this is a test string")
end
it "should read json base64" do
@trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
expect(@prot.read_json_base64).to eq("this is a test string")
end
it "should is json numeric" do
expect(@prot.is_json_numeric("A")).to eq(false)
expect(@prot.is_json_numeric("+")).to eq(true)
expect(@prot.is_json_numeric("-")).to eq(true)
expect(@prot.is_json_numeric(".")).to eq(true)
expect(@prot.is_json_numeric("0")).to eq(true)
expect(@prot.is_json_numeric("1")).to eq(true)
expect(@prot.is_json_numeric("2")).to eq(true)
expect(@prot.is_json_numeric("3")).to eq(true)
expect(@prot.is_json_numeric("4")).to eq(true)
expect(@prot.is_json_numeric("5")).to eq(true)
expect(@prot.is_json_numeric("6")).to eq(true)
expect(@prot.is_json_numeric("7")).to eq(true)
expect(@prot.is_json_numeric("8")).to eq(true)
expect(@prot.is_json_numeric("9")).to eq(true)
expect(@prot.is_json_numeric("E")).to eq(true)
expect(@prot.is_json_numeric("e")).to eq(true)
end
it "should read json numeric chars" do
@trans.write("1.453E45T")
expect(@prot.read_json_numeric_chars).to eq("1.453E45")
end
it "should read json integer" do
@trans.write("1.45\"\"")
expect {@prot.read_json_integer}.to raise_error(Thrift::ProtocolException)
@prot.read_string
@trans.write("1453T")
expect(@prot.read_json_integer).to eq(1453)
end
it "should read json double" do
@trans.write("1.45e3e01\"\"")
expect {@prot.read_json_double}.to raise_error(Thrift::ProtocolException)
@prot.read_string
@trans.write("\"1.453e01\"")
expect {@prot.read_json_double}.to raise_error(Thrift::ProtocolException)
@trans.write("1.453e01\"\"")
expect(@prot.read_json_double).to eq(14.53)
@prot.read_string
@trans.write("\"NaN\"")
expect(@prot.read_json_double.nan?).to eq(true)
@trans.write("\"Infinity\"")
expect(@prot.read_json_double).to eq(+1.0/0.0)
@trans.write("\"-Infinity\"")
expect(@prot.read_json_double).to eq(-1.0/0.0)
end
it "should read json object start" do
@trans.write("{")
expect(@prot.read_json_object_start).to eq(nil)
end
it "should read json object end" do
@trans.write("}")
expect(@prot.read_json_object_end).to eq(nil)
end
it "should read json array start" do
@trans.write("[")
expect(@prot.read_json_array_start).to eq(nil)
end
it "should read json array end" do
@trans.write("]")
expect(@prot.read_json_array_end).to eq(nil)
end
it "should read_message_begin" do
@trans.write("[2,")
expect {@prot.read_message_begin}.to raise_error(Thrift::ProtocolException)
@trans.write("[1,\"name\",12,32\"\"")
expect(@prot.read_message_begin).to eq(["name", 12, 32])
end
it "should read message end" do
@trans.write("]")
expect(@prot.read_message_end).to eq(nil)
end
it "should read struct begin" do
@trans.write("{")
expect(@prot.read_struct_begin).to eq(nil)
end
it "should read struct end" do
@trans.write("}")
expect(@prot.read_struct_end).to eq(nil)
end
it "should read field begin" do
@trans.write("1{\"rec\"")
expect(@prot.read_field_begin).to eq([nil, 12, 1])
end
it "should read field end" do
@trans.write("}")
expect(@prot.read_field_end).to eq(nil)
end
it "should read map begin" do
@trans.write("[\"rec\",\"lst\",2,{")
expect(@prot.read_map_begin).to eq([12, 15, 2])
end
it "should read map end" do
@trans.write("}]")
expect(@prot.read_map_end).to eq(nil)
end
it "should read list begin" do
@trans.write("[\"rec\",2\"\"")
expect(@prot.read_list_begin).to eq([12, 2])
end
it "should read list end" do
@trans.write("]")
expect(@prot.read_list_end).to eq(nil)
end
it "should read set begin" do
@trans.write("[\"rec\",2\"\"")
expect(@prot.read_set_begin).to eq([12, 2])
end
it "should read set end" do
@trans.write("]")
expect(@prot.read_set_end).to eq(nil)
end
it "should read bool" do
@trans.write("0\"\"")
expect(@prot.read_bool).to eq(false)
@prot.read_string
@trans.write("1\"\"")
expect(@prot.read_bool).to eq(true)
end
it "should read byte" do
@trans.write("60\"\"")
expect(@prot.read_byte).to eq(60)
end
it "should read i16" do
@trans.write("1000\"\"")
expect(@prot.read_i16).to eq(1000)
end
it "should read i32" do
@trans.write("3000000000\"\"")
expect(@prot.read_i32).to eq(3000000000)
end
it "should read i64" do
@trans.write("6000000000\"\"")
expect(@prot.read_i64).to eq(6000000000)
end
it "should read double" do
@trans.write("12.23\"\"")
expect(@prot.read_double).to eq(12.23)
end
if RUBY_VERSION >= '1.9'
it 'should read string' do
@trans.write('"this is a test string"'.force_encoding(Encoding::BINARY))
a = @prot.read_string
expect(a).to eq('this is a test string')
expect(a.encoding).to eq(Encoding::UTF_8)
end
it 'should read string with unicode characters' do
@trans.write('"this is a test string with unicode characters: \u20AC \u20AD"'.force_encoding(Encoding::BINARY))
a = @prot.read_string
expect(a).to eq("this is a test string with unicode characters: \u20AC \u20AD")
expect(a.encoding).to eq(Encoding::UTF_8)
end
else
it 'should read string' do
@trans.write('"this is a test string"')
expect(@prot.read_string).to eq('this is a test string')
end
end
it "should read binary" do
@trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
expect(@prot.read_binary).to eq("this is a test string")
end
it "should read long binary" do
@trans.write("\"AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w==\"")
expect(@prot.read_binary.bytes.to_a).to eq((0...256).to_a)
end
it "should provide a reasonable to_s" do
expect(@prot.to_s).to eq("json(memory)")
end
end
describe Thrift::JsonProtocolFactory do
it "should create a JsonProtocol" do
expect(Thrift::JsonProtocolFactory.new.get_protocol(double("MockTransport"))).to be_instance_of(Thrift::JsonProtocol)
end
it "should provide a reasonable to_s" do
expect(Thrift::JsonProtocolFactory.new.to_s).to eq("json")
end
end
end
thrift-0.14.0/spec/socket_spec_shared.rb 0000644 0000041 0000041 00000010137 14020410666 020234 0 ustar www-data www-data #
# 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 'spec_helper'
shared_examples_for "a socket" do
it "should open a socket" do
expect(@socket.open).to eq(@handle)
end
it "should be open whenever it has a handle" do
expect(@socket).not_to be_open
@socket.open
expect(@socket).to be_open
@socket.handle = nil
expect(@socket).not_to be_open
@socket.handle = @handle
@socket.close
expect(@socket).not_to be_open
end
it "should write data to the handle" do
@socket.open
expect(@handle).to receive(:write).with("foobar")
@socket.write("foobar")
expect(@handle).to receive(:write).with("fail").and_raise(StandardError)
expect { @socket.write("fail") }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
end
it "should raise an error when it cannot read from the handle" do
@socket.open
expect(@handle).to receive(:readpartial).with(17).and_raise(StandardError)
expect { @socket.read(17) }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
end
it "should return the data read when reading from the handle works" do
@socket.open
expect(@handle).to receive(:readpartial).with(17).and_return("test data")
expect(@socket.read(17)).to eq("test data")
end
it "should declare itself as closed when it has an error" do
@socket.open
expect(@handle).to receive(:write).with("fail").and_raise(StandardError)
expect(@socket).to be_open
expect { @socket.write("fail") }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
expect(@socket).not_to be_open
end
it "should raise an error when the stream is closed" do
@socket.open
allow(@handle).to receive(:closed?).and_return(true)
expect(@socket).not_to be_open
expect { @socket.write("fail") }.to raise_error(IOError, "closed stream")
expect { @socket.read(10) }.to raise_error(IOError, "closed stream")
end
it "should support the timeout accessor for read" do
@socket.timeout = 3
@socket.open
expect(IO).to receive(:select).with([@handle], nil, nil, 3).and_return([[@handle], [], []])
expect(@handle).to receive(:readpartial).with(17).and_return("test data")
expect(@socket.read(17)).to eq("test data")
end
it "should support the timeout accessor for write" do
@socket.timeout = 3
@socket.open
expect(IO).to receive(:select).with(nil, [@handle], nil, 3).twice.and_return([[], [@handle], []])
expect(@handle).to receive(:write_nonblock).with("test data").and_return(4)
expect(@handle).to receive(:write_nonblock).with(" data").and_return(5)
expect(@socket.write("test data")).to eq(9)
end
it "should raise an error when read times out" do
@socket.timeout = 0.5
@socket.open
expect(IO).to receive(:select).once {sleep(0.5); nil}
expect { @socket.read(17) }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::TIMED_OUT) }
end
it "should raise an error when write times out" do
@socket.timeout = 0.5
@socket.open
allow(IO).to receive(:select).with(nil, [@handle], nil, 0.5).and_return(nil)
expect { @socket.write("test data") }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::TIMED_OUT) }
end
end
thrift-0.14.0/spec/ThriftSpec.thrift 0000644 0000041 0000041 00000007714 14020410666 017363 0 ustar www-data www-data /*
* 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.
*/
#
# 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.
#
namespace rb SpecNamespace
struct Hello {
1: string greeting = "hello world"
}
enum SomeEnum {
ONE
TWO
}
struct StructWithSomeEnum {
1: SomeEnum some_enum;
}
union TestUnion {
/**
* A doc string
*/
1: string string_field;
2: i32 i32_field;
3: i32 other_i32_field;
4: SomeEnum enum_field;
5: binary binary_field;
}
struct Foo {
1: i32 simple = 53,
2: string words = "words",
3: Hello hello = {'greeting' : "hello, world!"},
4: list ints = [1, 2, 2, 3],
5: map> complex,
6: set shorts = [5, 17, 239],
7: optional string opt_string
8: bool my_bool
}
struct Foo2 {
1: binary my_binary
}
struct BoolStruct {
1: bool yesno = 1
}
struct SimpleList {
1: list bools,
2: list bytes,
3: list i16s,
4: list i32s,
5: list i64s,
6: list doubles,
7: list strings,
8: list