protobuffy 3.6.0 → 4.0.0

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 (209) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +67 -0
  4. data/.rubocop_todo.yml +145 -0
  5. data/.travis.yml +25 -5
  6. data/CHANGES.md +55 -0
  7. data/CONTRIBUTING.md +1 -1
  8. data/LICENSE.txt +17 -9
  9. data/README.md +13 -12
  10. data/Rakefile +15 -11
  11. data/bin/protoc-gen-ruby +8 -3
  12. data/bin/rpc_server +1 -0
  13. data/examples/lib/example/reverse-client.rb +2 -2
  14. data/install-protobuf.sh +28 -0
  15. data/lib/protobuf.rb +57 -53
  16. data/lib/protobuf/cli.rb +94 -74
  17. data/lib/protobuf/code_generator.rb +60 -9
  18. data/lib/protobuf/decoder.rb +19 -65
  19. data/lib/protobuf/deprecation.rb +117 -0
  20. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +11 -1
  21. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +55 -3
  22. data/lib/protobuf/encoder.rb +13 -53
  23. data/lib/protobuf/enum.rb +58 -63
  24. data/lib/protobuf/field.rb +4 -4
  25. data/lib/protobuf/field/base_field.rb +101 -173
  26. data/lib/protobuf/field/bool_field.rb +17 -11
  27. data/lib/protobuf/field/bytes_field.rb +21 -35
  28. data/lib/protobuf/field/double_field.rb +0 -1
  29. data/lib/protobuf/field/enum_field.rb +23 -22
  30. data/lib/protobuf/field/field_array.rb +5 -4
  31. data/lib/protobuf/field/fixed32_field.rb +1 -1
  32. data/lib/protobuf/field/fixed64_field.rb +0 -1
  33. data/lib/protobuf/field/float_field.rb +4 -1
  34. data/lib/protobuf/field/int32_field.rb +0 -1
  35. data/lib/protobuf/field/int64_field.rb +0 -1
  36. data/lib/protobuf/field/integer_field.rb +0 -1
  37. data/lib/protobuf/field/message_field.rb +13 -28
  38. data/lib/protobuf/field/sfixed32_field.rb +0 -1
  39. data/lib/protobuf/field/sfixed64_field.rb +0 -1
  40. data/lib/protobuf/field/signed_integer_field.rb +0 -1
  41. data/lib/protobuf/field/sint32_field.rb +0 -1
  42. data/lib/protobuf/field/sint64_field.rb +0 -1
  43. data/lib/protobuf/field/string_field.rb +2 -4
  44. data/lib/protobuf/field/uint32_field.rb +0 -1
  45. data/lib/protobuf/field/uint64_field.rb +0 -1
  46. data/lib/protobuf/field/varint_field.rb +30 -13
  47. data/lib/protobuf/generators/base.rb +30 -16
  48. data/lib/protobuf/generators/enum_generator.rb +6 -9
  49. data/lib/protobuf/generators/extension_generator.rb +1 -2
  50. data/lib/protobuf/generators/field_generator.rb +25 -13
  51. data/lib/protobuf/generators/file_generator.rb +157 -35
  52. data/lib/protobuf/generators/group_generator.rb +22 -17
  53. data/lib/protobuf/generators/message_generator.rb +13 -14
  54. data/lib/protobuf/generators/option_generator.rb +17 -0
  55. data/lib/protobuf/generators/printable.rb +12 -13
  56. data/lib/protobuf/generators/service_generator.rb +2 -3
  57. data/lib/protobuf/http.rb +2 -2
  58. data/lib/protobuf/lifecycle.rb +20 -33
  59. data/lib/protobuf/logging.rb +39 -0
  60. data/lib/protobuf/message.rb +114 -47
  61. data/lib/protobuf/message/fields.rb +170 -88
  62. data/lib/protobuf/message/serialization.rb +19 -18
  63. data/lib/protobuf/optionable.rb +53 -6
  64. data/lib/protobuf/rpc/buffer.rb +18 -19
  65. data/lib/protobuf/rpc/client.rb +22 -50
  66. data/lib/protobuf/rpc/connectors/base.rb +177 -12
  67. data/lib/protobuf/rpc/connectors/http.rb +14 -9
  68. data/lib/protobuf/rpc/connectors/ping.rb +89 -0
  69. data/lib/protobuf/rpc/connectors/socket.rb +13 -8
  70. data/lib/protobuf/rpc/connectors/zmq.rb +178 -73
  71. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +4 -1
  72. data/lib/protobuf/rpc/env.rb +12 -12
  73. data/lib/protobuf/rpc/error.rb +3 -3
  74. data/lib/protobuf/rpc/error/client_error.rb +4 -4
  75. data/lib/protobuf/rpc/error/server_error.rb +9 -9
  76. data/lib/protobuf/rpc/middleware/exception_handler.rb +6 -2
  77. data/lib/protobuf/rpc/middleware/logger.rb +8 -4
  78. data/lib/protobuf/rpc/middleware/request_decoder.rb +17 -21
  79. data/lib/protobuf/rpc/middleware/response_encoder.rb +22 -27
  80. data/lib/protobuf/rpc/middleware/statsd.rb +3 -3
  81. data/lib/protobuf/rpc/rpc.pb.rb +4 -1
  82. data/lib/protobuf/rpc/server.rb +1 -1
  83. data/lib/protobuf/rpc/servers/http/server.rb +19 -17
  84. data/lib/protobuf/rpc/servers/socket/server.rb +78 -70
  85. data/lib/protobuf/rpc/servers/socket/worker.rb +4 -4
  86. data/lib/protobuf/rpc/servers/socket_runner.rb +27 -15
  87. data/lib/protobuf/rpc/servers/zmq/broker.rb +70 -31
  88. data/lib/protobuf/rpc/servers/zmq/server.rb +55 -47
  89. data/lib/protobuf/rpc/servers/zmq/util.rb +14 -13
  90. data/lib/protobuf/rpc/servers/zmq/worker.rb +16 -16
  91. data/lib/protobuf/rpc/servers/zmq_runner.rb +26 -7
  92. data/lib/protobuf/rpc/service.rb +21 -27
  93. data/lib/protobuf/rpc/service_directory.rb +43 -27
  94. data/lib/protobuf/rpc/service_dispatcher.rb +9 -10
  95. data/lib/protobuf/rpc/service_filters.rb +32 -55
  96. data/lib/protobuf/rpc/stat.rb +4 -8
  97. data/lib/protobuf/socket.rb +1 -2
  98. data/lib/protobuf/tasks/compile.rake +3 -4
  99. data/lib/protobuf/varint.rb +9 -0
  100. data/lib/protobuf/varint_pure.rb +13 -0
  101. data/lib/protobuf/version.rb +1 -1
  102. data/lib/protobuf/zmq.rb +2 -2
  103. data/proto/google/protobuf/descriptor.proto +190 -31
  104. data/protobuffy.gemspec +30 -17
  105. data/spec/benchmark/tasks.rb +27 -19
  106. data/spec/bin/protoc-gen-ruby_spec.rb +11 -6
  107. data/spec/encoding/all_types_spec.rb +96 -84
  108. data/spec/encoding/extreme_values_spec.rb +0 -0
  109. data/spec/functional/class_inheritance_spec.rb +52 -0
  110. data/spec/functional/code_generator_spec.rb +38 -0
  111. data/spec/functional/socket_server_spec.rb +15 -15
  112. data/spec/functional/zmq_server_spec.rb +29 -27
  113. data/spec/lib/protobuf/cli_spec.rb +82 -67
  114. data/spec/lib/protobuf/code_generator_spec.rb +37 -10
  115. data/spec/lib/protobuf/enum_spec.rb +77 -46
  116. data/spec/lib/protobuf/field/bool_field_spec.rb +91 -0
  117. data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
  118. data/spec/lib/protobuf/field/enum_field_spec.rb +26 -0
  119. data/spec/lib/protobuf/field/field_array_spec.rb +69 -0
  120. data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
  121. data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
  122. data/spec/lib/protobuf/field/float_field_spec.rb +90 -0
  123. data/spec/lib/protobuf/field/int32_field_spec.rb +114 -1
  124. data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
  125. data/spec/lib/protobuf/field/message_field_spec.rb +132 -0
  126. data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
  127. data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
  128. data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
  129. data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
  130. data/spec/lib/protobuf/field/string_field_spec.rb +44 -11
  131. data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
  132. data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
  133. data/spec/lib/protobuf/field_spec.rb +4 -6
  134. data/spec/lib/protobuf/generators/base_spec.rb +80 -13
  135. data/spec/lib/protobuf/generators/enum_generator_spec.rb +35 -21
  136. data/spec/lib/protobuf/generators/extension_generator_spec.rb +12 -13
  137. data/spec/lib/protobuf/generators/field_generator_spec.rb +73 -21
  138. data/spec/lib/protobuf/generators/file_generator_spec.rb +89 -6
  139. data/spec/lib/protobuf/generators/service_generator_spec.rb +25 -13
  140. data/spec/lib/protobuf/lifecycle_spec.rb +25 -20
  141. data/spec/lib/protobuf/message_spec.rb +578 -79
  142. data/spec/lib/protobuf/optionable_spec.rb +202 -26
  143. data/spec/lib/protobuf/rpc/client_spec.rb +16 -16
  144. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +167 -13
  145. data/spec/lib/protobuf/rpc/connectors/connector_spec.rb +4 -5
  146. data/spec/lib/protobuf/rpc/connectors/http_spec.rb +13 -11
  147. data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
  148. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +6 -7
  149. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +35 -52
  150. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +10 -10
  151. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +11 -11
  152. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +23 -23
  153. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +11 -11
  154. data/spec/lib/protobuf/rpc/middleware/statsd_spec.rb +6 -6
  155. data/spec/lib/protobuf/rpc/servers/http/server_spec.rb +47 -44
  156. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +6 -6
  157. data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +12 -10
  158. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +11 -11
  159. data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +7 -7
  160. data/spec/lib/protobuf/rpc/service_directory_spec.rb +47 -49
  161. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +8 -25
  162. data/spec/lib/protobuf/rpc/service_filters_spec.rb +102 -69
  163. data/spec/lib/protobuf/rpc/service_spec.rb +37 -36
  164. data/spec/lib/protobuf/rpc/stat_spec.rb +7 -9
  165. data/spec/lib/protobuf/varint_spec.rb +29 -0
  166. data/spec/lib/protobuf_spec.rb +55 -28
  167. data/spec/spec_helper.rb +12 -27
  168. data/spec/support/all.rb +0 -1
  169. data/spec/support/packed_field.rb +4 -3
  170. data/spec/support/{test → protos}/all_types.data.bin +0 -0
  171. data/spec/support/{test → protos}/all_types.data.txt +0 -0
  172. data/spec/support/{test → protos}/enum.pb.rb +8 -4
  173. data/spec/support/{test → protos}/enum.proto +4 -1
  174. data/spec/support/{test → protos}/extreme_values.data.bin +0 -0
  175. data/spec/support/protos/google_unittest.bin +0 -0
  176. data/spec/support/protos/google_unittest.pb.rb +798 -0
  177. data/spec/support/{test → protos}/google_unittest.proto +237 -66
  178. data/spec/support/protos/google_unittest_custom_options.bin +0 -0
  179. data/spec/support/protos/google_unittest_custom_options.pb.rb +268 -0
  180. data/spec/support/protos/google_unittest_custom_options.proto +424 -0
  181. data/spec/support/protos/google_unittest_import.pb.rb +55 -0
  182. data/spec/support/{test → protos}/google_unittest_import.proto +19 -10
  183. data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
  184. data/spec/support/{test → protos}/google_unittest_import_public.proto +8 -5
  185. data/spec/support/{test → protos}/multi_field_extensions.pb.rb +5 -2
  186. data/spec/support/{test → protos}/multi_field_extensions.proto +2 -0
  187. data/spec/support/{test → protos}/resource.pb.rb +47 -11
  188. data/spec/support/{test → protos}/resource.proto +24 -1
  189. data/spec/support/resource_service.rb +23 -0
  190. data/spec/support/server.rb +32 -61
  191. metadata +119 -59
  192. data/lib/protobuf/deprecator.rb +0 -42
  193. data/lib/protobuf/logger.rb +0 -93
  194. data/lib/protobuf/rpc/connector.rb +0 -21
  195. data/lib/protobuf/rpc/connectors/common.rb +0 -172
  196. data/spec/data/data.bin +0 -3
  197. data/spec/data/types.bin +0 -0
  198. data/spec/lib/protobuf/logger_spec.rb +0 -145
  199. data/spec/lib/protobuf/rpc/connector_spec.rb +0 -26
  200. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +0 -170
  201. data/spec/support/test/defaults.pb.rb +0 -25
  202. data/spec/support/test/defaults.proto +0 -9
  203. data/spec/support/test/extended.pb.rb +0 -22
  204. data/spec/support/test/extended.proto +0 -10
  205. data/spec/support/test/google_unittest.pb.rb +0 -543
  206. data/spec/support/test/google_unittest_import.pb.rb +0 -37
  207. data/spec/support/test/google_unittest_import_public.pb.rb +0 -8
  208. data/spec/support/test/resource_service.rb +0 -26
  209. data/spec/support/tolerance_matcher.rb +0 -40
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
- require 'spec/support/test/resource_service'
2
+ require SUPPORT_PATH.join('resource_service')
3
3
 
