upfluence-thrift 1.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.
Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +43 -0
  3. data/benchmark/Benchmark.thrift +24 -0
  4. data/benchmark/benchmark.rb +271 -0
  5. data/benchmark/client.rb +74 -0
  6. data/benchmark/gen-rb/benchmark_constants.rb +11 -0
  7. data/benchmark/gen-rb/benchmark_service.rb +80 -0
  8. data/benchmark/gen-rb/benchmark_types.rb +10 -0
  9. data/benchmark/server.rb +82 -0
  10. data/benchmark/thin_server.rb +44 -0
  11. data/ext/binary_protocol_accelerated.c +460 -0
  12. data/ext/binary_protocol_accelerated.h +20 -0
  13. data/ext/bytes.c +36 -0
  14. data/ext/bytes.h +31 -0
  15. data/ext/compact_protocol.c +637 -0
  16. data/ext/compact_protocol.h +20 -0
  17. data/ext/constants.h +99 -0
  18. data/ext/extconf.rb +34 -0
  19. data/ext/macros.h +41 -0
  20. data/ext/memory_buffer.c +134 -0
  21. data/ext/memory_buffer.h +20 -0
  22. data/ext/protocol.c +0 -0
  23. data/ext/protocol.h +0 -0
  24. data/ext/strlcpy.c +41 -0
  25. data/ext/strlcpy.h +34 -0
  26. data/ext/struct.c +707 -0
  27. data/ext/struct.h +25 -0
  28. data/ext/thrift_native.c +201 -0
  29. data/lib/thrift.rb +68 -0
  30. data/lib/thrift/bytes.rb +131 -0
  31. data/lib/thrift/client.rb +71 -0
  32. data/lib/thrift/core_ext.rb +23 -0
  33. data/lib/thrift/core_ext/fixnum.rb +29 -0
  34. data/lib/thrift/exceptions.rb +87 -0
  35. data/lib/thrift/multiplexed_processor.rb +76 -0
  36. data/lib/thrift/processor.rb +57 -0
  37. data/lib/thrift/protocol/base_protocol.rb +379 -0
  38. data/lib/thrift/protocol/binary_protocol.rb +237 -0
  39. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  40. data/lib/thrift/protocol/compact_protocol.rb +435 -0
  41. data/lib/thrift/protocol/json_protocol.rb +769 -0
  42. data/lib/thrift/protocol/multiplexed_protocol.rb +40 -0
  43. data/lib/thrift/protocol/protocol_decorator.rb +194 -0
  44. data/lib/thrift/serializer/deserializer.rb +33 -0
  45. data/lib/thrift/serializer/serializer.rb +34 -0
  46. data/lib/thrift/server/base_server.rb +31 -0
  47. data/lib/thrift/server/mongrel_http_server.rb +60 -0
  48. data/lib/thrift/server/nonblocking_server.rb +305 -0
  49. data/lib/thrift/server/rack_application.rb +61 -0
  50. data/lib/thrift/server/simple_server.rb +43 -0
  51. data/lib/thrift/server/thin_http_server.rb +51 -0
  52. data/lib/thrift/server/thread_pool_server.rb +75 -0
  53. data/lib/thrift/server/threaded_server.rb +47 -0
  54. data/lib/thrift/struct.rb +237 -0
  55. data/lib/thrift/struct_union.rb +192 -0
  56. data/lib/thrift/thrift_native.rb +24 -0
  57. data/lib/thrift/transport/base_server_transport.rb +37 -0
  58. data/lib/thrift/transport/base_transport.rb +109 -0
  59. data/lib/thrift/transport/buffered_transport.rb +114 -0
  60. data/lib/thrift/transport/framed_transport.rb +117 -0
  61. data/lib/thrift/transport/http_client_transport.rb +56 -0
  62. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  63. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  64. data/lib/thrift/transport/server_socket.rb +63 -0
  65. data/lib/thrift/transport/socket.rb +139 -0
  66. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  67. data/lib/thrift/transport/unix_socket.rb +40 -0
  68. data/lib/thrift/types.rb +101 -0
  69. data/lib/thrift/union.rb +179 -0
  70. data/spec/BaseService.thrift +27 -0
  71. data/spec/ExtendedService.thrift +25 -0
  72. data/spec/Referenced.thrift +44 -0
  73. data/spec/ThriftNamespacedSpec.thrift +53 -0
  74. data/spec/ThriftSpec.thrift +183 -0
  75. data/spec/base_protocol_spec.rb +217 -0
  76. data/spec/base_transport_spec.rb +350 -0
  77. data/spec/binary_protocol_accelerated_spec.rb +42 -0
  78. data/spec/binary_protocol_spec.rb +66 -0
  79. data/spec/binary_protocol_spec_shared.rb +455 -0
  80. data/spec/bytes_spec.rb +160 -0
  81. data/spec/client_spec.rb +99 -0
  82. data/spec/compact_protocol_spec.rb +143 -0
  83. data/spec/exception_spec.rb +141 -0
  84. data/spec/flat_spec.rb +62 -0
  85. data/spec/gen-rb/base/base_service.rb +80 -0
  86. data/spec/gen-rb/base/base_service_constants.rb +11 -0
  87. data/spec/gen-rb/base/base_service_types.rb +26 -0
  88. data/spec/gen-rb/extended/extended_service.rb +78 -0
  89. data/spec/gen-rb/extended/extended_service_constants.rb +11 -0
  90. data/spec/gen-rb/extended/extended_service_types.rb +12 -0
  91. data/spec/gen-rb/flat/namespaced_nonblocking_service.rb +272 -0
  92. data/spec/gen-rb/flat/referenced_constants.rb +11 -0
  93. data/spec/gen-rb/flat/referenced_types.rb +17 -0
  94. data/spec/gen-rb/flat/thrift_namespaced_spec_constants.rb +11 -0
  95. data/spec/gen-rb/flat/thrift_namespaced_spec_types.rb +28 -0
  96. data/spec/gen-rb/namespaced_spec_namespace/namespaced_nonblocking_service.rb +272 -0
  97. data/spec/gen-rb/namespaced_spec_namespace/thrift_namespaced_spec_constants.rb +11 -0
  98. data/spec/gen-rb/namespaced_spec_namespace/thrift_namespaced_spec_types.rb +28 -0
  99. data/spec/gen-rb/nonblocking_service.rb +272 -0
  100. data/spec/gen-rb/other_namespace/referenced_constants.rb +11 -0
  101. data/spec/gen-rb/other_namespace/referenced_types.rb +17 -0
  102. data/spec/gen-rb/thrift_spec_constants.rb +11 -0
  103. data/spec/gen-rb/thrift_spec_types.rb +538 -0
  104. data/spec/http_client_spec.rb +120 -0
  105. data/spec/json_protocol_spec.rb +513 -0
  106. data/spec/namespaced_spec.rb +67 -0
  107. data/spec/nonblocking_server_spec.rb +263 -0
  108. data/spec/processor_spec.rb +80 -0
  109. data/spec/serializer_spec.rb +67 -0
  110. data/spec/server_socket_spec.rb +79 -0
  111. data/spec/server_spec.rb +147 -0
  112. data/spec/socket_spec.rb +61 -0
  113. data/spec/socket_spec_shared.rb +104 -0
  114. data/spec/spec_helper.rb +64 -0
  115. data/spec/struct_nested_containers_spec.rb +191 -0
  116. data/spec/struct_spec.rb +293 -0
  117. data/spec/thin_http_server_spec.rb +141 -0
  118. data/spec/types_spec.rb +115 -0
  119. data/spec/union_spec.rb +203 -0
  120. data/spec/unix_socket_spec.rb +107 -0
  121. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +274 -0
  122. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +761 -0
  123. data/test/debug_proto/gen-rb/empty_service.rb +24 -0
  124. data/test/debug_proto/gen-rb/inherited.rb +79 -0
  125. data/test/debug_proto/gen-rb/reverse_order_service.rb +82 -0
  126. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +81 -0
  127. data/test/debug_proto/gen-rb/srv.rb +330 -0
  128. metadata +388 -0
