{"push_id":1309317101,"size":2,"distinct_size":2,"ref":"refs/heads/counter-api","head":"d6642a57104defc222b16490a18c3ecac7d84086","before":"9c3803b8625b60541dce65e621a1b9aa6cd813ba","commits":[{"sha":"fa3a0cda12b82fe88102e045aac66e0957401ea8","author":{"name":"ganmacs","email":"cda6bbdd934884d829a70b6110f3e3773d9f4e66@gmail.com"},"message":"[from now] 2016/09/21 17:53:14\n\ndiff --git a/lib/fluent/counter/server.rb b/lib/fluent/counter/server.rb\nindex cec3b48..a16cd66 100644\n--- a/lib/fluent/counter/server.rb\n+++ b/lib/fluent/counter/server.rb\n@@ -52,13 +52,11 @@ module Fluent\n     end\n\n     class Core\n-      C = Struct.new(:name, :total, :current, :type, :reset_interval, :last_reset_at, :last_modified_at) do\n+      VALUE = Struct.new(:name, :total, :current, :type, :reset_interval, :last_reset_at, :last_modified_at) do\n         def self.init(data)\n-          reset_interval = data['reset_interval']\n           type = data['type'] || 'numerics'\n-          name = data['name']\n           now = EventTime.now\n-          C.new(name, 0, 0, type, reset_interval, now, now)\n+          VALUE.new(data['name'], 0, 0, type, data['reset_interval'], now, now)\n         end\n\n         def to_hash\n@@ -78,8 +76,8 @@ module Fluent\n\n       def initialize(opt = {})\n         @opt = opt\n-        @store = {}\n-        @store_mutex = Mutex.new\n+        @counters = {}\n+        @value_mutex = Mutex.new\n       end\n\n       def on_message(data)\n@@ -118,15 +116,15 @@ module Fluent\n           name = param['name']\n           key = scope + \"\\t\" + name\n\n-          if v = @store[key]\n+          if v = @counters[key]\n             if options['ingore']\n               res.push_data(v)\n             else\n               res.push_error(Fluent::Counter::InvalidParams.new(\"Aready exist\"))\n             end\n           else\n-            v = C.init(param)\n-            @store[key] = v\n+            v = VALUE.init(param)\n+            @counters[key] = v\n             res.push_data(v)\n           end\n         end\n@@ -142,13 +140,13 @@ module Fluent\n         valid_params.each do |param|\n           key = scope + \"\\t\" + param\n\n-          unless @store.key?(key)\n+          unless @counters.key?(key)\n             res.push_error(Fluent::Counter::InvalidParams.new(\"Key `#{param}` doesn't exists in counter\"))\n             next\n           end\n\n           # syncronize\n-          v = @store.delete(key)\n+          v = @counters.delete(key)\n           res.push_data(v)\n         end\n\n@@ -163,13 +161,13 @@ module Fluent\n         valid_params.each do |param|\n           value = param['value']\n           key = scope + \"\\t\" + param['name']\n-          v = @store[key]\n+          v = @counters[key]\n\n           if v\n             v.total += value\n             v.current += value\n             v.last_modified_at = EventTime.now\n-            @store[key] = v\n+            @counters[key] = v\n             res.push_data(v.to_hash)\n           else\n             res.push_error Fluent::Counter::UnknowKey.new(\"#{key} doesn't exist in counter\")\n@@ -186,7 +184,7 @@ module Fluent\n         valid_params.each do |param|\n           success = false\n           key = scope + \"\\t\" + param\n-          v = @store[key]\n+          v = @counters[key]\n\n           unless v\n             res.push_error Fluent::Counter::UnknowKey.new(\"`#{key}` doesn't exist in counter\")\n@@ -200,7 +198,7 @@ module Fluent\n             success = true\n             v.current = 0\n             v.last_reset_at = now\n-            @store[key] = v\n+            @counters[key] = v\n           end\n\n           res.push_data(\n@@ -220,7 +218,7 @@ module Fluent\n         valid_params.each do |param|\n           key = scope + \"\\t\" + param\n\n-          if v = @store[key]\n+          if v = @counters[key]\n             res.push_data(v)\n           else\n             res.push_error Fluent::Counter::UnknowKey.new(\"`#{key}` doesn't exist in counter\")\ndiff --git a/test/counter/test_server.rb b/test/counter/test_server.rb\nindex 1c08c97..f8a1457 100644\n--- a/test/counter/test_server.rb\n+++ b/test/counter/test_server.rb\n@@ -46,7 +46,7 @@ class CounterCoreTest < ::Test::Unit::TestCase\n   sub_test_case 'establish' do\n     test 'establish a scope in counter' do\n       result = @core.send('establish', ['key'], nil, nil)\n-      expected = { 'data' => [\"somthing_name\\key\"] }\n+      expected = { 'data' => [\"somthing_name\\tkey\"] }\n       assert_equal expected, result\n     end\n\n@@ -135,8 +135,8 @@ class CounterCoreTest < ::Test::Unit::TestCase\n       assert_equal 'numerics', counter['type']\n       assert_equal 'key1', counter['name']\n\n-      store = @core.instance_variable_get(:@store)\n-      c = store[\"#{@scope}\\tkey1\"]\n+      counters = @core.instance_variable_get(:@counters)\n+      c = counters[\"#{@scope}\\tkey1\"]\n       assert_equal 10, c['current']\n       assert_equal 10, c['total']\n     end\n@@ -175,8 +175,8 @@ class CounterCoreTest < ::Test::Unit::TestCase\n       assert_equal 'numerics', counter['type']\n       assert_equal 'key', counter['name']\n\n-      store = @core.instance_variable_get(:@store)\n-      c = store[\"#{@scope}\\tkey\"]\n+      counters = @core.instance_variable_get(:@counters)\n+      c = counters[\"#{@scope}\\tkey\"]\n       assert_equal 0, c['current']\n       assert_equal 10, c['total']\n     end\ndiff --git a/test/counter/test_validator.rb b/test/counter/test_validator.rb\nindex e69de29..6e6f615 100644\n--- a/test/counter/test_validator.rb\n+++ b/test/counter/test_validator.rb\n@@ -0,0 +1,5 @@\n+require_relative '../helper'\n+require 'fluent/counter/validator'\n+\n+class ArrayValidatorTest < ::Test::Unit::TestCase\n+end","distinct":true,"url":"https://api.github.com/repos/ganmacs/fluentd/commits/fa3a0cda12b82fe88102e045aac66e0957401ea8"},{"sha":"d6642a57104defc222b16490a18c3ecac7d84086","author":{"name":"ganmacs","email":"cda6bbdd934884d829a70b6110f3e3773d9f4e66@gmail.com"},"message":"[from now] 2016/09/21 18:59:48\n\ndiff --git a/lib/fluent/counter/validator.rb b/lib/fluent/counter/validator.rb\nindex 6c75f53..bf203b4 100644\n--- a/lib/fluent/counter/validator.rb\n+++ b/lib/fluent/counter/validator.rb\n@@ -19,7 +19,7 @@ require 'fluent/counter/error'\n module Fluent\n   module Counter\n     class Validator\n-      VALID_NAME = /\\A[a-z][\\ta-zA-Z0-9\\-_]*\\Z/\n+      VALID_NAME = /\\A[a-z][a-zA-Z0-9\\-_]*\\Z/\n       VALID_METHODS = %w(establish init delete inc get reset)\n\n       def self.request(data)\n@@ -72,16 +72,12 @@ module Fluent\n\n     class ArrayValidator < Validator\n       def validate_key!(name)\n-        case name\n-        when nil\n-          # TODO chnage a  message to array version\n-          raise Fluent::Counter::InvalidParams.new('`name` key is required')\n-        when name.is_a?(String)\n-          raise Fluent::Counter::InvalidParams.new(\"name' type should be String\")\n-        when VALID_NAME\n-        # success\n-        else\n-          raise Fluent::Counter::InvalidParams.new('Invalid `name` format')\n+        unless name.is_a?(String)\n+          raise Fluent::Counter::InvalidParams.new(\"The item's type in params should be String, but recieved #{name}\")\n+        end\n+\n+        unless VALID_NAME =~ name\n+          raise Fluent::Counter::InvalidParams.new(\"params include a invalid key: #{name}\")\n         end\n       end\n     end\n@@ -89,33 +85,43 @@ module Fluent\n     class HashValidator < Validator\n       def validate_name!(hash)\n         name = hash['name']\n+        unless name\n+          raise Fluent::Counter::InvalidParams.new('`name` is required')\n+        end\n\n-        case name\n-        when nil\n-          raise Fluent::Counter::InvalidParams.new('`name` key is required')\n-        when name.is_a?(String)\n-          raise Fluent::Counter::InvalidParams.new(\"name' type should be String\")\n-        when VALID_NAME\n-        # success\n-        else\n-          raise Fluent::Counter::InvalidParams.new('Invalid `name` format')\n+        unless name.is_a?(String)\n+          raise Fluent::Counter::InvalidParams.new(\"`name`'s type should be String, but recieved #{name}\")\n+        end\n+\n+        unless VALID_NAME =~ name\n+          raise Fluent::Counter::InvalidParams.new(\"`name` is a invalid string #{name}\")\n         end\n       end\n\n       def validate_value!(hash)\n-        unless hash['value']\n-          raise Fluent::Counter::InvalidParams.new('`value` key is required')\n+        value = hash['value']\n+        unless value\n+          raise Fluent::Counter::InvalidParams.new('`value` is required')\n+        end\n+\n+        unless value.is_a?(Numeric)\n+          raise Fluent::Counter::InvalidParams.new(\"`value`'s type should be Numerics\")\n         end\n       end\n\n       def validate_reset_interval!(hash)\n         interval = hash['reset_interval']\n-        if interval.nil?\n-          raise Fluent::Counter::InvalidParams.new('`reset_interval` key is required')\n-        elsif interval < 0\n-          raise Fluent::Counter::InvalidParams.new('`reset_interval` should be a positive number')\n-        else\n-          # success\n+\n+        unless interval\n+          raise Fluent::Counter::InvalidParams.new('`reset_interval` is required')\n+        end\n+\n+        unless interval.is_a?(Numeric)\n+          raise Fluent::Counter::InvalidParams.new(\"`reset_interval`'s type should be Numerics\")\n+        end\n+\n+        if interval < 0\n+          raise Fluent::Counter::InvalidParams.new(\"`reset_interval`'s should be a positive number\")\n         end\n       end\n     end\ndiff --git a/test/counter/test_error.rb b/test/counter/test_error.rb\nindex 1a0e04f..ccc25ef 100644\n--- a/test/counter/test_error.rb\n+++ b/test/counter/test_error.rb\n@@ -2,7 +2,7 @@ require_relative '../helper'\n\n require 'fluent/counter/error'\n\n-class CounterErrorsTest < ::Test::Unit::TestCase\n+class CounterErrorTest < ::Test::Unit::TestCase\n   setup do\n     @message = 'error message'\n   end\ndiff --git a/test/counter/test_validator.rb b/test/counter/test_validator.rb\nindex 6e6f615..fbd8003 100644\n--- a/test/counter/test_validator.rb\n+++ b/test/counter/test_validator.rb\n@@ -1,5 +1,81 @@\n require_relative '../helper'\n require 'fluent/counter/validator'\n\n-class ArrayValidatorTest < ::Test::Unit::TestCase\n+class CounterValidatorTest < ::Test::Unit::TestCase\n+  data(\n+    invald_name1: '',\n+    invald_name3: '_',\n+    invald_name4: 'A',\n+    invald_name5: 'a*',\n+    invald_name6: \"a\\t\",\n+    invald_name7: \"\\n\",\n+  )\n+  test 'invalid name' do |invalid_name|\n+    assert_nil(Fluent::Counter::Validator::VALID_NAME =~ invalid_name)\n+  end\n+\n+  test 'validate_empty!' do\n+    v = Fluent::Counter::Validator.new(:empty)\n+    success, errors = v.call([])\n+    assert_empty success\n+    assert_equal [Fluent::Counter::InvalidParams.new('`params` is required at least one')], errors\n+  end\n+end\n+\n+class CounterArrayValidatorTest < ::Test::Unit::TestCase\n+  test 'validate_key!' do\n+    ary = ['key', 100, '_']\n+    error_expected = [\n+      Fluent::Counter::InvalidParams.new(\"The item's type in params should be String, but recieved 100\"),\n+      Fluent::Counter::InvalidParams.new('params include a invalid key: _')\n+    ]\n+    v = Fluent::Counter::ArrayValidator.new(:key)\n+    success, errors = v.call(ary)\n+\n+    assert_equal ['key'], success\n+    assert_equal error_expected.map(&:to_hash), errors.map(&:to_hash)\n+  end\n+end\n+\n+class CounterHashValidatorTest < ::Test::Unit::TestCase\n+  test 'validate_name!' do\n+    hash = [{ 'name' => 'key' }, {}, { 'name' => 10 }, { 'name' => '_' }]\n+    error_expected = [\n+      Fluent::Counter::InvalidParams.new('`name` is required'),\n+      Fluent::Counter::InvalidParams.new(\"`name`'s type should be String, but recieved 10\"),\n+      Fluent::Counter::InvalidParams.new('`name` is a invalid string _')\n+    ]\n+    v = Fluent::Counter::HashValidator.new(:name)\n+    success, errors = v.call(hash)\n+\n+    assert_equal [{'name' => 'key'}], success\n+    assert_equal error_expected.map(&:to_hash), errors.map(&:to_hash)\n+  end\n+\n+  test 'validate_value!' do\n+    hash = [{ 'value' => 1 }, { 'value' => -1 }, {}, { 'value' => 'str' }]\n+    error_expected = [\n+      Fluent::Counter::InvalidParams.new('`value` is required'),\n+      Fluent::Counter::InvalidParams.new(\"`value`'s type should be Numerics\"),\n+    ]\n+    v = Fluent::Counter::HashValidator.new(:value)\n+    success, errors = v.call(hash)\n+\n+    assert_equal [{ 'value' => 1 }, { 'value' => -1 }], success\n+    assert_equal error_expected.map(&:to_hash), errors.map(&:to_hash)\n+  end\n+\n+  test 'validate_reset_interval!' do\n+    hash = [{ 'reset_interval' => 1 }, { 'reset_interval' => 1.0 }, {}, { 'reset_interval' => -1 }, { 'reset_interval' => 'str' }]\n+    error_expected = [\n+      Fluent::Counter::InvalidParams.new('`reset_interval` is required'),\n+      Fluent::Counter::InvalidParams.new(\"`reset_interval`'s should be a positive number\"),\n+      Fluent::Counter::InvalidParams.new(\"`reset_interval`'s type should be Numerics\"),\n+    ]\n+    v = Fluent::Counter::HashValidator.new(:reset_interval)\n+    success, errors = v.call(hash)\n+\n+    assert_equal [{ 'reset_interval' => 1 }, { 'reset_interval' => 1.0 }], success\n+    assert_equal error_expected.map(&:to_hash), errors.map(&:to_hash)\n+  end\n end","distinct":true,"url":"https://api.github.com/repos/ganmacs/fluentd/commits/d6642a57104defc222b16490a18c3ecac7d84086"}]}
{"actor":{"display_login":"ganmacs"}}