fix ruby impl and test

pull/5327/head
yang-g 9 years ago
parent a4598b4c47
commit f4fc61cf68
  1. 14
      src/ruby/pb/grpc/health/checker.rb
  2. 42
      src/ruby/spec/pb/health/checker_spec.rb

@ -50,20 +50,20 @@ module Grpc
def check(req, _call) def check(req, _call)
status = nil status = nil
@status_mutex.synchronize do @status_mutex.synchronize do
status = @statuses["#{req.host}/#{req.service}"] status = @statuses["#{req.service}"]
end end
fail GRPC::BadStatus, StatusCodes::NOT_FOUND if status.nil? fail GRPC::BadStatus, StatusCodes::NOT_FOUND if status.nil?
HealthCheckResponse.new(status: status) HealthCheckResponse.new(status: status)
end end
# Adds the health status for a given host and service. # Adds the health status for a given service.
def add_status(host, service, status) def add_status(service, status)
@status_mutex.synchronize { @statuses["#{host}/#{service}"] = status } @status_mutex.synchronize { @statuses["#{service}"] = status }
end end
# Clears the status for the given host or service. # Clears the status for the given service.
def clear_status(host, service) def clear_status(service)
@status_mutex.synchronize { @statuses.delete("#{host}/#{service}") } @status_mutex.synchronize { @statuses.delete("#{service}") }
end end
# Clears alls the statuses. # Clears alls the statuses.

@ -87,21 +87,11 @@ describe Grpc::Health::Checker do
success_tests = success_tests =
[ [
{ {
desc: 'neither host or service are specified', desc: 'the service is not specified',
host: '',
service: '' service: ''
}, { }, {
desc: 'only the host is specified', desc: 'the service is specified',
host: 'test-fake-host',
service: ''
}, {
desc: 'the host and service are specified',
host: 'test-fake-host',
service: 'fake-service-1' service: 'fake-service-1'
}, {
desc: 'only the service is specified',
host: '',
service: 'fake-service-2'
} }
] ]
@ -114,9 +104,8 @@ describe Grpc::Health::Checker do
context 'method `add_status` and `check`' do context 'method `add_status` and `check`' do
success_tests.each do |t| success_tests.each do |t|
it "should succeed when #{t[:desc]}" do it "should succeed when #{t[:desc]}" do
subject.add_status(t[:host], t[:service], ServingStatus::NOT_SERVING) subject.add_status(t[:service], ServingStatus::NOT_SERVING)
got = subject.check(HCReq.new(host: t[:host], service: t[:service]), got = subject.check(HCReq.new(service: t[:service]), nil)
nil)
want = HCResp.new(status: ServingStatus::NOT_SERVING) want = HCResp.new(status: ServingStatus::NOT_SERVING)
expect(got).to eq(want) expect(got).to eq(want)
end end
@ -127,7 +116,7 @@ describe Grpc::Health::Checker do
success_tests.each do |t| success_tests.each do |t|
it "should fail with NOT_FOUND when #{t[:desc]}" do it "should fail with NOT_FOUND when #{t[:desc]}" do
blk = proc do blk = proc do
subject.check(HCReq.new(host: t[:host], service: t[:service]), nil) subject.check(HCReq.new(service: t[:service]), nil)
end end
expected_msg = /#{StatusCodes::NOT_FOUND}/ expected_msg = /#{StatusCodes::NOT_FOUND}/
expect(&blk).to raise_error GRPC::BadStatus, expected_msg expect(&blk).to raise_error GRPC::BadStatus, expected_msg
@ -138,16 +127,14 @@ describe Grpc::Health::Checker do
context 'method `clear_status`' do context 'method `clear_status`' do
success_tests.each do |t| success_tests.each do |t|
it "should fail after clearing status when #{t[:desc]}" do it "should fail after clearing status when #{t[:desc]}" do
subject.add_status(t[:host], t[:service], ServingStatus::NOT_SERVING) subject.add_status(t[:service], ServingStatus::NOT_SERVING)
got = subject.check(HCReq.new(host: t[:host], service: t[:service]), got = subject.check(HCReq.new(service: t[:service]), nil)
nil)
want = HCResp.new(status: ServingStatus::NOT_SERVING) want = HCResp.new(status: ServingStatus::NOT_SERVING)
expect(got).to eq(want) expect(got).to eq(want)
subject.clear_status(t[:host], t[:service]) subject.clear_status(t[:service])
blk = proc do blk = proc do
subject.check(HCReq.new(host: t[:host], service: t[:service]), subject.check(HCReq.new(service: t[:service]), nil)
nil)
end end
expected_msg = /#{StatusCodes::NOT_FOUND}/ expected_msg = /#{StatusCodes::NOT_FOUND}/
expect(&blk).to raise_error GRPC::BadStatus, expected_msg expect(&blk).to raise_error GRPC::BadStatus, expected_msg
@ -158,9 +145,8 @@ describe Grpc::Health::Checker do
context 'method `clear_all`' do context 'method `clear_all`' do
it 'should return NOT_FOUND after being invoked' do it 'should return NOT_FOUND after being invoked' do
success_tests.each do |t| success_tests.each do |t|
subject.add_status(t[:host], t[:service], ServingStatus::NOT_SERVING) subject.add_status(t[:service], ServingStatus::NOT_SERVING)
got = subject.check(HCReq.new(host: t[:host], service: t[:service]), got = subject.check(HCReq.new(service: t[:service]), nil)
nil)
want = HCResp.new(status: ServingStatus::NOT_SERVING) want = HCResp.new(status: ServingStatus::NOT_SERVING)
expect(got).to eq(want) expect(got).to eq(want)
end end
@ -169,7 +155,7 @@ describe Grpc::Health::Checker do
success_tests.each do |t| success_tests.each do |t|
blk = proc do blk = proc do
subject.check(HCReq.new(host: t[:host], service: t[:service]), nil) subject.check(HCReq.new(service: t[:service]), nil)
end end
expected_msg = /#{StatusCodes::NOT_FOUND}/ expected_msg = /#{StatusCodes::NOT_FOUND}/
expect(&blk).to raise_error GRPC::BadStatus, expected_msg expect(&blk).to raise_error GRPC::BadStatus, expected_msg
@ -203,7 +189,7 @@ describe Grpc::Health::Checker do
it 'should receive the correct status', server: true do it 'should receive the correct status', server: true do
@srv.handle(subject) @srv.handle(subject)
subject.add_status('', '', ServingStatus::NOT_SERVING) subject.add_status('', ServingStatus::NOT_SERVING)
t = Thread.new { @srv.run } t = Thread.new { @srv.run }
@srv.wait_till_running @srv.wait_till_running
@ -221,7 +207,7 @@ describe Grpc::Health::Checker do
@srv.wait_till_running @srv.wait_till_running
blk = proc do blk = proc do
stub = CheckerStub.new(@host, :this_channel_is_insecure, **@client_opts) stub = CheckerStub.new(@host, :this_channel_is_insecure, **@client_opts)
stub.check(HCReq.new(host: 'unknown', service: 'unknown')) stub.check(HCReq.new(service: 'unknown'))
end end
expected_msg = /#{StatusCodes::NOT_FOUND}/ expected_msg = /#{StatusCodes::NOT_FOUND}/
expect(&blk).to raise_error GRPC::BadStatus, expected_msg expect(&blk).to raise_error GRPC::BadStatus, expected_msg

Loading…
Cancel
Save