grpc 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +10 -0
  5. data/.rubocop_todo.yml +52 -0
  6. data/Gemfile +4 -0
  7. data/README.md +82 -0
  8. data/Rakefile +54 -0
  9. data/bin/apis/google/protobuf/empty.rb +44 -0
  10. data/bin/apis/pubsub_demo.rb +267 -0
  11. data/bin/apis/tech/pubsub/proto/pubsub.rb +174 -0
  12. data/bin/apis/tech/pubsub/proto/pubsub_services.rb +103 -0
  13. data/bin/interop/README.md +8 -0
  14. data/bin/interop/interop_client.rb +334 -0
  15. data/bin/interop/interop_server.rb +192 -0
  16. data/bin/interop/test/cpp/interop/empty.rb +44 -0
  17. data/bin/interop/test/cpp/interop/messages.rb +89 -0
  18. data/bin/interop/test/cpp/interop/test.rb +43 -0
  19. data/bin/interop/test/cpp/interop/test_services.rb +60 -0
  20. data/bin/math.proto +80 -0
  21. data/bin/math.rb +61 -0
  22. data/bin/math_client.rb +147 -0
  23. data/bin/math_server.rb +190 -0
  24. data/bin/math_services.rb +56 -0
  25. data/bin/noproto_client.rb +108 -0
  26. data/bin/noproto_server.rb +112 -0
  27. data/ext/grpc/extconf.rb +76 -0
  28. data/ext/grpc/rb_byte_buffer.c +241 -0
  29. data/ext/grpc/rb_byte_buffer.h +54 -0
  30. data/ext/grpc/rb_call.c +569 -0
  31. data/ext/grpc/rb_call.h +59 -0
  32. data/ext/grpc/rb_channel.c +264 -0
  33. data/ext/grpc/rb_channel.h +49 -0
  34. data/ext/grpc/rb_channel_args.c +154 -0
  35. data/ext/grpc/rb_channel_args.h +52 -0
  36. data/ext/grpc/rb_completion_queue.c +185 -0
  37. data/ext/grpc/rb_completion_queue.h +50 -0
  38. data/ext/grpc/rb_credentials.c +281 -0
  39. data/ext/grpc/rb_credentials.h +50 -0
  40. data/ext/grpc/rb_event.c +361 -0
  41. data/ext/grpc/rb_event.h +53 -0
  42. data/ext/grpc/rb_grpc.c +274 -0
  43. data/ext/grpc/rb_grpc.h +74 -0
  44. data/ext/grpc/rb_metadata.c +215 -0
  45. data/ext/grpc/rb_metadata.h +53 -0
  46. data/ext/grpc/rb_server.c +278 -0
  47. data/ext/grpc/rb_server.h +50 -0
  48. data/ext/grpc/rb_server_credentials.c +210 -0
  49. data/ext/grpc/rb_server_credentials.h +50 -0
  50. data/grpc.gemspec +41 -0
  51. data/lib/grpc.rb +39 -0
  52. data/lib/grpc/core/event.rb +44 -0
  53. data/lib/grpc/core/time_consts.rb +71 -0
  54. data/lib/grpc/errors.rb +61 -0
  55. data/lib/grpc/generic/active_call.rb +536 -0
  56. data/lib/grpc/generic/bidi_call.rb +221 -0
  57. data/lib/grpc/generic/client_stub.rb +413 -0
  58. data/lib/grpc/generic/rpc_desc.rb +150 -0
  59. data/lib/grpc/generic/rpc_server.rb +404 -0
  60. data/lib/grpc/generic/service.rb +235 -0
  61. data/lib/grpc/logconfig.rb +40 -0
  62. data/lib/grpc/version.rb +33 -0
  63. data/spec/alloc_spec.rb +44 -0
  64. data/spec/byte_buffer_spec.rb +67 -0
  65. data/spec/call_spec.rb +163 -0
  66. data/spec/channel_spec.rb +181 -0
  67. data/spec/client_server_spec.rb +372 -0
  68. data/spec/completion_queue_spec.rb +74 -0
  69. data/spec/credentials_spec.rb +71 -0
  70. data/spec/event_spec.rb +53 -0
  71. data/spec/generic/active_call_spec.rb +373 -0
  72. data/spec/generic/client_stub_spec.rb +519 -0
  73. data/spec/generic/rpc_desc_spec.rb +357 -0
  74. data/spec/generic/rpc_server_pool_spec.rb +139 -0
  75. data/spec/generic/rpc_server_spec.rb +404 -0
  76. data/spec/generic/service_spec.rb +342 -0
  77. data/spec/metadata_spec.rb +64 -0
  78. data/spec/server_credentials_spec.rb +69 -0
  79. data/spec/server_spec.rb +212 -0
  80. data/spec/spec_helper.rb +51 -0
  81. data/spec/testdata/README +1 -0
  82. data/spec/testdata/ca.pem +15 -0
  83. data/spec/testdata/server1.key +16 -0
  84. data/spec/testdata/server1.pem +16 -0
  85. data/spec/time_consts_spec.rb +89 -0
  86. metadata +353 -0
