Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

counter: Add wait API #1997

Merged
merged 1 commit into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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