protobuf 2.0.0.rc2 → 2.0.0.rc3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/.gitignore +1 -0
  2. data/ext/ruby_generator/Makefile +10 -0
  3. data/ext/ruby_generator/RubyGenerator.cpp +85 -70
  4. data/ext/ruby_generator/RubyGenerator.h +23 -4
  5. data/lib/protobuf.rb +33 -26
  6. data/lib/protobuf/cli.rb +18 -13
  7. data/lib/protobuf/enum.rb +39 -34
  8. data/lib/protobuf/enum_value.rb +29 -0
  9. data/lib/protobuf/field/base_field.rb +34 -5
  10. data/lib/protobuf/field/enum_field.rb +6 -14
  11. data/lib/protobuf/field/extension_fields.rb +13 -5
  12. data/lib/protobuf/field/field_array.rb +17 -7
  13. data/lib/protobuf/field/varint_field.rb +4 -6
  14. data/lib/protobuf/message.rb +44 -148
  15. data/lib/protobuf/rpc/server.rb +2 -2
  16. data/lib/protobuf/version.rb +1 -1
  17. data/spec/benchmark/tasks.rb +7 -8
  18. data/spec/functional/evented_server_spec.rb +9 -9
  19. data/spec/functional/socket_server_spec.rb +8 -8
  20. data/spec/functional/zmq_server_spec.rb +8 -8
  21. data/spec/lib/protobuf/cli_spec.rb +30 -11
  22. data/spec/lib/protobuf/enum_spec.rb +90 -0
  23. data/spec/lib/protobuf/enum_value_spec.rb +13 -0
  24. data/spec/lib/protobuf/message/encoder_spec.rb +1 -1
  25. data/spec/lib/protobuf/message_spec.rb +50 -0
  26. data/spec/lib/protobuf/rpc/client_spec.rb +23 -23
  27. data/spec/lib/protobuf/rpc/servers/evented_server_spec.rb +1 -1
  28. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +1 -1
  29. data/spec/lib/protobuf/rpc/service_spec.rb +18 -18
  30. data/spec/lib/protobuf_spec.rb +62 -0
  31. data/spec/spec_helper.rb +12 -1
  32. data/spec/support/all.rb +0 -1
  33. data/spec/support/server.rb +1 -1
  34. data/spec/support/test/enum.pb.rb +32 -0
  35. data/spec/support/test/enum.proto +12 -0
  36. data/spec/support/test/resource.pb.rb +52 -0
  37. data/spec/{proto/test.proto → support/test/resource.proto} +2 -2
  38. data/spec/support/test/resource_service.rb +14 -0
  39. metadata +51 -48
  40. data/ext/Makefile +0 -11
  41. data/spec/lib/protobuf/message/enum_spec.rb +0 -13
  42. data/spec/lib/protobuf/message/message_spec.rb +0 -67
  43. data/spec/proto/test.pb.rb +0 -54
  44. data/spec/proto/test_service.rb +0 -30
  45. data/spec/proto/test_service_impl.rb +0 -18
  46. data/spec/support/silent_constants.rb +0 -44
@@ -20,7 +20,7 @@ module Protobuf
20
20
 
21
21
  # Call the service method
22
22
  log_debug { "[#{log_signature}] Dispatching client request to service" }
23
- ::GC.disable if ::Protobuf.gc_pause_server_request
23
+ ::GC.disable if ::Protobuf.gc_pause_server_request?
24
24
  invoke_rpc_method
25
25
  rescue => error
26
26
  # Ensure we're handling any errors that try to slip out the back door
@@ -133,7 +133,7 @@ module Protobuf
133
133
  @stats.log_stats
134
134
  @did_respond = true
135
135
  ensure
136
- ::GC.enable if ::Protobuf.gc_pause_server_request
136
+ ::GC.enable if ::Protobuf.gc_pause_server_request?
137
137
  end
138
138
  end
139
139
  end
@@ -1,4 +1,4 @@
1
1
  module Protobuf
