Add some simple test code to get a Protobuf::Rpc::Service into an Elementary::Connection

This commit is contained in:
R. Tyler Croy 2014-06-03 19:58:00 -07:00
parent 5fc7521038
commit 3adea95e0b
8 changed files with 106 additions and 1 deletions

1
.rspec Normal file
View File

@ -0,0 +1 @@
--color --order random --fail-fast

View File

@ -1,2 +1,13 @@
require "bundler/gem_tasks"
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new
task :default => ['protobuf:spec', 'spec', 'build']
namespace :protobuf do
desc 'Generate rspec protos'
task :spec do
sh "protoc --proto_path=./spec/support/proto --ruby_out=./spec/support #{Dir['./spec/support/proto/**/*.proto'].join(' ')}"
end
end

View File

@ -1,5 +1,25 @@
require 'rubygems'
require 'protobuf'
module Elementary
class Connection
attr_reader :raise_on_error, :service
# Initialize a connection to the +Protobuf::Rpc::Service+
#
# @param [Protobuf::Rpc::Service] service
# @param [Hash] opts
# @options opts [Boolean] :raise_on_error Defaults to true, will cause
# rpc errors to raise exceptions
def initialize(service, opts={})
if service.nil? || service.superclass != Protobuf::Rpc::Service
raise ArgumentError,
"Cannot construct an Elementary::Connection with `#{service}` (#{service.class})"
end
@raise_on_error = opts[:raise_on_error] || true
@service = service
end
end
end

View File

@ -1,4 +1,7 @@
require 'rubygems'
require 'concurrent'
module Elementary
class Future
class Future < Concurrent::Future
end
end

View File

@ -2,4 +2,22 @@ require 'spec_helper'
require 'elementary/connection'
describe Elementary::Connection do
describe '#initialize' do
subject(:connection) { described_class.new(service, opts) }
let(:opts) { {} }
context 'without a Protobuf::Rpc::Service' do
let(:service) { nil }
it 'should raise ArgumentError' do
expect { connection }.to raise_error(ArgumentError)
end
end
context 'with a Protobuf::Rpc::Service' do
let(:service) { Elementary::Rspec::Simple }
it { should be_instance_of described_class }
end
end
end

View File

@ -1,2 +1,8 @@
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + '/../'))
require 'elementary'
# Reequire all our generate test protos
Dir[File.expand_path(File.dirname(__FILE__) + '/support/**/*.pb.rb')].each do |f|
require f
end

View File

@ -0,0 +1,11 @@
package elementary.rspec;
message String {
required string data = 1;
optional int64 status = 2;
}
service Simple {
rpc Echo (String) returns (String);
}

View File

@ -0,0 +1,35 @@
##
# This file is auto-generated. DO NOT EDIT!
#
require 'protobuf/message'
require 'protobuf/rpc/service'
module Elementary
module Rspec
##
# Message Classes
#
class String < ::Protobuf::Message; end
##
# Message Fields
#
class String
required :string, :data, 1
optional :int64, :status, 2
end
##
# Service Classes
#
class Simple < ::Protobuf::Rpc::Service
rpc :echo, ::Elementary::Rspec::String, ::Elementary::Rspec::String
end
end
end