4
- describe Protobuf::Rpc::Service do
4
+ RSpec.describe Protobuf::Rpc::Service do
5
5
 
6
6
  context 'class methods' do
7
7
  subject { Test::ResourceService }
@@ -11,73 +11,74 @@ describe Protobuf::Rpc::Service do
11
11
  end
12
12
 
13
13
  describe '.host' do
14
- its(:host) { should eq described_class::DEFAULT_HOST }
14
+ specify { expect(subject.host).to eq described_class::DEFAULT_HOST }
15
15
  end
16
16
 
17
17
  describe '.host=' do
18
18
  before { subject.host = 'mynewhost.com' }
19
- its(:host) { should eq 'mynewhost.com' }
19
+ specify { expect(subject.host).to eq 'mynewhost.com' }
20
20
  end
21
21
 
22
22
  describe '.port' do
23
- its(:port) { should eq described_class::DEFAULT_PORT }
23
+ specify { expect(subject.port).to eq described_class::DEFAULT_PORT }
24
24
  end
25
25
 
26
26
  describe '.port=' do
27
27
  before { subject.port = 12345 }
28
- its(:port) { should eq 12345 }
28
+ specify { expect(subject.port).to eq 12345 }
29
29
  end
30
30
 
31
31
  describe '.configure' do
