Skip to content

Commit

Permalink
Check scope is not nil before calling methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ganmacs committed Sep 27, 2016
1 parent 9192219 commit 095ee68
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/fluent/counter/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def establish(scope)
# 3. init([{ name: 'name1',reset_interval: 20 }, { name: 'name2',reset_interval: 20 }])
# 4. init([{ name: 'name1',reset_interval: 20 }, { name: 'name2',reset_interval: 20 }], options: {})
def init(params, options: {})
exist_scope!
params = [params] unless params.is_a?(Array)
res = send_request('init', @scope, params, options)

Expand All @@ -72,6 +73,7 @@ def init(params, options: {})
end

def delete(*params, options: {})
exist_scope!
res = send_request('delete', @scope, params, options)
options[:async] ? res : res.get
end
Expand All @@ -84,23 +86,30 @@ def delete(*params, options: {})
# 3. init([{ name: 'name1',value: 20 }, { name: 'name2',value: 20 }])
# 4. init([{ name: 'name1',value: 20 }, { name: 'name2',value: 20 }], options: {})
def inc(params, options: {})
exist_scope!
params = [params] unless params.is_a?(Array)
res = send_request('inc', @scope, params, options)
options[:async] ? res : res.get
end

def get(*params, options: {})
exist_scope!
res = send_request('get', @scope, params, options)
options[:async] ? res : res.get
end

def reset(*params, options: {})
exist_scope!
res = send_request('reset', @scope, params, options)
options[:async] ? res : res.get
end

private

def exist_scope!
raise 'Call `establish` method to get a `scope` before calling this method' unless @scope
end

def on_message(data)
if response = @responses.delete(data['id'])
response.set(data)
Expand Down
35 changes: 35 additions & 0 deletions test/counter/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ def extract_value_from_server(server, scope, name)
assert_equal initial_value, v.current
end

test 'raise an error when @scope is nil' do
@client.instance_variable_set(:@scope, nil)
assert_raise 'Call `establish` method to get a `scope` before calling this method' do
@client.init(name: 'key1', reset_interval: 10)
end
end

data(
already_exist_key: [
{ name: 'key1', reset_interval: 10 },
Expand Down Expand Up @@ -208,6 +215,13 @@ def extract_value_from_server(server, scope, name)
assert_nil extract_value_from_server(@server, @scope, @name)
end

test 'raise an error when @scope is nil' do
@client.instance_variable_set(:@scope, nil)
assert_raise 'Call `establish` method to get a `scope` before calling this method' do
@client.delete(@name)
end
end

data(
key_not_found: [
'key2',
Expand Down Expand Up @@ -291,6 +305,13 @@ def extract_value_from_server(server, scope, name)
assert_equal param[:value], v.total
end

test 'raise an error when @scope is nil' do
@client.instance_variable_set(:@scope, nil)
assert_raise 'Call `establish` method to get a `scope` before calling this method' do
@client.inc(name: 'name', value: 1)
end
end

data(
not_exist_key: [
{ name: 'key2', value: 10 },
Expand Down Expand Up @@ -362,6 +383,13 @@ def extract_value_from_server(server, scope, name)
assert_equal v1.type, v2['type']
end

test 'raise an error when @scope is nil' do
@client.instance_variable_set(:@scope, nil)
assert_raise 'Call `establish` method to get a `scope` before calling this method' do
@client.get(@name)
end
end

data(
key_not_found: [
'key2',
Expand Down Expand Up @@ -469,6 +497,13 @@ def extract_value_from_server(server, scope, name)
assert_equal @now, v1.last_reset_at
end

test 'raise an error when @scope is nil' do
@client.instance_variable_set(:@scope, nil)
assert_raise 'Call `establish` method to get a `scope` before calling this method' do
@client.reset(@name)
end
end

data(
key_not_found: [
'key2',
Expand Down

0 comments on commit 095ee68

Please sign in to comment.