Back to Repositories

Testing Plugin Helper Module Integration in Fluentd

This test suite validates the plugin helper functionality in Fluentd, focusing on helper module inclusion and method availability. It ensures proper integration of various helper modules like event emitters, threads, timers, and child processes within Fluentd plugins.

Test Coverage Overview

The test suite provides comprehensive coverage of Fluentd’s plugin helper system, verifying the inclusion and functionality of multiple helper modules.

Key areas tested include:
  • Event emitter helper integration and router availability
  • Thread helper methods and execution states
  • Event loop attachment and status verification
  • Timer execution capabilities
  • Child process management functions
  • Multiple helper module inclusion scenarios

Implementation Analysis

The testing approach uses a systematic module-by-module verification strategy, creating separate test classes for each helper type. It employs Ruby’s module inclusion testing patterns and method response checking.

Implementation features:
  • Subclass testing with TestBase inheritance
  • Module inclusion verification
  • Method availability checks
  • Multiple helper combination testing

Technical Details

Testing infrastructure includes:
  • Test::Unit framework implementation
  • Fluent::Plugin::Base extension
  • Custom FluentTest module namespace
  • Helper method verification through respond_to? checks
  • Multiple test cases using sub_test_case blocks

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby.

Notable practices include:
  • Isolated test cases for each helper module
  • Systematic verification of module inclusion
  • Comprehensive method availability checking
  • Clear test case organization
  • Proper setup of test hierarchies
  • Effective use of assertion methods

fluent/fluentd

test/test_plugin_helper.rb

            
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::TestBase
      helpers :event_emitter
    end
    class FluentTest::PluginTest2 < Fluent::Plugin::TestBase
      helpers :thread
    end
    class FluentTest::PluginTest3 < Fluent::Plugin::TestBase
      helpers :event_loop
    end
    class FluentTest::PluginTest4 < Fluent::Plugin::TestBase
      helpers :timer
    end
    class FluentTest::PluginTest5 < Fluent::Plugin::TestBase
      helpers :child_process
    end
    class FluentTest::PluginTest6 < Fluent::Plugin::TestBase
      helpers :retry_state
    end
    class FluentTest::PluginTest0 < Fluent::Plugin::TestBase
      helpers :event_emitter, :thread, :event_loop, :timer, :child_process, :retry_state
    end

    test 'plugin can include helper event_emitter' do
      assert FluentTest::PluginTest1.include?(Fluent::PluginHelper::EventEmitter)
      p1 = FluentTest::PluginTest1.new
      assert p1.respond_to?(:has_router?)
      assert p1.has_router?
    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?(:has_router?)
    end
  end
end