32
32
  context 'when providing a host' do
33
33
  before { subject.configure(:host => 'mynewhost.com') }
34
- its(:host) { should eq 'mynewhost.com' }
34
+ specify { expect(subject.host).to eq 'mynewhost.com' }
35
35
  end
36
36
 
37
37
  context 'when providing a port' do
38
38
  before { subject.configure(:port => 12345) }
39
- its(:port) { should eq 12345 }
39
+ specify { expect(subject.port).to eq 12345 }
40
40
  end
41
41
  end
42
42
 
43
43
  describe '.located_at' do
44
44
  context 'when given location is empty' do
45
45
  before { subject.located_at(nil) }
46
- its(:host) { should eq described_class::DEFAULT_HOST }
47
- its(:port) { should eq described_class::DEFAULT_PORT }
46
+ specify { expect(subject.host).to eq described_class::DEFAULT_HOST }
47
+ specify { expect(subject.port).to eq described_class::DEFAULT_PORT }
48
48
  end
49
49
 
50
50
  context 'when given location is invalid' do
51
51
  before { subject.located_at('i like pie') }
52
- its(:host) { should eq described_class::DEFAULT_HOST }
53
- its(:port) { should eq described_class::DEFAULT_PORT }
52
+ specify { expect(subject.host).to eq described_class::DEFAULT_HOST }
53
+ specify { expect(subject.port).to eq described_class::DEFAULT_PORT }
54
54
  end