2
- VERSION = '2.0.0.rc2'
2
+ VERSION = '2.0.0.rc3'
3
3
  PROTOC_VERSION = '2.4.1'
4
4
  end
@@ -1,6 +1,6 @@
1
1
  require 'benchmark'
2
2
  require 'support/all'
3
- require 'proto/test_service_impl'
3
+ require 'support/test/resource_service'
4
4
  require 'perftools'
5
5
 
6
6
  # Including a way to turn on debug logger for spec runs
@@ -11,7 +11,6 @@ if ENV["DEBUG"]
11
11
  end
12
12
 
13
13
  namespace :benchmark do
14
- include SilentConstants
15
14
 
16
15
  def benchmark_wrapper(global_bench = nil)
17
16
  if global_bench
@@ -28,7 +27,7 @@ namespace :benchmark do
28
27
 
29
28
  EventMachine.fiber_run do
30
29
  StubServer.new do |server|
31
- client = Spec::Proto::TestService.client(:async => false)
30
+ client = ::Test::ResourceService.client(:async => false)
32
31
 
33
32
  benchmark_wrapper(global_bench) do |bench|
34
33
  bench.report("ES / EC") do
@@ -46,7 +45,7 @@ namespace :benchmark do
46
45
 
47
46
  EventMachine.fiber_run do
48
47
  StubServer.new(:server => Protobuf::Rpc::Socket::Server, :port => 9399) do |server|
49
- client = Spec::Proto::TestService.client(:async => false, :port => 9399)
48
+ client = ::Test::ResourceService.client(:async => false, :port => 9399)
50
49
 
51
50
  benchmark_wrapper(global_bench) do |bench|
52
51
  bench.report("SS / EC") do
@@ -65,7 +64,7 @@ namespace :benchmark do
65
64
  EM.stop if EM.reactor_running?
66
65
 
67
66
  StubServer.new(:server => Protobuf::Rpc::Socket::Server, :port => 9399) do |server|
68
- client = Spec::Proto::TestService.client(:async => false, :port => 9399)
67
+ client = ::Test::ResourceService.client(:async => false, :port => 9399)
69
68
 
70
69
  benchmark_wrapper(global_bench) do |bench|
71
70
  bench.report("SS / SC") do
@@ -83,7 +82,7 @@ namespace :benchmark do
83
82
  Thread.pass until EM.reactor_running?
84
83
 
85
84
  StubServer.new(:port => 9399) do |server|
86
- client = Spec::Proto::TestService.client(:async => false, :port => 9399)
85
+ client = ::Test::ResourceService.client(:async => false, :port => 9399)
87
86
 
88
87
  benchmark_wrapper(global_bench) do |bench|
89
88
  bench.report("ES / SC") do
@@ -100,7 +99,7 @@ namespace :benchmark do
100
99
  load "protobuf/zmq.rb"
101
100
  ::Protobuf::Rpc::Connector.connector_for_client(true)
102
101
  StubServer.new(:port => 9399, :server => Protobuf::Rpc::Zmq::Server) do |server|
103
- client = Spec::Proto::TestService.client(:async => false, :port => 9399)
102
+ client = ::Test::ResourceService.client(:async => false, :port => 9399)
104
103
 
105
104
  benchmark_wrapper(global_bench) do |bench|
106
105
  bench.report("ZS / ZC") do
@@ -132,7 +131,7 @@ namespace :benchmark do
132
131
  args.with_defaults(:number => 1000, :profile_output => "/tmp/profiler_new_#{Time.now.to_i}")
133
132
  create_params = { :name => "The name that we set", :date_created => Time.now.to_i, :status => 2 }
134
133
  ::PerfTools::CpuProfiler.start(args[:profile_output]) do
135
- args[:number].to_i.times { Spec::Proto::Resource.new(create_params) }
134
+ args[:number].to_i.times { Test::Resource.new(create_params) }
136
135
  end
137
136
 
138
137
  puts args[:profile_output]
