Skip to content

Commit

Permalink
add shortcut method "helpers" and its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tagomoris committed Mar 11, 2016
1 parent 1a9af08 commit be98274
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 20 deletions.
38 changes: 19 additions & 19 deletions lib/fluent/plugin.rb
Expand Up @@ -18,7 +18,7 @@
require 'fluent/config/error'

module Fluent
class Plugin
module Plugin
SEARCH_PATHS = []

# plugins for fluentd: fluent/plugin/type_NAME.rb
Expand All @@ -35,24 +35,6 @@ class Plugin

REGISTRIES = [INPUT_REGISTRY, OUTPUT_REGISTRY, FILTER_REGISTRY, BUFFER_REGISTRY, PARSER_REGISTRY, FORMATTER_REGISTRY]

def self.lookup_type_from_class(klass_or_its_name)
klass = if klass_or_its_name.is_a? Class
klass_or_its_name
elsif klass_or_its_name.is_a? String
eval(klass_or_its_name) # const_get can't handle qualified klass name (ex: A::B)
else
raise ArgumentError, "invalid argument type #{klass_or_its_name.class}: #{klass_or_its_name}"
end
REGISTRIES.reduce(nil){|a, r| a || r.reverse_lookup(klass) }
end

def self.add_plugin_dir(dir)
REGISTRIES.each do |r|
r.paths.push(dir)
end
nil
end

def self.register_input(type, klass)
register_impl('input', INPUT_REGISTRY, type, klass)
end
Expand Down Expand Up @@ -89,6 +71,24 @@ def self.register_formatter(type, klass_or_proc)
end
end

def self.lookup_type_from_class(klass_or_its_name)
klass = if klass_or_its_name.is_a? Class
klass_or_its_name
elsif klass_or_its_name.is_a? String
eval(klass_or_its_name) # const_get can't handle qualified klass name (ex: A::B)
else
raise ArgumentError, "invalid argument type #{klass_or_its_name.class}: #{klass_or_its_name}"
end
REGISTRIES.reduce(nil){|a, r| a || r.reverse_lookup(klass) }
end

def self.add_plugin_dir(dir)
REGISTRIES.each do |r|
r.paths.push(dir)
end
nil
end

def self.new_input(type)
new_impl('input', INPUT_REGISTRY, type)
end
Expand Down
40 changes: 40 additions & 0 deletions lib/fluent/plugin/base.rb
@@ -0,0 +1,40 @@
#
# Fluentd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'fluent/plugin'
require 'fluent/configurable'
require 'fluent/plugin_id'
require 'fluent/log'
require 'fluent/plugin_helper'

module Fluent
module Plugin
class Base
include Configurable
include PluginId
include PluginLoggerMixin
include PluginHelper::Mixin

def initialize; end
def configure(conf); end
def start; end
def stop; end
def shutdown; end
def close; end
def terminate; end
end
end
end
36 changes: 36 additions & 0 deletions lib/fluent/plugin_helper.rb
@@ -0,0 +1,36 @@
#
# Fluentd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require 'fluent/plugin_helper/event_emitter'
require 'fluent/plugin_helper/thread'
require 'fluent/plugin_helper/event_loop'
require 'fluent/plugin_helper/timer'
require 'fluent/plugin_helper/child_process'

module Fluent
module PluginHelper
module Mixin
def self.included(mod)
mod.extend(Fluent::PluginHelper)
end
end

def helpers(*snake_case_symbols)
helper_modules = snake_case_symbols.map{|name| Fluent::PluginHelper.const_get(name.to_s.split('_').map(&:capitalize).join) }
include *helper_modules
end
end
end
2 changes: 1 addition & 1 deletion test/test_input.rb
Expand Up @@ -17,7 +17,7 @@ def test_router
assert_equal Engine.root_agent.event_router, d.instance.router

d = nil
assert_nothing_raised {
assert_nothing_raised {
d = create_driver('@label @known')
}
expected = Engine.root_agent.find_label('@known').event_router
Expand Down
78 changes: 78 additions & 0 deletions test/test_plugin_helper.rb
@@ -0,0 +1,78 @@
require_relative 'helper'
require 'fluent/plugin_helper'
require 'fluent/plugin/base'

class ConfigTest < Test::Unit::TestCase
module FluentTest; end

sub_test_case 'Fluent::Plugin::Base.helpers method works as shortcut to include helper modules' do
class FluentTest::PluginTest1 < Fluent::Plugin::Base
helpers :event_emitter
end
class FluentTest::PluginTest2 < Fluent::Plugin::Base
helpers :thread
end
class FluentTest::PluginTest3 < Fluent::Plugin::Base
helpers :event_loop
end
class FluentTest::PluginTest4 < Fluent::Plugin::Base
helpers :timer
end
class FluentTest::PluginTest5 < Fluent::Plugin::Base
helpers :child_process
end
class FluentTest::PluginTest0 < Fluent::Plugin::Base
helpers :event_emitter, :thread, :event_loop, :timer, :child_process, :child_process
end

test 'plugin can include helper event_emitter' do
assert FluentTest::PluginTest1.include?(Fluent::PluginHelper::EventEmitter)
p1 = FluentTest::PluginTest1.new
assert p1.respond_to?(:emits?)
assert p1.emits?
end

test 'plugin can include helper thread' do
assert FluentTest::PluginTest2.include?(Fluent::PluginHelper::Thread)
p2 = FluentTest::PluginTest2.new
assert p2.respond_to?(:thread_current_running?)
assert p2.respond_to?(:thread_create)
end

test 'plugin can include helper event_loop' do
assert FluentTest::PluginTest3.include?(Fluent::PluginHelper::EventLoop)
p3 = FluentTest::PluginTest3.new
assert p3.respond_to?(:event_loop_attach)
assert p3.respond_to?(:event_loop_running?)
end

test 'plugin can include helper timer' do
assert FluentTest::PluginTest4.include?(Fluent::PluginHelper::Timer)
p4 = FluentTest::PluginTest4.new
assert p4.respond_to?(:timer_execute)
end

test 'plugin can include helper child_process' do
assert FluentTest::PluginTest5.include?(Fluent::PluginHelper::ChildProcess)
p5 = FluentTest::PluginTest5.new
assert p5.respond_to?(:child_process_execute)
end

test 'plugin can 2 or more helpers at once' do
assert FluentTest::PluginTest0.include?(Fluent::PluginHelper::EventEmitter)
assert FluentTest::PluginTest0.include?(Fluent::PluginHelper::Thread)
assert FluentTest::PluginTest0.include?(Fluent::PluginHelper::EventLoop)
assert FluentTest::PluginTest0.include?(Fluent::PluginHelper::Timer)
assert FluentTest::PluginTest0.include?(Fluent::PluginHelper::ChildProcess)

p0 = FluentTest::PluginTest0.new
assert p0.respond_to?(:child_process_execute)
assert p0.respond_to?(:timer_execute)
assert p0.respond_to?(:event_loop_attach)
assert p0.respond_to?(:event_loop_running?)
assert p0.respond_to?(:thread_current_running?)
assert p0.respond_to?(:thread_create)
assert p0.respond_to?(:emits?)
end
end
end

0 comments on commit be98274

Please sign in to comment.