protobuf-cucumber 3.10.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +21 -0
- data/.rubocop.yml +70 -0
- data/.rubocop_todo.yml +145 -0
- data/.travis.yml +40 -0
- data/.yardopts +5 -0
- data/CHANGES.md +344 -0
- data/CONTRIBUTING.md +16 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +64 -0
- data/bin/protoc-gen-ruby +22 -0
- data/bin/rpc_server +5 -0
- data/install-protobuf.sh +28 -0
- data/lib/protobuf.rb +129 -0
- data/lib/protobuf/cli.rb +257 -0
- data/lib/protobuf/code_generator.rb +120 -0
- data/lib/protobuf/decoder.rb +28 -0
- data/lib/protobuf/deprecation.rb +117 -0
- data/lib/protobuf/descriptors.rb +3 -0
- data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +62 -0
- data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +301 -0
- data/lib/protobuf/encoder.rb +11 -0
- data/lib/protobuf/enum.rb +365 -0
- data/lib/protobuf/exceptions.rb +9 -0
- data/lib/protobuf/field.rb +74 -0
- data/lib/protobuf/field/base_field.rb +380 -0
- data/lib/protobuf/field/base_field_object_definitions.rb +504 -0
- data/lib/protobuf/field/bool_field.rb +64 -0
- data/lib/protobuf/field/bytes_field.rb +78 -0
- data/lib/protobuf/field/double_field.rb +25 -0
- data/lib/protobuf/field/enum_field.rb +61 -0
- data/lib/protobuf/field/field_array.rb +104 -0
- data/lib/protobuf/field/field_hash.rb +122 -0
- data/lib/protobuf/field/fixed32_field.rb +25 -0
- data/lib/protobuf/field/fixed64_field.rb +28 -0
- data/lib/protobuf/field/float_field.rb +43 -0
- data/lib/protobuf/field/int32_field.rb +21 -0
- data/lib/protobuf/field/int64_field.rb +34 -0
- data/lib/protobuf/field/integer_field.rb +23 -0
- data/lib/protobuf/field/message_field.rb +51 -0
- data/lib/protobuf/field/sfixed32_field.rb +27 -0
- data/lib/protobuf/field/sfixed64_field.rb +28 -0
- data/lib/protobuf/field/signed_integer_field.rb +29 -0
- data/lib/protobuf/field/sint32_field.rb +21 -0
- data/lib/protobuf/field/sint64_field.rb +21 -0
- data/lib/protobuf/field/string_field.rb +51 -0
- data/lib/protobuf/field/uint32_field.rb +21 -0
- data/lib/protobuf/field/uint64_field.rb +21 -0
- data/lib/protobuf/field/varint_field.rb +77 -0
- data/lib/protobuf/generators/base.rb +85 -0
- data/lib/protobuf/generators/enum_generator.rb +39 -0
- data/lib/protobuf/generators/extension_generator.rb +27 -0
- data/lib/protobuf/generators/field_generator.rb +193 -0
- data/lib/protobuf/generators/file_generator.rb +262 -0
- data/lib/protobuf/generators/group_generator.rb +122 -0
- data/lib/protobuf/generators/message_generator.rb +104 -0
- data/lib/protobuf/generators/option_generator.rb +17 -0
- data/lib/protobuf/generators/printable.rb +160 -0
- data/lib/protobuf/generators/service_generator.rb +50 -0
- data/lib/protobuf/lifecycle.rb +33 -0
- data/lib/protobuf/logging.rb +39 -0
- data/lib/protobuf/message.rb +260 -0
- data/lib/protobuf/message/fields.rb +233 -0
- data/lib/protobuf/message/serialization.rb +85 -0
- data/lib/protobuf/optionable.rb +70 -0
- data/lib/protobuf/rpc/buffer.rb +78 -0
- data/lib/protobuf/rpc/client.rb +140 -0
- data/lib/protobuf/rpc/connectors/base.rb +221 -0
- data/lib/protobuf/rpc/connectors/ping.rb +89 -0
- data/lib/protobuf/rpc/connectors/socket.rb +78 -0
- data/lib/protobuf/rpc/connectors/zmq.rb +319 -0
- data/lib/protobuf/rpc/dynamic_discovery.pb.rb +50 -0
- data/lib/protobuf/rpc/env.rb +60 -0
- data/lib/protobuf/rpc/error.rb +28 -0
- data/lib/protobuf/rpc/error/client_error.rb +31 -0
- data/lib/protobuf/rpc/error/server_error.rb +43 -0
- data/lib/protobuf/rpc/middleware.rb +25 -0
- data/lib/protobuf/rpc/middleware/exception_handler.rb +40 -0
- data/lib/protobuf/rpc/middleware/logger.rb +95 -0
- data/lib/protobuf/rpc/middleware/request_decoder.rb +79 -0
- data/lib/protobuf/rpc/middleware/response_encoder.rb +83 -0
- data/lib/protobuf/rpc/middleware/runner.rb +18 -0
- data/lib/protobuf/rpc/rpc.pb.rb +64 -0
- data/lib/protobuf/rpc/rpc_method.rb +16 -0
- data/lib/protobuf/rpc/server.rb +39 -0
- data/lib/protobuf/rpc/servers/socket/server.rb +121 -0
- data/lib/protobuf/rpc/servers/socket/worker.rb +56 -0
- data/lib/protobuf/rpc/servers/socket_runner.rb +46 -0
- data/lib/protobuf/rpc/servers/zmq/broker.rb +194 -0
- data/lib/protobuf/rpc/servers/zmq/server.rb +321 -0
- data/lib/protobuf/rpc/servers/zmq/util.rb +48 -0
- data/lib/protobuf/rpc/servers/zmq/worker.rb +105 -0
- data/lib/protobuf/rpc/servers/zmq_runner.rb +70 -0
- data/lib/protobuf/rpc/service.rb +172 -0
- data/lib/protobuf/rpc/service_directory.rb +261 -0
- data/lib/protobuf/rpc/service_dispatcher.rb +45 -0
- data/lib/protobuf/rpc/service_filters.rb +250 -0
- data/lib/protobuf/rpc/stat.rb +119 -0
- data/lib/protobuf/socket.rb +21 -0
- data/lib/protobuf/tasks.rb +1 -0
- data/lib/protobuf/tasks/compile.rake +80 -0
- data/lib/protobuf/varint.rb +20 -0
- data/lib/protobuf/varint_pure.rb +31 -0
- data/lib/protobuf/version.rb +3 -0
- data/lib/protobuf/wire_type.rb +10 -0
- data/lib/protobuf/zmq.rb +21 -0
- data/profile.html +5103 -0
- data/proto/dynamic_discovery.proto +44 -0
- data/proto/google/protobuf/compiler/plugin.proto +147 -0
- data/proto/google/protobuf/descriptor.proto +779 -0
- data/proto/rpc.proto +69 -0
- data/protobuf-cucumber.gemspec +57 -0
- data/spec/benchmark/tasks.rb +143 -0
- data/spec/bin/protoc-gen-ruby_spec.rb +23 -0
- data/spec/encoding/all_types_spec.rb +103 -0
- data/spec/encoding/extreme_values_spec.rb +0 -0
- data/spec/functional/class_inheritance_spec.rb +52 -0
- data/spec/functional/code_generator_spec.rb +58 -0
- data/spec/functional/socket_server_spec.rb +59 -0
- data/spec/functional/zmq_server_spec.rb +105 -0
- data/spec/lib/protobuf/cli_spec.rb +317 -0
- data/spec/lib/protobuf/code_generator_spec.rb +87 -0
- data/spec/lib/protobuf/enum_spec.rb +307 -0
- data/spec/lib/protobuf/field/bool_field_spec.rb +91 -0
- data/spec/lib/protobuf/field/double_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/enum_field_spec.rb +44 -0
- data/spec/lib/protobuf/field/field_array_spec.rb +105 -0
- data/spec/lib/protobuf/field/field_hash_spec.rb +168 -0
- data/spec/lib/protobuf/field/fixed32_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/fixed64_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/float_field_spec.rb +90 -0
- data/spec/lib/protobuf/field/int32_field_spec.rb +120 -0
- data/spec/lib/protobuf/field/int64_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/message_field_spec.rb +132 -0
- data/spec/lib/protobuf/field/sfixed32_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/sfixed64_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/sint32_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/sint64_field_spec.rb +9 -0
- data/spec/lib/protobuf/field/string_field_spec.rb +79 -0
- data/spec/lib/protobuf/field/uint32_field_spec.rb +7 -0
- data/spec/lib/protobuf/field/uint64_field_spec.rb +7 -0
- data/spec/lib/protobuf/field_spec.rb +192 -0
- data/spec/lib/protobuf/generators/base_spec.rb +154 -0
- data/spec/lib/protobuf/generators/enum_generator_spec.rb +82 -0
- data/spec/lib/protobuf/generators/extension_generator_spec.rb +42 -0
- data/spec/lib/protobuf/generators/field_generator_spec.rb +197 -0
- data/spec/lib/protobuf/generators/file_generator_spec.rb +119 -0
- data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
- data/spec/lib/protobuf/generators/service_generator_spec.rb +99 -0
- data/spec/lib/protobuf/lifecycle_spec.rb +94 -0
- data/spec/lib/protobuf/message_spec.rb +944 -0
- data/spec/lib/protobuf/optionable_spec.rb +265 -0
- data/spec/lib/protobuf/rpc/client_spec.rb +66 -0
- data/spec/lib/protobuf/rpc/connectors/base_spec.rb +226 -0
- data/spec/lib/protobuf/rpc/connectors/ping_spec.rb +69 -0
- data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +34 -0
- data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +110 -0
- data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
- data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
- data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
- data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +91 -0
- data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +38 -0
- data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +43 -0
- data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +55 -0
- data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +35 -0
- data/spec/lib/protobuf/rpc/service_directory_spec.rb +293 -0
- data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +35 -0
- data/spec/lib/protobuf/rpc/service_filters_spec.rb +517 -0
- data/spec/lib/protobuf/rpc/service_spec.rb +162 -0
- data/spec/lib/protobuf/rpc/stat_spec.rb +101 -0
- data/spec/lib/protobuf/varint_spec.rb +29 -0
- data/spec/lib/protobuf_spec.rb +105 -0
- data/spec/spec_helper.rb +42 -0
- data/spec/support/all.rb +6 -0
- data/spec/support/packed_field.rb +23 -0
- data/spec/support/protos/all_types.data.bin +0 -0
- data/spec/support/protos/all_types.data.txt +119 -0
- data/spec/support/protos/enum.pb.rb +63 -0
- data/spec/support/protos/enum.proto +37 -0
- data/spec/support/protos/extreme_values.data.bin +0 -0
- data/spec/support/protos/google_unittest.bin +0 -0
- data/spec/support/protos/google_unittest.pb.rb +798 -0
- data/spec/support/protos/google_unittest.proto +884 -0
- data/spec/support/protos/google_unittest_custom_options.bin +0 -0
- data/spec/support/protos/google_unittest_custom_options.pb.rb +361 -0
- data/spec/support/protos/google_unittest_custom_options.proto +424 -0
- data/spec/support/protos/google_unittest_import.pb.rb +55 -0
- data/spec/support/protos/google_unittest_import.proto +73 -0
- data/spec/support/protos/google_unittest_import_public.pb.rb +31 -0
- data/spec/support/protos/google_unittest_import_public.proto +41 -0
- data/spec/support/protos/map-test.bin +157 -0
- data/spec/support/protos/map-test.pb.rb +85 -0
- data/spec/support/protos/map-test.proto +68 -0
- data/spec/support/protos/multi_field_extensions.pb.rb +59 -0
- data/spec/support/protos/multi_field_extensions.proto +35 -0
- data/spec/support/protos/resource.pb.rb +172 -0
- data/spec/support/protos/resource.proto +137 -0
- data/spec/support/resource_service.rb +23 -0
- data/spec/support/server.rb +65 -0
- data/spec/support/test_app_file.rb +2 -0
- data/varint_prof.rb +82 -0
- metadata +579 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'protobuf/rpc/servers/socket_runner'
|
3
|
+
require 'protobuf/socket'
|
4
|
+
require SUPPORT_PATH.join('resource_service')
|
5
|
+
|
6
|
+
RSpec.describe Protobuf::Rpc::Socket::Server do
|
7
|
+
before(:each) do
|
8
|
+
load 'protobuf/socket.rb'
|
9
|
+
end
|
10
|
+
|
11
|
+
before(:all) do
|
12
|
+
load 'protobuf/socket.rb'
|
13
|
+
Thread.abort_on_exception = true
|
14
|
+
@options = OpenStruct.new(:host => "127.0.0.1", :port => 9399, :backlog => 100, :threshold => 100)
|
15
|
+
@runner = ::Protobuf::Rpc::SocketRunner.new(@options)
|
16
|
+
@server = @runner.instance_variable_get(:@server)
|
17
|
+
@server_thread = Thread.new(@runner, &:run)
|
18
|
+
Thread.pass until @server.running?
|
19
|
+
end
|
20
|
+
|
21
|
+
after(:all) do
|
22
|
+
@server.stop
|
23
|
+
@server_thread.join
|
24
|
+
end
|
25
|
+
|
26
|
+
it "Runner provides a stop method" do
|
27
|
+
expect(@runner).to respond_to(:stop)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "provides a stop method" do
|
31
|
+
expect(@server).to respond_to(:stop)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "signals the Server is running" do
|
35
|
+
expect(@server).to be_running
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'protobuf/rpc/servers/zmq/server'
|
3
|
+
|
4
|
+
RSpec.describe Protobuf::Rpc::Zmq::Server do
|
5
|
+
subject { described_class.new(options) }
|
6
|
+
|
7
|
+
let(:options) do
|
8
|
+
{
|
9
|
+
:host => '127.0.0.1',
|
10
|
+
:port => 9399,
|
11
|
+
:worker_port => 9400,
|
12
|
+
:workers_only => true,
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
before do
|
17
|
+
load 'protobuf/zmq.rb'
|
18
|
+
end
|
19
|
+
|
20
|
+
after do
|
21
|
+
subject.teardown
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '.running?' do
|
25
|
+
it 'returns true if running' do
|
26
|
+
subject.instance_variable_set(:@running, true)
|
27
|
+
expect(subject.running?).to be true
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'returns false if not running' do
|
31
|
+
subject.instance_variable_set(:@running, false)
|
32
|
+
expect(subject.running?).to be false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '.stop' do
|
37
|
+
it 'sets running to false' do
|
38
|
+
subject.instance_variable_set(:@workers, [])
|
39
|
+
subject.stop
|
40
|
+
expect(subject.instance_variable_get(:@running)).to be false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class UtilTest
|
4
|
+
include ::Protobuf::Rpc::Zmq::Util
|
5
|
+
end
|
6
|
+
|
7
|
+
RSpec.describe ::Protobuf::Rpc::Zmq::Util do
|
8
|
+
before(:each) do
|
9
|
+
load 'protobuf/zmq.rb'
|
10
|
+
end
|
11
|
+
|
12
|
+
subject { UtilTest.new }
|
13
|
+
describe '#zmq_error_check' do
|
14
|
+
it 'raises when the error code is less than 0' do
|
15
|
+
expect do
|
16
|
+
subject.zmq_error_check(-1, :test)
|
17
|
+
end.to raise_error(/test/)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'retrieves the error string from ZeroMQ' do
|
21
|
+
allow(ZMQ::Util).to receive(:error_string).and_return('an error from zmq')
|
22
|
+
expect do
|
23
|
+
subject.zmq_error_check(-1, :test)
|
24
|
+
end.to raise_error(RuntimeError, /an error from zmq/i)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'does nothing if the error code is > 0' do
|
28
|
+
expect do
|
29
|
+
subject.zmq_error_check(1, :test)
|
30
|
+
end.to_not raise_error
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'does nothing if the error code is == 0' do
|
34
|
+
expect do
|
35
|
+
subject.zmq_error_check(0, :test)
|
36
|
+
end.to_not raise_error
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#log_signature' do
|
41
|
+
it 'returns the signature for the log' do
|
42
|
+
expect(subject.log_signature).to include('server', 'UtilTest')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '.resolve_ip' do
|
47
|
+
it 'resolves ips' do
|
48
|
+
expect(subject.resolve_ip('127.0.0.1')).to eq('127.0.0.1')
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'resolves non ips' do
|
52
|
+
expect(subject.resolve_ip('localhost')).to eq('127.0.0.1')
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe ::Protobuf::Rpc::Zmq::Worker do
|
4
|
+
before(:each) do
|
5
|
+
load 'protobuf/zmq.rb'
|
6
|
+
|
7
|
+
fake_socket = double
|
8
|
+
expect(fake_socket).to receive(:connect).and_return(0)
|
9
|
+
expect(fake_socket).to receive(:send_string).and_return(0)
|
10
|
+
|
11
|
+
fake_context = double
|
12
|
+
expect(fake_context).to receive(:socket).and_return(fake_socket)
|
13
|
+
expect(::ZMQ::Context).to receive(:new).and_return(fake_context)
|
14
|
+
end
|
15
|
+
|
16
|
+
subject do
|
17
|
+
described_class.new(:host => '127.0.0.1', :port => 9400)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#run' do
|
21
|
+
# not tested via unit tests
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#handle_request' do
|
25
|
+
# not tested via unit tests
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#initialize_buffers' do
|
29
|
+
# not tested via unit tests
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#send_data' do
|
33
|
+
# not tested via unit tests
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,293 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require 'protobuf/rpc/service_directory'
|
4
|
+
|
5
|
+
RSpec.describe ::Protobuf::Rpc::ServiceDirectory do
|
6
|
+
subject { described_class.instance }
|
7
|
+
|
8
|
+
let(:echo_server) do
|
9
|
+
::Protobuf::Rpc::DynamicDiscovery::Server.new(
|
10
|
+
:uuid => 'echo',
|
11
|
+
:address => '127.0.0.1',
|
12
|
+
:port => '1111',
|
13
|
+
:ttl => 10,
|
14
|
+
:services => %w(EchoService),
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:hello_server) do
|
19
|
+
::Protobuf::Rpc::DynamicDiscovery::Server.new(
|
20
|
+
:uuid => "hello",
|
21
|
+
:address => '127.0.0.1',
|
22
|
+
:port => "1112",
|
23
|
+
:ttl => 10,
|
24
|
+
:services => %w(HelloService),
|
25
|
+
)
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:hello_server_with_short_ttl) do
|
29
|
+
::Protobuf::Rpc::DynamicDiscovery::Server.new(
|
30
|
+
:uuid => "hello_server_with_short_ttl",
|
31
|
+
:address => '127.0.0.1',
|
32
|
+
:port => '1113',
|
33
|
+
:ttl => 1,
|
34
|
+
:services => %w(HelloService),
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
let(:combo_server) do
|
39
|
+
::Protobuf::Rpc::DynamicDiscovery::Server.new(
|
40
|
+
:uuid => "combo",
|
41
|
+
:address => '127.0.0.1',
|
42
|
+
:port => '1114',
|
43
|
+
:ttl => 10,
|
44
|
+
:services => %w(HelloService EchoService),
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
before(:all) do
|
49
|
+
@address = "127.0.0.1"
|
50
|
+
@port = 33333
|
51
|
+
@socket = UDPSocket.new
|
52
|
+
EchoService = Class.new
|
53
|
+
|
54
|
+
described_class.address = @address
|
55
|
+
described_class.port = @port
|
56
|
+
end
|
57
|
+
|
58
|
+
def expect_event_trigger(event)
|
59
|
+
expect(::ActiveSupport::Notifications).to receive(:instrument)
|
60
|
+
.with(event, hash_including(:listing => an_instance_of(::Protobuf::Rpc::ServiceDirectory::Listing))).once
|
61
|
+
end
|
62
|
+
|
63
|
+
def send_beacon(type, server)
|
64
|
+
type = type.to_s.upcase
|
65
|
+
beacon = ::Protobuf::Rpc::DynamicDiscovery::Beacon.new(
|
66
|
+
:server => server,
|
67
|
+
:beacon_type => ::Protobuf::Rpc::DynamicDiscovery::BeaconType.fetch(type),
|
68
|
+
)
|
69
|
+
|
70
|
+
@socket.send(beacon.encode, 0, @address, @port)
|
71
|
+
sleep 0.01 # give the service directory time to process the beacon
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should be a singleton" do
|
75
|
+
expect(subject).to be_a_kind_of(Singleton)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should be configured to listen to address 127.0.0.1" do
|
79
|
+
expect(described_class.address).to eq '127.0.0.1'
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should be configured to listen to port 33333" do
|
83
|
+
expect(described_class.port).to eq 33333
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should defer .start to the instance#start" do
|
87
|
+
expect(described_class.instance).to receive(:start)
|
88
|
+
described_class.start
|
89
|
+
end
|
90
|
+
|
91
|
+
it "should yeild itself to blocks passed to .start" do
|
92
|
+
allow(described_class.instance).to receive(:start)
|
93
|
+
expect { |b| described_class.start(&b) }.to yield_with_args(described_class)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should defer .stop to the instance#stop" do
|
97
|
+
expect(described_class.instance).to receive(:stop)
|
98
|
+
described_class.stop
|
99
|
+
end
|
100
|
+
|
101
|
+
context "stopped" do
|
102
|
+
before { subject.stop }
|
103
|
+
|
104
|
+
describe "#lookup" do
|
105
|
+
it "should return nil" do
|
106
|
+
send_beacon(:heartbeat, echo_server)
|
107
|
+
expect(subject.lookup("EchoService")).to be_nil
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "#restart" do
|
112
|
+
it "should start the service" do
|
113
|
+
subject.restart
|
114
|
+
expect(subject).to be_running
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
describe "#running" do
|
119
|
+
it "should be false" do
|
120
|
+
expect(subject).to_not be_running
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "#stop" do
|
125
|
+
it "has no effect" do
|
126
|
+
subject.stop
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
context "started" do
|
132
|
+
before { subject.start }
|
133
|
+
after { subject.stop }
|
134
|
+
|
135
|
+
specify { expect(subject).to be_running }
|
136
|
+
|
137
|
+
it "should trigger added events" do
|
138
|
+
expect_event_trigger("directory.listing.added")
|
139
|
+
send_beacon(:heartbeat, echo_server)
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should trigger updated events" do
|
143
|
+
send_beacon(:heartbeat, echo_server)
|
144
|
+
expect_event_trigger("directory.listing.updated")
|
145
|
+
send_beacon(:heartbeat, echo_server)
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should trigger removed events" do
|
149
|
+
send_beacon(:heartbeat, echo_server)
|
150
|
+
expect_event_trigger("directory.listing.removed")
|
151
|
+
send_beacon(:flatline, echo_server)
|
152
|
+
end
|
153
|
+
|
154
|
+
describe "#all_listings_for" do
|
155
|
+
context "when listings are present" do
|
156
|
+
it "returns all listings for a given service" do
|
157
|
+
send_beacon(:heartbeat, hello_server)
|
158
|
+
send_beacon(:heartbeat, combo_server)
|
159
|
+
|
160
|
+
expect(subject.all_listings_for("HelloService").size).to eq(2)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
context "when no listings are present" do
|
165
|
+
it "returns and empty array" do
|
166
|
+
expect(subject.all_listings_for("HelloService").size).to eq(0)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe "#each_listing" do
|
172
|
+
it "should yield to a block for each listing" do
|
173
|
+
send_beacon(:heartbeat, hello_server)
|
174
|
+
send_beacon(:heartbeat, echo_server)
|
175
|
+
send_beacon(:heartbeat, combo_server)
|
176
|
+
|
177
|
+
expect do |block|
|
178
|
+
subject.each_listing(&block)
|
179
|
+
end.to yield_control.exactly(3).times
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe "#lookup" do
|
184
|
+
it "should provide listings by service" do
|
185
|
+
send_beacon(:heartbeat, hello_server)
|
186
|
+
expect(subject.lookup("HelloService").to_hash).to eq hello_server.to_hash
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should return random listings" do
|
190
|
+
send_beacon(:heartbeat, hello_server)
|
191
|
+
send_beacon(:heartbeat, combo_server)
|
192
|
+
|
193
|
+
uuids = 100.times.map { subject.lookup("HelloService").uuid }
|
194
|
+
expect(uuids.count("hello")).to be_within(25).of(50)
|
195
|
+
expect(uuids.count("combo")).to be_within(25).of(50)
|
196
|
+
end
|
197
|
+
|
198
|
+
it "should not return expired listings" do
|
199
|
+
send_beacon(:heartbeat, hello_server_with_short_ttl)
|
200
|
+
sleep 5
|
201
|
+
expect(subject.lookup("HelloService")).to be_nil
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should not return flatlined servers" do
|
205
|
+
send_beacon(:heartbeat, echo_server)
|
206
|
+
send_beacon(:heartbeat, combo_server)
|
207
|
+
send_beacon(:flatline, echo_server)
|
208
|
+
|
209
|
+
uuids = 100.times.map { subject.lookup("EchoService").uuid }
|
210
|
+
expect(uuids.count("combo")).to eq 100
|
211
|
+
end
|
212
|
+
|
213
|
+
it "should return up-to-date listings" do
|
214
|
+
send_beacon(:heartbeat, echo_server)
|
215
|
+
echo_server.port = "7777"
|
216
|
+
send_beacon(:heartbeat, echo_server)
|
217
|
+
|
218
|
+
expect(subject.lookup("EchoService").port).to eq "7777"
|
219
|
+
end
|
220
|
+
|
221
|
+
context 'when given service identifier is a class name' do
|
222
|
+
it 'returns the listing corresponding to the class name' do
|
223
|
+
send_beacon(:heartbeat, echo_server)
|
224
|
+
expect(subject.lookup(EchoService).uuid).to eq echo_server.uuid
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
describe "#restart" do
|
230
|
+
it "should clear all listings" do
|
231
|
+
send_beacon(:heartbeat, echo_server)
|
232
|
+
send_beacon(:heartbeat, combo_server)
|
233
|
+
subject.restart
|
234
|
+
expect(subject.lookup("EchoService")).to be_nil
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
describe "#running" do
|
239
|
+
it "should be true" do
|
240
|
+
expect(subject).to be_running
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
describe "#stop" do
|
245
|
+
it "should clear all listings" do
|
246
|
+
send_beacon(:heartbeat, echo_server)
|
247
|
+
send_beacon(:heartbeat, combo_server)
|
248
|
+
subject.stop
|
249
|
+
expect(subject.lookup("EchoService")).to be_nil
|
250
|
+
end
|
251
|
+
|
252
|
+
it "should stop the server" do
|
253
|
+
subject.stop
|
254
|
+
expect(subject).to_not be_running
|
255
|
+
end
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
if ENV.key?("BENCH")
|
260
|
+
context "performance" do
|
261
|
+
let(:servers) do
|
262
|
+
100.times.map do |x|
|
263
|
+
::Protobuf::Rpc::DynamicDiscovery::Server.new(
|
264
|
+
:uuid => "performance_server#{x + 1}",
|
265
|
+
:address => '127.0.0.1',
|
266
|
+
:port => (5555 + x).to_s,
|
267
|
+
:ttl => rand(1..5),
|
268
|
+
:services => 10.times.map { |y| "PerformanceService#{y}" },
|
269
|
+
)
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
before do
|
274
|
+
require 'benchmark'
|
275
|
+
subject.start
|
276
|
+
servers.each { |server| send_beacon(:heartbeat, server) }
|
277
|
+
end
|
278
|
+
|
279
|
+
after do
|
280
|
+
subject.stop
|
281
|
+
end
|
282
|
+
|
283
|
+
it "should perform lookups in constant time" do
|
284
|
+
print "\n\n"
|
285
|
+
Benchmark.bm(17) do |x|
|
286
|
+
x.report(" 1_000 lookups:") { 1_000.times { subject.lookup("PerformanceService#{rand(0..9)}") } }
|
287
|
+
x.report(" 10_000 lookups:") { 10_000.times { subject.lookup("PerformanceService#{rand(0..9)}") } }
|
288
|
+
x.report("100_000 lookups:") { 100_000.times { subject.lookup("PerformanceService#{rand(0..9)}") } }
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|