@@ -0,0 +1,141 @@
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
+ require 'thrift/server/thin_http_server'
23
+
24
+ describe Thrift::ThinHTTPServer do
25
+
26
+ let(:processor) { mock('processor') }
27
+
28
+ describe "#initialize" do
29
+
30
+ context "when using the defaults" do
31
+
32
+ it "binds to port 80, with host 0.0.0.0, a path of '/'" do
33
+ Thin::Server.should_receive(:new).with('0.0.0.0', 80, an_instance_of(Rack::Builder))
34
+ Thrift::ThinHTTPServer.new(processor)
35
+ end
36
+
37
+ it 'creates a ThinHTTPServer::RackApplicationContext' do
38
+ Thrift::ThinHTTPServer::RackApplication.should_receive(:for).with("/", processor, an_instance_of(Thrift::BinaryProtocolFactory)).and_return(anything)
39
+ Thrift::ThinHTTPServer.new(processor)
40
+ end
41
+
42
+ it "uses the BinaryProtocolFactory" do
43
+ Thrift::BinaryProtocolFactory.should_receive(:new)
44
+ Thrift::ThinHTTPServer.new(processor)
45
+ end
46
+
47
+ end
48
+
49
+ context "when using the options" do
50
+
51
+ it 'accepts :ip, :port, :path' do
52
+ ip = "192.168.0.1"
53
+ port = 3000
54
+ path = "/thin"
55
+ Thin::Server.should_receive(:new).with(ip, port, an_instance_of(Rack::Builder))
56
+ Thrift::ThinHTTPServer.new(processor,
57
+ :ip => ip,
58
+ :port => port,
59
+ :path => path)
60
+ end
61
+
62
+ it 'creates a ThinHTTPServer::RackApplicationContext with a different protocol factory' do
63
+ Thrift::ThinHTTPServer::RackApplication.should_receive(:for).with("/", processor, an_instance_of(Thrift::JsonProtocolFactory)).and_return(anything)
64
+ Thrift::ThinHTTPServer.new(processor,
65
+ :protocol_factory => Thrift::JsonProtocolFactory.new)
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ describe "#serve" do
73
+
74
+ it 'starts the Thin server' do
75
+ underlying_thin_server = mock('thin server', :start => true)
76
+ Thin::Server.stub(:new).and_return(underlying_thin_server)
77
+
78
+ thin_thrift_server = Thrift::ThinHTTPServer.new(processor)
79
+
80
+ underlying_thin_server.should_receive(:start)
81
+ thin_thrift_server.serve
82
+ end
83
+ end
84
+
85
+ end
86
+
87
+ describe Thrift::ThinHTTPServer::RackApplication do
88
+ include Rack::Test::Methods
89
+
90
+ let(:processor) { mock('processor') }
91
+ let(:protocol_factory) { mock('protocol factory') }
92
+
93
+ def app
94
+ Thrift::ThinHTTPServer::RackApplication.for("/", processor, protocol_factory)
95
+ end
96
+
97
+ context "404 response" do
98
+
99
+ it 'receives a non-POST' do
100
+ header('Content-Type', "application/x-thrift")
101
+ get "/"
102
+ last_response.status.should be 404
103
+ end
104
+
105
+ it 'receives a header other than application/x-thrift' do
106
+ header('Content-Type', "application/json")
107
+ post "/"
108
+ last_response.status.should be 404
109
+ end
110
+
111
+ end
112
+
113
+ context "200 response" do
114
+
115
+ before do
116
+ protocol_factory.stub(:get_protocol)
117
+ processor.stub(:process)
118
+ end
119
+
120
+ it 'creates an IOStreamTransport' do
121
+ header('Content-Type', "application/x-thrift")
122
+ Thrift::IOStreamTransport.should_receive(:new).with(an_instance_of(Rack::Lint::InputWrapper), an_instance_of(Rack::Response))
123
+ post "/"
124
+ end
125
+
126
+ it 'fetches the right protocol based on the Transport' do
127
+ header('Content-Type', "application/x-thrift")
128
+ protocol_factory.should_receive(:get_protocol).with(an_instance_of(Thrift::IOStreamTransport))
129
+ post "/"
130
+ end
131
+
132
+ it 'status code 200' do
133
+ header('Content-Type', "application/x-thrift")
134
+ post "/"
135
+ last_response.ok?.should be_true
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+
@@ -0,0 +1,115 @@
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
+
22
+ describe Thrift::Types do
23
+
24
+ before(:each) do
25
+ Thrift.type_checking = true
26
+ end
27
+
28
+ after(:each) do
29
+ Thrift.type_checking = false
30
+ end
31
+
32
+ context 'type checking' do
33
+ it "should return the proper name for each type" do
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"
37
+ Thrift.type_name(42).should be_nil
38
+ end
39
+
40
+ it "should check types properly" do
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)
56
+ hello = SpecNamespace::Hello.new
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)
66
+ end
67
+
68
+ it "should error out if nil is passed and skip_types is false" do
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)
80
+ end
81
+
82
+ it "should check element types on containers" do
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)
97
+ end
98
+
99
+ it "should give the Thrift::TypeError a readable message" do
100
+ msg = "Expected Types::STRING, received Fixnum for field foo"
101
+ lambda { Thrift.check_type(3, {:type => Thrift::Types::STRING}, :foo) }.should raise_error(Thrift::TypeError, msg)
102
+ msg = "Expected Types::STRING, received Fixnum for field foo.element"
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)
105
+ msg = "Expected Types::I32, received NilClass for field foo.element.key"
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)
111
+ msg = "Expected Types::I32, received NilClass for field foo.element.value"
112
+ lambda { Thrift.check_type([{1 => nil}], field, :foo) }.should raise_error(Thrift::TypeError, msg)
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,203 @@
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
+
22
+ describe 'Union' do
23
+
24
+ describe Thrift::Union do
25
+ it "should return nil value in unset union" do
26
+ union = SpecNamespace::My_union.new
27
+ union.get_set_field.should == nil
28
+ union.get_value.should == nil
29
+ end
30
+
31
+ it "should set a field and be accessible through get_value and the named field accessor" do
32
+ union = SpecNamespace::My_union.new
33
+ union.integer32 = 25
34
+ union.get_set_field.should == :integer32
35
+ union.get_value.should == 25
36
+ union.integer32.should == 25
37
+ end
38
+
39
+ it "should work correctly when instantiated with static field constructors" do
40
+ union = SpecNamespace::My_union.integer32(5)
41
+ union.get_set_field.should == :integer32
42
+ union.integer32.should == 5
43
+ end
44
+
45
+ it "should raise for wrong set field" do
46
+ union = SpecNamespace::My_union.new
47
+ union.integer32 = 25
48
+ lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
49
+ end
50
+
51
+ it "should not be equal to nil" do
52
+ union = SpecNamespace::My_union.new
53
+ union.should_not == nil
54
+ end
55
+
56
+ it "should not equate two different unions, i32 vs. string" do
57
+ union = SpecNamespace::My_union.new(:integer32, 25)
58
+ other_union = SpecNamespace::My_union.new(:some_characters, "blah!")
59
+ union.should_not == other_union
60
+ end
61
+
62
+ it "should properly reset setfield and setvalue" do
63
+ union = SpecNamespace::My_union.new(:integer32, 25)
64
+ union.get_set_field.should == :integer32
65
+ union.some_characters = "blah!"
66
+ union.get_set_field.should == :some_characters
67
+ union.get_value.should == "blah!"
68
+ lambda { union.integer32 }.should raise_error(RuntimeError, "integer32 is not union's set field.")
69
+ end
70
+
71
+ it "should not equate two different unions with different values" do
72
+ union = SpecNamespace::My_union.new(:integer32, 25)
73
+ other_union = SpecNamespace::My_union.new(:integer32, 400)
74
+ union.should_not == other_union
75
+ end
76
+
77
+ it "should not equate two different unions with different fields" do
78
+ union = SpecNamespace::My_union.new(:integer32, 25)
79
+ other_union = SpecNamespace::My_union.new(:other_i32, 25)
80
+ union.should_not == other_union
81
+ end
82
+
83
+ it "should inspect properly" do
84
+ union = SpecNamespace::My_union.new(:integer32, 25)
85
+ union.inspect.should == "<SpecNamespace::My_union integer32: 25>"
86
+ end
87
+
88
+ it "should not allow setting with instance_variable_set" do
89
+ union = SpecNamespace::My_union.new(:integer32, 27)
90
+ union.instance_variable_set(:@some_characters, "hallo!")
91
+ union.get_set_field.should == :integer32
92
+ union.get_value.should == 27
93
+ lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
94
+ end
95
+
96
+ it "should serialize to binary correctly" do
97
+ trans = Thrift::MemoryBufferTransport.new
98
+ proto = Thrift::BinaryProtocol.new(trans)
99
+
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)
113
+ union.write(proto)
114
+
115
+ other_union = SpecNamespace::My_union.new(:integer32, 25)
116
+ other_union.read(proto)
117
+ other_union.should == union
118
+ end
119
+
120
+ it "should raise when validating unset union" do
121
+ union = SpecNamespace::My_union.new
122
+ lambda { union.validate }.should raise_error(StandardError, "Union fields are not set.")
123
+
124
+ other_union = SpecNamespace::My_union.new(:integer32, 1)
125
+ lambda { other_union.validate }.should_not raise_error(StandardError, "Union fields are not set.")
126
+ end
127
+
128
+ it "should validate an enum field properly" do
129
+ union = SpecNamespace::TestUnion.new(:enum_field, 3)
130
+ union.get_set_field.should == :enum_field
131
+ lambda { union.validate }.should raise_error(Thrift::ProtocolException, "Invalid value of field enum_field!")
132
+
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!")
135
+ end
136
+
137
+ it "should properly serialize and match structs with a union" do
138
+ union = SpecNamespace::My_union.new(:integer32, 26)
139
+ swu = SpecNamespace::Struct_with_union.new(:fun_union => union)
140
+
141
+ trans = Thrift::MemoryBufferTransport.new
142
+ proto = Thrift::CompactProtocol.new(trans)
143
+
144
+ swu.write(proto)
145
+
146
+ other_union = SpecNamespace::My_union.new(:some_characters, "hello there")
147
+ swu2 = SpecNamespace::Struct_with_union.new(:fun_union => other_union)
148
+
149
+ swu2.should_not == swu
150
+
151
+ swu2.read(proto)
152
+ swu2.should == swu
153
+ end
154
+
155
+ it "should support old style constructor" do
156
+ union = SpecNamespace::My_union.new(:integer32 => 26)
157
+ union.get_set_field.should == :integer32
158
+ union.get_value.should == 26
159
+ end
160
+
161
+ it "should not throw an error when inspected and unset" do
162
+ lambda{SpecNamespace::TestUnion.new().inspect}.should_not raise_error
163
+ end
164
+
165
+ it "should print enum value name when inspected" do
166
+ SpecNamespace::My_union.new(:some_enum => SpecNamespace::SomeEnum::ONE).inspect.should == "<SpecNamespace::My_union some_enum: ONE (0)>"
167
+
168
+ SpecNamespace::My_union.new(:my_map => {SpecNamespace::SomeEnum::ONE => [SpecNamespace::SomeEnum::TWO]}).inspect.should == "<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>"
169
+ end
170
+
171
+ it "should offer field? methods" do
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
176
+ end
177
+
178
+ it "should pretty print binary fields" do
179
+ SpecNamespace::TestUnion.new(:binary_field => "\001\002\003").inspect.should == "<SpecNamespace::TestUnion binary_field: 010203>"
180
+ end
181
+
182
+ it "should be comparable" do
183
+ relationships = [
184
+ [0, -1, -1, -1],
185
+ [1, 0, -1, -1],
186
+ [1, 1, 0, -1],
187
+ [1, 1, 1, 0]]
188
+
189
+ objs = [
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()]
194
+
195
+ for y in 0..3
196
+ for x in 0..3
197
+ # puts "#{objs[y].inspect} <=> #{objs[x].inspect} should == #{relationships[y][x]}"
198
+ (objs[y] <=> objs[x]).should == relationships[y][x]
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end