Skip to content

Commit

Permalink
Merge pull request #1997 from fluent/couter-client-wait
Browse files Browse the repository at this point in the history
counter: Add wait API
  • Loading branch information
repeatedly authored May 23, 2018
2 parents a93e3e7 + c884d91 commit 909e110
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 909e110

Please sign in to comment.