55
55
 
56
56
  context 'when given location contains a host and port' do
57
57
  before { subject.located_at('mynewdomain.com:12345') }
58
- its(:host) { should eq 'mynewdomain.com' }
59
- its(:port) { should eq 12345 }
58
+ specify { expect(subject.host).to eq 'mynewdomain.com' }
59
+ specify { expect(subject.port).to eq 12345 }
60
60
  end
61
61
  end
62
62
 
63
63
  describe '.client' do
64
64
  it 'initializes a client object for this service' do
65
65
  client = double('client')
66
- ::Protobuf::Rpc::Client.should_receive(:new)
67
- .with(hash_including({ :service => subject,
68
- :host => subject.host,
69
- :port => subject.port }))
70
- .and_return(client)
71
- subject.client.should eq client
66
+ expect(::Protobuf::Rpc::Client).to receive(:new)
67
+ .with(hash_including(
68
+ :service => subject,
69
+ :host => subject.host,
70
+ :port => subject.port
71
+ )).and_return(client)
72
+ expect(subject.client).to eq client
72
73
  end
73
74
  end
74
75
 
75
76
  describe '.rpc' do
76
77
  before { Test::ResourceService.rpc(:update, Test::ResourceFindRequest, Test::Resource) }
77
78
  subject { Test::ResourceService.rpcs[:update] }