@@ -1,23 +1,23 @@
1
1
  require 'spec_helper'
2
- require 'spec/proto/test_service_impl'
2
+ require 'spec/support/test/resource_service'
3
3
 
4
4
  describe 'Functional EventMachine Client' do
5
5
  before(:each) do
6
6
  load 'protobuf/evented.rb'
7
7
  ::Protobuf::Rpc::Connector.connector_for_client(true)
8
- ::Spec::Proto::TestService.configure(::Spec::Proto::TestService::DEFAULT_LOCATION)
8
+ ::Test::ResourceService.configure(::Test::ResourceService::DEFAULT_LOCATION)
9
9
  end
10
10
 
11
11
  it 'runs fine when required fields are set' do
12
12
  expect {
13
13
  EventMachine.fiber_run do
14
14
  StubServer.new do |server|
15
- client = ::Spec::Proto::TestService.client(:async => false, :timeout => 5)
15
+ client = ::Test::ResourceService.client(:async => false, :timeout => 5)
16
16
 
17
17
  client.find(:name => 'Test Name', :active => true) do |c|
18
18
  c.on_success do |succ|
19
19
  succ.name.should eq("Test Name")
20
- succ.status.should eq(::Spec::Proto::StatusType::ENABLED)
20
+ succ.status.should eq(::Test::StatusType::ENABLED)
21
21
  end
22
22
 
23
23
  c.on_failure do |err|
@@ -34,8 +34,8 @@ describe 'Functional EventMachine Client' do
34
34
  error = nil
35
35
  EventMachine.fiber_run do
36
36
  StubServer.new do |server|
37
- request = ::Spec::Proto::ResourceFindRequest.new(:active => true)
38
- client = ::Spec::Proto::TestService.client(:async => false)
37
+ request = ::Test::ResourceFindRequest.new(:active => true)
38
+ client = ::Test::ResourceService.client(:async => false)
39
39
 
40
40
  client.find(request) do |c|
41
41
  c.on_success { raise "shouldn't pass"}
@@ -44,15 +44,15 @@ describe 'Functional EventMachine Client' do
44
44
  end
45
45
  EM.stop
46
46
  end
47
- error.message.should =~ /ResourceFindRequest.*fields.*improperly set.*"name"/
47
+ error.message.should =~ /ResourceFindRequest.*fields.*improperly set/
48
48
  end
49
49
 
50
50
  it 'calls the on_failure callback when the request type is wrong' do
51
51
  error = nil
52
52
  EventMachine.fiber_run do
53
53
  StubServer.new do |server|
54
- request = ::Spec::Proto::Resource.new(:name => 'Test Name')
55
- client = ::Spec::Proto::TestService.client(:async => false)
54
+ request = ::Test::Resource.new(:name => 'Test Name')
55
+ client = ::Test::ResourceService.client(:async => false)
56
56
 
57
57
  client.find(request) do |c|
58
58
  c.on_success { raise "shouldn't pass"}
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require 'spec/proto/test_service_impl'
2
+ require 'spec/support/test/resource_service'
3
3
 
4
4
  describe 'Functional Socket Client' do
5
5
  before(:all) do
@@ -18,12 +18,12 @@ describe 'Functional Socket Client' do
18
18
 
19
19
  it 'runs fine when required fields are set' do
20
20
  expect {
21
- client = ::Spec::Proto::TestService.client(:async => false)
21
+ client = ::Test::ResourceService.client(:async => false)
22
22
 
23
23
  client.find(:name => 'Test Name', :active => true) do |c|
24
24
  c.on_success do |succ|
25
25
  succ.name.should eq("Test Name")
26
- succ.status.should eq(::Spec::Proto::StatusType::ENABLED)
26
+ succ.status.should eq(::Test::StatusType::ENABLED)
27
27
  end
28
28
 
29
29
  c.on_failure do |err|
@@ -35,20 +35,20 @@ describe 'Functional Socket Client' do
35
35
 
36
36
  it 'calls the on_failure callback when a message is malformed' do
37
37
  error = nil
38
- request = ::Spec::Proto::ResourceFindRequest.new(:active => true)
39
- client = ::Spec::Proto::TestService.client(:async => false)
38
+ request = ::Test::ResourceFindRequest.new(:active => true)
39
+ client = ::Test::ResourceService.client(:async => false)
40
40
 
41
41
  client.find(request) do |c|
42
42
  c.on_success { raise "shouldn't pass"}
43
43
  c.on_failure {|e| error = e}
44
44
  end
45
- error.message.should =~ /ResourceFindRequest.*fields.*improperly set.*"name"/
45
+ error.message.should =~ /ResourceFindRequest.*fields.*improperly set/
46
46
  end
47
47
 
48
48
  it 'calls the on_failure callback when the request type is wrong' do
49
49
  error = nil
50
- request = ::Spec::Proto::Resource.new(:name => 'Test Name')
51
- client = ::Spec::Proto::TestService.client(:async => false)
50
+ request = ::Test::Resource.new(:name => 'Test Name')
51
+ client = ::Test::ResourceService.client(:async => false)
52
52
 
53
53
  client.find(request) do |c|
54
54
  c.on_success { raise "shouldn't pass"}
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require 'spec/proto/test_service_impl'
2
+ require 'spec/support/test/resource_service'
3
3
 
4
4
  describe 'Functional ZMQ Client' do
5
5
  before(:all) do
@@ -18,12 +18,12 @@ describe 'Functional ZMQ Client' do
18
18
 
19
19
  it 'runs fine when required fields are set' do
20
20
  expect {
21
- client = ::Spec::Proto::TestService.client(:async => false)
21
+ client = ::Test::ResourceService.client(:async => false)
22
22
 
23
23
  client.find(:name => 'Test Name', :active => true) do |c|
24
24
  c.on_success do |succ|
25
25
  succ.name.should eq("Test Name")
26
- succ.status.should eq(::Spec::Proto::StatusType::ENABLED)
26
+ succ.status.should eq(::Test::StatusType::ENABLED)
27
27
  end
28
28
 
29
29
  c.on_failure do |err|
@@ -35,20 +35,20 @@ describe 'Functional ZMQ Client' do
35
35
 
36
36
  it 'calls the on_failure callback when a message is malformed' do
37
37
  error = nil
38
- request = ::Spec::Proto::ResourceFindRequest.new(:active => true)
39
- client = ::Spec::Proto::TestService.client(:async => false)
38
+ request = ::Test::ResourceFindRequest.new(:active => true)
39
+ client = ::Test::ResourceService.client(:async => false)
40
40
 
41
41
  client.find(request) do |c|
42
42
  c.on_success { raise "shouldn't pass"}
43
43
  c.on_failure {|e| error = e}
44
44
  end
45
- error.message.should =~ /ResourceFindRequest.*fields.*improperly set.*"name"/
45
+ error.message.should =~ /ResourceFindRequest.*fields.*improperly set/
46
46
  end
47
47
 
48
48
  it 'calls the on_failure callback when the request type is wrong' do
49
49
  error = nil
50
- request = ::Spec::Proto::Resource.new(:name => 'Test Name')
51
- client = ::Spec::Proto::TestService.client(:async => false)
50
+ request = ::Test::Resource.new(:name => 'Test Name')
51
+ client = ::Test::ResourceService.client(:async => false)
52
52
 
53
53
  client.find(request) do |c|
54
54
  c.on_success { raise "shouldn't pass"}
@@ -92,8 +92,7 @@ describe ::Protobuf::CLI do
92
92
 
93
93
  it 'sets both request and serialization pausing to false' do
94
94
  described_class.start(args)
95
- ::Protobuf.gc_pause_server_request.should be_false
96
- ::Protobuf.gc_pause_server_serialization.should be_false
95
+ ::Protobuf.gc_pause_server_request?.should be_false
97
96
  end
98
97
  end
99
98
 
@@ -102,18 +101,38 @@ describe ::Protobuf::CLI do
102
101
 
103
102
  it 'sets the configuration option to GC pause server request' do
104
103
  described_class.start(args)
105
- ::Protobuf.gc_pause_server_request.should be_true
104
+ ::Protobuf.gc_pause_server_request?.should be_true
106
105
  end
107
106
  end
107
+ end
108
108
 
109
- context 'serialization pausing' do
110
- let(:test_args) { [ '--gc_pause_serialization' ] }
111
-
112
- it 'sets the configuration option to GC pause server serializations' do
113
- described_class.start(args)
114
- ::Protobuf.gc_pause_server_serialization.should be_true
115
- end
116
- end
109
+ context 'deprecation options' do
110
+ context 'when not given' do
111
+ let(:test_args) { [] }
112
+
113
+ it 'sets the deprecation warning flag to its default value' do
114
+ described_class.start(args)
115
+ ::Protobuf.print_deprecation_warnings?.should be_true
116
+ end
117
+ end
118
+
119
+ context 'when enabled' do
120
+ let(:test_args) { [ '--print_deprecation_warnings' ] }
121
+
122
+ it 'sets the deprecation warning flag to true' do
123
+ described_class.start(args)
124
+ ::Protobuf.print_deprecation_warnings?.should be_true
125
+ end
126
+ end
127
+
128
+ context 'when disabled' do
129
+ let(:test_args) { [ '--no-print_deprecation_warnings' ] }
130
+
131
+ it 'sets the deprecation warning flag to false' do
132
+ described_class.start(args)
133
+ ::Protobuf.print_deprecation_warnings?.should be_false
134
+ end
135
+ end
117
136
  end
118
137
 
119
138
  context 'run modes' do
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ describe Protobuf::Enum do
4
+ let(:name) { :THREE }
5
+ let(:tag) { 3 }
6
+
7
+ before(:all) do
8
+ Test::EnumTestType.define(name, tag)
9
+ end
10
+
11
+ describe '.define' do
12
+ it 'defines a constant enum value on the parent class' do
13
+ Test::EnumTestType.constants.should include(name)
14
+ Test::EnumTestType::THREE.should be_a(Protobuf::EnumValue)
15
+ end
16
+ end
17
+
18
+ describe '.fetch' do
19
+ context 'when value is an EnumValue' do
20
+ it 'responds with that object' do
21
+ Test::EnumTestType.fetch(Test::EnumTestType::THREE).should eq Test::EnumTestType::THREE
22
+ end
23
+ end
24
+
25
+ context 'when value can be coerced to a symbol' do
26
+ it 'fetches based on the symbol name value' do
27
+ Test::EnumTestType.fetch("ONE").should eq Test::EnumTestType::ONE
28
+ end
29
+ end
30
+
31
+ context 'when value can be coerced to an integer' do
32
+ it 'fetches based on the integer tag value' do
33
+ Test::EnumTestType.fetch(3.0).should eq Test::EnumTestType::THREE
34
+ end
35
+ end
36
+
37
+ context 'when value is not an applicable type' do
38
+ it 'returns a nil' do
39
+ Test::EnumTestType.fetch(Test::Resource.new).should be_nil
40
+ end
41
+ end
42
+ end
43
+
44
+ describe '.enum_by_value' do
45
+ it 'gets the EnumValue corresponding to the given value (tag)' do
46
+ Test::EnumTestType.enum_by_value(tag).should eq Test::EnumTestType::THREE
47
+ end
48
+ end
49
+
50
+ describe '.name_by_value' do
51
+ it 'gets the name of the enum corresponding to the given value (tag)' do
52
+ Test::EnumTestType.name_by_value(tag).should eq name
53
+ end
54
+ end
55
+
56
+ describe '.valid_tag?' do
57
+ context 'when tag is defined' do
58
+ specify { Test::EnumTestType.valid_tag?(tag).should be_true }
59
+ end
60
+
61
+ context 'when tag is not defined' do
62
+ specify { Test::EnumTestType.valid_tag?(300).should be_false }
63
+ end
64
+ end
65
+
66
+ describe '.value_by_name' do
67
+ it 'gets the EnumValue corresponding to the given name' do
68
+ Test::EnumTestType.value_by_name(name).should eq Test::EnumTestType::THREE
69
+ end
70
+ end
71
+
72
+ describe '.values' do
73
+ it 'provides a hash of defined EnumValues' do
74
+ Test::EnumTestType.values.should eq({
75
+ :ONE => Test::EnumTestType::ONE,
76
+ :TWO => Test::EnumTestType::TWO,
77
+ :THREE => Test::EnumTestType::THREE
78
+ })
79
+ end
80
+ end
81
+
82
+ context 'when coercing from enum' do
83
+ subject { Test::StatusType::PENDING }
84
+ it { should eq(0) }
85
+ end
86
+
87
+ context 'when coercing from integer' do
88
+ specify { 0.should eq(Test::StatusType::PENDING) }
89
+ end
90
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Protobuf::EnumValue do
4
+
5
+ subject { Test::EnumTestType::ONE }
6
+ its(:parent_class) { should eq Test::EnumTestType }
7
+ its(:name) { should eq :ONE }
8
+ its(:value) { should eq 1 }
9
+ its(:to_hash_value) { should eq 1 }
10
+ its(:to_s) { should eq "ONE" }
11
+ its(:inspect) { should eq '#<Protobuf::EnumValue Test::EnumTestType::ONE=1>' }
12
+
13
+ end
@@ -7,7 +7,7 @@ end
7
7
  describe Protobuf::Encoder do
8
8
  describe '#encode' do
9
9
  context "when there's no value for a required field" do
10
- let(:message) { ::Spec::Proto::Resource.new }
10
+ let(:message) { ::Test::Resource.new }
11
11
  let(:stream) { StringIO.new }
12
12
  it "raises a 'message not initialized' error" do
13
13
  expect {
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe Protobuf::Message do
4
+
5
+ describe '#to_hash' do
6
+ context 'generating values for an ENUM field' do
7
+ it 'converts the enum to its tag representation' do
8
+ hash = Test::EnumTestMessage.new(:non_default_enum => :TWO).to_hash
9
+ hash.should eq({ :non_default_enum => 2 })
10
+ end
11
+
12
+ it 'does not populate default values' do
13
+ hash = Test::EnumTestMessage.new.to_hash
14
+ hash.should eq(Hash.new)
15
+ end
16
+
17
+ it 'converts repeated enum fields to an array of the tags' do
18
+ hash = Test::EnumTestMessage.new(:repeated_enums => [ :ONE, :TWO, :TWO, :ONE ]).to_hash
19
+ hash.should eq({ :repeated_enums => [ 1, 2, 2, 1 ] })
20
+ end
21
+ end
22
+
23
+ context 'generating values for a Message field' do
24
+ it 'recursively hashes field messages' do
25
+ hash = Test::Nested.new({ :resource => { :name => 'Nested' } }).to_hash
26
+ hash.should eq({ :resource => { :name => 'Nested' } })
27
+ end
28
+
29
+ it 'recursively hashes a repeated set of messages' do
30
+ proto = Test::Nested.new(:multiple_resources => [
31
+ Test::Resource.new(:name => 'Resource 1'),
32
+ Test::Resource.new(:name => 'Resource 2')
33
+ ])
34
+
35
+ proto.to_hash.should eq({ :multiple_resources => [ { :name => 'Resource 1' },
36
+ { :name => 'Resource 2' } ] })
37
+
38
+ end
39
+ end
40
+ end
41
+
42
+ describe '#to_json' do
43
+ subject do
44
+ ::Test::ResourceFindRequest.new({ :name => 'Test Name', :active => false })
45
+ end
46
+
47
+ its(:to_json) { should eq '{"name":"Test Name","active":false}' }
48
+ end
49
+
50
+ end