Back to Repositories

Testing GC Statistics Input Plugin Implementation in Fluentd

This test suite validates the GC (Garbage Collection) statistics input plugin functionality in Fluentd. It ensures proper configuration, data collection, and emission of GC metrics with customizable key formats.

Test Coverage Overview

The test suite covers essential GC stat input plugin functionality including:

  • Configuration validation for emit intervals and tags
  • GC statistics collection and emission verification
  • Support for both symbol and string keys in output
  • Event time stamping accuracy

Implementation Analysis

The testing approach utilizes Fluentd’s test driver framework to simulate input plugin behavior. It implements stub methods for GC stat collection and verifies event emission patterns with configurable intervals.

Key patterns include driver configuration testing, event assertion validation, and output format verification using Ruby’s Test::Unit framework.

Technical Details

Testing tools and setup:

  • Fluent::Test::Driver::Input for plugin testing
  • Test::Unit assertion framework
  • GC.stat stubbing for controlled test environment
  • Custom configuration strings for different test scenarios

Best Practices Demonstrated

The test suite exemplifies robust testing practices through:

  • Proper test isolation using setup methods
  • Comprehensive configuration testing
  • Event data structure validation
  • Multiple assertion points for thorough verification
  • Clear test case organization and naming

fluent/fluentd

test/plugin/test_in_gc_stat.rb

            
require_relative '../helper'
require 'fluent/test/driver/input'
require 'fluent/plugin/in_gc_stat'

class GCStatInputTest < Test::Unit::TestCase
  def setup
    Fluent::Test.setup
  end

  CONFIG = %[
    emit_interval 1
    tag t1
  ]

  def create_driver(conf=CONFIG)
    Fluent::Test::Driver::Input.new(Fluent::Plugin::GCStatInput).configure(conf)
  end

  def test_configure
    d = create_driver
    assert_equal(1, d.instance.emit_interval)
    assert_equal("t1", d.instance.tag)
  end

  def setup_gc_stat
    stat = GC.stat
    stub(GC).stat { stat }
    stat
  end

  def test_emit
    stat = setup_gc_stat

    d = create_driver
    d.run(expect_emits: 2)

    events = d.events
    assert(events.length > 0)
    events.each_index {|i|
      assert_equal(stat, events[i][2])
      assert(events[i][1].is_a?(Fluent::EventTime))
    }
  end

  def test_emit_with_use_symbol_keys_false
    stat = setup_gc_stat
    result = {}
    stat.each_pair { |k, v|
      result[k.to_s] = v
    }

    d = create_driver(CONFIG + "use_symbol_keys false")
    d.run(expect_emits: 2)

    events = d.events
    assert(events.length > 0)
    events.each_index {|i|
      assert_equal(result, events[i][2])
      assert(events[i][1].is_a?(Fluent::EventTime))
    }
  end
end