78
- its(:method) { should eq :update }
79
- its(:request_type) { should eq Test::ResourceFindRequest }
80
- its(:response_type) { should eq Test::Resource }
79
+ specify { expect(subject.method).to eq :update }
80
+ specify { expect(subject.request_type).to eq Test::ResourceFindRequest }
81
+ specify { expect(subject.response_type).to eq Test::Resource }
81
82
  end
82
83
 
83
84
  describe '.rpc_method?' do
@@ -85,13 +86,13 @@ describe Protobuf::Rpc::Service do
85
86
 
86
87
  context 'when given name is a pre-defined rpc method' do
87
88
  it 'returns true' do
88
- subject.rpc_method?(:delete).should be_true
89
+ expect(subject.rpc_method?(:delete)).to be true
89
90
  end
90
91
  end
91
92
 
92
93
  context 'when given name is not a pre-defined rpc method' do
93
94
  it 'returns false' do
94
- subject.rpc_method?(:zoobaboo).should be_false
95
+ expect(subject.rpc_method?(:zoobaboo)).to be false
95
96
  end
96
97
  end
97
98
  end
@@ -99,8 +100,8 @@ describe Protobuf::Rpc::Service do
99
100
 
100
101
  context 'instance methods' do
101
102
  context 'when invoking a service call' do
102
- before(:all) do
103
- class ::NewTestService < Protobuf::Rpc::Service
103
+ before do
104
+ stub_const('NewTestService', Class.new(Protobuf::Rpc::Service) do
104
105
  rpc :find_with_implied_response, Test::ResourceFindRequest, Test::Resource
105
106
  def find_with_implied_response
106
107
  response.name = 'Implicit response'
@@ -117,7 +118,7 @@ describe Protobuf::Rpc::Service do
117
118
  rpc_failed('This is a failed endpoint')
118
119
  response.name = 'Name will still be set'
119
120
  end
120
- end
121
+ end)
121
122
  end
122
123
 
123
124
  let(:request) { Test::ResourceFindRequest.new(:name => 'resource') }
@@ -125,35 +126,35 @@ describe Protobuf::Rpc::Service do
125
126
 
126
127
  context 'when calling the rpc method' do
127
128
  context 'when response is implied' do
128
- let(:env) {
129
+ let(:env) do
129
130
  Protobuf::Rpc::Env.new(
130
131
  'request' => request,
131
132
  'response_type' => response_type
132
133
  )
133
- }
134
+ end
134
135
  let(:response_type) { service.rpcs[:find_with_implied_response].response_type }
135
136
  let(:service) { NewTestService }
136
137
 
137
138
  subject { NewTestService.new(env) }
138
139
 
139
140
  before { subject.find_with_implied_response }
140
- its(:response) { should be_a(Test::Resource) }
141
- specify { subject.response.name.should eq 'Implicit response' }
141
+ specify { expect(subject.response).to be_a(Test::Resource) }
142
+ specify { expect(subject.response.name).to eq 'Implicit response' }
142
143
  end
143
144
 
144
145
  context 'when using respond_with paradigm' do
145
- let(:env) {
146
+ let(:env) do
146
147
  Protobuf::Rpc::Env.new(
147
148
  'method_name' => :find_with_respond_with,
148
149
  'request' => request
149
150
  )
150
- }
151
+ end
151
152
 
152
153
  subject { NewTestService.new(env) }
153
154
 
154
155
  before { subject.find_with_respond_with }
155
- its(:response) { should be_a(Test::Resource) }
156
- specify { subject.response.name.should eq 'Custom response' }
156
+ specify { expect(subject.response).to be_a(Test::Resource) }
157
+ specify { expect(subject.response.name).to eq 'Custom response' }
157
158
  end
158
159
  end
159
160
  end
