thrift-mavericks 0.8.0 → 0.9.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/ext/binary_protocol_accelerated.c +21 -2
- data/ext/bytes.c +36 -0
- data/ext/bytes.h +31 -0
- data/ext/compact_protocol.c +24 -7
- data/ext/constants.h +5 -5
- data/ext/extconf.rb +3 -1
- data/ext/memory_buffer.c +11 -8
- data/ext/protocol.c +0 -185
- data/ext/protocol.h +0 -20
- data/ext/struct.c +0 -3
- data/ext/thrift_native.c +10 -11
- data/lib/thrift.rb +3 -0
- data/lib/thrift/bytes.rb +131 -0
- data/lib/thrift/exceptions.rb +3 -0
- data/lib/thrift/protocol/base_protocol.rb +96 -9
- data/lib/thrift/protocol/binary_protocol.rb +15 -7
- data/lib/thrift/protocol/compact_protocol.rb +14 -6
- data/lib/thrift/protocol/json_protocol.rb +766 -0
- data/lib/thrift/server/mongrel_http_server.rb +2 -0
- data/lib/thrift/server/thin_http_server.rb +91 -0
- data/lib/thrift/struct.rb +1 -1
- data/lib/thrift/struct_union.rb +2 -2
- data/lib/thrift/transport/base_transport.rb +22 -20
- data/lib/thrift/transport/buffered_transport.rb +16 -10
- data/lib/thrift/transport/framed_transport.rb +11 -10
- data/lib/thrift/transport/http_client_transport.rb +11 -6
- data/lib/thrift/transport/io_stream_transport.rb +1 -1
- data/lib/thrift/transport/memory_buffer_transport.rb +6 -6
- data/lib/thrift/transport/socket.rb +4 -2
- data/spec/ThriftSpec.thrift +52 -1
- data/spec/base_protocol_spec.rb +108 -51
- data/spec/base_transport_spec.rb +49 -50
- data/spec/binary_protocol_accelerated_spec.rb +9 -13
- data/spec/binary_protocol_spec.rb +15 -10
- data/spec/binary_protocol_spec_shared.rb +92 -12
- data/spec/bytes_spec.rb +160 -0
- data/spec/client_spec.rb +13 -14
- data/spec/compact_protocol_spec.rb +4 -5
- data/spec/exception_spec.rb +39 -40
- data/spec/gen-rb/thrift_spec_types.rb +192 -0
- data/spec/http_client_spec.rb +65 -9
- data/spec/json_protocol_spec.rb +513 -0
- data/spec/nonblocking_server_spec.rb +18 -20
- data/spec/processor_spec.rb +13 -16
- data/spec/serializer_spec.rb +17 -19
- data/spec/server_socket_spec.rb +6 -7
- data/spec/server_spec.rb +46 -58
- data/spec/socket_spec.rb +11 -11
- data/spec/socket_spec_shared.rb +1 -1
- data/spec/spec_helper.rb +13 -10
- data/spec/struct_nested_containers_spec.rb +191 -0
- data/spec/struct_spec.rb +84 -86
- data/spec/thin_http_server_spec.rb +140 -0
- data/spec/types_spec.rb +65 -66
- data/spec/union_spec.rb +57 -47
- data/spec/unix_socket_spec.rb +8 -9
- metadata +72 -14
- data/spec/mongrel_http_server_spec.rb +0 -117
@@ -0,0 +1,140 @@
|
|
1
|
+
#
|
2
|
+
# Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
# or more contributor license agreements. See the NOTICE file
|
4
|
+
# distributed with this work for additional information
|
5
|
+
# regarding copyright ownership. The ASF licenses this file
|
6
|
+
# to you under the Apache License, Version 2.0 (the
|
7
|
+
# "License"); you may not use this file except in compliance
|
8
|
+
# with the License. You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing,
|
13
|
+
# software distributed under the License is distributed on an
|
14
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
# KIND, either express or implied. See the License for the
|
16
|
+
# specific language governing permissions and limitations
|
17
|
+
# under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
require 'spec_helper'
|
21
|
+
require 'rack/test'
|
22
|
+
|
23
|
+
describe Thrift::ThinHTTPServer do
|
24
|
+
|
25
|
+
let(:processor) { mock('processor') }
|
26
|
+
|
27
|
+
describe "#initialize" do
|
28
|
+
|
29
|
+
context "when using the defaults" do
|
30
|
+
|
31
|
+
it "binds to port 80, with host 0.0.0.0, a path of '/'" do
|
32
|
+
Thin::Server.should_receive(:new).with('0.0.0.0', 80, an_instance_of(Rack::Builder))
|
33
|
+
Thrift::ThinHTTPServer.new(processor)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'creates a ThinHTTPServer::RackApplicationContext' do
|
37
|
+
Thrift::ThinHTTPServer::RackApplication.should_receive(:for).with("/", processor, an_instance_of(Thrift::BinaryProtocolFactory)).and_return(anything)
|
38
|
+
Thrift::ThinHTTPServer.new(processor)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "uses the BinaryProtocolFactory" do
|
42
|
+
Thrift::BinaryProtocolFactory.should_receive(:new)
|
43
|
+
Thrift::ThinHTTPServer.new(processor)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
context "when using the options" do
|
49
|
+
|
50
|
+
it 'accepts :ip, :port, :path' do
|
51
|
+
ip = "192.168.0.1"
|
52
|
+
port = 3000
|
53
|
+
path = "/thin"
|
54
|
+
Thin::Server.should_receive(:new).with(ip, port, an_instance_of(Rack::Builder))
|
55
|
+
Thrift::ThinHTTPServer.new(processor,
|
56
|
+
:ip => ip,
|
57
|
+
:port => port,
|
58
|
+
:path => path)
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'creates a ThinHTTPServer::RackApplicationContext with a different protocol factory' do
|
62
|
+
Thrift::ThinHTTPServer::RackApplication.should_receive(:for).with("/", processor, an_instance_of(Thrift::JsonProtocolFactory)).and_return(anything)
|
63
|
+
Thrift::ThinHTTPServer.new(processor,
|
64
|
+
:protocol_factory => Thrift::JsonProtocolFactory.new)
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "#serve" do
|
72
|
+
|
73
|
+
it 'starts the Thin server' do
|
74
|
+
underlying_thin_server = mock('thin server', :start => true)
|
75
|
+
Thin::Server.stub(:new).and_return(underlying_thin_server)
|
76
|
+
|
77
|
+
thin_thrift_server = Thrift::ThinHTTPServer.new(processor)
|
78
|
+
|
79
|
+
underlying_thin_server.should_receive(:start)
|
80
|
+
thin_thrift_server.serve
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
describe Thrift::ThinHTTPServer::RackApplication do
|
87
|
+
include Rack::Test::Methods
|
88
|
+
|
89
|
+
let(:processor) { mock('processor') }
|
90
|
+
let(:protocol_factory) { mock('protocol factory') }
|
91
|
+
|
92
|
+
def app
|
93
|
+
Thrift::ThinHTTPServer::RackApplication.for("/", processor, protocol_factory)
|
94
|
+
end
|
95
|
+
|
96
|
+
context "404 response" do
|
97
|
+
|
98
|
+
it 'receives a non-POST' do
|
99
|
+
header('Content-Type', "application/x-thrift")
|
100
|
+
get "/"
|
101
|
+
last_response.status.should be 404
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'receives a header other than application/x-thrift' do
|
105
|
+
header('Content-Type', "application/json")
|
106
|
+
post "/"
|
107
|
+
last_response.status.should be 404
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
context "200 response" do
|
113
|
+
|
114
|
+
before do
|
115
|
+
protocol_factory.stub(:get_protocol)
|
116
|
+
processor.stub(:process)
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'creates an IOStreamTransport' do
|
120
|
+
header('Content-Type', "application/x-thrift")
|
121
|
+
Thrift::IOStreamTransport.should_receive(:new).with(an_instance_of(Rack::Lint::InputWrapper), an_instance_of(Rack::Response))
|
122
|
+
post "/"
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'fetches the right protocol based on the Transport' do
|
126
|
+
header('Content-Type', "application/x-thrift")
|
127
|
+
protocol_factory.should_receive(:get_protocol).with(an_instance_of(Thrift::IOStreamTransport))
|
128
|
+
post "/"
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'status code 200' do
|
132
|
+
header('Content-Type', "application/x-thrift")
|
133
|
+
post "/"
|
134
|
+
last_response.ok?.should be_true
|
135
|
+
end
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
140
|
+
|
data/spec/types_spec.rb
CHANGED
@@ -17,10 +17,9 @@
|
|
17
17
|
# under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require 'spec_helper'
|
21
21
|
|
22
|
-
|
23
|
-
include Thrift
|
22
|
+
describe Thrift::Types do
|
24
23
|
|
25
24
|
before(:each) do
|
26
25
|
Thrift.type_checking = true
|
@@ -30,87 +29,87 @@ class ThriftTypesSpec < Spec::ExampleGroup
|
|
30
29
|
Thrift.type_checking = false
|
31
30
|
end
|
32
31
|
|
33
|
-
|
32
|
+
context 'type checking' do
|
34
33
|
it "should return the proper name for each type" do
|
35
|
-
Thrift.type_name(Types::I16).should == "Types::I16"
|
36
|
-
Thrift.type_name(Types::VOID).should == "Types::VOID"
|
37
|
-
Thrift.type_name(Types::LIST).should == "Types::LIST"
|
34
|
+
Thrift.type_name(Thrift::Types::I16).should == "Types::I16"
|
35
|
+
Thrift.type_name(Thrift::Types::VOID).should == "Types::VOID"
|
36
|
+
Thrift.type_name(Thrift::Types::LIST).should == "Types::LIST"
|
38
37
|
Thrift.type_name(42).should be_nil
|
39
38
|
end
|
40
39
|
|
41
40
|
it "should check types properly" do
|
42
|
-
# lambda { Thrift.check_type(nil, Types::STOP) }.should raise_error(TypeError)
|
43
|
-
lambda { Thrift.check_type(3, {:type => Types::STOP}, :foo) }.should raise_error(TypeError)
|
44
|
-
lambda { Thrift.check_type(nil, {:type => Types::VOID}, :foo) }.should_not raise_error(TypeError)
|
45
|
-
lambda { Thrift.check_type(3, {:type => Types::VOID}, :foo) }.should raise_error(TypeError)
|
46
|
-
lambda { Thrift.check_type(true, {:type => Types::BOOL}, :foo) }.should_not raise_error(TypeError)
|
47
|
-
lambda { Thrift.check_type(3, {:type => Types::BOOL}, :foo) }.should raise_error(TypeError)
|
48
|
-
lambda { Thrift.check_type(42, {:type => Types::BYTE}, :foo) }.should_not raise_error(TypeError)
|
49
|
-
lambda { Thrift.check_type(42, {:type => Types::I16}, :foo) }.should_not raise_error(TypeError)
|
50
|
-
lambda { Thrift.check_type(42, {:type => Types::I32}, :foo) }.should_not raise_error(TypeError)
|
51
|
-
lambda { Thrift.check_type(42, {:type => Types::I64}, :foo) }.should_not raise_error(TypeError)
|
52
|
-
lambda { Thrift.check_type(3.14, {:type => Types::I32}, :foo) }.should raise_error(TypeError)
|
53
|
-
lambda { Thrift.check_type(3.14, {:type => Types::DOUBLE}, :foo) }.should_not raise_error(TypeError)
|
54
|
-
lambda { Thrift.check_type(3, {:type => Types::DOUBLE}, :foo) }.should raise_error(TypeError)
|
55
|
-
lambda { Thrift.check_type("3", {:type => Types::STRING}, :foo) }.should_not raise_error(TypeError)
|
56
|
-
lambda { Thrift.check_type(3, {:type => Types::STRING}, :foo) }.should raise_error(TypeError)
|
41
|
+
# lambda { Thrift.check_type(nil, Thrift::Types::STOP) }.should raise_error(Thrift::TypeError)
|
42
|
+
lambda { Thrift.check_type(3, {:type => Thrift::Types::STOP}, :foo) }.should raise_error(Thrift::TypeError)
|
43
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::VOID}, :foo) }.should_not raise_error(Thrift::TypeError)
|
44
|
+
lambda { Thrift.check_type(3, {:type => Thrift::Types::VOID}, :foo) }.should raise_error(Thrift::TypeError)
|
45
|
+
lambda { Thrift.check_type(true, {:type => Thrift::Types::BOOL}, :foo) }.should_not raise_error(Thrift::TypeError)
|
46
|
+
lambda { Thrift.check_type(3, {:type => Thrift::Types::BOOL}, :foo) }.should raise_error(Thrift::TypeError)
|
47
|
+
lambda { Thrift.check_type(42, {:type => Thrift::Types::BYTE}, :foo) }.should_not raise_error(Thrift::TypeError)
|
48
|
+
lambda { Thrift.check_type(42, {:type => Thrift::Types::I16}, :foo) }.should_not raise_error(Thrift::TypeError)
|
49
|
+
lambda { Thrift.check_type(42, {:type => Thrift::Types::I32}, :foo) }.should_not raise_error(Thrift::TypeError)
|
50
|
+
lambda { Thrift.check_type(42, {:type => Thrift::Types::I64}, :foo) }.should_not raise_error(Thrift::TypeError)
|
51
|
+
lambda { Thrift.check_type(3.14, {:type => Thrift::Types::I32}, :foo) }.should raise_error(Thrift::TypeError)
|
52
|
+
lambda { Thrift.check_type(3.14, {:type => Thrift::Types::DOUBLE}, :foo) }.should_not raise_error(Thrift::TypeError)
|
53
|
+
lambda { Thrift.check_type(3, {:type => Thrift::Types::DOUBLE}, :foo) }.should raise_error(Thrift::TypeError)
|
54
|
+
lambda { Thrift.check_type("3", {:type => Thrift::Types::STRING}, :foo) }.should_not raise_error(Thrift::TypeError)
|
55
|
+
lambda { Thrift.check_type(3, {:type => Thrift::Types::STRING}, :foo) }.should raise_error(Thrift::TypeError)
|
57
56
|
hello = SpecNamespace::Hello.new
|
58
|
-
lambda { Thrift.check_type(hello, {:type => Types::STRUCT, :class => SpecNamespace::Hello}, :foo) }.should_not raise_error(TypeError)
|
59
|
-
lambda { Thrift.check_type("foo", {:type => Types::STRUCT}, :foo) }.should raise_error(TypeError)
|
60
|
-
lambda { Thrift.check_type({:foo => 1}, {:type => Types::MAP}, :foo) }.should_not raise_error(TypeError)
|
61
|
-
lambda { Thrift.check_type([1], {:type => Types::MAP}, :foo) }.should raise_error(TypeError)
|
62
|
-
lambda { Thrift.check_type([1], {:type => Types::LIST}, :foo) }.should_not raise_error(TypeError)
|
63
|
-
lambda { Thrift.check_type({:foo => 1}, {:type => Types::LIST}, :foo) }.should raise_error(TypeError)
|
64
|
-
lambda { Thrift.check_type(Set.new([1,2]), {:type => Types::SET}, :foo) }.should_not raise_error(TypeError)
|
65
|
-
lambda { Thrift.check_type([1,2], {:type => Types::SET}, :foo) }.should raise_error(TypeError)
|
66
|
-
lambda { Thrift.check_type({:foo => true}, {:type => Types::SET}, :foo) }.should raise_error(TypeError)
|
57
|
+
lambda { Thrift.check_type(hello, {:type => Thrift::Types::STRUCT, :class => SpecNamespace::Hello}, :foo) }.should_not raise_error(Thrift::TypeError)
|
58
|
+
lambda { Thrift.check_type("foo", {:type => Thrift::Types::STRUCT}, :foo) }.should raise_error(Thrift::TypeError)
|
59
|
+
lambda { Thrift.check_type({:foo => 1}, {:type => Thrift::Types::MAP}, :foo) }.should_not raise_error(Thrift::TypeError)
|
60
|
+
lambda { Thrift.check_type([1], {:type => Thrift::Types::MAP}, :foo) }.should raise_error(Thrift::TypeError)
|
61
|
+
lambda { Thrift.check_type([1], {:type => Thrift::Types::LIST}, :foo) }.should_not raise_error(Thrift::TypeError)
|
62
|
+
lambda { Thrift.check_type({:foo => 1}, {:type => Thrift::Types::LIST}, :foo) }.should raise_error(Thrift::TypeError)
|
63
|
+
lambda { Thrift.check_type(Set.new([1,2]), {:type => Thrift::Types::SET}, :foo) }.should_not raise_error(Thrift::TypeError)
|
64
|
+
lambda { Thrift.check_type([1,2], {:type => Thrift::Types::SET}, :foo) }.should raise_error(Thrift::TypeError)
|
65
|
+
lambda { Thrift.check_type({:foo => true}, {:type => Thrift::Types::SET}, :foo) }.should raise_error(Thrift::TypeError)
|
67
66
|
end
|
68
67
|
|
69
68
|
it "should error out if nil is passed and skip_types is false" do
|
70
|
-
lambda { Thrift.check_type(nil, {:type => Types::BOOL}, :foo, false) }.should raise_error(TypeError)
|
71
|
-
lambda { Thrift.check_type(nil, {:type => Types::BYTE}, :foo, false) }.should raise_error(TypeError)
|
72
|
-
lambda { Thrift.check_type(nil, {:type => Types::I16}, :foo, false) }.should raise_error(TypeError)
|
73
|
-
lambda { Thrift.check_type(nil, {:type => Types::I32}, :foo, false) }.should raise_error(TypeError)
|
74
|
-
lambda { Thrift.check_type(nil, {:type => Types::I64}, :foo, false) }.should raise_error(TypeError)
|
75
|
-
lambda { Thrift.check_type(nil, {:type => Types::DOUBLE}, :foo, false) }.should raise_error(TypeError)
|
76
|
-
lambda { Thrift.check_type(nil, {:type => Types::STRING}, :foo, false) }.should raise_error(TypeError)
|
77
|
-
lambda { Thrift.check_type(nil, {:type => Types::STRUCT}, :foo, false) }.should raise_error(TypeError)
|
78
|
-
lambda { Thrift.check_type(nil, {:type => Types::LIST}, :foo, false) }.should raise_error(TypeError)
|
79
|
-
lambda { Thrift.check_type(nil, {:type => Types::SET}, :foo, false) }.should raise_error(TypeError)
|
80
|
-
lambda { Thrift.check_type(nil, {:type => Types::MAP}, :foo, false) }.should raise_error(TypeError)
|
69
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::BOOL}, :foo, false) }.should raise_error(Thrift::TypeError)
|
70
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::BYTE}, :foo, false) }.should raise_error(Thrift::TypeError)
|
71
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::I16}, :foo, false) }.should raise_error(Thrift::TypeError)
|
72
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::I32}, :foo, false) }.should raise_error(Thrift::TypeError)
|
73
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::I64}, :foo, false) }.should raise_error(Thrift::TypeError)
|
74
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::DOUBLE}, :foo, false) }.should raise_error(Thrift::TypeError)
|
75
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::STRING}, :foo, false) }.should raise_error(Thrift::TypeError)
|
76
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::STRUCT}, :foo, false) }.should raise_error(Thrift::TypeError)
|
77
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::LIST}, :foo, false) }.should raise_error(Thrift::TypeError)
|
78
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::SET}, :foo, false) }.should raise_error(Thrift::TypeError)
|
79
|
+
lambda { Thrift.check_type(nil, {:type => Thrift::Types::MAP}, :foo, false) }.should raise_error(Thrift::TypeError)
|
81
80
|
end
|
82
81
|
|
83
82
|
it "should check element types on containers" do
|
84
|
-
field = {:type => Types::LIST, :element => {:type => Types::I32}}
|
85
|
-
lambda { Thrift.check_type([1, 2], field, :foo) }.should_not raise_error(TypeError)
|
86
|
-
lambda { Thrift.check_type([1, nil, 2], field, :foo) }.should raise_error(TypeError)
|
87
|
-
field = {:type => Types::MAP, :key => {:type => Types::I32}, :value => {:type => Types::STRING}}
|
88
|
-
lambda { Thrift.check_type({1 => "one", 2 => "two"}, field, :foo) }.should_not raise_error(TypeError)
|
89
|
-
lambda { Thrift.check_type({1 => "one", nil => "nil"}, field, :foo) }.should raise_error(TypeError)
|
90
|
-
lambda { Thrift.check_type({1 => nil, 2 => "two"}, field, :foo) }.should raise_error(TypeError)
|
91
|
-
field = {:type => Types::SET, :element => {:type => Types::I32}}
|
92
|
-
lambda { Thrift.check_type(Set.new([1, 2]), field, :foo) }.should_not raise_error(TypeError)
|
93
|
-
lambda { Thrift.check_type(Set.new([1, nil, 2]), field, :foo) }.should raise_error(TypeError)
|
94
|
-
lambda { Thrift.check_type(Set.new([1, 2.3, 2]), field, :foo) }.should raise_error(TypeError)
|
95
|
-
|
96
|
-
field = {:type => Types::STRUCT, :class => SpecNamespace::Hello}
|
97
|
-
lambda { Thrift.check_type(SpecNamespace::BoolStruct, field, :foo) }.should raise_error(TypeError)
|
83
|
+
field = {:type => Thrift::Types::LIST, :element => {:type => Thrift::Types::I32}}
|
84
|
+
lambda { Thrift.check_type([1, 2], field, :foo) }.should_not raise_error(Thrift::TypeError)
|
85
|
+
lambda { Thrift.check_type([1, nil, 2], field, :foo) }.should raise_error(Thrift::TypeError)
|
86
|
+
field = {:type => Thrift::Types::MAP, :key => {:type => Thrift::Types::I32}, :value => {:type => Thrift::Types::STRING}}
|
87
|
+
lambda { Thrift.check_type({1 => "one", 2 => "two"}, field, :foo) }.should_not raise_error(Thrift::TypeError)
|
88
|
+
lambda { Thrift.check_type({1 => "one", nil => "nil"}, field, :foo) }.should raise_error(Thrift::TypeError)
|
89
|
+
lambda { Thrift.check_type({1 => nil, 2 => "two"}, field, :foo) }.should raise_error(Thrift::TypeError)
|
90
|
+
field = {:type => Thrift::Types::SET, :element => {:type => Thrift::Types::I32}}
|
91
|
+
lambda { Thrift.check_type(Set.new([1, 2]), field, :foo) }.should_not raise_error(Thrift::TypeError)
|
92
|
+
lambda { Thrift.check_type(Set.new([1, nil, 2]), field, :foo) }.should raise_error(Thrift::TypeError)
|
93
|
+
lambda { Thrift.check_type(Set.new([1, 2.3, 2]), field, :foo) }.should raise_error(Thrift::TypeError)
|
94
|
+
|
95
|
+
field = {:type => Thrift::Types::STRUCT, :class => SpecNamespace::Hello}
|
96
|
+
lambda { Thrift.check_type(SpecNamespace::BoolStruct, field, :foo) }.should raise_error(Thrift::TypeError)
|
98
97
|
end
|
99
98
|
|
100
|
-
it "should give the TypeError a readable message" do
|
99
|
+
it "should give the Thrift::TypeError a readable message" do
|
101
100
|
msg = "Expected Types::STRING, received Fixnum for field foo"
|
102
|
-
lambda { Thrift.check_type(3, {:type => Types::STRING}, :foo) }.should raise_error(TypeError, msg)
|
101
|
+
lambda { Thrift.check_type(3, {:type => Thrift::Types::STRING}, :foo) }.should raise_error(Thrift::TypeError, msg)
|
103
102
|
msg = "Expected Types::STRING, received Fixnum for field foo.element"
|
104
|
-
field = {:type => Types::LIST, :element => {:type => Types::STRING}}
|
105
|
-
lambda { Thrift.check_type([3], field, :foo) }.should raise_error(TypeError, msg)
|
103
|
+
field = {:type => Thrift::Types::LIST, :element => {:type => Thrift::Types::STRING}}
|
104
|
+
lambda { Thrift.check_type([3], field, :foo) }.should raise_error(Thrift::TypeError, msg)
|
106
105
|
msg = "Expected Types::I32, received NilClass for field foo.element.key"
|
107
|
-
field = {:type => Types::LIST,
|
108
|
-
:element => {:type => Types::MAP,
|
109
|
-
:key => {:type => Types::I32},
|
110
|
-
:value => {:type => Types::I32}}}
|
111
|
-
lambda { Thrift.check_type([{nil => 3}], field, :foo) }.should raise_error(TypeError, msg)
|
106
|
+
field = {:type => Thrift::Types::LIST,
|
107
|
+
:element => {:type => Thrift::Types::MAP,
|
108
|
+
:key => {:type => Thrift::Types::I32},
|
109
|
+
:value => {:type => Thrift::Types::I32}}}
|
110
|
+
lambda { Thrift.check_type([{nil => 3}], field, :foo) }.should raise_error(Thrift::TypeError, msg)
|
112
111
|
msg = "Expected Types::I32, received NilClass for field foo.element.value"
|
113
|
-
lambda { Thrift.check_type([{1 => nil}], field, :foo) }.should raise_error(TypeError, msg)
|
112
|
+
lambda { Thrift.check_type([{1 => nil}], field, :foo) }.should raise_error(Thrift::TypeError, msg)
|
114
113
|
end
|
115
114
|
end
|
116
115
|
end
|
data/spec/union_spec.rb
CHANGED
@@ -17,21 +17,19 @@
|
|
17
17
|
# under the License.
|
18
18
|
#
|
19
19
|
|
20
|
-
require
|
20
|
+
require 'spec_helper'
|
21
21
|
|
22
|
-
|
23
|
-
include Thrift
|
24
|
-
include SpecNamespace
|
22
|
+
describe 'Union' do
|
25
23
|
|
26
|
-
describe Union do
|
24
|
+
describe Thrift::Union do
|
27
25
|
it "should return nil value in unset union" do
|
28
|
-
union = My_union.new
|
26
|
+
union = SpecNamespace::My_union.new
|
29
27
|
union.get_set_field.should == nil
|
30
28
|
union.get_value.should == nil
|
31
29
|
end
|
32
30
|
|
33
31
|
it "should set a field and be accessible through get_value and the named field accessor" do
|
34
|
-
union = My_union.new
|
32
|
+
union = SpecNamespace::My_union.new
|
35
33
|
union.integer32 = 25
|
36
34
|
union.get_set_field.should == :integer32
|
37
35
|
union.get_value.should == 25
|
@@ -39,30 +37,30 @@ class ThriftUnionSpec < Spec::ExampleGroup
|
|
39
37
|
end
|
40
38
|
|
41
39
|
it "should work correctly when instantiated with static field constructors" do
|
42
|
-
union = My_union.integer32(5)
|
40
|
+
union = SpecNamespace::My_union.integer32(5)
|
43
41
|
union.get_set_field.should == :integer32
|
44
42
|
union.integer32.should == 5
|
45
43
|
end
|
46
44
|
|
47
45
|
it "should raise for wrong set field" do
|
48
|
-
union = My_union.new
|
46
|
+
union = SpecNamespace::My_union.new
|
49
47
|
union.integer32 = 25
|
50
48
|
lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
|
51
49
|
end
|
52
|
-
|
50
|
+
|
53
51
|
it "should not be equal to nil" do
|
54
|
-
union = My_union.new
|
52
|
+
union = SpecNamespace::My_union.new
|
55
53
|
union.should_not == nil
|
56
54
|
end
|
57
|
-
|
55
|
+
|
58
56
|
it "should not equate two different unions, i32 vs. string" do
|
59
|
-
union = My_union.new(:integer32, 25)
|
60
|
-
other_union = My_union.new(:some_characters, "blah!")
|
57
|
+
union = SpecNamespace::My_union.new(:integer32, 25)
|
58
|
+
other_union = SpecNamespace::My_union.new(:some_characters, "blah!")
|
61
59
|
union.should_not == other_union
|
62
60
|
end
|
63
61
|
|
64
62
|
it "should properly reset setfield and setvalue" do
|
65
|
-
union = My_union.new(:integer32, 25)
|
63
|
+
union = SpecNamespace::My_union.new(:integer32, 25)
|
66
64
|
union.get_set_field.should == :integer32
|
67
65
|
union.some_characters = "blah!"
|
68
66
|
union.get_set_field.should == :some_characters
|
@@ -71,70 +69,82 @@ class ThriftUnionSpec < Spec::ExampleGroup
|
|
71
69
|
end
|
72
70
|
|
73
71
|
it "should not equate two different unions with different values" do
|
74
|
-
union = My_union.new(:integer32, 25)
|
75
|
-
other_union = My_union.new(:integer32, 400)
|
72
|
+
union = SpecNamespace::My_union.new(:integer32, 25)
|
73
|
+
other_union = SpecNamespace::My_union.new(:integer32, 400)
|
76
74
|
union.should_not == other_union
|
77
75
|
end
|
78
76
|
|
79
77
|
it "should not equate two different unions with different fields" do
|
80
|
-
union = My_union.new(:integer32, 25)
|
81
|
-
other_union = My_union.new(:other_i32, 25)
|
78
|
+
union = SpecNamespace::My_union.new(:integer32, 25)
|
79
|
+
other_union = SpecNamespace::My_union.new(:other_i32, 25)
|
82
80
|
union.should_not == other_union
|
83
81
|
end
|
84
82
|
|
85
83
|
it "should inspect properly" do
|
86
|
-
union = My_union.new(:integer32, 25)
|
84
|
+
union = SpecNamespace::My_union.new(:integer32, 25)
|
87
85
|
union.inspect.should == "<SpecNamespace::My_union integer32: 25>"
|
88
86
|
end
|
89
87
|
|
90
88
|
it "should not allow setting with instance_variable_set" do
|
91
|
-
union = My_union.new(:integer32, 27)
|
89
|
+
union = SpecNamespace::My_union.new(:integer32, 27)
|
92
90
|
union.instance_variable_set(:@some_characters, "hallo!")
|
93
91
|
union.get_set_field.should == :integer32
|
94
92
|
union.get_value.should == 27
|
95
93
|
lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
|
96
94
|
end
|
97
95
|
|
98
|
-
it "should serialize correctly" do
|
96
|
+
it "should serialize to binary correctly" do
|
99
97
|
trans = Thrift::MemoryBufferTransport.new
|
100
98
|
proto = Thrift::BinaryProtocol.new(trans)
|
101
99
|
|
102
|
-
union = My_union.new(:integer32, 25)
|
100
|
+
union = SpecNamespace::My_union.new(:integer32, 25)
|
101
|
+
union.write(proto)
|
102
|
+
|
103
|
+
other_union = SpecNamespace::My_union.new(:integer32, 25)
|
104
|
+
other_union.read(proto)
|
105
|
+
other_union.should == union
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should serialize to json correctly" do
|
109
|
+
trans = Thrift::MemoryBufferTransport.new
|
110
|
+
proto = Thrift::JsonProtocol.new(trans)
|
111
|
+
|
112
|
+
union = SpecNamespace::My_union.new(:integer32, 25)
|
103
113
|
union.write(proto)
|
104
114
|
|
105
|
-
other_union = My_union.new(:integer32, 25)
|
115
|
+
other_union = SpecNamespace::My_union.new(:integer32, 25)
|
106
116
|
other_union.read(proto)
|
107
117
|
other_union.should == union
|
108
118
|
end
|
109
119
|
|
110
120
|
it "should raise when validating unset union" do
|
111
|
-
union = My_union.new
|
121
|
+
union = SpecNamespace::My_union.new
|
112
122
|
lambda { union.validate }.should raise_error(StandardError, "Union fields are not set.")
|
113
123
|
|
114
|
-
other_union = My_union.new(:integer32, 1)
|
124
|
+
other_union = SpecNamespace::My_union.new(:integer32, 1)
|
115
125
|
lambda { other_union.validate }.should_not raise_error(StandardError, "Union fields are not set.")
|
116
126
|
end
|
117
127
|
|
118
128
|
it "should validate an enum field properly" do
|
119
|
-
union = TestUnion.new(:enum_field, 3)
|
129
|
+
union = SpecNamespace::TestUnion.new(:enum_field, 3)
|
120
130
|
union.get_set_field.should == :enum_field
|
121
|
-
lambda { union.validate }.should raise_error(ProtocolException, "Invalid value of field enum_field!")
|
131
|
+
lambda { union.validate }.should raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!")
|
122
132
|
|
123
|
-
other_union = TestUnion.new(:enum_field, 1)
|
124
|
-
lambda { other_union.validate }.should_not raise_error(ProtocolException, "Invalid value of field enum_field!")
|
133
|
+
other_union = SpecNamespace::TestUnion.new(:enum_field, 1)
|
134
|
+
lambda { other_union.validate }.should_not raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!")
|
125
135
|
end
|
126
136
|
|
127
137
|
it "should properly serialize and match structs with a union" do
|
128
|
-
union = My_union.new(:integer32, 26)
|
129
|
-
swu = Struct_with_union.new(:fun_union => union)
|
138
|
+
union = SpecNamespace::My_union.new(:integer32, 26)
|
139
|
+
swu = SpecNamespace::Struct_with_union.new(:fun_union => union)
|
130
140
|
|
131
141
|
trans = Thrift::MemoryBufferTransport.new
|
132
142
|
proto = Thrift::CompactProtocol.new(trans)
|
133
143
|
|
134
144
|
swu.write(proto)
|
135
145
|
|
136
|
-
other_union = My_union.new(:some_characters, "hello there")
|
137
|
-
swu2 = Struct_with_union.new(:fun_union => other_union)
|
146
|
+
other_union = SpecNamespace::My_union.new(:some_characters, "hello there")
|
147
|
+
swu2 = SpecNamespace::Struct_with_union.new(:fun_union => other_union)
|
138
148
|
|
139
149
|
swu2.should_not == swu
|
140
150
|
|
@@ -143,30 +153,30 @@ class ThriftUnionSpec < Spec::ExampleGroup
|
|
143
153
|
end
|
144
154
|
|
145
155
|
it "should support old style constructor" do
|
146
|
-
union = My_union.new(:integer32 => 26)
|
156
|
+
union = SpecNamespace::My_union.new(:integer32 => 26)
|
147
157
|
union.get_set_field.should == :integer32
|
148
158
|
union.get_value.should == 26
|
149
159
|
end
|
150
160
|
|
151
161
|
it "should not throw an error when inspected and unset" do
|
152
|
-
lambda{TestUnion.new().inspect}.should_not raise_error
|
162
|
+
lambda{SpecNamespace::TestUnion.new().inspect}.should_not raise_error
|
153
163
|
end
|
154
164
|
|
155
165
|
it "should print enum value name when inspected" do
|
156
|
-
My_union.new(:some_enum => SomeEnum::ONE).inspect.should == "<SpecNamespace::My_union some_enum: ONE (0)>"
|
166
|
+
SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect.should == "<SpecNamespace::My_union some_enum: ONE (0)>"
|
157
167
|
|
158
|
-
My_union.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>"
|
168
|
+
SpecNamespace::My_union.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect.should == "<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>"
|
159
169
|
end
|
160
170
|
|
161
171
|
it "should offer field? methods" do
|
162
|
-
My_union.new.some_enum?.should be_false
|
163
|
-
My_union.new(:some_enum => SomeEnum::ONE).some_enum?.should be_true
|
164
|
-
My_union.new(:im_true => false).im_true?.should be_true
|
165
|
-
My_union.new(:im_true => true).im_true?.should be_true
|
172
|
+
SpecNamespace::My_union.new.some_enum?.should be_false
|
173
|
+
SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).some_enum?.should be_true
|
174
|
+
SpecNamespace::My_union.new(:im_true => false).im_true?.should be_true
|
175
|
+
SpecNamespace::My_union.new(:im_true => true).im_true?.should be_true
|
166
176
|
end
|
167
177
|
|
168
178
|
it "should pretty print binary fields" do
|
169
|
-
TestUnion.new(:binary_field => "\001\002\003").inspect.should == "<SpecNamespace::TestUnion binary_field: 010203>"
|
179
|
+
SpecNamespace::TestUnion.new(:binary_field => "\001\002\003").inspect.should == "<SpecNamespace::TestUnion binary_field: 010203>"
|
170
180
|
end
|
171
181
|
|
172
182
|
it "should be comparable" do
|
@@ -177,10 +187,10 @@ class ThriftUnionSpec < Spec::ExampleGroup
|
|
177
187
|
[1, 1, 1, 0]]
|
178
188
|
|
179
189
|
objs = [
|
180
|
-
TestUnion.new(:string_field, "blah"),
|
181
|
-
TestUnion.new(:string_field, "blahblah"),
|
182
|
-
TestUnion.new(:i32_field, 1),
|
183
|
-
TestUnion.new()]
|
190
|
+
SpecNamespace::TestUnion.new(:string_field, "blah"),
|
191
|
+
SpecNamespace::TestUnion.new(:string_field, "blahblah"),
|
192
|
+
SpecNamespace::TestUnion.new(:i32_field, 1),
|
193
|
+
SpecNamespace::TestUnion.new()]
|
184
194
|
|
185
195
|
for y in 0..3
|
186
196
|
for x in 0..3
|