Skip to content

Commit

Permalink
counter: Add wait API
Browse files Browse the repository at this point in the history
Similar to "get" but raises an error when API call has a problem.

Signed-off-by: Masahiro Nakagawa <[email protected]>
  • Loading branch information
repeatedly committed May 21, 2018
1 parent 6fd23bb commit c884d91
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/fluent/counter/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

require 'cool.io'
require 'fluent/counter/base_socket'
require 'fluent/counter/error'
require 'timeout'

module Fluent
Expand Down Expand Up @@ -59,7 +60,7 @@ def stop
def establish(scope)
scope = Timeout.timeout(@timeout) {
response = send_request('establish', nil, [scope])
raise response.errors.first if response.errors?
Fluent::Counter.raise_error(response.errors.first) if response.errors?
data = response.data
data.first
}
Expand Down Expand Up @@ -274,6 +275,14 @@ def get
@result
end

def wait
res = get
if res.error?
Fluent::Counter.raise_error(res.errors.first)
end
res
end

private

def join
Expand Down
21 changes: 21 additions & 0 deletions lib/fluent/counter/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,26 @@ def code
'internal_server_error'
end
end

def raise_error(response)
msg = response['message']
case response['code']
when 'invalid_params'
raise InvalidParams.new(msg)
when 'unknown_key'
raise UnknownKey.new(msg)
when 'parse_error'
raise ParseError.new(msg)
when 'invalid_request'
raise InvalidRequest.new(msg)
when 'method_not_found'
raise MethodNotFound.new(msg)
when 'internal_server_error'
raise InternalServerError.new(msg)
else
raise "Unknown code: #{response['code']}"
end
end
module_function :raise_error
end
end
4 changes: 4 additions & 0 deletions test/counter/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ def extract_value_from_server(server, scope, name)

assert_empty response.data
assert_equal expected_error, errors

assert_raise {
@client.init(param).wait
}
end

test 'return an existing value when passed key already exists and ignore option is true' do
Expand Down

0 comments on commit c884d91

Please sign in to comment.