@@ -2,12 +2,10 @@ require 'spec_helper'
2
2
  require 'timecop'
3
3
  require 'active_support/core_ext/numeric/time'
4
4
 
5
- describe ::Protobuf::Rpc::Stat do
5
+ RSpec.describe ::Protobuf::Rpc::Stat do
6
6
 
7
7
  before(:all) do
8
- unless defined?(BarService)
9
- class BarService < ::Struct.new(:method_name); end
10
- end
8
+ BarService = ::Struct.new(:method_name) unless defined?(BarService)
11
9
  end
12
10
 
13
11
  describe 'server mode' do
@@ -21,7 +19,7 @@ describe ::Protobuf::Rpc::Stat do
21
19
 
22
20
  ::Timecop.freeze(1.62.seconds.from_now) do
23
21
  stats.stop
24
- stats.to_s.should eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/1302B - 1.62s - #{::Time.now.iso8601}"
22
+ expect(stats.to_s).to eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/1302B - 1.62s - #{::Time.now.iso8601}"
25
23
  end
26
24
  end
27
25
  end
@@ -32,7 +30,7 @@ describe ::Protobuf::Rpc::Stat do
32
30
  stats.client = 'myserver1'
33
31
  stats.dispatcher = double('dispatcher', :service => BarService.new(:find_bars))
34
32
  stats.request_size = 43
35
- stats.to_s.should eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/-"
33
+ expect(stats.to_s).to eq "[SRV] - myserver1 - #{stats.trace_id} - BarService#find_bars - 43B/-"
36
34
  end
37
35
  end
38
36
  end
@@ -49,7 +47,7 @@ describe ::Protobuf::Rpc::Stat do
49
47
 
50
48
  ::Timecop.freeze(0.832.seconds.from_now) do
51
49
  stats.stop
52
- stats.to_s.should eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/12345B - 0.832s - #{::Time.now.iso8601}"
50
+ expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/12345B - 0.832s - #{::Time.now.iso8601}"
53
51
  end
54
52
 
55
53
  end
@@ -62,7 +60,7 @@ describe ::Protobuf::Rpc::Stat do
62
60
  stats.service = 'Foo::BarService'
63
61
  stats.method_name = 'find_bars'
64
62
  stats.request_size = 37
65
- stats.to_s.should eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/-"
63
+ expect(stats.to_s).to eq "[CLT] - myserver1.myhost.com:30000 - #{stats.trace_id} - Foo::BarService#find_bars - 37B/-"
66
64
  end
67
65
  end
68
66
  end
@@ -101,7 +99,7 @@ describe ::Protobuf::Rpc::Stat do
101
99
 
102
100
  context "when statsd_client hasn't been set" do
103
101
  it "should not raise" do
104
- expect{ stop }.not_to raise_error
102
+ expect { stop }.not_to raise_error
105
103
  end
106
104
  end
107
105
 
@@ -0,0 +1,29 @@
1
+ require 'base64'
2
+ require 'spec_helper'
3
+
4
+ RSpec.describe Protobuf::Varint do
5
+ VALUES = {
6
+ 0 => "AA==",
7
+ 5 => "BQ==",
8
+ 51 => "Mw==",
9
+ 9_192 => "6Ec=",
10
+ 80_389 => "hfQE",
11
+ 913_389 => "7d83",
12
+ 516_192_829_912_693 => "9eyMkpivdQ==",
13
+ 9_999_999_999_999_999_999 => "//+fz8jgyOOKAQ==",
14
+ }.freeze
15
+
16
+ [defined?(::Varint) ? ::Varint : nil, Protobuf::VarintPure].compact.each do |klass|
17
+ context "with #{klass}" do
18
+ before { described_class.extend(klass) }
19
+ after { load ::File.expand_path('../../../../lib/protobuf/varint.rb', __FILE__) }
20
+
21
+ VALUES.each do |number, encoded|
22
+ it "decodes #{number}" do
23
+ io = StringIO.new(Base64.decode64(encoded))
24
+ expect(described_class.decode(io)).to eq(number)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,77 +1,104 @@
1
1
  require 'spec_helper'
2
2
  require 'protobuf'
3
3
 
4
- describe ::Protobuf do
4
+ RSpec.describe ::Protobuf do
5
5
 
6
6
  describe '.client_host' do
