Moving from govendor to dep, updated dependencies (#48)

* Moving from govendor to dep.

* Making the pull request template more friendly.

* Fixing akward space in PR template.

* goimports run on whole project using ` goimports -w $(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./gen-go/*")`

source of command: https://gist.github.com/bgentry/fd1ffef7dbde01857f66
This commit is contained in:
Renan DelValle 2018-01-07 13:13:47 -08:00 committed by GitHub
parent 9631aa3aab
commit 8d445c1c77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2186 changed files with 400410 additions and 352 deletions

View file

@ -0,0 +1,27 @@
# 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)
}

View file

@ -0,0 +1,25 @@
# 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 Extended
include "BaseService.thrift"
service ExtendedService extends BaseService.BaseService {
void ping()
}

View file

@ -0,0 +1,44 @@
/*
* 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 OtherNamespace
enum SomeEnum {
ONE
TWO
}

View file

@ -0,0 +1,53 @@
/*
* 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 NamespacedSpecNamespace
include "Referenced.thrift"
struct Hello {
1: string greeting = "hello world"
}
service NamespacedNonblockingService {
Hello greeting(1:bool english)
bool block()
oneway void unblock(1:i32 n)
oneway void shutdown()
void sleep(1:double seconds)
}

View file

@ -0,0 +1,183 @@
/*
* 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<i32> ints = [1, 2, 2, 3],
5: map<i32, map<string, double>> complex,
6: set<i16> 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<bool> bools,
2: list<byte> bytes,
3: list<i16> i16s,
4: list<i32> i32s,
5: list<i64> i64s,
6: list<double> doubles,
7: list<string> strings,
8: list<map<i16, i16>> maps,
9: list<list<i16>> lists,
10: list<set<i16>> sets,
11: list<Hello> hellos
}
exception Xception {
1: string message,
2: i32 code = 1
}
service NonblockingService {
Hello greeting(1:bool english)
bool block()
oneway void unblock(1:i32 n)
oneway void shutdown()
void sleep(1:double seconds)
}
union My_union {
1: bool im_true,
2: byte a_bite,
3: i16 integer16,
4: i32 integer32,
5: i64 integer64,
6: double double_precision,
7: string some_characters,
8: i32 other_i32
9: SomeEnum some_enum;
10: map<SomeEnum, list<SomeEnum>> my_map;
}
struct Struct_with_union {
1: My_union fun_union
2: i32 integer32
3: string some_characters
}
struct StructWithEnumMap {
1: map<SomeEnum, list<SomeEnum>> my_map;
}
# Nested lists
struct NestedListInList {
1: list<list<byte>> value
}
struct NestedListInSet {
1: set<list<byte>> value
}
struct NestedListInMapKey {
1: map<list<byte>, byte> value
}
struct NestedListInMapValue {
1: map<byte, list<byte>> value
}
# Nested sets
struct NestedSetInList {
1: list<set<byte>> value
}
struct NestedSetInSet {
1: set<set<byte>> value
}
struct NestedSetInMapKey {
1: map<set<byte>, byte> value
}
struct NestedSetInMapValue {
1: map<byte, set<byte>> value
}
# Nested maps
struct NestedMapInList {
1: list<map<byte, byte>> value
}
struct NestedMapInSet {
1: set<map<byte, byte>> value
}
struct NestedMapInMapKey {
2: map<map<byte, byte>, byte> value
}
struct NestedMapInMapValue {
2: map<byte, map<byte, byte>> value
}

View file

@ -0,0 +1,217 @@
#
# 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 'BaseProtocol' do
before(:each) do
@trans = mock("MockTransport")
@prot = Thrift::BaseProtocol.new(@trans)
end
describe Thrift::BaseProtocol do
# most of the methods are stubs, so we can ignore them
it "should make trans accessible" do
@prot.trans.should eql(@trans)
end
it 'should write out a field nicely (deprecated write_field signature)' do
@prot.should_receive(:write_field_begin).with('field', 'type', 'fid').ordered
@prot.should_receive(:write_type).with({:name => 'field', :type => 'type'}, 'value').ordered
@prot.should_receive(:write_field_end).ordered
@prot.write_field('field', 'type', 'fid', 'value')
end
it 'should write out a field nicely' do
@prot.should_receive(:write_field_begin).with('field', 'type', 'fid').ordered
@prot.should_receive(:write_type).with({:name => 'field', :type => 'type', :binary => false}, 'value').ordered
@prot.should_receive(:write_field_end).ordered
@prot.write_field({:name => 'field', :type => 'type', :binary => false}, 'fid', 'value')
end
it 'should write out the different types (deprecated write_type signature)' do
@prot.should_receive(:write_bool).with('bool').ordered
@prot.should_receive(:write_byte).with('byte').ordered
@prot.should_receive(:write_double).with('double').ordered
@prot.should_receive(:write_i16).with('i16').ordered
@prot.should_receive(:write_i32).with('i32').ordered
@prot.should_receive(:write_i64).with('i64').ordered
@prot.should_receive(:write_string).with('string').ordered
struct = mock('Struct')
struct.should_receive(:write).with(@prot).ordered
@prot.write_type(Thrift::Types::BOOL, 'bool')
@prot.write_type(Thrift::Types::BYTE, 'byte')
@prot.write_type(Thrift::Types::DOUBLE, 'double')
@prot.write_type(Thrift::Types::I16, 'i16')
@prot.write_type(Thrift::Types::I32, 'i32')
@prot.write_type(Thrift::Types::I64, 'i64')
@prot.write_type(Thrift::Types::STRING, 'string')
@prot.write_type(Thrift::Types::STRUCT, struct)
# all other types are not implemented
[Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP, Thrift::Types::SET, Thrift::Types::LIST].each do |type|
expect { @prot.write_type(type, type.to_s) }.to raise_error(NotImplementedError)
end
end
it 'should write out the different types' do
@prot.should_receive(:write_bool).with('bool').ordered
@prot.should_receive(:write_byte).with('byte').ordered
@prot.should_receive(:write_double).with('double').ordered
@prot.should_receive(:write_i16).with('i16').ordered
@prot.should_receive(:write_i32).with('i32').ordered
@prot.should_receive(:write_i64).with('i64').ordered
@prot.should_receive(:write_string).with('string').ordered
@prot.should_receive(:write_binary).with('binary').ordered
struct = mock('Struct')
struct.should_receive(:write).with(@prot).ordered
@prot.write_type({:type => Thrift::Types::BOOL}, 'bool')
@prot.write_type({:type => Thrift::Types::BYTE}, 'byte')
@prot.write_type({:type => Thrift::Types::DOUBLE}, 'double')
@prot.write_type({:type => Thrift::Types::I16}, 'i16')
@prot.write_type({:type => Thrift::Types::I32}, 'i32')
@prot.write_type({:type => Thrift::Types::I64}, 'i64')
@prot.write_type({:type => Thrift::Types::STRING}, 'string')
@prot.write_type({:type => Thrift::Types::STRING, :binary => true}, 'binary')
@prot.write_type({:type => Thrift::Types::STRUCT}, struct)
# all other types are not implemented
[Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP, Thrift::Types::SET, Thrift::Types::LIST].each do |type|
expect { @prot.write_type({:type => type}, type.to_s) }.to raise_error(NotImplementedError)
end
end
it 'should read the different types (deprecated read_type signature)' do
@prot.should_receive(:read_bool).ordered
@prot.should_receive(:read_byte).ordered
@prot.should_receive(:read_i16).ordered
@prot.should_receive(:read_i32).ordered
@prot.should_receive(:read_i64).ordered
@prot.should_receive(:read_double).ordered
@prot.should_receive(:read_string).ordered
@prot.read_type(Thrift::Types::BOOL)
@prot.read_type(Thrift::Types::BYTE)
@prot.read_type(Thrift::Types::I16)
@prot.read_type(Thrift::Types::I32)
@prot.read_type(Thrift::Types::I64)
@prot.read_type(Thrift::Types::DOUBLE)
@prot.read_type(Thrift::Types::STRING)
# all other types are not implemented
[Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP,
Thrift::Types::SET, Thrift::Types::LIST, Thrift::Types::STRUCT].each do |type|
expect { @prot.read_type(type) }.to raise_error(NotImplementedError)
end
end
it 'should read the different types' do
@prot.should_receive(:read_bool).ordered
@prot.should_receive(:read_byte).ordered
@prot.should_receive(:read_i16).ordered
@prot.should_receive(:read_i32).ordered
@prot.should_receive(:read_i64).ordered
@prot.should_receive(:read_double).ordered
@prot.should_receive(:read_string).ordered
@prot.should_receive(:read_binary).ordered
@prot.read_type({:type => Thrift::Types::BOOL})
@prot.read_type({:type => Thrift::Types::BYTE})
@prot.read_type({:type => Thrift::Types::I16})
@prot.read_type({:type => Thrift::Types::I32})
@prot.read_type({:type => Thrift::Types::I64})
@prot.read_type({:type => Thrift::Types::DOUBLE})
@prot.read_type({:type => Thrift::Types::STRING})
@prot.read_type({:type => Thrift::Types::STRING, :binary => true})
# all other types are not implemented
[Thrift::Types::STOP, Thrift::Types::VOID, Thrift::Types::MAP,
Thrift::Types::SET, Thrift::Types::LIST, Thrift::Types::STRUCT].each do |type|
expect { @prot.read_type({:type => type}) }.to raise_error(NotImplementedError)
end
end
it "should skip the basic types" do
@prot.should_receive(:read_bool).ordered
@prot.should_receive(:read_byte).ordered
@prot.should_receive(:read_i16).ordered
@prot.should_receive(:read_i32).ordered
@prot.should_receive(:read_i64).ordered
@prot.should_receive(:read_double).ordered
@prot.should_receive(:read_string).ordered
@prot.skip(Thrift::Types::BOOL)
@prot.skip(Thrift::Types::BYTE)
@prot.skip(Thrift::Types::I16)
@prot.skip(Thrift::Types::I32)
@prot.skip(Thrift::Types::I64)
@prot.skip(Thrift::Types::DOUBLE)
@prot.skip(Thrift::Types::STRING)
@prot.skip(Thrift::Types::STOP) # should do absolutely nothing
end
it "should skip structs" do
real_skip = @prot.method(:skip)
@prot.should_receive(:read_struct_begin).ordered
@prot.should_receive(:read_field_begin).exactly(4).times.and_return(
['field 1', Thrift::Types::STRING, 1],
['field 2', Thrift::Types::I32, 2],
['field 3', Thrift::Types::MAP, 3],
[nil, Thrift::Types::STOP, 0]
)
@prot.should_receive(:read_field_end).exactly(3).times
@prot.should_receive(:read_string).exactly(3).times
@prot.should_receive(:read_i32).ordered
@prot.should_receive(:read_map_begin).ordered.and_return([Thrift::Types::STRING, Thrift::Types::STRING, 1])
# @prot.should_receive(:read_string).exactly(2).times
@prot.should_receive(:read_map_end).ordered
@prot.should_receive(:read_struct_end).ordered
real_skip.call(Thrift::Types::STRUCT)
end
it "should skip maps" do
real_skip = @prot.method(:skip)
@prot.should_receive(:read_map_begin).ordered.and_return([Thrift::Types::STRING, Thrift::Types::STRUCT, 1])
@prot.should_receive(:read_string).ordered
@prot.should_receive(:read_struct_begin).ordered.and_return(["some_struct"])
@prot.should_receive(:read_field_begin).ordered.and_return([nil, Thrift::Types::STOP, nil]);
@prot.should_receive(:read_struct_end).ordered
@prot.should_receive(:read_map_end).ordered
real_skip.call(Thrift::Types::MAP)
end
it "should skip sets" do
real_skip = @prot.method(:skip)
@prot.should_receive(:read_set_begin).ordered.and_return([Thrift::Types::I64, 9])
@prot.should_receive(:read_i64).ordered.exactly(9).times
@prot.should_receive(:read_set_end)
real_skip.call(Thrift::Types::SET)
end
it "should skip lists" do
real_skip = @prot.method(:skip)
@prot.should_receive(:read_list_begin).ordered.and_return([Thrift::Types::DOUBLE, 11])
@prot.should_receive(:read_double).ordered.exactly(11).times
@prot.should_receive(:read_list_end)
real_skip.call(Thrift::Types::LIST)
end
end
describe Thrift::BaseProtocolFactory do
it "should raise NotImplementedError" do
# returning nil since Protocol is just an abstract class
lambda {Thrift::BaseProtocolFactory.new.get_protocol(mock("MockTransport"))}.should raise_error(NotImplementedError)
end
end
end

View file

@ -0,0 +1,350 @@
#
# 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 'BaseTransport' do
describe Thrift::TransportException do
it "should make type accessible" do
exc = Thrift::TransportException.new(Thrift::TransportException::ALREADY_OPEN, "msg")
exc.type.should == Thrift::TransportException::ALREADY_OPEN
exc.message.should == "msg"
end
end
describe Thrift::BaseTransport do
it "should read the specified size" do
transport = Thrift::BaseTransport.new
transport.should_receive(:read).with(40).ordered.and_return("10 letters")
transport.should_receive(:read).with(30).ordered.and_return("fifteen letters")
transport.should_receive(:read).with(15).ordered.and_return("more characters")
transport.read_all(40).should == "10 lettersfifteen lettersmore characters"
end
it "should stub out the rest of the methods" do
# can't test for stubbiness, so just make sure they're defined
[:open?, :open, :close, :read, :write, :flush].each do |sym|
Thrift::BaseTransport.method_defined?(sym).should be_true
end
end
it "should alias << to write" do
Thrift::BaseTransport.instance_method(:<<).should == Thrift::BaseTransport.instance_method(:write)
end
end
describe Thrift::BaseServerTransport do
it "should stub out its methods" do
[:listen, :accept, :close].each do |sym|
Thrift::BaseServerTransport.method_defined?(sym).should be_true
end
end
end
describe Thrift::BaseTransportFactory do
it "should return the transport it's given" do
transport = mock("Transport")
Thrift::BaseTransportFactory.new.get_transport(transport).should eql(transport)
end
end
describe Thrift::BufferedTransport do
it "should pass through everything but write/flush/read" do
trans = mock("Transport")
trans.should_receive(:open?).ordered.and_return("+ open?")
trans.should_receive(:open).ordered.and_return("+ open")
trans.should_receive(:flush).ordered # from the close
trans.should_receive(:close).ordered.and_return("+ close")
btrans = Thrift::BufferedTransport.new(trans)
btrans.open?.should == "+ open?"
btrans.open.should == "+ open"
btrans.close.should == "+ close"
end
it "should buffer reads in chunks of #{Thrift::BufferedTransport::DEFAULT_BUFFER}" do
trans = mock("Transport")
trans.should_receive(:read).with(Thrift::BufferedTransport::DEFAULT_BUFFER).and_return("lorum ipsum dolor emet")
btrans = Thrift::BufferedTransport.new(trans)
btrans.read(6).should == "lorum "
btrans.read(6).should == "ipsum "
btrans.read(6).should == "dolor "
btrans.read(6).should == "emet"
end
it "should buffer writes and send them on flush" do
trans = mock("Transport")
btrans = Thrift::BufferedTransport.new(trans)
btrans.write("one/")
btrans.write("two/")
btrans.write("three/")
trans.should_receive(:write).with("one/two/three/").ordered
trans.should_receive(:flush).ordered
btrans.flush
end
it "should only send buffered data once" do
trans = mock("Transport")
btrans = Thrift::BufferedTransport.new(trans)
btrans.write("one/")
btrans.write("two/")
btrans.write("three/")
trans.should_receive(:write).with("one/two/three/")
trans.stub!(:flush)
btrans.flush
# Nothing to flush with no data
btrans.flush
end
it "should flush on close" do
trans = mock("Transport")
trans.should_receive(:close)
btrans = Thrift::BufferedTransport.new(trans)
btrans.should_receive(:flush)
btrans.close
end
it "should not write to socket if there's no data" do
trans = mock("Transport")
trans.should_receive(:flush)
btrans = Thrift::BufferedTransport.new(trans)
btrans.flush
end
end
describe Thrift::BufferedTransportFactory do
it "should wrap the given transport in a BufferedTransport" do
trans = mock("Transport")
btrans = mock("BufferedTransport")
Thrift::BufferedTransport.should_receive(:new).with(trans).and_return(btrans)
Thrift::BufferedTransportFactory.new.get_transport(trans).should == btrans
end
end
describe Thrift::FramedTransport do
before(:each) do
@trans = mock("Transport")
end
it "should pass through open?/open/close" do
ftrans = Thrift::FramedTransport.new(@trans)
@trans.should_receive(:open?).ordered.and_return("+ open?")
@trans.should_receive(:open).ordered.and_return("+ open")
@trans.should_receive(:close).ordered.and_return("+ close")
ftrans.open?.should == "+ open?"
ftrans.open.should == "+ open"
ftrans.close.should == "+ close"
end
it "should pass through read when read is turned off" do
ftrans = Thrift::FramedTransport.new(@trans, false, true)
@trans.should_receive(:read).with(17).ordered.and_return("+ read")
ftrans.read(17).should == "+ read"
end
it "should pass through write/flush when write is turned off" do
ftrans = Thrift::FramedTransport.new(@trans, true, false)
@trans.should_receive(:write).with("foo").ordered.and_return("+ write")
@trans.should_receive(:flush).ordered.and_return("+ flush")
ftrans.write("foo").should == "+ write"
ftrans.flush.should == "+ flush"
end
it "should return a full frame if asked for >= the frame's length" do
frame = "this is a frame"
@trans.should_receive(:read_all).with(4).and_return("\000\000\000\017")
@trans.should_receive(:read_all).with(frame.length).and_return(frame)
Thrift::FramedTransport.new(@trans).read(frame.length + 10).should == frame
end
it "should return slices of the frame when asked for < the frame's length" do
frame = "this is a frame"
@trans.should_receive(:read_all).with(4).and_return("\000\000\000\017")
@trans.should_receive(:read_all).with(frame.length).and_return(frame)
ftrans = Thrift::FramedTransport.new(@trans)
ftrans.read(4).should == "this"
ftrans.read(4).should == " is "
ftrans.read(16).should == "a frame"
end
it "should return nothing if asked for <= 0" do
Thrift::FramedTransport.new(@trans).read(-2).should == ""
end
it "should pull a new frame when the first is exhausted" do
frame = "this is a frame"
frame2 = "yet another frame"
@trans.should_receive(:read_all).with(4).and_return("\000\000\000\017", "\000\000\000\021")
@trans.should_receive(:read_all).with(frame.length).and_return(frame)
@trans.should_receive(:read_all).with(frame2.length).and_return(frame2)
ftrans = Thrift::FramedTransport.new(@trans)
ftrans.read(4).should == "this"
ftrans.read(8).should == " is a fr"
ftrans.read(6).should == "ame"
ftrans.read(4).should == "yet "
ftrans.read(16).should == "another frame"
end
it "should buffer writes" do
ftrans = Thrift::FramedTransport.new(@trans)
@trans.should_not_receive(:write)
ftrans.write("foo")
ftrans.write("bar")
ftrans.write("this is a frame")
end
it "should write slices of the buffer" do
ftrans = Thrift::FramedTransport.new(@trans)
ftrans.write("foobar", 3)
ftrans.write("barfoo", 1)
@trans.stub!(:flush)
@trans.should_receive(:write).with("\000\000\000\004foob")
ftrans.flush
end
it "should flush frames with a 4-byte header" do
ftrans = Thrift::FramedTransport.new(@trans)
@trans.should_receive(:write).with("\000\000\000\035one/two/three/this is a frame").ordered
@trans.should_receive(:flush).ordered
ftrans.write("one/")
ftrans.write("two/")
ftrans.write("three/")
ftrans.write("this is a frame")
ftrans.flush
end
it "should not flush the same buffered data twice" do
ftrans = Thrift::FramedTransport.new(@trans)
@trans.should_receive(:write).with("\000\000\000\007foo/bar")
@trans.stub!(:flush)
ftrans.write("foo")
ftrans.write("/bar")
ftrans.flush
@trans.should_receive(:write).with("\000\000\000\000")
ftrans.flush
end
end
describe Thrift::FramedTransportFactory do
it "should wrap the given transport in a FramedTransport" do
trans = mock("Transport")
Thrift::FramedTransport.should_receive(:new).with(trans)
Thrift::FramedTransportFactory.new.get_transport(trans)
end
end
describe Thrift::MemoryBufferTransport do
before(:each) do
@buffer = Thrift::MemoryBufferTransport.new
end
it "should accept a buffer on input and use it directly" do
s = "this is a test"
@buffer = Thrift::MemoryBufferTransport.new(s)
@buffer.read(4).should == "this"
s.slice!(-4..-1)
@buffer.read(@buffer.available).should == " is a "
end
it "should always remain open" do
@buffer.should be_open
@buffer.close
@buffer.should be_open
end
it "should respond to peek and available" do
@buffer.write "some data"
@buffer.peek.should be_true
@buffer.available.should == 9
@buffer.read(4)
@buffer.peek.should be_true
@buffer.available.should == 5
@buffer.read(5)
@buffer.peek.should be_false
@buffer.available.should == 0
end
it "should be able to reset the buffer" do
@buffer.write "test data"
@buffer.reset_buffer("foobar")
@buffer.available.should == 6
@buffer.read(@buffer.available).should == "foobar"
@buffer.reset_buffer
@buffer.available.should == 0
end
it "should copy the given string when resetting the buffer" do
s = "this is a test"
@buffer.reset_buffer(s)
@buffer.available.should == 14
@buffer.read(10)
@buffer.available.should == 4
s.should == "this is a test"
end
it "should return from read what was given in write" do
@buffer.write "test data"
@buffer.read(4).should == "test"
@buffer.read(@buffer.available).should == " data"
@buffer.write "foo"
@buffer.write " bar"
@buffer.read(@buffer.available).should == "foo bar"
end
it "should throw an EOFError when there isn't enough data in the buffer" do
@buffer.reset_buffer("")
lambda{@buffer.read(1)}.should raise_error(EOFError)
@buffer.reset_buffer("1234")
lambda{@buffer.read(5)}.should raise_error(EOFError)
end
end
describe Thrift::IOStreamTransport do
before(:each) do
@input = mock("Input", :closed? => false)
@output = mock("Output", :closed? => false)
@trans = Thrift::IOStreamTransport.new(@input, @output)
end
it "should be open as long as both input or output are open" do
@trans.should be_open
@input.stub!(:closed?).and_return(true)
@trans.should be_open
@input.stub!(:closed?).and_return(false)
@output.stub!(:closed?).and_return(true)
@trans.should be_open
@input.stub!(:closed?).and_return(true)
@trans.should_not be_open
end
it "should pass through read/write to input/output" do
@input.should_receive(:read).with(17).and_return("+ read")
@output.should_receive(:write).with("foobar").and_return("+ write")
@trans.read(17).should == "+ read"
@trans.write("foobar").should == "+ write"
end
it "should close both input and output when closed" do
@input.should_receive(:close)
@output.should_receive(:close)
@trans.close
end
end
end

View file

@ -0,0 +1,42 @@
#
# 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
Thrift::BinaryProtocolAcceleratedFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(Thrift::BinaryProtocolAccelerated)
end
end
end
else
puts "skipping BinaryProtocolAccelerated spec because it is not defined."
end

View file

@ -0,0 +1,66 @@
#
# 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")
describe 'BinaryProtocol' do
it_should_behave_like 'a binary protocol'
def protocol_class
Thrift::BinaryProtocol
end
describe Thrift::BinaryProtocol do
before(:each) do
@trans = Thrift::MemoryBufferTransport.new
@prot = protocol_class.new(@trans)
end
it "should read a message header" do
@trans.write([protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::REPLY].pack('N'))
@trans.write([42].pack('N'))
@prot.should_receive(:read_string).and_return('testMessage')
@prot.read_message_begin.should == ['testMessage', Thrift::MessageTypes::REPLY, 42]
end
it "should raise an exception if the message header has the wrong version" do
@prot.should_receive(:read_i32).and_return(-1)
lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'Missing version identifier') do |e|
e.type == Thrift::ProtocolException::BAD_VERSION
end
end
it "should raise an exception if the message header does not exist and strict_read is enabled" do
@prot.should_receive(:read_i32).and_return(42)
@prot.should_receive(:strict_read).and_return(true)
lambda { @prot.read_message_begin }.should raise_error(Thrift::ProtocolException, 'No version identifier, old protocol client?') do |e|
e.type == Thrift::ProtocolException::BAD_VERSION
end
end
end
describe Thrift::BinaryProtocolFactory do
it "should create a BinaryProtocol" do
Thrift::BinaryProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(Thrift::BinaryProtocol)
end
end
end

View file

@ -0,0 +1,455 @@
# encoding: ascii-8bit
#
# 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 binary protocol' do
before(:each) do
@trans = Thrift::MemoryBufferTransport.new
@prot = protocol_class.new(@trans)
end
it "should define the proper VERSION_1, VERSION_MASK AND TYPE_MASK" do
protocol_class.const_get(:VERSION_MASK).should == 0xffff0000
protocol_class.const_get(:VERSION_1).should == 0x80010000
protocol_class.const_get(:TYPE_MASK).should == 0x000000ff
end
it "should make strict_read readable" do
@prot.strict_read.should eql(true)
end
it "should make strict_write readable" do
@prot.strict_write.should eql(true)
end
it "should write the message header" do
@prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
@trans.read(@trans.available).should == [protocol_class.const_get(:VERSION_1) | Thrift::MessageTypes::CALL, "testMessage".size, "testMessage", 17].pack("NNa11N")
end
it "should write the message header without version when writes are not strict" do
@prot = protocol_class.new(@trans, true, false) # no strict write
@prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
@trans.read(@trans.available).should == "\000\000\000\vtestMessage\001\000\000\000\021"
end
it "should write the message header with a version when writes are strict" do
@prot = protocol_class.new(@trans) # strict write
@prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
@trans.read(@trans.available).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021"
end
# message footer is a noop
it "should write the field header" do
@prot.write_field_begin('foo', Thrift::Types::DOUBLE, 3)
@trans.read(@trans.available).should == [Thrift::Types::DOUBLE, 3].pack("cn")
end
# field footer is a noop
it "should write the STOP field" do
@prot.write_field_stop
@trans.read(1).should == "\000"
end
it "should write the map header" do
@prot.write_map_begin(Thrift::Types::STRING, Thrift::Types::LIST, 17)
@trans.read(@trans.available).should == [Thrift::Types::STRING, Thrift::Types::LIST, 17].pack("ccN");
end
# map footer is a noop
it "should write the list header" do
@prot.write_list_begin(Thrift::Types::I16, 42)
@trans.read(@trans.available).should == [Thrift::Types::I16, 42].pack("cN")
end
# list footer is a noop
it "should write the set header" do
@prot.write_set_begin(Thrift::Types::I16, 42)
@trans.read(@trans.available).should == [Thrift::Types::I16, 42].pack("cN")
end
it "should write a bool" do
@prot.write_bool(true)
@prot.write_bool(false)
@trans.read(@trans.available).should == "\001\000"
end
it "should treat a nil bool as false" do
@prot.write_bool(nil)
@trans.read(1).should == "\000"
end
it "should write a byte" do
# byte is small enough, let's check -128..127
(-128..127).each do |i|
@prot.write_byte(i)
@trans.read(1).should == [i].pack('c')
end
# handing it numbers out of signed range should clip
@trans.rspec_verify
(128..255).each do |i|
@prot.write_byte(i)
@trans.read(1).should == [i].pack('c')
end
# and lastly, a Bignum is going to error out
lambda { @prot.write_byte(2**65) }.should raise_error(RangeError)
end
it "should error gracefully when trying to write a nil byte" do
lambda { @prot.write_byte(nil) }.should raise_error
end
it "should write an i16" do
# try a random scattering of values
# include the signed i16 minimum/maximum
[-2**15, -1024, 17, 0, -10000, 1723, 2**15-1].each do |i|
@prot.write_i16(i)
end
# and try something out of signed range, it should clip
@prot.write_i16(2**15 + 5)
@trans.read(@trans.available).should == "\200\000\374\000\000\021\000\000\330\360\006\273\177\377\200\005"
# a Bignum should error
# lambda { @prot.write_i16(2**65) }.should raise_error(RangeError)
end
it "should error gracefully when trying to write a nil i16" do
lambda { @prot.write_i16(nil) }.should raise_error
end
it "should write an i32" do
# try a random scattering of values
# include the signed i32 minimum/maximum
[-2**31, -123123, -2532, -3, 0, 2351235, 12331, 2**31-1].each do |i|
@prot.write_i32(i)
end
# try something out of signed range, it should clip
@trans.read(@trans.available).should == "\200\000\000\000" + "\377\376\037\r" + "\377\377\366\034" + "\377\377\377\375" + "\000\000\000\000" + "\000#\340\203" + "\000\0000+" + "\177\377\377\377"
[2 ** 31 + 5, 2 ** 65 + 5].each do |i|
lambda { @prot.write_i32(i) }.should raise_error(RangeError)
end
end
it "should error gracefully when trying to write a nil i32" do
lambda { @prot.write_i32(nil) }.should raise_error
end
it "should write an i64" do
# try a random scattering of values
# try the signed i64 minimum/maximum
[-2**63, -12356123612323, -23512351, -234, 0, 1231, 2351236, 12361236213, 2**63-1].each do |i|
@prot.write_i64(i)
end
# try something out of signed range, it should clip
@trans.read(@trans.available).should == ["\200\000\000\000\000\000\000\000",
"\377\377\364\303\035\244+]",
"\377\377\377\377\376\231:\341",
"\377\377\377\377\377\377\377\026",
"\000\000\000\000\000\000\000\000",
"\000\000\000\000\000\000\004\317",
"\000\000\000\000\000#\340\204",
"\000\000\000\002\340\311~\365",
"\177\377\377\377\377\377\377\377"].join("")
lambda { @prot.write_i64(2 ** 65 + 5) }.should raise_error(RangeError)
end
it "should error gracefully when trying to write a nil i64" do
lambda { @prot.write_i64(nil) }.should raise_error
end
it "should write a double" do
# try a random scattering of values, including min/max
values = [Float::MIN,-1231.15325, -123123.23, -23.23515123, 0, 12351.1325, 523.23, Float::MAX]
values.each do |f|
@prot.write_double(f)
@trans.read(@trans.available).should == [f].pack("G")
end
end
it "should error gracefully when trying to write a nil double" do
lambda { @prot.write_double(nil) }.should raise_error
end
if RUBY_VERSION >= '1.9'
it 'should write a string' do
str = 'abc'
@prot.write_string(str)
a = @trans.read(@trans.available)
a.encoding.should == Encoding::BINARY
a.unpack('C*').should == [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63]
end
it 'should write a string with unicode characters' do
str = "abc \u20AC \u20AD".encode('UTF-8')
@prot.write_string(str)
a = @trans.read(@trans.available)
a.encoding.should == Encoding::BINARY
a.unpack('C*').should == [0x00, 0x00, 0x00, 0x0B, 0x61, 0x62, 0x63, 0x20,
0xE2, 0x82, 0xAC, 0x20, 0xE2, 0x82, 0xAD]
end
it 'should write should write a string with unicode characters and transcoding' do
str = "abc \u20AC".encode('ISO-8859-15')
@prot.write_string(str)
a = @trans.read(@trans.available)
a.encoding.should == Encoding::BINARY
a.unpack('C*').should == [0x00, 0x00, 0x00, 0x07, 0x61, 0x62, 0x63, 0x20, 0xE2, 0x82, 0xAC]
end
it 'should write a binary string' do
buffer = [0, 1, 2, 3].pack('C*')
@prot.write_binary(buffer)
a = @trans.read(@trans.available)
a.encoding.should == Encoding::BINARY
a.unpack('C*').should == [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03]
end
else
it 'should write a string' do
str = 'abc'
@prot.write_string(str)
a = @trans.read(@trans.available)
a.unpack('C*').should == [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63]
end
it 'should write a binary string' do
buffer = [0, 1, 2, 3].pack('C*')
@prot.write_binary(buffer)
a = @trans.read(@trans.available)
a.unpack('C*').should == [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03]
end
end
it "should error gracefully when trying to write a nil string" do
lambda { @prot.write_string(nil) }.should raise_error
end
it "should write the message header without version when writes are not strict" do
@prot = protocol_class.new(@trans, true, false) # no strict write
@prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
@trans.read(@trans.available).should == "\000\000\000\vtestMessage\001\000\000\000\021"
end
it "should write the message header with a version when writes are strict" do
@prot = protocol_class.new(@trans) # strict write
@prot.write_message_begin('testMessage', Thrift::MessageTypes::CALL, 17)
@trans.read(@trans.available).should == "\200\001\000\001\000\000\000\vtestMessage\000\000\000\021"
end
# message footer is a noop
it "should read a field header" do
@trans.write([Thrift::Types::STRING, 3].pack("cn"))
@prot.read_field_begin.should == [nil, Thrift::Types::STRING, 3]
end
# field footer is a noop
it "should read a stop field" do
@trans.write([Thrift::Types::STOP].pack("c"));
@prot.read_field_begin.should == [nil, Thrift::Types::STOP, 0]
end
it "should read a map header" do
@trans.write([Thrift::Types::DOUBLE, Thrift::Types::I64, 42].pack("ccN"))
@prot.read_map_begin.should == [Thrift::Types::DOUBLE, Thrift::Types::I64, 42]
end
# map footer is a noop
it "should read a list header" do
@trans.write([Thrift::Types::STRING, 17].pack("cN"))
@prot.read_list_begin.should == [Thrift::Types::STRING, 17]
end
# list footer is a noop
it "should read a set header" do
@trans.write([Thrift::Types::STRING, 17].pack("cN"))
@prot.read_set_begin.should == [Thrift::Types::STRING, 17]
end
# set footer is a noop
it "should read a bool" do
@trans.write("\001\000");
@prot.read_bool.should == true
@prot.read_bool.should == false
end
it "should read a byte" do
[-128, -57, -3, 0, 17, 24, 127].each do |i|
@trans.write([i].pack("c"))
@prot.read_byte.should == i
end
end
it "should read an i16" do
# try a scattering of values, including min/max
[-2**15, -5237, -353, 0, 1527, 2234, 2**15-1].each do |i|
@trans.write([i].pack("n"));
@prot.read_i16.should == i
end
end
it "should read an i32" do
# try a scattering of values, including min/max
[-2**31, -235125, -6236, 0, 2351, 123123, 2**31-1].each do |i|
@trans.write([i].pack("N"))
@prot.read_i32.should == i
end
end
it "should read an i64" do
# try a scattering of values, including min/max
[-2**63, -123512312, -6346, 0, 32, 2346322323, 2**63-1].each do |i|
@trans.write([i >> 32, i & 0xFFFFFFFF].pack("NN"))
@prot.read_i64.should == i
end
end
it "should read a double" do
# try a random scattering of values, including min/max
[Float::MIN, -231231.12351, -323.233513, 0, 123.2351235, 2351235.12351235, Float::MAX].each do |f|
@trans.write([f].pack("G"));
@prot.read_double.should == f
end
end
if RUBY_VERSION >= '1.9'
it 'should read a string' do
# i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
@trans.write(buffer)
a = @prot.read_string
a.should == 'abc'.encode('UTF-8')
a.encoding.should == Encoding::UTF_8
end
it 'should read a string containing unicode characters from UTF-8 encoded buffer' do
# i32 of value 3, followed by one character U+20AC made up of three bytes
buffer = [0x00, 0x00, 0x00, 0x03, 0xE2, 0x82, 0xAC].pack('C*')
@trans.write(buffer)
a = @prot.read_string
a.should == "\u20AC".encode('UTF-8')
a.encoding.should == Encoding::UTF_8
end
it 'should read a binary string' do
buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
@trans.write(buffer)
a = @prot.read_binary
a.should == [0x00, 0x01, 0x02, 0x03].pack('C*')
a.encoding.should == Encoding::BINARY
end
else
it 'should read a string' do
# i32 of value 3, followed by three characters/UTF-8 bytes 'a', 'b', 'c'
buffer = [0x00, 0x00, 0x00, 0x03, 0x61, 0x62, 0x63].pack('C*')
@trans.write(buffer)
@prot.read_string.should == 'abc'
end
it 'should read a binary string' do
buffer = [0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x02, 0x03].pack('C*')
@trans.write(buffer)
a = @prot.read_binary
a.should == [0x00, 0x01, 0x02, 0x03].pack('C*')
end
end
it "should perform a complete rpc with no args or return" do
srv_test(
proc {|client| client.send_voidMethod()},
proc {|client| client.recv_voidMethod.should == nil}
)
end
it "should perform a complete rpc with a primitive return type" do
srv_test(
proc {|client| client.send_primitiveMethod()},
proc {|client| client.recv_primitiveMethod.should == 1}
)
end
it "should perform a complete rpc with a struct return type" do
srv_test(
proc {|client| client.send_structMethod()},
proc {|client|
result = client.recv_structMethod
result.set_byte_map = nil
result.map_byte_map = nil
result.should == Fixtures::COMPACT_PROTOCOL_TEST_STRUCT
}
)
end
def get_socket_connection
server = Thrift::ServerSocket.new("localhost", 9090)
server.listen
clientside = Thrift::Socket.new("localhost", 9090)
clientside.open
serverside = server.accept
[clientside, serverside, server]
end
def srv_test(firstblock, secondblock)
clientside, serverside, server = get_socket_connection
clientproto = protocol_class.new(clientside)
serverproto = protocol_class.new(serverside)
processor = Thrift::Test::Srv::Processor.new(SrvHandler.new)
client = Thrift::Test::Srv::Client.new(clientproto, clientproto)
# first block
firstblock.call(client)
processor.process(serverproto, serverproto)
# second block
secondblock.call(client)
ensure
clientside.close
serverside.close
server.close
end
class SrvHandler
def voidMethod()
end
def primitiveMethod
1
end
def structMethod
Fixtures::COMPACT_PROTOCOL_TEST_STRUCT
end
end
end

View file

@ -0,0 +1,160 @@
# 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 Thrift::Bytes do
if RUBY_VERSION >= '1.9'
describe '.empty_byte_buffer' do
it 'should create an empty buffer' do
b = Thrift::Bytes.empty_byte_buffer
b.length.should == 0
b.encoding.should == Encoding::BINARY
end
it 'should create an empty buffer of given size' do
b = Thrift::Bytes.empty_byte_buffer 2
b.length.should == 2
b.getbyte(0).should == 0
b.getbyte(1).should == 0
b.encoding.should == Encoding::BINARY
end
end
describe '.force_binary_encoding' do
it 'should change encoding' do
e = 'STRING'.encode('UTF-8')
e.encoding.should_not == Encoding::BINARY
a = Thrift::Bytes.force_binary_encoding e
a.encoding.should == Encoding::BINARY
end
end
describe '.get_string_byte' do
it 'should get the byte at index' do
s = "\x41\x42"
Thrift::Bytes.get_string_byte(s, 0).should == 0x41
Thrift::Bytes.get_string_byte(s, 1).should == 0x42
end
end
describe '.set_string_byte' do
it 'should set byte value at index' do
s = "\x41\x42"
Thrift::Bytes.set_string_byte(s, 0, 0x43)
s.getbyte(0).should == 0x43
s.should == 'CB'
end
end
describe '.convert_to_utf8_byte_buffer' do
it 'should convert UTF-8 String to byte buffer' do
e = "\u20AC".encode('UTF-8') # a string with euro sign character U+20AC
e.length.should == 1
a = Thrift::Bytes.convert_to_utf8_byte_buffer e
a.encoding.should == Encoding::BINARY
a.length.should == 3
a.unpack('C*').should == [0xE2, 0x82, 0xAC]
end
it 'should convert ISO-8859-15 String to UTF-8 byte buffer' do
# Assumptions
e = "\u20AC".encode('ISO-8859-15') # a string with euro sign character U+20AC, then converted to ISO-8859-15
e.length.should == 1
e.unpack('C*').should == [0xA4] # euro sign is a different code point in ISO-8859-15
a = Thrift::Bytes.convert_to_utf8_byte_buffer e
a.encoding.should == Encoding::BINARY
a.length.should == 3
a.unpack('C*').should == [0xE2, 0x82, 0xAC]
end
end
describe '.convert_to_string' do
it 'should convert UTF-8 byte buffer to a UTF-8 String' do
e = [0xE2, 0x82, 0xAC].pack("C*")
e.encoding.should == Encoding::BINARY
a = Thrift::Bytes.convert_to_string e
a.encoding.should == Encoding::UTF_8
a.should == "\u20AC"
end
end
else # RUBY_VERSION
describe '.empty_byte_buffer' do
it 'should create an empty buffer' do
b = Thrift::Bytes.empty_byte_buffer
b.length.should == 0
end
it 'should create an empty buffer of given size' do
b = Thrift::Bytes.empty_byte_buffer 2
b.length.should == 2
b[0].should == 0
b[1].should == 0
end
end
describe '.force_binary_encoding' do
it 'should be a no-op' do
e = 'STRING'
a = Thrift::Bytes.force_binary_encoding e
a.should == e
a.should be(e)
end
end
describe '.get_string_byte' do
it 'should get the byte at index' do
s = "\x41\x42"
Thrift::Bytes.get_string_byte(s, 0).should == 0x41
Thrift::Bytes.get_string_byte(s, 1).should == 0x42
end
end
describe '.set_string_byte' do
it 'should set byte value at index' do
s = "\x41\x42"
Thrift::Bytes.set_string_byte(s, 0, 0x43)
s[0].should == 0x43
s.should == 'CB'
end
end
describe '.convert_to_utf8_byte_buffer' do
it 'should be a no-op' do
e = 'STRING'
a = Thrift::Bytes.convert_to_utf8_byte_buffer e
a.should == e
a.should be(e)
end
end
describe '.convert_to_string' do
it 'should be a no-op' do
e = 'STRING'
a = Thrift::Bytes.convert_to_string e
a.should == e
a.should be(e)
end
end
end
end

View file

@ -0,0 +1,99 @@
#
# 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 'Client' do
class ClientSpec
include Thrift::Client
end
before(:each) do
@prot = mock("MockProtocol")
@client = ClientSpec.new(@prot)
end
describe Thrift::Client do
it "should re-use iprot for oprot if not otherwise specified" do
@client.instance_variable_get(:'@iprot').should eql(@prot)
@client.instance_variable_get(:'@oprot').should eql(@prot)
end
it "should send a test message" do
@prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0)
mock_args = mock('#<TestMessage_args:mock>')
mock_args.should_receive(:foo=).with('foo')
mock_args.should_receive(:bar=).with(42)
mock_args.should_receive(:write).with(@prot)
@prot.should_receive(:write_message_end)
@prot.should_receive(:trans) do
mock('trans').tap do |trans|
trans.should_receive(:flush)
end
end
klass = stub("TestMessage_args", :new => mock_args)
@client.send_message('testMessage', klass, :foo => 'foo', :bar => 42)
end
it "should increment the sequence id when sending messages" do
pending "it seems sequence ids are completely ignored right now" do
@prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::CALL, 0).ordered
@prot.should_receive(:write_message_begin).with('testMessage2', Thrift::MessageTypes::CALL, 1).ordered
@prot.should_receive(:write_message_begin).with('testMessage3', Thrift::MessageTypes::CALL, 2).ordered
@prot.stub!(:write_message_end)
@prot.stub!(:trans).and_return mock("trans").as_null_object
@client.send_message('testMessage', mock("args class").as_null_object)
@client.send_message('testMessage2', mock("args class").as_null_object)
@client.send_message('testMessage3', mock("args class").as_null_object)
end
end
it "should receive a test message" do
@prot.should_receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::CALL, 0]
@prot.should_receive(:read_message_end)
mock_klass = mock("#<MockClass:mock>")
mock_klass.should_receive(:read).with(@prot)
@client.receive_message(stub("MockClass", :new => mock_klass))
end
it "should handle received exceptions" do
@prot.should_receive(:read_message_begin).and_return [nil, Thrift::MessageTypes::EXCEPTION, 0]
@prot.should_receive(:read_message_end)
Thrift::ApplicationException.should_receive(:new).and_return do
StandardError.new.tap do |mock_exc|
mock_exc.should_receive(:read).with(@prot)
end
end
lambda { @client.receive_message(nil) }.should raise_error(StandardError)
end
it "should close the transport if an error occurs while sending a message" do
@prot.stub!(:write_message_begin)
@prot.should_not_receive(:write_message_end)
mock_args = mock("#<TestMessage_args:mock>")
mock_args.should_receive(:write).with(@prot).and_raise(StandardError)
trans = mock("MockTransport")
@prot.stub!(:trans).and_return(trans)
trans.should_receive(:close)
klass = mock("TestMessage_args", :new => mock_args)
lambda { @client.send_message("testMessage", klass) }.should raise_error(StandardError)
end
end
end

View file

@ -0,0 +1,143 @@
# 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 Thrift::CompactProtocol do
TESTS = {
:byte => (-127..127).to_a,
:i16 => (0..14).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort,
:i32 => (0..30).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort,
:i64 => (0..62).map {|shift| [1 << shift, -(1 << shift)]}.flatten.sort,
:string => ["", "1", "short", "fourteen123456", "fifteen12345678", "unicode characters: \u20AC \u20AD", "1" * 127, "1" * 3000],
:binary => ["", "\001", "\001" * 5, "\001" * 14, "\001" * 15, "\001" * 127, "\001" * 3000],
:double => [0.0, 1.0, -1.0, 1.1, -1.1, 10000000.1, 1.0/0.0, -1.0/0.0],
:bool => [true, false]
}
it "should encode and decode naked primitives correctly" do
TESTS.each_pair do |primitive_type, test_values|
test_values.each do |value|
# puts "testing #{value}" if primitive_type == :i64
trans = Thrift::MemoryBufferTransport.new
proto = Thrift::CompactProtocol.new(trans)
proto.send(writer(primitive_type), value)
# puts "buf: #{trans.inspect_buffer}" if primitive_type == :i64
read_back = proto.send(reader(primitive_type))
read_back.should == value
end
end
end
it "should encode and decode primitives in fields correctly" do
TESTS.each_pair do |primitive_type, test_values|
final_primitive_type = primitive_type == :binary ? :string : primitive_type
thrift_type = Thrift::Types.const_get(final_primitive_type.to_s.upcase)
# puts primitive_type
test_values.each do |value|
trans = Thrift::MemoryBufferTransport.new
proto = Thrift::CompactProtocol.new(trans)
proto.write_field_begin(nil, thrift_type, 15)
proto.send(writer(primitive_type), value)
proto.write_field_end
proto = Thrift::CompactProtocol.new(trans)
name, type, id = proto.read_field_begin
type.should == thrift_type
id.should == 15
read_back = proto.send(reader(primitive_type))
read_back.should == value
proto.read_field_end
end
end
end
it "should encode and decode a monster struct correctly" do
trans = Thrift::MemoryBufferTransport.new
proto = Thrift::CompactProtocol.new(trans)
struct = Thrift::Test::CompactProtoTestStruct.new
# sets and maps don't hash well... not sure what to do here.
struct.write(proto)
struct2 = Thrift::Test::CompactProtoTestStruct.new
struct2.read(proto)
struct2.should == struct
end
it "should make method calls correctly" do
client_out_trans = Thrift::MemoryBufferTransport.new
client_out_proto = Thrift::CompactProtocol.new(client_out_trans)
client_in_trans = Thrift::MemoryBufferTransport.new
client_in_proto = Thrift::CompactProtocol.new(client_in_trans)
processor = Thrift::Test::Srv::Processor.new(JankyHandler.new)
client = Thrift::Test::Srv::Client.new(client_in_proto, client_out_proto)
client.send_Janky(1)
# puts client_out_trans.inspect_buffer
processor.process(client_out_proto, client_in_proto)
client.recv_Janky.should == 2
end
it "should deal with fields following fields that have non-delta ids" do
brcp = Thrift::Test::BreaksRubyCompactProtocol.new(
:field1 => "blah",
:field2 => Thrift::Test::BigFieldIdStruct.new(
:field1 => "string1",
:field2 => "string2"),
:field3 => 3)
ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new)
bytes = ser.serialize(brcp)
deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new)
brcp2 = Thrift::Test::BreaksRubyCompactProtocol.new
deser.deserialize(brcp2, bytes)
brcp2.should == brcp
end
it "should deserialize an empty map to an empty hash" do
struct = Thrift::Test::SingleMapTestStruct.new(:i32_map => {})
ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new)
bytes = ser.serialize(struct)
deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new)
struct2 = Thrift::Test::SingleMapTestStruct.new
deser.deserialize(struct2, bytes)
struct.should == struct2
end
class JankyHandler
def Janky(i32arg)
i32arg * 2
end
end
def writer(sym)
"write_#{sym.to_s}"
end
def reader(sym)
"read_#{sym.to_s}"
end
end

View file

@ -0,0 +1,141 @@
#
# 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 'Exception' do
describe Thrift::Exception do
it "should have an accessible message" do
e = Thrift::Exception.new("test message")
e.message.should == "test message"
end
end
describe Thrift::ApplicationException do
it "should inherit from Thrift::Exception" do
Thrift::ApplicationException.superclass.should == Thrift::Exception
end
it "should have an accessible type and message" do
e = Thrift::ApplicationException.new
e.type.should == Thrift::ApplicationException::UNKNOWN
e.message.should be_nil
e = Thrift::ApplicationException.new(Thrift::ApplicationException::UNKNOWN_METHOD, "test message")
e.type.should == Thrift::ApplicationException::UNKNOWN_METHOD
e.message.should == "test message"
end
it "should read a struct off of a protocol" do
prot = mock("MockProtocol")
prot.should_receive(:read_struct_begin).ordered
prot.should_receive(:read_field_begin).exactly(3).times.and_return(
["message", Thrift::Types::STRING, 1],
["type", Thrift::Types::I32, 2],
[nil, Thrift::Types::STOP, 0]
)
prot.should_receive(:read_string).ordered.and_return "test message"
prot.should_receive(:read_i32).ordered.and_return Thrift::ApplicationException::BAD_SEQUENCE_ID
prot.should_receive(:read_field_end).exactly(2).times
prot.should_receive(:read_struct_end).ordered
e = Thrift::ApplicationException.new
e.read(prot)
e.message.should == "test message"
e.type.should == Thrift::ApplicationException::BAD_SEQUENCE_ID
end
it "should skip bad fields when reading a struct" do
prot = mock("MockProtocol")
prot.should_receive(:read_struct_begin).ordered
prot.should_receive(:read_field_begin).exactly(5).times.and_return(
["type", Thrift::Types::I32, 2],
["type", Thrift::Types::STRING, 2],
["message", Thrift::Types::MAP, 1],
["message", Thrift::Types::STRING, 3],
[nil, Thrift::Types::STOP, 0]
)
prot.should_receive(:read_i32).and_return Thrift::ApplicationException::INVALID_MESSAGE_TYPE
prot.should_receive(:skip).with(Thrift::Types::STRING).twice
prot.should_receive(:skip).with(Thrift::Types::MAP)
prot.should_receive(:read_field_end).exactly(4).times
prot.should_receive(:read_struct_end).ordered
e = Thrift::ApplicationException.new
e.read(prot)
e.message.should be_nil
e.type.should == Thrift::ApplicationException::INVALID_MESSAGE_TYPE
end
it "should write a Thrift::ApplicationException struct to the oprot" do
prot = mock("MockProtocol")
prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
prot.should_receive(:write_field_begin).with("message", Thrift::Types::STRING, 1).ordered
prot.should_receive(:write_string).with("test message").ordered
prot.should_receive(:write_field_begin).with("type", Thrift::Types::I32, 2).ordered
prot.should_receive(:write_i32).with(Thrift::ApplicationException::UNKNOWN_METHOD).ordered
prot.should_receive(:write_field_end).twice
prot.should_receive(:write_field_stop).ordered
prot.should_receive(:write_struct_end).ordered
e = Thrift::ApplicationException.new(Thrift::ApplicationException::UNKNOWN_METHOD, "test message")
e.write(prot)
end
it "should skip nil fields when writing to the oprot" do
prot = mock("MockProtocol")
prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
prot.should_receive(:write_field_begin).with("message", Thrift::Types::STRING, 1).ordered
prot.should_receive(:write_string).with("test message").ordered
prot.should_receive(:write_field_end).ordered
prot.should_receive(:write_field_stop).ordered
prot.should_receive(:write_struct_end).ordered
e = Thrift::ApplicationException.new(nil, "test message")
e.write(prot)
prot = mock("MockProtocol")
prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
prot.should_receive(:write_field_begin).with("type", Thrift::Types::I32, 2).ordered
prot.should_receive(:write_i32).with(Thrift::ApplicationException::BAD_SEQUENCE_ID).ordered
prot.should_receive(:write_field_end).ordered
prot.should_receive(:write_field_stop).ordered
prot.should_receive(:write_struct_end).ordered
e = Thrift::ApplicationException.new(Thrift::ApplicationException::BAD_SEQUENCE_ID)
e.write(prot)
prot = mock("MockProtocol")
prot.should_receive(:write_struct_begin).with("Thrift::ApplicationException").ordered
prot.should_receive(:write_field_stop).ordered
prot.should_receive(:write_struct_end).ordered
e = Thrift::ApplicationException.new(nil)
e.write(prot)
end
end
describe Thrift::ProtocolException do
it "should have an accessible type" do
prot = Thrift::ProtocolException.new(Thrift::ProtocolException::SIZE_LIMIT, "message")
prot.type.should == Thrift::ProtocolException::SIZE_LIMIT
prot.message.should == "message"
end
end
end

View file

@ -0,0 +1,62 @@
#
# 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 'generation' do
before do
require 'namespaced_nonblocking_service'
end
it "did not generate the wrong files" do
prefix = File.expand_path("../gen-rb/flat", __FILE__)
["namespaced_spec_namespace/namespaced_nonblocking_service.rb",
"namespaced_spec_namespace/thrift_namespaced_spec_constants.rb",
"namespaced_spec_namespace/thrift_namespaced_spec_types.rb",
"other_namespace/referenced_constants.rb",
"other_namespace/referenced_types.rb"
].each do |name|
File.exist?(File.join(prefix, name)).should_not be_true
end
end
it "generated the right files" do
prefix = File.expand_path("../gen-rb/flat", __FILE__)
["namespaced_nonblocking_service.rb",
"thrift_namespaced_spec_constants.rb",
"thrift_namespaced_spec_types.rb",
"referenced_constants.rb",
"referenced_types.rb"
].each do |name|
File.exist?(File.join(prefix, name)).should be_true
end
end
it "has a service class in the right place" do
defined?(NamespacedSpecNamespace::NamespacedNonblockingService).should be_true
end
it "has a struct in the right place" do
defined?(NamespacedSpecNamespace::Hello).should be_true
end
it "required an included file" do
defined?(OtherNamespace::SomeEnum).should be_true
end
end

View file

@ -0,0 +1,135 @@
#
# 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 'Thrift::HTTPClientTransport' do
describe Thrift::HTTPClientTransport do
before(:each) do
@client = Thrift::HTTPClientTransport.new("http://my.domain.com/path/to/service?param=value")
end
it "should always be open" do
@client.should be_open
@client.close
@client.should be_open
end
it "should post via HTTP and return the results" do
@client.write "a test"
@client.write " frame"
Net::HTTP.should_receive(:new).with("my.domain.com", 80).and_return do
mock("Net::HTTP").tap do |http|
http.should_receive(:use_ssl=).with(false)
http.should_receive(:post).with("/path/to/service?param=value", "a test frame", {"Content-Type"=>"application/x-thrift"}).and_return do
mock("Net::HTTPOK").tap do |response|
response.should_receive(:body).and_return "data"
end
end
end
end
@client.flush
@client.read(10).should == "data"
end
it "should send custom headers if defined" do
@client.write "test"
custom_headers = {"Cookie" => "Foo"}
headers = {"Content-Type"=>"application/x-thrift"}.merge(custom_headers)
@client.add_headers(custom_headers)
Net::HTTP.should_receive(:new).with("my.domain.com", 80).and_return do
mock("Net::HTTP").tap do |http|
http.should_receive(:use_ssl=).with(false)
http.should_receive(:post).with("/path/to/service?param=value", "test", headers).and_return do
mock("Net::HTTPOK").tap do |response|
response.should_receive(:body).and_return "data"
end
end
end
end
@client.flush
end
it 'should reset the outbuf on HTTP failures' do
@client.write "test"
Net::HTTP.should_receive(:new).with("my.domain.com", 80).and_return do
mock("Net::HTTP").tap do |http|
http.should_receive(:use_ssl=).with(false)
http.should_receive(:post).with("/path/to/service?param=value", "test", {"Content-Type"=>"application/x-thrift"}) { raise Net::ReadTimeout }
end
end
@client.flush rescue
@client.instance_variable_get(:@outbuf).should eq(Thrift::Bytes.empty_byte_buffer)
end
end
describe 'ssl enabled' do
before(:each) do
@service_path = "/path/to/service?param=value"
@server_uri = "https://my.domain.com"
end
it "should use SSL for https" do
client = Thrift::HTTPClientTransport.new("#{@server_uri}#{@service_path}")
client.write "test"
Net::HTTP.should_receive(:new).with("my.domain.com", 443).and_return do
mock("Net::HTTP").tap do |http|
http.should_receive(:use_ssl=).with(true)
http.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
http.should_receive(:post).with(@service_path, "test",
"Content-Type" => "application/x-thrift").and_return do
mock("Net::HTTPOK").tap do |response|
response.should_receive(:body).and_return "data"
end
end
end
end
client.flush
client.read(4).should == "data"
end
it "should set SSL verify mode when specified" do
client = Thrift::HTTPClientTransport.new("#{@server_uri}#{@service_path}",
:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE)
client.write "test"
Net::HTTP.should_receive(:new).with("my.domain.com", 443).and_return do
mock("Net::HTTP").tap do |http|
http.should_receive(:use_ssl=).with(true)
http.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
http.should_receive(:post).with(@service_path, "test",
"Content-Type" => "application/x-thrift").and_return do
mock("Net::HTTPOK").tap do |response|
response.should_receive(:body).and_return "data"
end
end
end
end
client.flush
client.read(4).should == "data"
end
end
end

View file

@ -0,0 +1,544 @@
# 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")
@trans.read(@trans.available).should == '\u000a'
@prot.write_json_escape_char(" ")
@trans.read(@trans.available).should == '\u0020'
end
it "should write json char" do
@prot.write_json_char("\n")
@trans.read(@trans.available).should == '\\n'
@prot.write_json_char(" ")
@trans.read(@trans.available).should == ' '
@prot.write_json_char("\\")
@trans.read(@trans.available).should == "\\\\"
@prot.write_json_char("@")
@trans.read(@trans.available).should == '@'
end
it "should write json string" do
@prot.write_json_string("this is a \\ json\nstring")
@trans.read(@trans.available).should == "\"this is a \\\\ json\\nstring\""
end
it "should write json base64" do
@prot.write_json_base64("this is a base64 string")
@trans.read(@trans.available).should == "\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\""
end
it "should write json integer" do
@prot.write_json_integer(45)
@trans.read(@trans.available).should == "45"
@prot.write_json_integer(33000)
@trans.read(@trans.available).should == "33000"
@prot.write_json_integer(3000000000)
@trans.read(@trans.available).should == "3000000000"
@prot.write_json_integer(6000000000)
@trans.read(@trans.available).should == "6000000000"
end
it "should write json double" do
@prot.write_json_double(12.3)
@trans.read(@trans.available).should == "12.3"
@prot.write_json_double(-3.21)
@trans.read(@trans.available).should == "-3.21"
@prot.write_json_double(((+1.0/0.0)/(+1.0/0.0)))
@trans.read(@trans.available).should == "\"NaN\""
@prot.write_json_double((+1.0/0.0))
@trans.read(@trans.available).should == "\"Infinity\""
@prot.write_json_double((-1.0/0.0))
@trans.read(@trans.available).should == "\"-Infinity\""
end
it "should write json object start" do
@prot.write_json_object_start
@trans.read(@trans.available).should == "{"
end
it "should write json object end" do
@prot.write_json_object_end
@trans.read(@trans.available).should == "}"
end
it "should write json array start" do
@prot.write_json_array_start
@trans.read(@trans.available).should == "["
end
it "should write json array end" do
@prot.write_json_array_end
@trans.read(@trans.available).should == "]"
end
it "should write message begin" do
@prot.write_message_begin("name", 12, 32)
@trans.read(@trans.available).should == "[1,\"name\",12,32"
end
it "should write message end" do
@prot.write_message_end
@trans.read(@trans.available).should == "]"
end
it "should write struct begin" do
@prot.write_struct_begin("name")
@trans.read(@trans.available).should == "{"
end
it "should write struct end" do
@prot.write_struct_end
@trans.read(@trans.available).should == "}"
end
it "should write field begin" do
@prot.write_field_begin("name", Thrift::Types::STRUCT, 32)
@trans.read(@trans.available).should == "32{\"rec\""
end
it "should write field end" do
@prot.write_field_end
@trans.read(@trans.available).should == "}"
end
it "should write field stop" do
@prot.write_field_stop
@trans.read(@trans.available).should == ""
end
it "should write map begin" do
@prot.write_map_begin(Thrift::Types::STRUCT, Thrift::Types::LIST, 32)
@trans.read(@trans.available).should == "[\"rec\",\"lst\",32,{"
end
it "should write map end" do
@prot.write_map_end
@trans.read(@trans.available).should == "}]"
end
it "should write list begin" do
@prot.write_list_begin(Thrift::Types::STRUCT, 32)
@trans.read(@trans.available).should == "[\"rec\",32"
end
it "should write list end" do
@prot.write_list_end
@trans.read(@trans.available).should == "]"
end
it "should write set begin" do
@prot.write_set_begin(Thrift::Types::STRUCT, 32)
@trans.read(@trans.available).should == "[\"rec\",32"
end
it "should write set end" do
@prot.write_set_end
@trans.read(@trans.available).should == "]"
end
it "should write bool" do
@prot.write_bool(true)
@trans.read(@trans.available).should == "1"
@prot.write_bool(false)
@trans.read(@trans.available).should == "0"
end
it "should write byte" do
@prot.write_byte(100)
@trans.read(@trans.available).should == "100"
end
it "should write i16" do
@prot.write_i16(1000)
@trans.read(@trans.available).should == "1000"
end
it "should write i32" do
@prot.write_i32(3000000000)
@trans.read(@trans.available).should == "3000000000"
end
it "should write i64" do
@prot.write_i64(6000000000)
@trans.read(@trans.available).should == "6000000000"
end
it "should write double" do
@prot.write_double(1.23)
@trans.read(@trans.available).should == "1.23"
@prot.write_double(-32.1)
@trans.read(@trans.available).should == "-32.1"
@prot.write_double(((+1.0/0.0)/(+1.0/0.0)))
@trans.read(@trans.available).should == "\"NaN\""
@prot.write_double((+1.0/0.0))
@trans.read(@trans.available).should == "\"Infinity\""
@prot.write_double((-1.0/0.0))
@trans.read(@trans.available).should == "\"-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)
a.should == '"this is a test string"'.force_encoding(Encoding::BINARY)
a.encoding.should == 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)
a.should == "\"this is a test string with unicode characters: \u20AC \u20AD\"".force_encoding(Encoding::BINARY)
a.encoding.should == Encoding::BINARY
end
else
it 'should write string' do
@prot.write_string('this is a test string')
@trans.read(@trans.available).should == '"this is a test string"'
end
end
it "should write binary" do
@prot.write_binary("this is a base64 string")
@trans.read(@trans.available).should == "\"dGhpcyBpcyBhIGJhc2U2NCBzdHJpbmc=\""
end
it "should write long binary" do
@prot.write_binary((0...256).to_a.pack('C*'))
@trans.read(@trans.available).should == "\"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)
@prot.get_type_name_for_type_id(Thrift::Types::BOOL).should == "tf"
@prot.get_type_name_for_type_id(Thrift::Types::BYTE).should == "i8"
@prot.get_type_name_for_type_id(Thrift::Types::DOUBLE).should == "dbl"
@prot.get_type_name_for_type_id(Thrift::Types::I16).should == "i16"
@prot.get_type_name_for_type_id(Thrift::Types::I32).should == "i32"
@prot.get_type_name_for_type_id(Thrift::Types::I64).should == "i64"
@prot.get_type_name_for_type_id(Thrift::Types::STRING).should == "str"
@prot.get_type_name_for_type_id(Thrift::Types::STRUCT).should == "rec"
@prot.get_type_name_for_type_id(Thrift::Types::MAP).should == "map"
@prot.get_type_name_for_type_id(Thrift::Types::SET).should == "set"
@prot.get_type_name_for_type_id(Thrift::Types::LIST).should == "lst"
end
it "should get type id for type name" do
expect {@prot.get_type_id_for_type_name("pp")}.to raise_error(NotImplementedError)
@prot.get_type_id_for_type_name("tf").should == Thrift::Types::BOOL
@prot.get_type_id_for_type_name("i8").should == Thrift::Types::BYTE
@prot.get_type_id_for_type_name("dbl").should == Thrift::Types::DOUBLE
@prot.get_type_id_for_type_name("i16").should == Thrift::Types::I16
@prot.get_type_id_for_type_name("i32").should == Thrift::Types::I32
@prot.get_type_id_for_type_name("i64").should == Thrift::Types::I64
@prot.get_type_id_for_type_name("str").should == Thrift::Types::STRING
@prot.get_type_id_for_type_name("rec").should == Thrift::Types::STRUCT
@prot.get_type_id_for_type_name("map").should == Thrift::Types::MAP
@prot.get_type_id_for_type_name("set").should == Thrift::Types::SET
@prot.get_type_id_for_type_name("lst").should == 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')
@prot.read_json_escape_char.should == 'T'
@trans.write("\"\\\"\"")
@prot.read_json_string(false).should == "\""
@trans.write("\"\\\\\"")
@prot.read_json_string(false).should == "\\"
@trans.write("\"\\/\"")
@prot.read_json_string(false).should == "\/"
@trans.write("\"\\b\"")
@prot.read_json_string(false).should == "\b"
@trans.write("\"\\f\"")
@prot.read_json_string(false).should == "\f"
@trans.write("\"\\n\"")
@prot.read_json_string(false).should == "\n"
@trans.write("\"\\r\"")
@prot.read_json_string(false).should == "\r"
@trans.write("\"\\t\"")
@prot.read_json_string(false).should == "\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\"")
@prot.read_json_string.should == "this is a test string"
end
it "should read json base64" do
@trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
@prot.read_json_base64.should == "this is a test string"
end
it "should is json numeric" do
@prot.is_json_numeric("A").should == false
@prot.is_json_numeric("+").should == true
@prot.is_json_numeric("-").should == true
@prot.is_json_numeric(".").should == true
@prot.is_json_numeric("0").should == true
@prot.is_json_numeric("1").should == true
@prot.is_json_numeric("2").should == true
@prot.is_json_numeric("3").should == true
@prot.is_json_numeric("4").should == true
@prot.is_json_numeric("5").should == true
@prot.is_json_numeric("6").should == true
@prot.is_json_numeric("7").should == true
@prot.is_json_numeric("8").should == true
@prot.is_json_numeric("9").should == true
@prot.is_json_numeric("E").should == true
@prot.is_json_numeric("e").should == true
end
it "should read json numeric chars" do
@trans.write("1.453E45T")
@prot.read_json_numeric_chars.should == "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")
@prot.read_json_integer.should == 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\"\"")
@prot.read_json_double.should == 14.53
@prot.read_string
@trans.write("\"NaN\"")
@prot.read_json_double.nan?.should == true
@trans.write("\"Infinity\"")
@prot.read_json_double.should == +1.0/0.0
@trans.write("\"-Infinity\"")
@prot.read_json_double.should == -1.0/0.0
end
it "should read json object start" do
@trans.write("{")
@prot.read_json_object_start.should == nil
end
it "should read json object end" do
@trans.write("}")
@prot.read_json_object_end.should == nil
end
it "should read json array start" do
@trans.write("[")
@prot.read_json_array_start.should == nil
end
it "should read json array end" do
@trans.write("]")
@prot.read_json_array_end.should == 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\"\"")
@prot.read_message_begin.should == ["name", 12, 32]
end
it "should read message end" do
@trans.write("]")
@prot.read_message_end.should == nil
end
it "should read struct begin" do
@trans.write("{")
@prot.read_struct_begin.should == nil
end
it "should read struct end" do
@trans.write("}")
@prot.read_struct_end.should == nil
end
it "should read field begin" do
@trans.write("1{\"rec\"")
@prot.read_field_begin.should == [nil, 12, 1]
end
it "should read field end" do
@trans.write("}")
@prot.read_field_end.should == nil
end
it "should read map begin" do
@trans.write("[\"rec\",\"lst\",2,{")
@prot.read_map_begin.should == [12, 15, 2]
end
it "should read map end" do
@trans.write("}]")
@prot.read_map_end.should == nil
end
it "should read list begin" do
@trans.write("[\"rec\",2\"\"")
@prot.read_list_begin.should == [12, 2]
end
it "should read list end" do
@trans.write("]")
@prot.read_list_end.should == nil
end
it "should read set begin" do
@trans.write("[\"rec\",2\"\"")
@prot.read_set_begin.should == [12, 2]
end
it "should read set end" do
@trans.write("]")
@prot.read_set_end.should == nil
end
it "should read bool" do
@trans.write("0\"\"")
@prot.read_bool.should == false
@prot.read_string
@trans.write("1\"\"")
@prot.read_bool.should == true
end
it "should read byte" do
@trans.write("60\"\"")
@prot.read_byte.should == 60
end
it "should read i16" do
@trans.write("1000\"\"")
@prot.read_i16.should == 1000
end
it "should read i32" do
@trans.write("3000000000\"\"")
@prot.read_i32.should == 3000000000
end
it "should read i64" do
@trans.write("6000000000\"\"")
@prot.read_i64.should == 6000000000
end
it "should read double" do
@trans.write("12.23\"\"")
@prot.read_double.should == 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
a.should == 'this is a test string'
a.encoding.should == 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
a.should == "this is a test string with unicode characters: \u20AC \u20AD"
a.encoding.should == Encoding::UTF_8
end
else
it 'should read string' do
@trans.write('"this is a test string"')
@prot.read_string.should == 'this is a test string'
end
end
it "should read binary" do
@trans.write("\"dGhpcyBpcyBhIHRlc3Qgc3RyaW5n\"")
@prot.read_binary.should == "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==\"")
@prot.read_binary.bytes.to_a.should == (0...256).to_a
end
end
describe Thrift::JsonProtocolFactory do
it "should create a JsonProtocol" do
Thrift::JsonProtocolFactory.new.get_protocol(mock("MockTransport")).should be_instance_of(Thrift::JsonProtocol)
end
end
end

View file

@ -0,0 +1,67 @@
#
# 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 'namespaced generation' do
before do
require 'namespaced_spec_namespace/namespaced_nonblocking_service'
end
it "generated the right files" do
prefix = File.expand_path("../gen-rb", __FILE__)
["namespaced_spec_namespace/namespaced_nonblocking_service.rb",
"namespaced_spec_namespace/thrift_namespaced_spec_constants.rb",
"namespaced_spec_namespace/thrift_namespaced_spec_types.rb",
"other_namespace/referenced_constants.rb",
"other_namespace/referenced_types.rb"
].each do |name|
File.exist?(File.join(prefix, name)).should be_true
end
end
it "did not generate the wrong files" do
prefix = File.expand_path("../gen-rb", __FILE__)
["namespaced_nonblocking_service.rb",
"thrift_namespaced_spec_constants.rb",
"thrift_namespaced_spec_types.rb",
"referenced_constants.rb",
"referenced_types.rb"
].each do |name|
File.exist?(File.join(prefix, name)).should_not be_true
end
end
it "has a service class in the right place" do
defined?(NamespacedSpecNamespace::NamespacedNonblockingService).should be_true
end
it "has a struct in the right place" do
defined?(NamespacedSpecNamespace::Hello).should be_true
end
it "required an included file" do
defined?(OtherNamespace::SomeEnum).should be_true
end
it "extended a service" do
require "extended/extended_service"
end
end

View file

@ -0,0 +1,263 @@
#
# 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 'NonblockingServer' do
class Handler
def initialize
@queue = Queue.new
end
attr_accessor :server
def greeting(english)
if english
SpecNamespace::Hello.new
else
SpecNamespace::Hello.new(:greeting => "Aloha!")
end
end
def block
@queue.pop
end
def unblock(n)
n.times { @queue.push true }
end
def sleep(time)
Kernel.sleep time
end
def shutdown
@server.shutdown(0, false)
end
end
class SpecTransport < Thrift::BaseTransport
def initialize(transport, queue)
@transport = transport
@queue = queue
@flushed = false
end
def open?
@transport.open?
end
def open
@transport.open
end
def close
@transport.close
end
def read(sz)
@transport.read(sz)
end
def write(buf,sz=nil)
@transport.write(buf, sz)
end
def flush
@queue.push :flushed unless @flushed or @queue.nil?
@flushed = true
@transport.flush
end
end
class SpecServerSocket < Thrift::ServerSocket
def initialize(host, port, queue)
super(host, port)
@queue = queue
end
def listen
super
@queue.push :listen
end
end
describe Thrift::NonblockingServer do
before(:each) do
@port = 43251
handler = Handler.new
processor = SpecNamespace::NonblockingService::Processor.new(handler)
queue = Queue.new
@transport = SpecServerSocket.new('localhost', @port, queue)
transport_factory = Thrift::FramedTransportFactory.new
logger = Logger.new(STDERR)
logger.level = Logger::WARN
@server = Thrift::NonblockingServer.new(processor, @transport, transport_factory, nil, 5, logger)
handler.server = @server
@server_thread = Thread.new(Thread.current) do |master_thread|
begin
@server.serve
rescue => e
p e
puts e.backtrace * "\n"
master_thread.raise e
end
end
queue.pop
@clients = []
@catch_exceptions = false
end
after(:each) do
@clients.each { |client, trans| trans.close }
# @server.shutdown(1)
@server_thread.kill
@transport.close
end
def setup_client(queue = nil)
transport = SpecTransport.new(Thrift::FramedTransport.new(Thrift::Socket.new('localhost', @port)), queue)
protocol = Thrift::BinaryProtocol.new(transport)
client = SpecNamespace::NonblockingService::Client.new(protocol)
transport.open
@clients << [client, transport]
client
end
def setup_client_thread(result)
queue = Queue.new
Thread.new do
begin
client = setup_client
while (cmd = queue.pop)
msg, *args = cmd
case msg
when :block
result << client.block
when :unblock
client.unblock(args.first)
when :hello
result << client.greeting(true) # ignore result
when :sleep
client.sleep(args[0] || 0.5)
result << :slept
when :shutdown
client.shutdown
when :exit
result << :done
break
end
end
@clients.each { |c,t| t.close and break if c == client } #close the transport
rescue => e
raise e unless @catch_exceptions
end
end
queue
end
it "should handle basic message passing" do
client = setup_client
client.greeting(true).should == SpecNamespace::Hello.new
client.greeting(false).should == SpecNamespace::Hello.new(:greeting => 'Aloha!')
@server.shutdown
end
it "should handle concurrent clients" do
queue = Queue.new
trans_queue = Queue.new
4.times do
Thread.new(Thread.current) do |main_thread|
begin
queue.push setup_client(trans_queue).block
rescue => e
main_thread.raise e
end
end
end
4.times { trans_queue.pop }
setup_client.unblock(4)
4.times { queue.pop.should be_true }
@server.shutdown
end
it "should handle messages from more than 5 long-lived connections" do
queues = []
result = Queue.new
7.times do |i|
queues << setup_client_thread(result)
Thread.pass if i == 4 # give the server time to accept connections
end
client = setup_client
# block 4 connections
4.times { |i| queues[i] << :block }
queues[4] << :hello
queues[5] << :hello
queues[6] << :hello
3.times { result.pop.should == SpecNamespace::Hello.new }
client.greeting(true).should == SpecNamespace::Hello.new
queues[5] << [:unblock, 4]
4.times { result.pop.should be_true }
queues[2] << :hello
result.pop.should == SpecNamespace::Hello.new
client.greeting(false).should == SpecNamespace::Hello.new(:greeting => 'Aloha!')
7.times { queues.shift << :exit }
client.greeting(true).should == SpecNamespace::Hello.new
@server.shutdown
end
it "should shut down when asked" do
# connect first to ensure it's running
client = setup_client
client.greeting(false) # force a message pass
@server.shutdown
@server_thread.join(2).should be_an_instance_of(Thread)
end
it "should continue processing active messages when shutting down" do
result = Queue.new
client = setup_client_thread(result)
client << :sleep
sleep 0.1 # give the server time to start processing the client's message
@server.shutdown
@server_thread.join(2).should be_an_instance_of(Thread)
result.pop.should == :slept
end
it "should kill active messages when they don't expire while shutting down" do
result = Queue.new
client = setup_client_thread(result)
client << [:sleep, 10]
sleep 0.1 # start processing the client's message
@server.shutdown(1)
@catch_exceptions = true
@server_thread.join(3).should_not be_nil
result.should be_empty
end
it "should allow shutting down in response to a message" do
client = setup_client
client.greeting(true).should == SpecNamespace::Hello.new
client.shutdown
@server_thread.join(2).should_not be_nil
end
end
end

View file

@ -0,0 +1,80 @@
#
# 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 'Processor' do
class ProcessorSpec
include Thrift::Processor
end
describe Thrift::Processor do
before(:each) do
@processor = ProcessorSpec.new(mock("MockHandler"))
@prot = mock("MockProtocol")
end
def mock_trans(obj)
obj.should_receive(:trans).ordered.and_return do
mock("trans").tap do |trans|
trans.should_receive(:flush).ordered
end
end
end
it "should call process_<message> when it receives that message" do
@prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 17]
@processor.should_receive(:process_testMessage).with(17, @prot, @prot).ordered
@processor.process(@prot, @prot).should == true
end
it "should raise an ApplicationException when the received message cannot be processed" do
@prot.should_receive(:read_message_begin).ordered.and_return ['testMessage', Thrift::MessageTypes::CALL, 4]
@prot.should_receive(:skip).with(Thrift::Types::STRUCT).ordered
@prot.should_receive(:read_message_end).ordered
@prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::EXCEPTION, 4).ordered
e = mock(Thrift::ApplicationException)
e.should_receive(:write).with(@prot).ordered
Thrift::ApplicationException.should_receive(:new).with(Thrift::ApplicationException::UNKNOWN_METHOD, "Unknown function testMessage").and_return(e)
@prot.should_receive(:write_message_end).ordered
mock_trans(@prot)
@processor.process(@prot, @prot)
end
it "should pass args off to the args class" do
args_class = mock("MockArgsClass")
args = mock("#<MockArgsClass:mock>").tap do |args|
args.should_receive(:read).with(@prot).ordered
end
args_class.should_receive(:new).and_return args
@prot.should_receive(:read_message_end).ordered
@processor.read_args(@prot, args_class).should eql(args)
end
it "should write out a reply when asked" do
@prot.should_receive(:write_message_begin).with('testMessage', Thrift::MessageTypes::REPLY, 23).ordered
result = mock("MockResult")
result.should_receive(:write).with(@prot).ordered
@prot.should_receive(:write_message_end).ordered
mock_trans(@prot)
@processor.write_result(result, @prot, 'testMessage', 23)
end
end
end

View file

@ -0,0 +1,67 @@
#
# 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 'Serializer' do
describe Thrift::Serializer do
it "should serialize structs to binary by default" do
serializer = Thrift::Serializer.new(Thrift::BinaryProtocolAcceleratedFactory.new)
data = serializer.serialize(SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!"))
data.should == "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
end
it "should serialize structs to the given protocol" do
protocol = Thrift::BaseProtocol.new(mock("transport"))
protocol.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
protocol.should_receive(:write_field_begin).with("greeting", Thrift::Types::STRING, 1)
protocol.should_receive(:write_string).with("Good day")
protocol.should_receive(:write_field_end)
protocol.should_receive(:write_field_stop)
protocol.should_receive(:write_struct_end)
protocol_factory = mock("ProtocolFactory")
protocol_factory.stub!(:get_protocol).and_return(protocol)
serializer = Thrift::Serializer.new(protocol_factory)
serializer.serialize(SpecNamespace::Hello.new(:greeting => "Good day"))
end
end
describe Thrift::Deserializer do
it "should deserialize structs from binary by default" do
deserializer = Thrift::Deserializer.new
data = "\x0B\x00\x01\x00\x00\x00\x0E'Ello guv'nor!\x00"
deserializer.deserialize(SpecNamespace::Hello.new, data).should == SpecNamespace::Hello.new(:greeting => "'Ello guv'nor!")
end
it "should deserialize structs from the given protocol" do
protocol = Thrift::BaseProtocol.new(mock("transport"))
protocol.should_receive(:read_struct_begin).and_return("SpecNamespace::Hello")
protocol.should_receive(:read_field_begin).and_return(["greeting", Thrift::Types::STRING, 1],
[nil, Thrift::Types::STOP, 0])
protocol.should_receive(:read_string).and_return("Good day")
protocol.should_receive(:read_field_end)
protocol.should_receive(:read_struct_end)
protocol_factory = mock("ProtocolFactory")
protocol_factory.stub!(:get_protocol).and_return(protocol)
deserializer = Thrift::Deserializer.new(protocol_factory)
deserializer.deserialize(SpecNamespace::Hello.new, "").should == SpecNamespace::Hello.new(:greeting => "Good day")
end
end
end

View file

@ -0,0 +1,79 @@
#
# 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 'Thrift::ServerSocket' do
describe Thrift::ServerSocket do
before(:each) do
@socket = Thrift::ServerSocket.new(1234)
end
it "should create a handle when calling listen" do
TCPServer.should_receive(:new).with(nil, 1234)
@socket.listen
end
it "should accept an optional host argument" do
@socket = Thrift::ServerSocket.new('localhost', 1234)
TCPServer.should_receive(:new).with('localhost', 1234)
@socket.listen
end
it "should create a Thrift::Socket to wrap accepted sockets" do
handle = mock("TCPServer")
TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
@socket.listen
sock = mock("sock")
handle.should_receive(:accept).and_return(sock)
trans = mock("Socket")
Thrift::Socket.should_receive(:new).and_return(trans)
trans.should_receive(:handle=).with(sock)
@socket.accept.should == trans
end
it "should close the handle when closed" do
handle = mock("TCPServer", :closed? => false)
TCPServer.should_receive(:new).with(nil, 1234).and_return(handle)
@socket.listen
handle.should_receive(:close)
@socket.close
end
it "should return nil when accepting if there is no handle" do
@socket.accept.should be_nil
end
it "should return true for closed? when appropriate" do
handle = mock("TCPServer", :closed? => false)
TCPServer.stub!(:new).and_return(handle)
@socket.listen
@socket.should_not be_closed
handle.stub!(:close)
@socket.close
@socket.should be_closed
@socket.listen
@socket.should_not be_closed
handle.stub!(:closed?).and_return(true)
@socket.should be_closed
end
end
end

View file

@ -0,0 +1,147 @@
#
# 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 'Server' do
describe Thrift::BaseServer do
it "should default to BaseTransportFactory and BinaryProtocolFactory when not specified" do
server = Thrift::BaseServer.new(mock("Processor"), mock("BaseServerTransport"))
server.instance_variable_get(:'@transport_factory').should be_an_instance_of(Thrift::BaseTransportFactory)
server.instance_variable_get(:'@protocol_factory').should be_an_instance_of(Thrift::BinaryProtocolFactory)
end
# serve is a noop, so can't test that
end
describe Thrift::SimpleServer do
before(:each) do
@processor = mock("Processor")
@serverTrans = mock("ServerTransport")
@trans = mock("BaseTransport")
@prot = mock("BaseProtocol")
@client = mock("Client")
@server = described_class.new(@processor, @serverTrans, @trans, @prot)
end
it "should serve in the main thread" do
@serverTrans.should_receive(:listen).ordered
@serverTrans.should_receive(:accept).exactly(3).times.and_return(@client)
@trans.should_receive(:get_transport).exactly(3).times.with(@client).and_return(@trans)
@prot.should_receive(:get_protocol).exactly(3).times.with(@trans).and_return(@prot)
x = 0
@processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do
case (x += 1)
when 1 then raise Thrift::TransportException
when 2 then raise Thrift::ProtocolException
when 3 then throw :stop
end
end
@trans.should_receive(:close).exactly(3).times
@serverTrans.should_receive(:close).ordered
lambda { @server.serve }.should throw_symbol(:stop)
end
end
describe Thrift::ThreadedServer do
before(:each) do
@processor = mock("Processor")
@serverTrans = mock("ServerTransport")
@trans = mock("BaseTransport")
@prot = mock("BaseProtocol")
@client = mock("Client")
@server = described_class.new(@processor, @serverTrans, @trans, @prot)
end
it "should serve using threads" do
@serverTrans.should_receive(:listen).ordered
@serverTrans.should_receive(:accept).exactly(3).times.and_return(@client)
@trans.should_receive(:get_transport).exactly(3).times.with(@client).and_return(@trans)
@prot.should_receive(:get_protocol).exactly(3).times.with(@trans).and_return(@prot)
Thread.should_receive(:new).with(@prot, @trans).exactly(3).times.and_yield(@prot, @trans)
x = 0
@processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do
case (x += 1)
when 1 then raise Thrift::TransportException
when 2 then raise Thrift::ProtocolException
when 3 then throw :stop
end
end
@trans.should_receive(:close).exactly(3).times
@serverTrans.should_receive(:close).ordered
lambda { @server.serve }.should throw_symbol(:stop)
end
end
describe Thrift::ThreadPoolServer do
before(:each) do
@processor = mock("Processor")
@server_trans = mock("ServerTransport")
@trans = mock("BaseTransport")
@prot = mock("BaseProtocol")
@client = mock("Client")
@server = described_class.new(@processor, @server_trans, @trans, @prot)
end
it "should serve inside a thread" do
exception_q = @server.instance_variable_get(:@exception_q)
described_class.any_instance.should_receive(:serve) do
exception_q.push(StandardError.new('ERROR'))
end
expect { @server.rescuable_serve }.to(raise_error('ERROR'))
end
it "should avoid running the server twice when retrying rescuable_serve" do
exception_q = @server.instance_variable_get(:@exception_q)
described_class.any_instance.should_receive(:serve) do
exception_q.push(StandardError.new('ERROR1'))
exception_q.push(StandardError.new('ERROR2'))
end
expect { @server.rescuable_serve }.to(raise_error('ERROR1'))
expect { @server.rescuable_serve }.to(raise_error('ERROR2'))
end
it "should serve using a thread pool" do
thread_q = mock("SizedQueue")
exception_q = mock("Queue")
@server.instance_variable_set(:@thread_q, thread_q)
@server.instance_variable_set(:@exception_q, exception_q)
@server_trans.should_receive(:listen).ordered
thread_q.should_receive(:push).with(:token)
thread_q.should_receive(:pop)
Thread.should_receive(:new).and_yield
@server_trans.should_receive(:accept).exactly(3).times.and_return(@client)
@trans.should_receive(:get_transport).exactly(3).times.and_return(@trans)
@prot.should_receive(:get_protocol).exactly(3).times.and_return(@prot)
x = 0
error = RuntimeError.new("Stopped")
@processor.should_receive(:process).exactly(3).times.with(@prot, @prot).and_return do
case (x += 1)
when 1 then raise Thrift::TransportException
when 2 then raise Thrift::ProtocolException
when 3 then raise error
end
end
@trans.should_receive(:close).exactly(3).times
exception_q.should_receive(:push).with(error).and_throw(:stop)
@server_trans.should_receive(:close)
expect { @server.serve }.to(throw_symbol(:stop))
end
end
end

View file

@ -0,0 +1,61 @@
#
# 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 = mock("Handle", :closed? => false)
@handle.stub!(:close)
@handle.stub!(:connect_nonblock)
@handle.stub!(:setsockopt)
::Socket.stub!(:new).and_return(@handle)
end
it_should_behave_like "a socket"
it "should raise a TransportException when it cannot open a socket" do
::Socket.should_receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]])
lambda { @socket.open }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
end
it "should open a ::Socket with default args" do
::Socket.should_receive(:new).and_return(mock("Handle", :connect_nonblock => true, :setsockopt => nil))
::Socket.should_receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]])
::Socket.should_receive(:sockaddr_in)
@socket.open
end
it "should accept host/port options" do
::Socket.should_receive(:new).and_return(mock("Handle", :connect_nonblock => true, :setsockopt => nil))
::Socket.should_receive(:getaddrinfo).with("my.domain", 1234, nil, ::Socket::SOCK_STREAM).and_return([[]])
::Socket.should_receive(:sockaddr_in)
Thrift::Socket.new('my.domain', 1234).open
end
it "should accept an optional timeout" do
::Socket.stub!(:new)
Thrift::Socket.new('localhost', 8080, 5).timeout.should == 5
end
end
end

View file

@ -0,0 +1,104 @@
#
# 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
@socket.open.should == @handle
end
it "should be open whenever it has a handle" do
@socket.should_not be_open
@socket.open
@socket.should be_open
@socket.handle = nil
@socket.should_not be_open
@socket.handle = @handle
@socket.close
@socket.should_not be_open
end
it "should write data to the handle" do
@socket.open
@handle.should_receive(:write).with("foobar")
@socket.write("foobar")
@handle.should_receive(:write).with("fail").and_raise(StandardError)
lambda { @socket.write("fail") }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
end
it "should raise an error when it cannot read from the handle" do
@socket.open
@handle.should_receive(:readpartial).with(17).and_raise(StandardError)
lambda { @socket.read(17) }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
end
it "should return the data read when reading from the handle works" do
@socket.open
@handle.should_receive(:readpartial).with(17).and_return("test data")
@socket.read(17).should == "test data"
end
it "should declare itself as closed when it has an error" do
@socket.open
@handle.should_receive(:write).with("fail").and_raise(StandardError)
@socket.should be_open
lambda { @socket.write("fail") }.should raise_error
@socket.should_not be_open
end
it "should raise an error when the stream is closed" do
@socket.open
@handle.stub!(:closed?).and_return(true)
@socket.should_not be_open
lambda { @socket.write("fail") }.should raise_error(IOError, "closed stream")
lambda { @socket.read(10) }.should raise_error(IOError, "closed stream")
end
it "should support the timeout accessor for read" do
@socket.timeout = 3
@socket.open
IO.should_receive(:select).with([@handle], nil, nil, 3).and_return([[@handle], [], []])
@handle.should_receive(:readpartial).with(17).and_return("test data")
@socket.read(17).should == "test data"
end
it "should support the timeout accessor for write" do
@socket.timeout = 3
@socket.open
IO.should_receive(:select).with(nil, [@handle], nil, 3).twice.and_return([[], [@handle], []])
@handle.should_receive(:write_nonblock).with("test data").and_return(4)
@handle.should_receive(:write_nonblock).with(" data").and_return(5)
@socket.write("test data").should == 9
end
it "should raise an error when read times out" do
@socket.timeout = 0.5
@socket.open
IO.should_receive(:select).once {sleep(0.5); nil}
lambda { @socket.read(17) }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::TIMED_OUT }
end
it "should raise an error when write times out" do
@socket.timeout = 0.5
@socket.open
IO.should_receive(:select).with(nil, [@handle], nil, 0.5).any_number_of_times.and_return(nil)
lambda { @socket.write("test data") }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::TIMED_OUT }
end
end

View file

@ -0,0 +1,64 @@
# 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 'rubygems'
require 'rspec'
$:.unshift File.join(File.dirname(__FILE__), *%w[.. ext])
# pretend we already loaded fastthread, otherwise the nonblocking_server_spec
# will get screwed up
# $" << 'fastthread.bundle'
require 'thrift'
unless Object.method_defined? :tap
# if Object#tap isn't defined, then add it; this should only happen in Ruby < 1.8.7
class Object
def tap(&block)
block.call(self)
self
end
end
end
RSpec.configure do |configuration|
configuration.before(:each) do
Thrift.type_checking = true
end
end
$:.unshift File.join(File.dirname(__FILE__), *%w[.. test debug_proto gen-rb])
require 'srv'
require 'debug_proto_test_constants'
$:.unshift File.join(File.dirname(__FILE__), *%w[gen-rb])
require 'thrift_spec_types'
require 'nonblocking_service'
module Fixtures
COMPACT_PROTOCOL_TEST_STRUCT = Thrift::Test::COMPACT_TEST.dup
COMPACT_PROTOCOL_TEST_STRUCT.a_binary = [0,1,2,3,4,5,6,7,8].pack('c*')
COMPACT_PROTOCOL_TEST_STRUCT.set_byte_map = nil
COMPACT_PROTOCOL_TEST_STRUCT.map_byte_map = nil
end
$:.unshift File.join(File.dirname(__FILE__), *%w[gen-rb/flat])

View file

@ -0,0 +1,74 @@
#
# 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 = mock("Handle", :closed? => false)
@simple_socket_handle.stub!(:close)
@simple_socket_handle.stub!(:connect_nonblock)
@simple_socket_handle.stub!(:setsockopt)
@handle = mock(mock("SSLHandle", :connect_nonblock => true, :post_connection_check => true), :closed? => false)
@handle.stub!(:connect_nonblock)
@handle.stub!(:close)
@handle.stub!(:post_connection_check)
::Socket.stub!(:new).and_return(@simple_socket_handle)
OpenSSL::SSL::SSLSocket.stub!(:new).and_return(@handle)
end
it_should_behave_like "a socket"
it "should raise a TransportException when it cannot open a ssl socket" do
::Socket.should_receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]])
lambda { @socket.open }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
end
it "should open a ::Socket with default args" do
OpenSSL::SSL::SSLSocket.should_receive(:new).with(@simple_socket_handle, nil).and_return(@handle)
@handle.should_receive(:post_connection_check).with('localhost')
@socket.open
end
it "should accept host/port options" do
handle = mock("Handle", :connect_nonblock => true, :setsockopt => nil)
::Socket.stub!(:new).and_return(handle)
::Socket.should_receive(:getaddrinfo).with("my.domain", 1234, nil, ::Socket::SOCK_STREAM).and_return([[]])
::Socket.should_receive(:sockaddr_in)
OpenSSL::SSL::SSLSocket.should_receive(:new).with(handle, nil).and_return(@handle)
@handle.should_receive(:post_connection_check).with('my.domain')
Thrift::SSLSocket.new('my.domain', 1234, 6000, nil).open
end
it "should accept an optional timeout" do
Thrift::SSLSocket.new('localhost', 8080, 5).timeout.should == 5
end
it "should accept an optional context" do
Thrift::SSLSocket.new('localhost', 8080, 5, @context).ssl_context.should == @context
end
end
end

View file

@ -0,0 +1,191 @@
#
# 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 'StructNestedContainers' do
def with_type_checking
saved_type_checking, Thrift.type_checking = Thrift.type_checking, true
begin
yield
ensure
Thrift.type_checking = saved_type_checking
end
end
describe Thrift::Struct do
# Nested container tests, see THRIFT-369.
it "should support nested lists inside lists" do
with_type_checking do
a, b = SpecNamespace::NestedListInList.new, SpecNamespace::NestedListInList.new
[a, b].each do |thrift_struct|
thrift_struct.value = [ [1, 2, 3], [2, 3, 4] ]
thrift_struct.validate
end
a.should == b
b.value.push [3, 4, 5]
a.should_not == b
end
end
it "should support nested lists inside sets" do
with_type_checking do
a, b = SpecNamespace::NestedListInSet.new, SpecNamespace::NestedListInSet.new
[a, b].each do |thrift_struct|
thrift_struct.value = [ [1, 2, 3], [2, 3, 4] ].to_set
thrift_struct.validate
end
a.should == b
b.value.add [3, 4, 5]
a.should_not == b
end
end
it "should support nested lists in map keys" do
with_type_checking do
a, b = SpecNamespace::NestedListInMapKey.new, SpecNamespace::NestedListInMapKey.new
[a, b].each do |thrift_struct|
thrift_struct.value = { [1, 2, 3] => 1, [2, 3, 4] => 2 }
thrift_struct.validate
end
a.should == b
b.value[[3, 4, 5]] = 3
a.should_not == b
end
end
it "should support nested lists in map values" do
with_type_checking do
a, b = SpecNamespace::NestedListInMapValue.new, SpecNamespace::NestedListInMapValue.new
[a, b].each do |thrift_struct|
thrift_struct.value = { 1 => [1, 2, 3], 2 => [2, 3, 4] }
thrift_struct.validate
end
a.should == b
b.value[3] = [3, 4, 5]
a.should_not == b
end
end
it "should support nested sets inside lists" do
with_type_checking do
a, b = SpecNamespace::NestedSetInList.new, SpecNamespace::NestedSetInList.new
[a, b].each do |thrift_struct|
thrift_struct.value = [ [1, 2, 3].to_set, [2, 3, 4].to_set ]
thrift_struct.validate
end
a.should == b
b.value.push([3, 4, 5].to_set)
a.should_not == b
end
end
it "should support nested sets inside sets" do
with_type_checking do
a, b = SpecNamespace::NestedSetInSet.new, SpecNamespace::NestedSetInSet.new
[a, b].each do |thrift_struct|
thrift_struct.value = [ [1, 2, 3].to_set, [2, 3, 4].to_set ].to_set
thrift_struct.validate
end
a.should == b
b.value.add([3, 4, 5].to_set)
a.should_not == b
end
end
it "should support nested sets in map keys" do
with_type_checking do
a, b = SpecNamespace::NestedSetInMapKey.new, SpecNamespace::NestedSetInMapKey.new
[a, b].each do |thrift_struct|
thrift_struct.value = { [1, 2, 3].to_set => 1, [2, 3, 4].to_set => 2 }
thrift_struct.validate
end
a.should == b
b.value[[3, 4, 5].to_set] = 3
a.should_not == b
end
end
it "should support nested sets in map values" do
with_type_checking do
a, b = SpecNamespace::NestedSetInMapValue.new, SpecNamespace::NestedSetInMapValue.new
[a, b].each do |thrift_struct|
thrift_struct.value = { 1 => [1, 2, 3].to_set, 2 => [2, 3, 4].to_set }
thrift_struct.validate
end
a.should == b
b.value[3] = [3, 4, 5].to_set
a.should_not == b
end
end
it "should support nested maps inside lists" do
with_type_checking do
a, b = SpecNamespace::NestedMapInList.new, SpecNamespace::NestedMapInList.new
[a, b].each do |thrift_struct|
thrift_struct.value = [ {1 => 2, 3 => 4}, {2 => 3, 4 => 5} ]
thrift_struct.validate
end
a.should == b
b.value.push({ 3 => 4, 5 => 6 })
a.should_not == b
end
end
it "should support nested maps inside sets" do
with_type_checking do
a, b = SpecNamespace::NestedMapInSet.new, SpecNamespace::NestedMapInSet.new
[a, b].each do |thrift_struct|
thrift_struct.value = [ {1 => 2, 3 => 4}, {2 => 3, 4 => 5} ].to_set
thrift_struct.validate
end
a.should == b
b.value.add({ 3 => 4, 5 => 6 })
a.should_not == b
end
end
it "should support nested maps in map keys" do
with_type_checking do
a, b = SpecNamespace::NestedMapInMapKey.new, SpecNamespace::NestedMapInMapKey.new
[a, b].each do |thrift_struct|
thrift_struct.value = { { 1 => 2, 3 => 4} => 1, {2 => 3, 4 => 5} => 2 }
thrift_struct.validate
end
a.should == b
b.value[{3 => 4, 5 => 6}] = 3
a.should_not == b
end
end
it "should support nested maps in map values" do
with_type_checking do
a, b = SpecNamespace::NestedMapInMapValue.new, SpecNamespace::NestedMapInMapValue.new
[a, b].each do |thrift_struct|
thrift_struct.value = { 1 => { 1 => 2, 3 => 4}, 2 => {2 => 3, 4 => 5} }
thrift_struct.validate
end
a.should == b
b.value[3] = { 3 => 4, 5 => 6 }
a.should_not == b
end
end
end
end

View file

@ -0,0 +1,293 @@
#
# 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 }
fields.should == 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)
object.simple.should == 53
object.words.should == "words"
object.hello.should == SpecNamespace::Hello.new(:greeting => 'hello, world!')
object.ints.should == [1, 2, 2, 3]
object.complex.should be_nil
object.shorts.should == Set.new([5, 17, 239])
end
it "should not share default values between instances" do
begin
struct = SpecNamespace::Foo.new
struct.ints << 17
SpecNamespace::Foo.new.ints.should == [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)
struct.yesno.should be_false
end
it "should have proper == semantics" do
SpecNamespace::Foo.new.should_not == SpecNamespace::Hello.new
SpecNamespace::Foo.new.should == SpecNamespace::Foo.new
SpecNamespace::Foo.new(:simple => 52).should_not == SpecNamespace::Foo.new
end
it "should print enum value names in inspect" do
SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect.should == "<SpecNamespace::StructWithSomeEnum some_enum:ONE (0)>"
SpecNamespace::StructWithEnumMap.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect.should == "<SpecNamespace::StructWithEnumMap my_map:{ONE (0): [TWO (1)]}>"
end
it "should pretty print binary fields" do
SpecNamespace::Foo2.new(:my_binary => "\001\002\003").inspect.should == "<SpecNamespace::Foo2 my_binary:010203>"
end
it "should offer field? methods" do
SpecNamespace::Foo.new.opt_string?.should be_false
SpecNamespace::Foo.new(:simple => 52).simple?.should be_true
SpecNamespace::Foo.new(:my_bool => false).my_bool?.should be_true
SpecNamespace::Foo.new(:my_bool => true).my_bool?.should be_true
end
it "should be comparable" do
s1 = SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::ONE)
s2 = SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::TWO)
(s1 <=> s2).should == -1
(s2 <=> s1).should == 1
(s1 <=> s1).should == 0
(s1 <=> SpecNamespace::StructWithSomeEnum.new()).should == -1
end
it "should read itself off the wire" do
struct = SpecNamespace::Foo.new
prot = Thrift::BaseProtocol.new(mock("transport"))
prot.should_receive(:read_struct_begin).twice
prot.should_receive(:read_struct_end).twice
prot.should_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
)
prot.should_receive(:read_field_end).exactly(7).times
prot.should_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
)
prot.should_receive(:read_map_end).exactly(3).times
prot.should_receive(:read_list_begin).and_return([Thrift::Types::I32, 4])
prot.should_receive(:read_list_end)
prot.should_receive(:read_set_begin).and_return([Thrift::Types::I16, 2])
prot.should_receive(:read_set_end)
prot.should_receive(:read_i32).and_return(
1, 14, # complex keys
42, # simple
4, 23, 4, 29 # ints
)
prot.should_receive(:read_string).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
prot.should_receive(:read_double).and_return(Math::PI, Math::E, 4.669201609)
prot.should_receive(:read_i16).and_return(2, 3)
prot.should_not_receive(:skip)
struct.read(prot)
struct.simple.should == 42
struct.complex.should == {1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}}
struct.hello.should == SpecNamespace::Hello.new(:greeting => "what's up?")
struct.words.should == "apple banana"
struct.ints.should == [4, 23, 4, 29]
struct.shorts.should == 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)
prot.should_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(mock("transport"))
prot.should_receive(:read_struct_begin)
prot.should_receive(:read_struct_end)
prot.should_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]
)
prot.should_receive(:read_field_end).exactly(5).times
prot.should_receive(:read_i32).and_return(42)
prot.should_receive(:read_string).and_return("foobar")
prot.should_receive(:skip).with(Thrift::Types::STRUCT)
prot.should_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)
prot.should_receive(:skip).with(Thrift::Types::I32)
struct.read(prot)
struct.simple.should == 42
struct.complex.should be_nil
struct.words.should == "foobar"
struct.hello.should == SpecNamespace::Hello.new(:greeting => 'hello, world!')
struct.ints.should == [1, 2, 2, 3]
struct.shorts.should == Set.new([5, 17, 239])
end
it "should write itself to the wire" do
prot = Thrift::BaseProtocol.new(mock("transport")) #mock("Protocol")
prot.should_receive(:write_struct_begin).with("SpecNamespace::Foo")
prot.should_receive(:write_struct_begin).with("SpecNamespace::Hello")
prot.should_receive(:write_struct_end).twice
prot.should_receive(:write_field_begin).with('ints', Thrift::Types::LIST, 4)
prot.should_receive(:write_i32).with(1)
prot.should_receive(:write_i32).with(2).twice
prot.should_receive(:write_i32).with(3)
prot.should_receive(:write_field_begin).with('complex', Thrift::Types::MAP, 5)
prot.should_receive(:write_i32).with(5)
prot.should_receive(:write_string).with('foo')
prot.should_receive(:write_double).with(1.23)
prot.should_receive(:write_field_begin).with('shorts', Thrift::Types::SET, 6)
prot.should_receive(:write_i16).with(5)
prot.should_receive(:write_i16).with(17)
prot.should_receive(:write_i16).with(239)
prot.should_receive(:write_field_stop).twice
prot.should_receive(:write_field_end).exactly(6).times
prot.should_receive(:write_field_begin).with('simple', Thrift::Types::I32, 1)
prot.should_receive(:write_i32).with(53)
prot.should_receive(:write_field_begin).with('hello', Thrift::Types::STRUCT, 3)
prot.should_receive(:write_field_begin).with('greeting', Thrift::Types::STRING, 1)
prot.should_receive(:write_string).with('hello, world!')
prot.should_receive(:write_map_begin).with(Thrift::Types::I32, Thrift::Types::MAP, 1)
prot.should_receive(:write_map_begin).with(Thrift::Types::STRING, Thrift::Types::DOUBLE, 1)
prot.should_receive(:write_map_end).twice
prot.should_receive(:write_list_begin).with(Thrift::Types::I32, 4)
prot.should_receive(:write_list_end)
prot.should_receive(:write_set_begin).with(Thrift::Types::I16, 3)
prot.should_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
lambda { struct.send :write_container, nil, nil, {:type => "foo"} }.should 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
lambda { SpecNamespace::Hello.new(:greeting => 3) }.should raise_error(Thrift::TypeError, "Expected Types::STRING, received Fixnum for field greeting")
ensure
Thrift.type_checking = false
end
lambda { SpecNamespace::Hello.new(:greeting => 3) }.should_not raise_error(Thrift::TypeError)
end
it "should support optional type-checking in field accessors" do
Thrift.type_checking = true
begin
hello = SpecNamespace::Hello.new
lambda { hello.greeting = 3 }.should raise_error(Thrift::TypeError, "Expected Types::STRING, received Fixnum for field greeting")
ensure
Thrift.type_checking = false
end
lambda { hello.greeting = 3 }.should_not raise_error(Thrift::TypeError)
end
it "should raise an exception when unknown types are given to Thrift::Struct.new" do
lambda { SpecNamespace::Hello.new(:fish => 'salmon') }.should 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
e.message.should == "something happened"
e.code.should == 1
# ensure it gets serialized properly, this is the really important part
prot = Thrift::BaseProtocol.new(mock("trans"))
prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
prot.should_receive(:write_struct_end)
prot.should_receive(:write_field_begin).with('message', Thrift::Types::STRING, 1)#, "something happened")
prot.should_receive(:write_string).with("something happened")
prot.should_receive(:write_field_begin).with('code', Thrift::Types::I32, 2)#, 1)
prot.should_receive(:write_i32).with(1)
prot.should_receive(:write_field_stop)
prot.should_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
e.message.should == "something happened"
e.code.should == 5
prot = Thrift::BaseProtocol.new(mock("trans"))
prot.should_receive(:write_struct_begin).with("SpecNamespace::Xception")
prot.should_receive(:write_struct_end)
prot.should_receive(:write_field_begin).with('message', Thrift::Types::STRING, 1)
prot.should_receive(:write_string).with("something happened")
prot.should_receive(:write_field_begin).with('code', Thrift::Types::I32, 2)
prot.should_receive(:write_i32).with(5)
prot.should_receive(:write_field_stop)
prot.should_receive(:write_field_end).twice
e.write(prot)
end
end
end
end

View file

@ -0,0 +1,141 @@
#
# 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 'rack/test'
require 'thrift/server/thin_http_server'
describe Thrift::ThinHTTPServer do
let(:processor) { mock('processor') }
describe "#initialize" do
context "when using the defaults" do
it "binds to port 80, with host 0.0.0.0, a path of '/'" do
Thin::Server.should_receive(:new).with('0.0.0.0', 80, an_instance_of(Rack::Builder))
Thrift::ThinHTTPServer.new(processor)
end
it 'creates a ThinHTTPServer::RackApplicationContext' do
Thrift::ThinHTTPServer::RackApplication.should_receive(:for).with("/", processor, an_instance_of(Thrift::BinaryProtocolFactory)).and_return(anything)
Thrift::ThinHTTPServer.new(processor)
end
it "uses the BinaryProtocolFactory" do
Thrift::BinaryProtocolFactory.should_receive(:new)
Thrift::ThinHTTPServer.new(processor)
end
end
context "when using the options" do
it 'accepts :ip, :port, :path' do
ip = "192.168.0.1"
port = 3000
path = "/thin"
Thin::Server.should_receive(:new).with(ip, port, an_instance_of(Rack::Builder))
Thrift::ThinHTTPServer.new(processor,
:ip => ip,
:port => port,
:path => path)
end
it 'creates a ThinHTTPServer::RackApplicationContext with a different protocol factory' do
Thrift::ThinHTTPServer::RackApplication.should_receive(:for).with("/", processor, an_instance_of(Thrift::JsonProtocolFactory)).and_return(anything)
Thrift::ThinHTTPServer.new(processor,
:protocol_factory => Thrift::JsonProtocolFactory.new)
end
end
end
describe "#serve" do
it 'starts the Thin server' do
underlying_thin_server = mock('thin server', :start => true)
Thin::Server.stub(:new).and_return(underlying_thin_server)
thin_thrift_server = Thrift::ThinHTTPServer.new(processor)
underlying_thin_server.should_receive(:start)
thin_thrift_server.serve
end
end
end
describe Thrift::ThinHTTPServer::RackApplication do
include Rack::Test::Methods
let(:processor) { mock('processor') }
let(:protocol_factory) { mock('protocol factory') }
def app
Thrift::ThinHTTPServer::RackApplication.for("/", processor, protocol_factory)
end
context "404 response" do
it 'receives a non-POST' do
header('Content-Type', "application/x-thrift")
get "/"
last_response.status.should be 404
end
it 'receives a header other than application/x-thrift' do
header('Content-Type', "application/json")
post "/"
last_response.status.should be 404
end
end
context "200 response" do
before do
protocol_factory.stub(:get_protocol)
processor.stub(:process)
end
it 'creates an IOStreamTransport' do
header('Content-Type', "application/x-thrift")
Thrift::IOStreamTransport.should_receive(:new).with(an_instance_of(Rack::Lint::InputWrapper), an_instance_of(Rack::Response))
post "/"
end
it 'fetches the right protocol based on the Transport' do
header('Content-Type', "application/x-thrift")
protocol_factory.should_receive(:get_protocol).with(an_instance_of(Thrift::IOStreamTransport))
post "/"
end
it 'status code 200' do
header('Content-Type', "application/x-thrift")
post "/"
last_response.ok?.should be_true
end
end
end

View file

@ -0,0 +1,115 @@
#
# 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 Thrift::Types do
before(:each) do
Thrift.type_checking = true
end
after(:each) do
Thrift.type_checking = false
end
context 'type checking' do
it "should return the proper name for each type" do
Thrift.type_name(Thrift::Types::I16).should == "Types::I16"
Thrift.type_name(Thrift::Types::VOID).should == "Types::VOID"
Thrift.type_name(Thrift::Types::LIST).should == "Types::LIST"
Thrift.type_name(42).should be_nil
end
it "should check types properly" do
# lambda { Thrift.check_type(nil, Thrift::Types::STOP) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(3, {:type => Thrift::Types::STOP}, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::VOID}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type(3, {:type => Thrift::Types::VOID}, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(true, {:type => Thrift::Types::BOOL}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type(3, {:type => Thrift::Types::BOOL}, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(42, {:type => Thrift::Types::BYTE}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type(42, {:type => Thrift::Types::I16}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type(42, {:type => Thrift::Types::I32}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type(42, {:type => Thrift::Types::I64}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type(3.14, {:type => Thrift::Types::I32}, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(3.14, {:type => Thrift::Types::DOUBLE}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type(3, {:type => Thrift::Types::DOUBLE}, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type("3", {:type => Thrift::Types::STRING}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type(3, {:type => Thrift::Types::STRING}, :foo) }.should raise_error(Thrift::TypeError)
hello = SpecNamespace::Hello.new
lambda { Thrift.check_type(hello, {:type => Thrift::Types::STRUCT, :class => SpecNamespace::Hello}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type("foo", {:type => Thrift::Types::STRUCT}, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type({:foo => 1}, {:type => Thrift::Types::MAP}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type([1], {:type => Thrift::Types::MAP}, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type([1], {:type => Thrift::Types::LIST}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type({:foo => 1}, {:type => Thrift::Types::LIST}, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(Set.new([1,2]), {:type => Thrift::Types::SET}, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type([1,2], {:type => Thrift::Types::SET}, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type({:foo => true}, {:type => Thrift::Types::SET}, :foo) }.should raise_error(Thrift::TypeError)
end
it "should error out if nil is passed and skip_types is false" do
lambda { Thrift.check_type(nil, {:type => Thrift::Types::BOOL}, :foo, false) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::BYTE}, :foo, false) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::I16}, :foo, false) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::I32}, :foo, false) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::I64}, :foo, false) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::DOUBLE}, :foo, false) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::STRING}, :foo, false) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::STRUCT}, :foo, false) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::LIST}, :foo, false) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::SET}, :foo, false) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(nil, {:type => Thrift::Types::MAP}, :foo, false) }.should raise_error(Thrift::TypeError)
end
it "should check element types on containers" do
field = {:type => Thrift::Types::LIST, :element => {:type => Thrift::Types::I32}}
lambda { Thrift.check_type([1, 2], field, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type([1, nil, 2], field, :foo) }.should raise_error(Thrift::TypeError)
field = {:type => Thrift::Types::MAP, :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::STRING}}
lambda { Thrift.check_type({1 => "one", 2 => "two"}, field, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type({1 => "one", nil => "nil"}, field, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type({1 => nil, 2 => "two"}, field, :foo) }.should raise_error(Thrift::TypeError)
field = {:type => Thrift::Types::SET, :element => {:type => Thrift::Types::I32}}
lambda { Thrift.check_type(Set.new([1, 2]), field, :foo) }.should_not raise_error(Thrift::TypeError)
lambda { Thrift.check_type(Set.new([1, nil, 2]), field, :foo) }.should raise_error(Thrift::TypeError)
lambda { Thrift.check_type(Set.new([1, 2.3, 2]), field, :foo) }.should raise_error(Thrift::TypeError)
field = {:type => Thrift::Types::STRUCT, :class => SpecNamespace::Hello}
lambda { Thrift.check_type(SpecNamespace::BoolStruct, field, :foo) }.should raise_error(Thrift::TypeError)
end
it "should give the Thrift::TypeError a readable message" do
msg = "Expected Types::STRING, received Fixnum for field foo"
lambda { Thrift.check_type(3, {:type => Thrift::Types::STRING}, :foo) }.should raise_error(Thrift::TypeError, msg)
msg = "Expected Types::STRING, received Fixnum for field foo.element"
field = {:type => Thrift::Types::LIST, :element => {:type => Thrift::Types::STRING}}
lambda { Thrift.check_type([3], field, :foo) }.should raise_error(Thrift::TypeError, msg)
msg = "Expected Types::I32, received NilClass for field foo.element.key"
field = {:type => Thrift::Types::LIST,
:element => {:type => Thrift::Types::MAP,
:key => {:type => Thrift::Types::I32},
:value => {:type => Thrift::Types::I32}}}
lambda { Thrift.check_type([{nil => 3}], field, :foo) }.should raise_error(Thrift::TypeError, msg)
msg = "Expected Types::I32, received NilClass for field foo.element.value"
lambda { Thrift.check_type([{1 => nil}], field, :foo) }.should raise_error(Thrift::TypeError, msg)
end
end
end

View file

@ -0,0 +1,215 @@
#
# 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
union.get_set_field.should == nil
union.get_value.should == 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
union.get_set_field.should == :integer32
union.get_value.should == 25
union.integer32.should == 25
end
it "should work correctly when instantiated with static field constructors" do
union = SpecNamespace::My_union.integer32(5)
union.get_set_field.should == :integer32
union.integer32.should == 5
end
it "should raise for wrong set field" do
union = SpecNamespace::My_union.new
union.integer32 = 25
lambda { union.some_characters }.should 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})
example = lambda { Thrift::Serializer.new.serialize(union) }
example.should 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
union.should_not == nil
end
it "should not be equal with an empty String" do
union = SpecNamespace::My_union.new
union.should_not == ''
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!")
union.should_not == other_union
end
it "should properly reset setfield and setvalue" do
union = SpecNamespace::My_union.new(:integer32, 25)
union.get_set_field.should == :integer32
union.some_characters = "blah!"
union.get_set_field.should == :some_characters
union.get_value.should == "blah!"
lambda { union.integer32 }.should 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)
union.should_not == 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)
union.should_not == other_union
end
it "should inspect properly" do
union = SpecNamespace::My_union.new(:integer32, 25)
union.inspect.should == "<SpecNamespace::My_union integer32: 25>"
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!")
union.get_set_field.should == :integer32
union.get_value.should == 27
lambda { union.some_characters }.should 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)
other_union.should == 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)
other_union.should == union
end
it "should raise when validating unset union" do
union = SpecNamespace::My_union.new
lambda { union.validate }.should raise_error(StandardError, "Union fields are not set.")
other_union = SpecNamespace::My_union.new(:integer32, 1)
lambda { other_union.validate }.should_not raise_error(StandardError, "Union fields are not set.")
end
it "should validate an enum field properly" do
union = SpecNamespace::TestUnion.new(:enum_field, 3)
union.get_set_field.should == :enum_field
lambda { union.validate }.should raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!")
other_union = SpecNamespace::TestUnion.new(:enum_field, 1)
lambda { other_union.validate }.should_not raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!")
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)
swu2.should_not == swu
swu2.read(proto)
swu2.should == swu
end
it "should support old style constructor" do
union = SpecNamespace::My_union.new(:integer32 => 26)
union.get_set_field.should == :integer32
union.get_value.should == 26
end
it "should not throw an error when inspected and unset" do
lambda{SpecNamespace::TestUnion.new().inspect}.should_not raise_error
end
it "should print enum value name when inspected" do
SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect.should == "<SpecNamespace::My_union some_enum: ONE (0)>"
SpecNamespace::My_union.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect.should == "<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>"
end
it "should offer field? methods" do
SpecNamespace::My_union.new.some_enum?.should be_false
SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).some_enum?.should be_true
SpecNamespace::My_union.new(:im_true => false).im_true?.should be_true
SpecNamespace::My_union.new(:im_true => true).im_true?.should be_true
end
it "should pretty print binary fields" do
SpecNamespace::TestUnion.new(:binary_field => "\001\002\003").inspect.should == "<SpecNamespace::TestUnion binary_field: 010203>"
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]}"
(objs[y] <=> objs[x]).should == relationships[y][x]
end
end
end
end
end

View file

@ -0,0 +1,107 @@
#
# 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 = mock("Handle", :closed? => false)
@handle.stub!(:close)
::UNIXSocket.stub!(:new).and_return(@handle)
end
it_should_behave_like "a socket"
it "should raise a TransportException when it cannot open a socket" do
::UNIXSocket.should_receive(:new).and_raise(StandardError)
lambda { @socket.open }.should raise_error(Thrift::TransportException) { |e| e.type.should == Thrift::TransportException::NOT_OPEN }
end
it "should accept an optional timeout" do
::UNIXSocket.stub!(:new)
Thrift::UNIXSocket.new(@path, 5).timeout.should == 5
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
UNIXServer.should_receive(:new).with(@path)
@socket.listen
end
it "should create a Thrift::UNIXSocket to wrap accepted sockets" do
handle = mock("UNIXServer")
UNIXServer.should_receive(:new).with(@path).and_return(handle)
@socket.listen
sock = mock("sock")
handle.should_receive(:accept).and_return(sock)
trans = mock("UNIXSocket")
Thrift::UNIXSocket.should_receive(:new).and_return(trans)
trans.should_receive(:handle=).with(sock)
@socket.accept.should == trans
end
it "should close the handle when closed" do
handle = mock("UNIXServer", :closed? => false)
UNIXServer.should_receive(:new).with(@path).and_return(handle)
@socket.listen
handle.should_receive(:close)
File.stub!(:delete)
@socket.close
end
it "should delete the socket when closed" do
handle = mock("UNIXServer", :closed? => false)
UNIXServer.should_receive(:new).with(@path).and_return(handle)
@socket.listen
handle.stub!(:close)
File.should_receive(:delete).with(@path)
@socket.close
end
it "should return nil when accepting if there is no handle" do
@socket.accept.should be_nil
end
it "should return true for closed? when appropriate" do
handle = mock("UNIXServer", :closed? => false)
UNIXServer.stub!(:new).and_return(handle)
File.stub!(:delete)
@socket.listen
@socket.should_not be_closed
handle.stub!(:close)
@socket.close
@socket.should be_closed
@socket.listen
@socket.should_not be_closed
handle.stub!(:closed?).and_return(true)
@socket.should be_closed
end
end
end