thrift 0.9.2.0 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/binary_protocol_accelerated.c +12 -12
- data/ext/struct.c +5 -1
- data/lib/thrift.rb +8 -4
- data/lib/thrift/multiplexed_processor.rb +76 -0
- data/lib/thrift/processor.rb +24 -6
- data/lib/thrift/protocol/base_protocol.rb +11 -3
- data/lib/thrift/protocol/binary_protocol.rb +8 -1
- data/lib/thrift/protocol/binary_protocol_accelerated.rb +8 -0
- data/lib/thrift/protocol/compact_protocol.rb +8 -0
- data/lib/thrift/protocol/json_protocol.rb +21 -4
- data/lib/thrift/protocol/multiplexed_protocol.rb +44 -0
- data/lib/thrift/protocol/protocol_decorator.rb +194 -0
- data/lib/thrift/server/base_server.rb +8 -2
- data/lib/thrift/server/simple_server.rb +5 -1
- data/lib/thrift/server/thread_pool_server.rb +5 -1
- data/lib/thrift/server/threaded_server.rb +5 -1
- data/lib/thrift/transport/base_server_transport.rb +1 -1
- data/lib/thrift/transport/base_transport.rb +8 -0
- data/lib/thrift/transport/buffered_transport.rb +9 -1
- data/lib/thrift/transport/framed_transport.rb +9 -1
- data/lib/thrift/transport/http_client_transport.rb +7 -0
- data/lib/thrift/transport/io_stream_transport.rb +4 -1
- data/lib/thrift/transport/memory_buffer_transport.rb +4 -0
- data/lib/thrift/transport/server_socket.rb +6 -1
- data/lib/thrift/transport/socket.rb +21 -17
- data/lib/thrift/transport/ssl_server_socket.rb +41 -0
- data/lib/thrift/transport/ssl_socket.rb +51 -0
- data/lib/thrift/transport/unix_server_socket.rb +5 -1
- data/lib/thrift/transport/unix_socket.rb +5 -1
- data/lib/thrift/union.rb +3 -6
- data/spec/BaseService.thrift +27 -0
- data/spec/ExtendedService.thrift +25 -0
- data/spec/base_protocol_spec.rb +79 -71
- data/spec/base_transport_spec.rb +155 -117
- data/spec/binary_protocol_accelerated_spec.rb +6 -2
- data/spec/binary_protocol_spec.rb +16 -8
- data/spec/binary_protocol_spec_shared.rb +75 -72
- data/spec/bytes_spec.rb +38 -38
- data/spec/client_spec.rb +41 -42
- data/spec/compact_protocol_spec.rb +32 -17
- data/spec/exception_spec.rb +54 -54
- data/spec/flat_spec.rb +62 -0
- data/spec/http_client_spec.rb +74 -33
- data/spec/json_protocol_spec.rb +170 -131
- data/spec/namespaced_spec.rb +10 -5
- data/spec/nonblocking_server_spec.rb +16 -16
- data/spec/processor_spec.rb +26 -26
- data/spec/serializer_spec.rb +20 -20
- data/spec/server_socket_spec.rb +27 -22
- data/spec/server_spec.rb +91 -51
- data/spec/socket_spec.rb +23 -16
- data/spec/socket_spec_shared.rb +31 -31
- data/spec/spec_helper.rb +4 -1
- data/spec/ssl_server_socket_spec.rb +34 -0
- data/spec/ssl_socket_spec.rb +78 -0
- data/spec/struct_nested_containers_spec.rb +24 -24
- data/spec/struct_spec.rb +120 -120
- data/spec/thin_http_server_spec.rb +18 -18
- data/spec/types_spec.rb +56 -53
- data/spec/union_spec.rb +51 -40
- data/spec/unix_socket_spec.rb +43 -34
- metadata +191 -143
data/spec/socket_spec.rb
CHANGED
@@ -25,37 +25,44 @@ describe 'Socket' do
|
|
25
25
|
describe Thrift::Socket do
|
26
26
|
before(:each) do
|
27
27
|
@socket = Thrift::Socket.new
|
28
|
-
@handle =
|
29
|
-
@handle.
|
30
|
-
@handle.
|
31
|
-
@handle.
|
32
|
-
::Socket.
|
28
|
+
@handle = double("Handle", :closed? => false)
|
29
|
+
allow(@handle).to receive(:close)
|
30
|
+
allow(@handle).to receive(:connect_nonblock)
|
31
|
+
allow(@handle).to receive(:setsockopt)
|
32
|
+
allow(::Socket).to receive(:new).and_return(@handle)
|
33
33
|
end
|
34
34
|
|
35
35
|
it_should_behave_like "a socket"
|
36
36
|
|
37
37
|
it "should raise a TransportException when it cannot open a socket" do
|
38
|
-
::Socket.
|
39
|
-
|
38
|
+
expect(::Socket).to receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]])
|
39
|
+
expect { @socket.open }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should open a ::Socket with default args" do
|
43
|
-
::Socket.
|
44
|
-
::Socket.
|
45
|
-
::Socket.
|
43
|
+
expect(::Socket).to receive(:new).and_return(double("Handle", :connect_nonblock => true, :setsockopt => nil))
|
44
|
+
expect(::Socket).to receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]])
|
45
|
+
expect(::Socket).to receive(:sockaddr_in)
|
46
|
+
@socket.to_s == "socket(localhost:9090)"
|
46
47
|
@socket.open
|
47
48
|
end
|
48
49
|
|
49
50
|
it "should accept host/port options" do
|
50
|
-
::Socket.
|
51
|
-
::Socket.
|
52
|
-
::Socket.
|
53
|
-
Thrift::Socket.new('my.domain', 1234).open
|
51
|
+
expect(::Socket).to receive(:new).and_return(double("Handle", :connect_nonblock => true, :setsockopt => nil))
|
52
|
+
expect(::Socket).to receive(:getaddrinfo).with("my.domain", 1234, nil, ::Socket::SOCK_STREAM).and_return([[]])
|
53
|
+
expect(::Socket).to receive(:sockaddr_in)
|
54
|
+
@socket = Thrift::Socket.new('my.domain', 1234).open
|
55
|
+
@socket.to_s == "socket(my.domain:1234)"
|
54
56
|
end
|
55
57
|
|
56
58
|
it "should accept an optional timeout" do
|
57
|
-
::Socket.
|
58
|
-
Thrift::Socket.new('localhost', 8080, 5).timeout.
|
59
|
+
allow(::Socket).to receive(:new)
|
60
|
+
expect(Thrift::Socket.new('localhost', 8080, 5).timeout).to eq(5)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should provide a reasonable to_s" do
|
64
|
+
allow(::Socket).to receive(:new)
|
65
|
+
expect(Thrift::Socket.new('myhost', 8090).to_s).to eq("socket(myhost:8090)")
|
59
66
|
end
|
60
67
|
end
|
61
68
|
end
|
data/spec/socket_spec_shared.rb
CHANGED
@@ -21,84 +21,84 @@ require 'spec_helper'
|
|
21
21
|
|
22
22
|
shared_examples_for "a socket" do
|
23
23
|
it "should open a socket" do
|
24
|
-
@socket.open.
|
24
|
+
expect(@socket.open).to eq(@handle)
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should be open whenever it has a handle" do
|
28
|
-
@socket.
|
28
|
+
expect(@socket).not_to be_open
|
29
29
|
@socket.open
|
30
|
-
@socket.
|
30
|
+
expect(@socket).to be_open
|
31
31
|
@socket.handle = nil
|
32
|
-
@socket.
|
32
|
+
expect(@socket).not_to be_open
|
33
33
|
@socket.handle = @handle
|
34
34
|
@socket.close
|
35
|
-
@socket.
|
35
|
+
expect(@socket).not_to be_open
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should write data to the handle" do
|
39
39
|
@socket.open
|
40
|
-
@handle.
|
40
|
+
expect(@handle).to receive(:write).with("foobar")
|
41
41
|
@socket.write("foobar")
|
42
|
-
@handle.
|
43
|
-
|
42
|
+
expect(@handle).to receive(:write).with("fail").and_raise(StandardError)
|
43
|
+
expect { @socket.write("fail") }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should raise an error when it cannot read from the handle" do
|
47
47
|
@socket.open
|
48
|
-
@handle.
|
49
|
-
|
48
|
+
expect(@handle).to receive(:readpartial).with(17).and_raise(StandardError)
|
49
|
+
expect { @socket.read(17) }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should return the data read when reading from the handle works" do
|
53
53
|
@socket.open
|
54
|
-
@handle.
|
55
|
-
@socket.read(17).
|
54
|
+
expect(@handle).to receive(:readpartial).with(17).and_return("test data")
|
55
|
+
expect(@socket.read(17)).to eq("test data")
|
56
56
|
end
|
57
57
|
|
58
58
|
it "should declare itself as closed when it has an error" do
|
59
59
|
@socket.open
|
60
|
-
@handle.
|
61
|
-
@socket.
|
62
|
-
|
63
|
-
@socket.
|
60
|
+
expect(@handle).to receive(:write).with("fail").and_raise(StandardError)
|
61
|
+
expect(@socket).to be_open
|
62
|
+
expect { @socket.write("fail") }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
|
63
|
+
expect(@socket).not_to be_open
|
64
64
|
end
|
65
65
|
|
66
66
|
it "should raise an error when the stream is closed" do
|
67
67
|
@socket.open
|
68
|
-
@handle.
|
69
|
-
@socket.
|
70
|
-
|
71
|
-
|
68
|
+
allow(@handle).to receive(:closed?).and_return(true)
|
69
|
+
expect(@socket).not_to be_open
|
70
|
+
expect { @socket.write("fail") }.to raise_error(IOError, "closed stream")
|
71
|
+
expect { @socket.read(10) }.to raise_error(IOError, "closed stream")
|
72
72
|
end
|
73
73
|
|
74
74
|
it "should support the timeout accessor for read" do
|
75
75
|
@socket.timeout = 3
|
76
76
|
@socket.open
|
77
|
-
IO.
|
78
|
-
@handle.
|
79
|
-
@socket.read(17).
|
77
|
+
expect(IO).to receive(:select).with([@handle], nil, nil, 3).and_return([[@handle], [], []])
|
78
|
+
expect(@handle).to receive(:readpartial).with(17).and_return("test data")
|
79
|
+
expect(@socket.read(17)).to eq("test data")
|
80
80
|
end
|
81
81
|
|
82
82
|
it "should support the timeout accessor for write" do
|
83
83
|
@socket.timeout = 3
|
84
84
|
@socket.open
|
85
|
-
IO.
|
86
|
-
@handle.
|
87
|
-
@handle.
|
88
|
-
@socket.write("test data").
|
85
|
+
expect(IO).to receive(:select).with(nil, [@handle], nil, 3).twice.and_return([[], [@handle], []])
|
86
|
+
expect(@handle).to receive(:write_nonblock).with("test data").and_return(4)
|
87
|
+
expect(@handle).to receive(:write_nonblock).with(" data").and_return(5)
|
88
|
+
expect(@socket.write("test data")).to eq(9)
|
89
89
|
end
|
90
90
|
|
91
91
|
it "should raise an error when read times out" do
|
92
92
|
@socket.timeout = 0.5
|
93
93
|
@socket.open
|
94
|
-
IO.
|
95
|
-
|
94
|
+
expect(IO).to receive(:select).once {sleep(0.5); nil}
|
95
|
+
expect { @socket.read(17) }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::TIMED_OUT) }
|
96
96
|
end
|
97
97
|
|
98
98
|
it "should raise an error when write times out" do
|
99
99
|
@socket.timeout = 0.5
|
100
100
|
@socket.open
|
101
|
-
IO.
|
102
|
-
|
101
|
+
allow(IO).to receive(:select).with(nil, [@handle], nil, 0.5).and_return(nil)
|
102
|
+
expect { @socket.write("test data") }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::TIMED_OUT) }
|
103
103
|
end
|
104
104
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -54,8 +54,11 @@ require 'thrift_spec_types'
|
|
54
54
|
require 'nonblocking_service'
|
55
55
|
|
56
56
|
module Fixtures
|
57
|
-
COMPACT_PROTOCOL_TEST_STRUCT = COMPACT_TEST.dup
|
57
|
+
COMPACT_PROTOCOL_TEST_STRUCT = Thrift::Test::COMPACT_TEST.dup
|
58
58
|
COMPACT_PROTOCOL_TEST_STRUCT.a_binary = [0,1,2,3,4,5,6,7,8].pack('c*')
|
59
59
|
COMPACT_PROTOCOL_TEST_STRUCT.set_byte_map = nil
|
60
60
|
COMPACT_PROTOCOL_TEST_STRUCT.map_byte_map = nil
|
61
61
|
end
|
62
|
+
|
63
|
+
$:.unshift File.join(File.dirname(__FILE__), *%w[gen-rb/flat])
|
64
|
+
|
@@ -0,0 +1,34 @@
|
|
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 File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared")
|
22
|
+
|
23
|
+
describe 'SSLServerSocket' do
|
24
|
+
|
25
|
+
describe Thrift::SSLServerSocket do
|
26
|
+
before(:each) do
|
27
|
+
@socket = Thrift::SSLServerSocket.new(1234)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should provide a reasonable to_s" do
|
31
|
+
expect(@socket.to_s).to eq("ssl(socket(:1234))")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,78 @@
|
|
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 File.expand_path("#{File.dirname(__FILE__)}/socket_spec_shared")
|
22
|
+
|
23
|
+
describe 'SSLSocket' do
|
24
|
+
|
25
|
+
describe Thrift::SSLSocket do
|
26
|
+
before(:each) do
|
27
|
+
@context = OpenSSL::SSL::SSLContext.new
|
28
|
+
@socket = Thrift::SSLSocket.new
|
29
|
+
@simple_socket_handle = double("Handle", :closed? => false)
|
30
|
+
allow(@simple_socket_handle).to receive(:close)
|
31
|
+
allow(@simple_socket_handle).to receive(:connect_nonblock)
|
32
|
+
allow(@simple_socket_handle).to receive(:setsockopt)
|
33
|
+
|
34
|
+
@handle = double(double("SSLHandle", :connect_nonblock => true, :post_connection_check => true), :closed? => false)
|
35
|
+
allow(@handle).to receive(:connect_nonblock)
|
36
|
+
allow(@handle).to receive(:close)
|
37
|
+
allow(@handle).to receive(:post_connection_check)
|
38
|
+
|
39
|
+
allow(::Socket).to receive(:new).and_return(@simple_socket_handle)
|
40
|
+
allow(OpenSSL::SSL::SSLSocket).to receive(:new).and_return(@handle)
|
41
|
+
end
|
42
|
+
|
43
|
+
it_should_behave_like "a socket"
|
44
|
+
|
45
|
+
it "should raise a TransportException when it cannot open a ssl socket" do
|
46
|
+
expect(::Socket).to receive(:getaddrinfo).with("localhost", 9090, nil, ::Socket::SOCK_STREAM).and_return([[]])
|
47
|
+
expect { @socket.open }.to raise_error(Thrift::TransportException) { |e| expect(e.type).to eq(Thrift::TransportException::NOT_OPEN) }
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should open a ::Socket with default args" do
|
51
|
+
expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(@simple_socket_handle, nil).and_return(@handle)
|
52
|
+
expect(@handle).to receive(:post_connection_check).with('localhost')
|
53
|
+
@socket.open
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should accept host/port options" do
|
57
|
+
handle = double("Handle", :connect_nonblock => true, :setsockopt => nil)
|
58
|
+
allow(::Socket).to receive(:new).and_return(handle)
|
59
|
+
expect(::Socket).to receive(:getaddrinfo).with("my.domain", 1234, nil, ::Socket::SOCK_STREAM).and_return([[]])
|
60
|
+
expect(::Socket).to receive(:sockaddr_in)
|
61
|
+
expect(OpenSSL::SSL::SSLSocket).to receive(:new).with(handle, nil).and_return(@handle)
|
62
|
+
expect(@handle).to receive(:post_connection_check).with('my.domain')
|
63
|
+
Thrift::SSLSocket.new('my.domain', 1234, 6000, nil).open
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should accept an optional timeout" do
|
67
|
+
expect(Thrift::SSLSocket.new('localhost', 8080, 5).timeout).to eq(5)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should accept an optional context" do
|
71
|
+
expect(Thrift::SSLSocket.new('localhost', 8080, 5, @context).ssl_context).to eq(@context)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should provide a reasonable to_s" do
|
75
|
+
expect(Thrift::SSLSocket.new('myhost', 8090).to_s).to eq("ssl(socket(myhost:8090))")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -39,9 +39,9 @@ describe 'StructNestedContainers' do
|
|
39
39
|
thrift_struct.value = [ [1, 2, 3], [2, 3, 4] ]
|
40
40
|
thrift_struct.validate
|
41
41
|
end
|
42
|
-
a.
|
42
|
+
expect(a).to eq(b)
|
43
43
|
b.value.push [3, 4, 5]
|
44
|
-
a.
|
44
|
+
expect(a).not_to eq(b)
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
@@ -52,9 +52,9 @@ describe 'StructNestedContainers' do
|
|
52
52
|
thrift_struct.value = [ [1, 2, 3], [2, 3, 4] ].to_set
|
53
53
|
thrift_struct.validate
|
54
54
|
end
|
55
|
-
a.
|
55
|
+
expect(a).to eq(b)
|
56
56
|
b.value.add [3, 4, 5]
|
57
|
-
a.
|
57
|
+
expect(a).not_to eq(b)
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -65,9 +65,9 @@ describe 'StructNestedContainers' do
|
|
65
65
|
thrift_struct.value = { [1, 2, 3] => 1, [2, 3, 4] => 2 }
|
66
66
|
thrift_struct.validate
|
67
67
|
end
|
68
|
-
a.
|
68
|
+
expect(a).to eq(b)
|
69
69
|
b.value[[3, 4, 5]] = 3
|
70
|
-
a.
|
70
|
+
expect(a).not_to eq(b)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
@@ -78,9 +78,9 @@ describe 'StructNestedContainers' do
|
|
78
78
|
thrift_struct.value = { 1 => [1, 2, 3], 2 => [2, 3, 4] }
|
79
79
|
thrift_struct.validate
|
80
80
|
end
|
81
|
-
a.
|
81
|
+
expect(a).to eq(b)
|
82
82
|
b.value[3] = [3, 4, 5]
|
83
|
-
a.
|
83
|
+
expect(a).not_to eq(b)
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
@@ -91,9 +91,9 @@ describe 'StructNestedContainers' do
|
|
91
91
|
thrift_struct.value = [ [1, 2, 3].to_set, [2, 3, 4].to_set ]
|
92
92
|
thrift_struct.validate
|
93
93
|
end
|
94
|
-
a.
|
94
|
+
expect(a).to eq(b)
|
95
95
|
b.value.push([3, 4, 5].to_set)
|
96
|
-
a.
|
96
|
+
expect(a).not_to eq(b)
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -104,9 +104,9 @@ describe 'StructNestedContainers' do
|
|
104
104
|
thrift_struct.value = [ [1, 2, 3].to_set, [2, 3, 4].to_set ].to_set
|
105
105
|
thrift_struct.validate
|
106
106
|
end
|
107
|
-
a.
|
107
|
+
expect(a).to eq(b)
|
108
108
|
b.value.add([3, 4, 5].to_set)
|
109
|
-
a.
|
109
|
+
expect(a).not_to eq(b)
|
110
110
|
end
|
111
111
|
end
|
112
112
|
|
@@ -117,9 +117,9 @@ describe 'StructNestedContainers' do
|
|
117
117
|
thrift_struct.value = { [1, 2, 3].to_set => 1, [2, 3, 4].to_set => 2 }
|
118
118
|
thrift_struct.validate
|
119
119
|
end
|
120
|
-
a.
|
120
|
+
expect(a).to eq(b)
|
121
121
|
b.value[[3, 4, 5].to_set] = 3
|
122
|
-
a.
|
122
|
+
expect(a).not_to eq(b)
|
123
123
|
end
|
124
124
|
end
|
125
125
|
|
@@ -130,9 +130,9 @@ describe 'StructNestedContainers' do
|
|
130
130
|
thrift_struct.value = { 1 => [1, 2, 3].to_set, 2 => [2, 3, 4].to_set }
|
131
131
|
thrift_struct.validate
|
132
132
|
end
|
133
|
-
a.
|
133
|
+
expect(a).to eq(b)
|
134
134
|
b.value[3] = [3, 4, 5].to_set
|
135
|
-
a.
|
135
|
+
expect(a).not_to eq(b)
|
136
136
|
end
|
137
137
|
end
|
138
138
|
|
@@ -143,9 +143,9 @@ describe 'StructNestedContainers' do
|
|
143
143
|
thrift_struct.value = [ {1 => 2, 3 => 4}, {2 => 3, 4 => 5} ]
|
144
144
|
thrift_struct.validate
|
145
145
|
end
|
146
|
-
a.
|
146
|
+
expect(a).to eq(b)
|
147
147
|
b.value.push({ 3 => 4, 5 => 6 })
|
148
|
-
a.
|
148
|
+
expect(a).not_to eq(b)
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
@@ -156,9 +156,9 @@ describe 'StructNestedContainers' do
|
|
156
156
|
thrift_struct.value = [ {1 => 2, 3 => 4}, {2 => 3, 4 => 5} ].to_set
|
157
157
|
thrift_struct.validate
|
158
158
|
end
|
159
|
-
a.
|
159
|
+
expect(a).to eq(b)
|
160
160
|
b.value.add({ 3 => 4, 5 => 6 })
|
161
|
-
a.
|
161
|
+
expect(a).not_to eq(b)
|
162
162
|
end
|
163
163
|
end
|
164
164
|
|
@@ -169,9 +169,9 @@ describe 'StructNestedContainers' do
|
|
169
169
|
thrift_struct.value = { { 1 => 2, 3 => 4} => 1, {2 => 3, 4 => 5} => 2 }
|
170
170
|
thrift_struct.validate
|
171
171
|
end
|
172
|
-
a.
|
172
|
+
expect(a).to eq(b)
|
173
173
|
b.value[{3 => 4, 5 => 6}] = 3
|
174
|
-
a.
|
174
|
+
expect(a).not_to eq(b)
|
175
175
|
end
|
176
176
|
end
|
177
177
|
|
@@ -182,9 +182,9 @@ describe 'StructNestedContainers' do
|
|
182
182
|
thrift_struct.value = { 1 => { 1 => 2, 3 => 4}, 2 => {2 => 3, 4 => 5} }
|
183
183
|
thrift_struct.validate
|
184
184
|
end
|
185
|
-
a.
|
185
|
+
expect(a).to eq(b)
|
186
186
|
b.value[3] = { 3 => 4, 5 => 6 }
|
187
|
-
a.
|
187
|
+
expect(a).not_to eq(b)
|
188
188
|
end
|
189
189
|
end
|
190
190
|
end
|
data/spec/struct_spec.rb
CHANGED
@@ -25,7 +25,7 @@ describe 'Struct' do
|
|
25
25
|
it "should iterate over all fields properly" do
|
26
26
|
fields = {}
|
27
27
|
SpecNamespace::Foo.new.each_field { |fid,field_info| fields[fid] = field_info }
|
28
|
-
fields.
|
28
|
+
expect(fields).to eq(SpecNamespace::Foo::FIELDS)
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should initialize all fields to defaults" do
|
@@ -39,19 +39,19 @@ describe 'Struct' do
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def validate_default_arguments(object)
|
42
|
-
object.simple.
|
43
|
-
object.words.
|
44
|
-
object.hello.
|
45
|
-
object.ints.
|
46
|
-
object.complex.
|
47
|
-
object.shorts.
|
42
|
+
expect(object.simple).to eq(53)
|
43
|
+
expect(object.words).to eq("words")
|
44
|
+
expect(object.hello).to eq(SpecNamespace::Hello.new(:greeting => 'hello, world!'))
|
45
|
+
expect(object.ints).to eq([1, 2, 2, 3])
|
46
|
+
expect(object.complex).to be_nil
|
47
|
+
expect(object.shorts).to eq(Set.new([5, 17, 239]))
|
48
48
|
end
|
49
49
|
|
50
50
|
it "should not share default values between instances" do
|
51
51
|
begin
|
52
52
|
struct = SpecNamespace::Foo.new
|
53
53
|
struct.ints << 17
|
54
|
-
SpecNamespace::Foo.new.ints.
|
54
|
+
expect(SpecNamespace::Foo.new.ints).to eq([1,2,2,3])
|
55
55
|
ensure
|
56
56
|
# ensure no leakage to other tests
|
57
57
|
SpecNamespace::Foo::FIELDS[4][:default] = [1,2,2,3]
|
@@ -60,48 +60,48 @@ describe 'Struct' do
|
|
60
60
|
|
61
61
|
it "should properly initialize boolean values" do
|
62
62
|
struct = SpecNamespace::BoolStruct.new(:yesno => false)
|
63
|
-
struct.yesno.
|
63
|
+
expect(struct.yesno).to be_falsey
|
64
64
|
end
|
65
65
|
|
66
66
|
it "should have proper == semantics" do
|
67
|
-
SpecNamespace::Foo.new.
|
68
|
-
SpecNamespace::Foo.new.
|
69
|
-
SpecNamespace::Foo.new(:simple => 52).
|
67
|
+
expect(SpecNamespace::Foo.new).not_to eq(SpecNamespace::Hello.new)
|
68
|
+
expect(SpecNamespace::Foo.new).to eq(SpecNamespace::Foo.new)
|
69
|
+
expect(SpecNamespace::Foo.new(:simple => 52)).not_to eq(SpecNamespace::Foo.new)
|
70
70
|
end
|
71
71
|
|
72
72
|
it "should print enum value names in inspect" do
|
73
|
-
SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect.
|
73
|
+
expect(SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect).to eq("<SpecNamespace::StructWithSomeEnum some_enum:ONE (0)>")
|
74
74
|
|
75
|
-
SpecNamespace::StructWithEnumMap.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect.
|
75
|
+
expect(SpecNamespace::StructWithEnumMap.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect).to eq("<SpecNamespace::StructWithEnumMap my_map:{ONE (0): [TWO (1)]}>")
|
76
76
|
end
|
77
77
|
|
78
78
|
it "should pretty print binary fields" do
|
79
|
-
SpecNamespace::Foo2.new(:my_binary => "\001\002\003").inspect.
|
79
|
+
expect(SpecNamespace::Foo2.new(:my_binary => "\001\002\003").inspect).to eq("<SpecNamespace::Foo2 my_binary:010203>")
|
80
80
|
end
|
81
81
|
|
82
82
|
it "should offer field? methods" do
|
83
|
-
SpecNamespace::Foo.new.opt_string
|
84
|
-
SpecNamespace::Foo.new(:simple => 52).simple
|
85
|
-
SpecNamespace::Foo.new(:my_bool => false).my_bool
|
86
|
-
SpecNamespace::Foo.new(:my_bool => true).my_bool
|
83
|
+
expect(SpecNamespace::Foo.new.opt_string?).to be_falsey
|
84
|
+
expect(SpecNamespace::Foo.new(:simple => 52).simple?).to be_truthy
|
85
|
+
expect(SpecNamespace::Foo.new(:my_bool => false).my_bool?).to be_truthy
|
86
|
+
expect(SpecNamespace::Foo.new(:my_bool => true).my_bool?).to be_truthy
|
87
87
|
end
|
88
88
|
|
89
89
|
it "should be comparable" do
|
90
90
|
s1 = SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::ONE)
|
91
91
|
s2 = SpecNamespace::StructWithSomeEnum.new(:some_enum => SpecNamespace::SomeEnum::TWO)
|
92
92
|
|
93
|
-
(s1 <=> s2).
|
94
|
-
(s2 <=> s1).
|
95
|
-
(s1 <=> s1).
|
96
|
-
(s1 <=> SpecNamespace::StructWithSomeEnum.new()).
|
93
|
+
expect(s1 <=> s2).to eq(-1)
|
94
|
+
expect(s2 <=> s1).to eq(1)
|
95
|
+
expect(s1 <=> s1).to eq(0)
|
96
|
+
expect(s1 <=> SpecNamespace::StructWithSomeEnum.new()).to eq(-1)
|
97
97
|
end
|
98
98
|
|
99
99
|
it "should read itself off the wire" do
|
100
100
|
struct = SpecNamespace::Foo.new
|
101
|
-
prot = Thrift::BaseProtocol.new(
|
102
|
-
prot.
|
103
|
-
prot.
|
104
|
-
prot.
|
101
|
+
prot = Thrift::BaseProtocol.new(double("transport"))
|
102
|
+
expect(prot).to receive(:read_struct_begin).twice
|
103
|
+
expect(prot).to receive(:read_struct_end).twice
|
104
|
+
expect(prot).to receive(:read_field_begin).and_return(
|
105
105
|
['complex', Thrift::Types::MAP, 5], # Foo
|
106
106
|
['words', Thrift::Types::STRING, 2], # Foo
|
107
107
|
['hello', Thrift::Types::STRUCT, 3], # Foo
|
@@ -112,49 +112,49 @@ describe 'Struct' do
|
|
112
112
|
['shorts', Thrift::Types::SET, 6], # Foo
|
113
113
|
[nil, Thrift::Types::STOP, 0] # Hello
|
114
114
|
)
|
115
|
-
prot.
|
116
|
-
prot.
|
115
|
+
expect(prot).to receive(:read_field_end).exactly(7).times
|
116
|
+
expect(prot).to receive(:read_map_begin).and_return(
|
117
117
|
[Thrift::Types::I32, Thrift::Types::MAP, 2], # complex
|
118
118
|
[Thrift::Types::STRING, Thrift::Types::DOUBLE, 2], # complex/1/value
|
119
119
|
[Thrift::Types::STRING, Thrift::Types::DOUBLE, 1] # complex/2/value
|
120
120
|
)
|
121
|
-
prot.
|
122
|
-
prot.
|
123
|
-
prot.
|
124
|
-
prot.
|
125
|
-
prot.
|
126
|
-
prot.
|
121
|
+
expect(prot).to receive(:read_map_end).exactly(3).times
|
122
|
+
expect(prot).to receive(:read_list_begin).and_return([Thrift::Types::I32, 4])
|
123
|
+
expect(prot).to receive(:read_list_end)
|
124
|
+
expect(prot).to receive(:read_set_begin).and_return([Thrift::Types::I16, 2])
|
125
|
+
expect(prot).to receive(:read_set_end)
|
126
|
+
expect(prot).to receive(:read_i32).and_return(
|
127
127
|
1, 14, # complex keys
|
128
128
|
42, # simple
|
129
129
|
4, 23, 4, 29 # ints
|
130
130
|
)
|
131
|
-
prot.
|
132
|
-
prot.
|
133
|
-
prot.
|
134
|
-
prot.
|
131
|
+
expect(prot).to receive(:read_string).and_return("pi", "e", "feigenbaum", "apple banana", "what's up?")
|
132
|
+
expect(prot).to receive(:read_double).and_return(Math::PI, Math::E, 4.669201609)
|
133
|
+
expect(prot).to receive(:read_i16).and_return(2, 3)
|
134
|
+
expect(prot).not_to receive(:skip)
|
135
135
|
struct.read(prot)
|
136
136
|
|
137
|
-
struct.simple.
|
138
|
-
struct.complex.
|
139
|
-
struct.hello.
|
140
|
-
struct.words.
|
141
|
-
struct.ints.
|
142
|
-
struct.shorts.
|
137
|
+
expect(struct.simple).to eq(42)
|
138
|
+
expect(struct.complex).to eq({1 => {"pi" => Math::PI, "e" => Math::E}, 14 => {"feigenbaum" => 4.669201609}})
|
139
|
+
expect(struct.hello).to eq(SpecNamespace::Hello.new(:greeting => "what's up?"))
|
140
|
+
expect(struct.words).to eq("apple banana")
|
141
|
+
expect(struct.ints).to eq([4, 23, 4, 29])
|
142
|
+
expect(struct.shorts).to eq(Set.new([3, 2]))
|
143
143
|
end
|
144
144
|
|
145
145
|
it "should serialize false boolean fields correctly" do
|
146
146
|
b = SpecNamespace::BoolStruct.new(:yesno => false)
|
147
147
|
prot = Thrift::BinaryProtocol.new(Thrift::MemoryBufferTransport.new)
|
148
|
-
prot.
|
148
|
+
expect(prot).to receive(:write_bool).with(false)
|
149
149
|
b.write(prot)
|
150
150
|
end
|
151
151
|
|
152
152
|
it "should skip unexpected fields in structs and use default values" do
|
153
153
|
struct = SpecNamespace::Foo.new
|
154
|
-
prot = Thrift::BaseProtocol.new(
|
155
|
-
prot.
|
156
|
-
prot.
|
157
|
-
prot.
|
154
|
+
prot = Thrift::BaseProtocol.new(double("transport"))
|
155
|
+
expect(prot).to receive(:read_struct_begin)
|
156
|
+
expect(prot).to receive(:read_struct_end)
|
157
|
+
expect(prot).to receive(:read_field_begin).and_return(
|
158
158
|
['simple', Thrift::Types::I32, 1],
|
159
159
|
['complex', Thrift::Types::STRUCT, 5],
|
160
160
|
['thinz', Thrift::Types::MAP, 7],
|
@@ -162,55 +162,55 @@ describe 'Struct' do
|
|
162
162
|
['words', Thrift::Types::STRING, 2],
|
163
163
|
[nil, Thrift::Types::STOP, 0]
|
164
164
|
)
|
165
|
-
prot.
|
166
|
-
prot.
|
167
|
-
prot.
|
168
|
-
prot.
|
169
|
-
prot.
|
165
|
+
expect(prot).to receive(:read_field_end).exactly(5).times
|
166
|
+
expect(prot).to receive(:read_i32).and_return(42)
|
167
|
+
expect(prot).to receive(:read_string).and_return("foobar")
|
168
|
+
expect(prot).to receive(:skip).with(Thrift::Types::STRUCT)
|
169
|
+
expect(prot).to receive(:skip).with(Thrift::Types::MAP)
|
170
170
|
# prot.should_receive(:read_map_begin).and_return([Thrift::Types::I32, Thrift::Types::I32, 0])
|
171
171
|
# prot.should_receive(:read_map_end)
|
172
|
-
prot.
|
172
|
+
expect(prot).to receive(:skip).with(Thrift::Types::I32)
|
173
173
|
struct.read(prot)
|
174
174
|
|
175
|
-
struct.simple.
|
176
|
-
struct.complex.
|
177
|
-
struct.words.
|
178
|
-
struct.hello.
|
179
|
-
struct.ints.
|
180
|
-
struct.shorts.
|
175
|
+
expect(struct.simple).to eq(42)
|
176
|
+
expect(struct.complex).to be_nil
|
177
|
+
expect(struct.words).to eq("foobar")
|
178
|
+
expect(struct.hello).to eq(SpecNamespace::Hello.new(:greeting => 'hello, world!'))
|
179
|
+
expect(struct.ints).to eq([1, 2, 2, 3])
|
180
|
+
expect(struct.shorts).to eq(Set.new([5, 17, 239]))
|
181
181
|
end
|
182
182
|
|
183
183
|
it "should write itself to the wire" do
|
184
|
-
prot = Thrift::BaseProtocol.new(
|
185
|
-
prot.
|
186
|
-
prot.
|
187
|
-
prot.
|
188
|
-
prot.
|
189
|
-
prot.
|
190
|
-
prot.
|
191
|
-
prot.
|
192
|
-
prot.
|
193
|
-
prot.
|
194
|
-
prot.
|
195
|
-
prot.
|
196
|
-
prot.
|
197
|
-
prot.
|
198
|
-
prot.
|
199
|
-
prot.
|
200
|
-
prot.
|
201
|
-
prot.
|
202
|
-
prot.
|
203
|
-
prot.
|
204
|
-
prot.
|
205
|
-
prot.
|
206
|
-
prot.
|
207
|
-
prot.
|
208
|
-
prot.
|
209
|
-
prot.
|
210
|
-
prot.
|
211
|
-
prot.
|
212
|
-
prot.
|
213
|
-
prot.
|
184
|
+
prot = Thrift::BaseProtocol.new(double("transport")) #mock("Protocol")
|
185
|
+
expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Foo")
|
186
|
+
expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Hello")
|
187
|
+
expect(prot).to receive(:write_struct_end).twice
|
188
|
+
expect(prot).to receive(:write_field_begin).with('ints', Thrift::Types::LIST, 4)
|
189
|
+
expect(prot).to receive(:write_i32).with(1)
|
190
|
+
expect(prot).to receive(:write_i32).with(2).twice
|
191
|
+
expect(prot).to receive(:write_i32).with(3)
|
192
|
+
expect(prot).to receive(:write_field_begin).with('complex', Thrift::Types::MAP, 5)
|
193
|
+
expect(prot).to receive(:write_i32).with(5)
|
194
|
+
expect(prot).to receive(:write_string).with('foo')
|
195
|
+
expect(prot).to receive(:write_double).with(1.23)
|
196
|
+
expect(prot).to receive(:write_field_begin).with('shorts', Thrift::Types::SET, 6)
|
197
|
+
expect(prot).to receive(:write_i16).with(5)
|
198
|
+
expect(prot).to receive(:write_i16).with(17)
|
199
|
+
expect(prot).to receive(:write_i16).with(239)
|
200
|
+
expect(prot).to receive(:write_field_stop).twice
|
201
|
+
expect(prot).to receive(:write_field_end).exactly(6).times
|
202
|
+
expect(prot).to receive(:write_field_begin).with('simple', Thrift::Types::I32, 1)
|
203
|
+
expect(prot).to receive(:write_i32).with(53)
|
204
|
+
expect(prot).to receive(:write_field_begin).with('hello', Thrift::Types::STRUCT, 3)
|
205
|
+
expect(prot).to receive(:write_field_begin).with('greeting', Thrift::Types::STRING, 1)
|
206
|
+
expect(prot).to receive(:write_string).with('hello, world!')
|
207
|
+
expect(prot).to receive(:write_map_begin).with(Thrift::Types::I32, Thrift::Types::MAP, 1)
|
208
|
+
expect(prot).to receive(:write_map_begin).with(Thrift::Types::STRING, Thrift::Types::DOUBLE, 1)
|
209
|
+
expect(prot).to receive(:write_map_end).twice
|
210
|
+
expect(prot).to receive(:write_list_begin).with(Thrift::Types::I32, 4)
|
211
|
+
expect(prot).to receive(:write_list_end)
|
212
|
+
expect(prot).to receive(:write_set_begin).with(Thrift::Types::I16, 3)
|
213
|
+
expect(prot).to receive(:write_set_end)
|
214
214
|
|
215
215
|
struct = SpecNamespace::Foo.new
|
216
216
|
struct.words = nil
|
@@ -221,50 +221,50 @@ describe 'Struct' do
|
|
221
221
|
it "should raise an exception if presented with an unknown container" do
|
222
222
|
# yeah this is silly, but I'm going for code coverage here
|
223
223
|
struct = SpecNamespace::Foo.new
|
224
|
-
|
224
|
+
expect { struct.send :write_container, nil, nil, {:type => "foo"} }.to raise_error(StandardError, "Not a container type: foo")
|
225
225
|
end
|
226
226
|
|
227
227
|
it "should support optional type-checking in Thrift::Struct.new" do
|
228
228
|
Thrift.type_checking = true
|
229
229
|
begin
|
230
|
-
|
230
|
+
expect { SpecNamespace::Hello.new(:greeting => 3) }.to raise_error(Thrift::TypeError, /Expected Types::STRING, received (Integer|Fixnum) for field greeting/)
|
231
231
|
ensure
|
232
232
|
Thrift.type_checking = false
|
233
233
|
end
|
234
|
-
|
234
|
+
expect { SpecNamespace::Hello.new(:greeting => 3) }.not_to raise_error
|
235
235
|
end
|
236
236
|
|
237
237
|
it "should support optional type-checking in field accessors" do
|
238
238
|
Thrift.type_checking = true
|
239
239
|
begin
|
240
240
|
hello = SpecNamespace::Hello.new
|
241
|
-
|
241
|
+
expect { hello.greeting = 3 }.to raise_error(Thrift::TypeError, /Expected Types::STRING, received (Integer|Fixnum) for field greeting/)
|
242
242
|
ensure
|
243
243
|
Thrift.type_checking = false
|
244
244
|
end
|
245
|
-
|
245
|
+
expect { hello.greeting = 3 }.not_to raise_error
|
246
246
|
end
|
247
247
|
|
248
248
|
it "should raise an exception when unknown types are given to Thrift::Struct.new" do
|
249
|
-
|
249
|
+
expect { SpecNamespace::Hello.new(:fish => 'salmon') }.to raise_error(Exception, "Unknown key given to SpecNamespace::Hello.new: fish")
|
250
250
|
end
|
251
251
|
|
252
252
|
it "should support `raise Xception, 'message'` for Exception structs" do
|
253
253
|
begin
|
254
254
|
raise SpecNamespace::Xception, "something happened"
|
255
255
|
rescue Thrift::Exception => e
|
256
|
-
e.message.
|
257
|
-
e.code.
|
256
|
+
expect(e.message).to eq("something happened")
|
257
|
+
expect(e.code).to eq(1)
|
258
258
|
# ensure it gets serialized properly, this is the really important part
|
259
|
-
prot = Thrift::BaseProtocol.new(
|
260
|
-
prot.
|
261
|
-
prot.
|
262
|
-
prot.
|
263
|
-
prot.
|
264
|
-
prot.
|
265
|
-
prot.
|
266
|
-
prot.
|
267
|
-
prot.
|
259
|
+
prot = Thrift::BaseProtocol.new(double("trans"))
|
260
|
+
expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Xception")
|
261
|
+
expect(prot).to receive(:write_struct_end)
|
262
|
+
expect(prot).to receive(:write_field_begin).with('message', Thrift::Types::STRING, 1)#, "something happened")
|
263
|
+
expect(prot).to receive(:write_string).with("something happened")
|
264
|
+
expect(prot).to receive(:write_field_begin).with('code', Thrift::Types::I32, 2)#, 1)
|
265
|
+
expect(prot).to receive(:write_i32).with(1)
|
266
|
+
expect(prot).to receive(:write_field_stop)
|
267
|
+
expect(prot).to receive(:write_field_end).twice
|
268
268
|
|
269
269
|
e.write(prot)
|
270
270
|
end
|
@@ -274,17 +274,17 @@ describe 'Struct' do
|
|
274
274
|
begin
|
275
275
|
raise SpecNamespace::Xception, :message => "something happened", :code => 5
|
276
276
|
rescue Thrift::Exception => e
|
277
|
-
e.message.
|
278
|
-
e.code.
|
279
|
-
prot = Thrift::BaseProtocol.new(
|
280
|
-
prot.
|
281
|
-
prot.
|
282
|
-
prot.
|
283
|
-
prot.
|
284
|
-
prot.
|
285
|
-
prot.
|
286
|
-
prot.
|
287
|
-
prot.
|
277
|
+
expect(e.message).to eq("something happened")
|
278
|
+
expect(e.code).to eq(5)
|
279
|
+
prot = Thrift::BaseProtocol.new(double("trans"))
|
280
|
+
expect(prot).to receive(:write_struct_begin).with("SpecNamespace::Xception")
|
281
|
+
expect(prot).to receive(:write_struct_end)
|
282
|
+
expect(prot).to receive(:write_field_begin).with('message', Thrift::Types::STRING, 1)
|
283
|
+
expect(prot).to receive(:write_string).with("something happened")
|
284
|
+
expect(prot).to receive(:write_field_begin).with('code', Thrift::Types::I32, 2)
|
285
|
+
expect(prot).to receive(:write_i32).with(5)
|
286
|
+
expect(prot).to receive(:write_field_stop)
|
287
|
+
expect(prot).to receive(:write_field_end).twice
|
288
288
|
|
289
289
|
e.write(prot)
|
290
290
|
end
|