7
- after { ::Protobuf.instance_variable_set(:@_client_host, nil) }
7
+ after { ::Protobuf.client_host = nil }
8
8
 
9
9
  subject { ::Protobuf.client_host }
10
10
 
11
11
  context 'when client_host is not pre-configured' do
12
- it { should eq `hostname`.chomp }
12
+ it { is_expected.to eq ::Socket.gethostname }
13
13
  end
14
14
 
15
15
  context 'when client_host is pre-configured' do
16
16
  let(:hostname) { 'override.myhost.com' }
17
17
  before { ::Protobuf.client_host = hostname }
18
- it { should eq hostname }
18
+ it { is_expected.to eq hostname }
19
19
  end
20
20
  end
21
21
 
22
- describe '.connector_type' do
23
- before { described_class.instance_variable_set(:@_connector_type, nil) }
24
-
25
- it 'defaults to socket' do
26
- described_class.connector_type.should eq :socket
22
+ describe '.connector_type_class' do
23
+ it "defaults to Socket" do
24
+ described_class.connector_type_class = nil
25
+ expect(described_class.connector_type_class).to eq(::Protobuf::Rpc::Connectors::Socket)
27
26
  end
28
27
 
29
- it 'accepts socket or zmq' do
30
- [:socket, :zmq].each do |type|
31
- described_class.connector_type = type
32
- described_class.connector_type.should eq type
33
- end
28
+ it 'fails if fails to load the PB_CLIENT_TYPE' do
29
+ ENV['PB_CLIENT_TYPE'] = "something_to_autoload"
30
+ expect { load 'protobuf.rb' }.to raise_error(LoadError, /something_to_autoload/)
31
+ ENV.delete('PB_CLIENT_TYPE')
34
32
  end
35
33
 
36
- it 'does not accept other types' do
37
- [:hello, :world, :evented].each do |type|
38
- expect {
39
- described_class.connector_type = type
40
- }.to raise_error(ArgumentError)
41
- end
34
+ it 'loads the connector type class from PB_CLIENT_TYPE' do
35
+ ENV['PB_CLIENT_TYPE'] = "protobuf/rpc/connectors/zmq"
36
+ load 'protobuf.rb'
37
+ expect(::Protobuf.connector_type_class).to eq(::Protobuf::Rpc::Connectors::Zmq)
38
+ ENV.delete('PB_CLIENT_TYPE')
42
39
  end
43
40
  end
44
41
 
45
42
  describe '.gc_pause_server_request?' do
46
- before { described_class.instance_variable_set(:@_gc_pause_server_request, nil) }
43
+ before { described_class.instance_variable_set(:@gc_pause_server_request, nil) }
47
44
 
48
45
  it 'defaults to a false value' do
49
- described_class.gc_pause_server_request?.should be_false
46
+ expect(described_class.gc_pause_server_request?).to be false
50
47
  end
51
48
 
52
49
  it 'is settable' do
53
50
  described_class.gc_pause_server_request = true
54
- described_class.gc_pause_server_request?.should be_true
51
+ expect(described_class.gc_pause_server_request?).to be true
55
52
  end
56
53
  end
57
54
 
58
55
  describe '.print_deprecation_warnings?' do
59
- before { described_class.instance_variable_set(:@_print_deprecation_warnings, nil) }
56
+ around do |example|
57
+ orig = described_class.print_deprecation_warnings?
58
+ example.call
59
+ described_class.print_deprecation_warnings = orig
60
+ end
60
61
 
61
62
  it 'defaults to a true value' do
62
- described_class.print_deprecation_warnings?.should be_true
63
+ allow(ENV).to receive(:key?).with('PB_IGNORE_DEPRECATIONS').and_return(false)
64
+ described_class.instance_variable_set('@field_deprecator', nil)
65
+ expect(described_class.print_deprecation_warnings?).to be true
63
66
  end
64
67
 
65
68
  it 'is settable' do
66
69
  described_class.print_deprecation_warnings = false
67
- described_class.print_deprecation_warnings?.should be_false
70
+ expect(described_class.print_deprecation_warnings?).to be false
68
71
  end
69
72
 
70
73
  context 'when ENV["PB_IGNORE_DEPRECATIONS"] present' do
71
74
  it 'defaults to a false value' do