@@ -0,0 +1,235 @@
1
+ # Copyright 2015, Google Inc.
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are
6
+ # met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright
9
+ # notice, this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above
11
+ # copyright notice, this list of conditions and the following disclaimer
12
+ # in the documentation and/or other materials provided with the
13
+ # distribution.
14
+ # * Neither the name of Google Inc. nor the names of its
15
+ # contributors may be used to endorse or promote products derived from
16
+ # this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ require 'grpc/generic/client_stub'
31
+ require 'grpc/generic/rpc_desc'
32
+
33
+ # Extend String to add a method underscore
34
+ class String
35
+ # creates a new string that is the underscore separate version of this one.
36
+ #
37
+ # E.g,
38
+ # PrintHTML -> print_html
39
+ # AMethod -> a_method
40
+ # AnRpc -> an_rpc
41
+ def underscore
42
+ word = dup
43
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
44
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
45
+ word.tr!('-', '_')
46
+ word.downcase!
47
+ word
48
+ end
49
+ end
50
+
51
+ # GRPC contains the General RPC module.
52
+ module GRPC
53
+ # Provides behaviour used to implement schema-derived service classes.
54
+ #
55
+ # Is intended to be used to support both client and server
56
+ # IDL-schema-derived servers.
57
+ module GenericService
58
+ # Used to indicate that a name has already been specified
59
+ class DuplicateRpcName < StandardError
60
+ def initialize(name)
61
+ super("rpc (#{name}) is already defined")
62
+ end
63
+ end
64
+
65
+ # Provides a simple DSL to describe RPC services.
66
+ #
67
+ # E.g, a Maths service that uses the serializable messages DivArgs,
68
+ # DivReply and Num might define its endpoint uses the following way:
69
+ #
70
+ # rpc :div DivArgs, DivReply # single request, single response
71
+ # rpc :sum stream(Num), Num # streamed input, single response
72
+ # rpc :fib FibArgs, stream(Num) # single request, streamed response
73
+ # rpc :div_many stream(DivArgs), stream(DivReply)
74
+ # # streamed req and resp
75
+ #
76
+ # Each 'rpc' adds an RpcDesc to classes including this module, and
77
+ # #assert_rpc_descs_have_methods is used to ensure the including class
78
+ # provides methods with signatures that support all the descriptors.
79
+ module Dsl
80
+ # This configures the method names that the serializable message
81
+ # implementation uses to marshal and unmarshal messages.
82
+ #
83
+ # - unmarshal_class method must be a class method on the serializable
84
+ # message type that takes a string (byte stream) and produces and object
85
+ #
86
+ # - marshal_class_method is called on a serializable message instance
87
+ # and produces a serialized string.
88
+ #
89
+ # The Dsl verifies that the types in the descriptor have both the
90
+ # unmarshal and marshal methods.
91
+ attr_writer(:marshal_class_method, :unmarshal_class_method)
92
+
93
+ # This allows configuration of the service name.
94
+ attr_accessor(:service_name)
95
+
96
+ # Adds an RPC spec.
97
+ #
98
+ # Takes the RPC name and the classes representing the types to be
99
+ # serialized, and adds them to the including classes rpc_desc hash.
100
+ #
101
+ # input and output should both have the methods #marshal and #unmarshal
102
+ # that are responsible for writing and reading an object instance from a
103
+ # byte buffer respectively.
104
+ #
105
+ # @param name [String] the name of the rpc
106
+ # @param input [Object] the input parameter's class
107
+ # @param output [Object] the output parameter's class
108
+ def rpc(name, input, output)
109
+ fail(DuplicateRpcName, name) if rpc_descs.key? name
110
+ assert_can_marshal(input)
111
+ assert_can_marshal(output)
112
+ rpc_descs[name] = RpcDesc.new(name, input, output,
113
+ marshal_class_method,
114
+ unmarshal_class_method)
115
+ end
116
+
117
+ def inherited(subclass)
118
+ # Each subclass should have a distinct class variable with its own
119
+ # rpc_descs
120
+ subclass.rpc_descs.merge!(rpc_descs)
121
+ subclass.service_name = service_name
122
+ end
123
+
124
+ # the name of the instance method used to marshal events to a byte
125
+ # stream.
126
+ def marshal_class_method
127
+ @marshal_class_method ||= :marshal
128
+ end
129
+
130
+ # the name of the class method used to unmarshal from a byte stream.
131
+ def unmarshal_class_method
132
+ @unmarshal_class_method ||= :unmarshal
133
+ end
134
+
135
+ def assert_can_marshal(cls)
136
+ cls = cls.type if cls.is_a? RpcDesc::Stream
137
+ mth = unmarshal_class_method
138
+ unless cls.methods.include? mth
139
+ fail(ArgumentError, "#{cls} needs #{cls}.#{mth}")
140
+ end
141
+ mth = marshal_class_method
142
+ return if cls.methods.include? mth
143
+ fail(ArgumentError, "#{cls} needs #{cls}.#{mth}")
144
+ end
145
+
146
+ # @param cls [Class] the class of a serializable type
147
+ # @return cls wrapped in a RpcDesc::Stream
148
+ def stream(cls)
149
+ assert_can_marshal(cls)
150
+ RpcDesc::Stream.new(cls)
151
+ end
152
+
153
+ # the RpcDescs defined for this GenericService, keyed by name.
154
+ def rpc_descs
155
+ @rpc_descs ||= {}
156
+ end
157
+
158
+ # Creates a rpc client class with methods for accessing the methods
159
+ # currently in rpc_descs.
160
+ def rpc_stub_class
161
+ descs = rpc_descs
162
+ route_prefix = service_name
163
+ Class.new(ClientStub) do
164
+ # @param host [String] the host the stub connects to
165
+ # @param kw [KeywordArgs] the channel arguments, plus any optional
166
+ # args for configuring the client's channel
167
+ def initialize(host, **kw)
168
+ super(host, Core::CompletionQueue.new, **kw)
169
+ end
170
+
171
+ # Used define_method to add a method for each rpc_desc. Each method
172
+ # calls the base class method for the given descriptor.
173
+ descs.each_pair do |name, desc|
174
+ mth_name = name.to_s.underscore.to_sym
175
+ marshal = desc.marshal_proc
176
+ unmarshal = desc.unmarshal_proc(:output)
177
+ route = "/#{route_prefix}/#{name}"
178
+ if desc.request_response?
179
+ define_method(mth_name) do |req, deadline = nil|
180
+ logger.debug("calling #{@host}:#{route}")
181
+ request_response(route, req, marshal, unmarshal, deadline)
182
+ end
183
+ elsif desc.client_streamer?
184
+ define_method(mth_name) do |reqs, deadline = nil|
185
+ logger.debug("calling #{@host}:#{route}")
186
+ client_streamer(route, reqs, marshal, unmarshal, deadline)
187
+ end
188
+ elsif desc.server_streamer?
189
+ define_method(mth_name) do |req, deadline = nil, &blk|
190
+ logger.debug("calling #{@host}:#{route}")
191
+ server_streamer(route, req, marshal, unmarshal, deadline,
192
+ &blk)
193
+ end
194
+ else # is a bidi_stream
195
+ define_method(mth_name) do |reqs, deadline = nil, &blk|
196
+ logger.debug("calling #{@host}:#{route}")
197
+ bidi_streamer(route, reqs, marshal, unmarshal, deadline, &blk)
198
+ end
199
+ end
200
+ end
201
+ end
202
+ end
203
+
204
+ # Asserts that the appropriate methods are defined for each added rpc
205
+ # spec. Is intended to aid verifying that server classes are correctly
206
+ # implemented.
207
+ def assert_rpc_descs_have_methods
208
+ rpc_descs.each_pair do |m, spec|
209
+ mth_name = m.to_s.underscore.to_sym
210
+ unless instance_methods.include?(mth_name)
211
+ fail "#{self} does not provide instance method '#{mth_name}'"
212
+ end
213
+ spec.assert_arity_matches(instance_method(mth_name))
214
+ end
215
+ end
216
+ end
217
+
218
+ def self.included(o)
219
+ o.extend(Dsl)
220
+ # Update to the use the service name including module. Proivde a default
221
+ # that can be nil e,g. when modules are declared dynamically.
222
+ return unless o.service_name.nil?
223
+ if o.name.nil?
224
+ o.service_name = 'GenericService'
225
+ else
226
+ modules = o.name.split('::')
227
+ if modules.length > 2
228
+ o.service_name = modules[modules.length - 2]
229
+ else
230
+ o.service_name = modules.first
231
+ end
232
+ end
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,40 @@
1
+ # Copyright 2015, Google Inc.
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are
6
+ # met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright
9
+ # notice, this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above
11
+ # copyright notice, this list of conditions and the following disclaimer
12
+ # in the documentation and/or other materials provided with the
13
+ # distribution.
14
+ # * Neither the name of Google Inc. nor the names of its
15
+ # contributors may be used to endorse or promote products derived from
16
+ # this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ require 'logging'
31
+
32
+ include Logging.globally # logger is accessible everywhere
33
+
34
+ Logging.logger.root.appenders = Logging.appenders.stdout
35
+ Logging.logger.root.level = :info
36
+
37
+ # TODO: provide command-line configuration for logging
38
+ Logging.logger['GRPC'].level = :debug
39
+ Logging.logger['GRPC::ActiveCall'].level = :info
40
+ Logging.logger['GRPC::BidiCall'].level = :info
@@ -0,0 +1,33 @@
1
+ # Copyright 2015, Google Inc.
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are
6
+ # met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright
9
+ # notice, this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above
11
+ # copyright notice, this list of conditions and the following disclaimer
12
+ # in the documentation and/or other materials provided with the
13
+ # distribution.
14
+ # * Neither the name of Google Inc. nor the names of its
15
+ # contributors may be used to endorse or promote products derived from
16
+ # this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ # GRPC contains the General RPC module.
31
+ module GRPC
32
+ VERSION = '0.5.0'
33
+ end
@@ -0,0 +1,44 @@
1
+ # Copyright 2015, Google Inc.
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are
6
+ # met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright
9
+ # notice, this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above
11
+ # copyright notice, this list of conditions and the following disclaimer
12
+ # in the documentation and/or other materials provided with the
13
+ # distribution.
14
+ # * Neither the name of Google Inc. nor the names of its
15
+ # contributors may be used to endorse or promote products derived from
16
+ # this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ require 'grpc'
31
+
32
+ describe 'Wrapped classes where .new cannot create an instance' do
33
+ describe GRPC::Core::Event do
34
+ it 'should fail .new fail with a runtime error' do
35
+ expect { GRPC::Core::Event.new }.to raise_error(TypeError)
36
+ end
37
+ end
38
+
39
+ describe GRPC::Core::Call do
40
+ it 'should fail .new fail with a runtime error' do
41
+ expect { GRPC::Core::Event.new }.to raise_error(TypeError)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,67 @@
1
+ # Copyright 2015, Google Inc.
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are
6
+ # met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright
9
+ # notice, this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above
11
+ # copyright notice, this list of conditions and the following disclaimer
12
+ # in the documentation and/or other materials provided with the
13
+ # distribution.
14
+ # * Neither the name of Google Inc. nor the names of its
15
+ # contributors may be used to endorse or promote products derived from
16
+ # this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ require 'grpc'
31
+
32
+ describe GRPC::Core::ByteBuffer do
33
+ describe '#new' do
34
+ it 'is constructed from a string' do
35
+ expect { GRPC::Core::ByteBuffer.new('#new') }.not_to raise_error
36
+ end
37
+
38
+ it 'can be constructed from the empty string' do
39
+ expect { GRPC::Core::ByteBuffer.new('') }.not_to raise_error
40
+ end
41
+
42
+ it 'cannot be constructed from nil' do
43
+ expect { GRPC::Core::ByteBuffer.new(nil) }.to raise_error TypeError
44
+ end
45
+
46
+ it 'cannot be constructed from non-strings' do
47
+ [1, Object.new, :a_symbol].each do |x|
48
+ expect { GRPC::Core::ByteBuffer.new(x) }.to raise_error TypeError
49
+ end
50
+ end
51
+ end
52
+
53
+ describe '#to_s' do
54
+ it 'is the string value the ByteBuffer was constructed with' do
55
+ expect(GRPC::Core::ByteBuffer.new('#to_s').to_s).to eq('#to_s')
56
+ end
57
+ end
58
+
59
+ describe '#dup' do
60
+ it 'makes an instance whose #to_s is the original string value' do
61
+ bb = GRPC::Core::ByteBuffer.new('#dup')
62
+ a_copy = bb.dup
63
+ expect(a_copy.to_s).to eq('#dup')
64
+ expect(a_copy.dup.to_s).to eq('#dup')
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,163 @@
1
+ # Copyright 2015, Google Inc.
2
+ # All rights reserved.
3
+ #
4
+ # Redistribution and use in source and binary forms, with or without
5
+ # modification, are permitted provided that the following conditions are
6
+ # met:
7
+ #
8
+ # * Redistributions of source code must retain the above copyright
9
+ # notice, this list of conditions and the following disclaimer.
10
+ # * Redistributions in binary form must reproduce the above
11
+ # copyright notice, this list of conditions and the following disclaimer
12
+ # in the documentation and/or other materials provided with the
13
+ # distribution.
14
+ # * Neither the name of Google Inc. nor the names of its
15
+ # contributors may be used to endorse or promote products derived from
16
+ # this software without specific prior written permission.
17
+ #
18
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
+ # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ require 'grpc'
31
+
32
+ include GRPC::Core::StatusCodes
33
+
34
+ describe GRPC::Core::RpcErrors do
35
+ before(:each) do
36
+ @known_types = {
37
+ OK: 0,
38
+ ERROR: 1,
39
+ NOT_ON_SERVER: 2,
40
+ NOT_ON_CLIENT: 3,
41
+ ALREADY_ACCEPTED: 4,
42
+ ALREADY_INVOKED: 5,
43
+ NOT_INVOKED: 6,
44
+ ALREADY_FINISHED: 7,
45
+ TOO_MANY_OPERATIONS: 8,
46
+ INVALID_FLAGS: 9,
47
+ ErrorMessages: {
48
+ 0 => 'ok',
49
+ 1 => 'unknown error',
50
+ 2 => 'not available on a server',
51
+ 3 => 'not available on a client',
52
+ 4 => 'call is already accepted',
53
+ 5 => 'call is already invoked',
54
+ 6 => 'call is not yet invoked',
55
+ 7 => 'call is already finished',
56
+ 8 => 'outstanding read or write present',
57
+ 9 => 'a bad flag was given'
58
+ }
59
+ }
60
+ end
61
+
62
+ it 'should have symbols for all the known error codes' do
63
+ m = GRPC::Core::RpcErrors
64
+ syms_and_codes = m.constants.collect { |c| [c, m.const_get(c)] }
65
+ expect(Hash[syms_and_codes]).to eq(@known_types)
66
+ end
67
+ end
68
+
69
+ describe GRPC::Core::Call do
70
+ before(:each) do
71
+ @tag = Object.new
72
+ @client_queue = GRPC::Core::CompletionQueue.new
73
+ fake_host = 'localhost:10101'
74
+ @ch = GRPC::Core::Channel.new(fake_host, nil)
75
+ end
76
+
77
+ describe '#start_read' do
78
+ xit 'should fail if called immediately' do
79
+ blk = proc { make_test_call.start_read(@tag) }
80
+ expect(&blk).to raise_error GRPC::Core::CallError
81
+ end
82
+ end
83
+
84
+ describe '#start_write' do
85
+ xit 'should fail if called immediately' do
86
+ bytes = GRPC::Core::ByteBuffer.new('test string')
87
+ blk = proc { make_test_call.start_write(bytes, @tag) }
88
+ expect(&blk).to raise_error GRPC::Core::CallError
89
+ end
90
+ end
91
+
92
+ describe '#start_write_status' do
93
+ xit 'should fail if called immediately' do
94
+ blk = proc { make_test_call.start_write_status(153, 'x', @tag) }
95
+ expect(&blk).to raise_error GRPC::Core::CallError
96
+ end
97
+ end
98
+
99
+ describe '#writes_done' do
100
+ xit 'should fail if called immediately' do
101
+ blk = proc { make_test_call.writes_done(Object.new) }
102
+ expect(&blk).to raise_error GRPC::Core::CallError
103
+ end
104
+ end
105
+
106
+ describe '#add_metadata' do
107
+ it 'adds metadata to a call without fail' do
108
+ call = make_test_call
109
+ n = 37
110
+ one_md = proc { |x| [sprintf('key%d', x), sprintf('value%d', x)] }
111
+ metadata = Hash[n.times.collect { |i| one_md.call i }]
112
+ expect { call.add_metadata(metadata) }.to_not raise_error
113
+ end
114
+ end
115
+
116
+ describe '#status' do
117
+ it 'can save the status and read it back' do
118
+ call = make_test_call
119
+ sts = Struct::Status.new(OK, 'OK')
120
+ expect { call.status = sts }.not_to raise_error
121
+ expect(call.status).to eq(sts)
122
+ end
123
+
124
+ it 'must be set to a status' do
125
+ call = make_test_call
126
+ bad_sts = Object.new
127
+ expect { call.status = bad_sts }.to raise_error(TypeError)
128
+ end
129
+
130
+ it 'can be set to nil' do
131
+ call = make_test_call
132
+ expect { call.status = nil }.not_to raise_error
133
+ end
134
+ end
135
+
136
+ describe '#metadata' do
137
+ it 'can save the metadata hash and read it back' do
138
+ call = make_test_call
139
+ md = { 'k1' => 'v1', 'k2' => 'v2' }
140
+ expect { call.metadata = md }.not_to raise_error
141
+ expect(call.metadata).to be(md)
142
+ end
143
+
144
+ it 'must be set with a hash' do
145
+ call = make_test_call
146
+ bad_md = Object.new
147
+ expect { call.metadata = bad_md }.to raise_error(TypeError)
148
+ end
149
+
150
+ it 'can be set to nil' do
151
+ call = make_test_call
152
+ expect { call.metadata = nil }.not_to raise_error
153
+ end
154
+ end
155
+
156
+ def make_test_call
157
+ @ch.create_call('dummy_method', 'dummy_host', deadline)
158
+ end
159
+
160
+ def deadline
161
+ Time.now + 2 # in 2 seconds; arbitrary
162
+ end
163
+ end