72
- ENV['PB_IGNORE_DEPRECATIONS'] = '1'
73
- described_class.print_deprecation_warnings?.should be_false
75
+ allow(ENV).to receive(:key?).with('PB_IGNORE_DEPRECATIONS').and_return(true)
76
+ described_class.instance_variable_set('@field_deprecator', nil)
77
+ expect(described_class.print_deprecation_warnings?).to be false
78
+ end
79
+ end
80
+ end
81
+
82
+ describe '.ignore_unknown_fields?' do
83
+ around do |example|
84
+ orig = described_class.ignore_unknown_fields?
85
+ example.call
86
+ described_class.ignore_unknown_fields = orig
87
+ end
88
+
89
+ it 'defaults to a true value' do
90
+ if described_class.instance_variable_defined?('@ignore_unknown_fields')
91
+ described_class.send(:remove_instance_variable, '@ignore_unknown_fields')
74
92
  end
93
+ expect(described_class.ignore_unknown_fields?).to be true
94
+ end
95
+
96
+ it 'is settable' do
97
+ expect do
98
+ described_class.ignore_unknown_fields = false
99
+ end.to change {
100
+ described_class.ignore_unknown_fields?
101
+ }.from(true).to(false)
75
102
  end
76
103
  end
77
104
 
data/spec/spec_helper.rb CHANGED
@@ -1,49 +1,34 @@
1
- require 'simplecov'
2
- SimpleCov.start
3
-
4
1
  require 'timeout'
5
2
  require 'rubygems'
6
3
  require 'bundler'
7
4
  Bundler.setup :default, :development, :test
8
5
  require 'pry'
6
+ require 'pathname'
9
7
 
10
- $: << ::File.expand_path('../..', __FILE__)
11
- $: << ::File.expand_path('../support', __FILE__)
8
+ $LOAD_PATH << ::File.expand_path('../..', __FILE__)
9
+ SUPPORT_PATH = Pathname.new(::File.expand_path('../support', __FILE__))
10
+ PROTOS_PATH = SUPPORT_PATH.join('protos')
11
+ $LOAD_PATH << SUPPORT_PATH
12
12
 
13
13
  require 'protobuf'
14
14
  require 'protobuf/rpc/server'
15
- require ::File.expand_path('../support/all', __FILE__)
15
+ require SUPPORT_PATH.join('all')
16
16
 
17
- $: << ::File.expand_path("../../lib/protobuf/descriptors", __FILE__)
17
+ $LOAD_PATH << ::File.expand_path("../../lib/protobuf/descriptors", __FILE__)
18
18
  require 'google/protobuf/compiler/plugin.pb'
19
19
 
20
20
  # Including a way to turn on debug logger for spec runs
21
21
  if ENV.key?('DEBUG')
22
- debug_log = ::File.expand_path('../debug_specs.log', File.dirname(__FILE__) )
23
- ::Protobuf::Logger.configure(:file => debug_log, :level => ::Logger::DEBUG)
22
+ debug_log = ::File.expand_path('../../debug_specs.log', __FILE__)
23
+ ::Protobuf::Logging.initialize_logger(debug_log, ::Logger::DEBUG)
24
+ else
25
+ ::Protobuf::Logging.initialize_logger('/dev/null')
24
26
  end
25
27
 
26
28
  # Get rid of the deprecation env var if present (messes with specs).
27
29
  ENV.delete("PB_IGNORE_DEPRECATIONS")
28
30
 
29
- ::RSpec.configure do |c|
30
- c.include(::Sander6::CustomMatchers)
31
- c.mock_with :rspec
32
-
33
- c.before(:suite) do
34
- unless ENV['NO_COMPILE_TEST_PROTOS']
35
- require 'rake'
36
- load ::File.expand_path('../../Rakefile', __FILE__)
37
- $stdout.puts 'Compiling test protos (use NO_COMPILE_TEST_PROTOS=1 to skip)'
38
- ::Rake::Task['compile:spec']
39
- end
40
- end
41
- end
42
-
43
- support_proto_glob = File.expand_path('../support/**/*.pb.rb', __FILE__)
44
- Dir[support_proto_glob].each { |proto_file| require proto_file }
45
-
46
- class ::Protobuf::Rpc::Client
31
+ ::Protobuf::Rpc::Client.class_eval do
47
32
  def ==(other)
48
33
  connector.options == other.options && \
49
34
  success_cb == other.success_cb && \