From 860b7bec3362c2d426fdce89ea21380a814ed73e Mon Sep 17 00:00:00 2001 From: ganmacs Date: Thu, 11 Aug 2016 01:36:11 +0900 Subject: [PATCH] Add optimized filter tests --- test/test_event_router.rb | 101 ++++++++++++++++++++++++++++++++------ 1 file changed, 85 insertions(+), 16 deletions(-) diff --git a/test/test_event_router.rb b/test/test_event_router.rb index c630d58644..f595fd68c3 100644 --- a/test/test_event_router.rb +++ b/test/test_event_router.rb @@ -184,28 +184,14 @@ def event_router end sub_test_case 'filter' do - test 'filter should be called when tag matched and with optimized' do - event_router.add_rule('test', filter) - - now = Engine.now - record = {'k' => 'v'} - assert_rr do - mock(filter).filter('test', now, record) { events } - event_router.emit('test', now, record) - end - end - - test 'filter should be called when tag matched and without optimized' do + test 'filter should be called when tag matched' do filter = Class.new(FluentTestFilter) { |x| - def filter_stream(_tag, es) - es - end + def filter_stream(_tag, es); end }.new event_router.add_rule('test', filter) assert_rr do - mock($log).info("Filtering works with worse performance, because #{[filter].map(&:class)} uses `#filter_stream` method.") mock(filter).filter_stream('test', is_a(OneEventStream)) { events } event_router.emit('test', Engine.now, 'k' => 'v') end @@ -247,6 +233,89 @@ def filter_stream(_tag, es) end end + sub_test_case 'optimized filter' do + setup do + @record = { 'k' => 'v' } + @now = Engine.now + end + + test 'call optimized filter when filter plugin that has #method' do + event_router.add_rule('test', filter) + + assert_rr do + mock(filter).filter('test', @now, @record) { @record } + event_router.emit('test', @now, @record) + end + end + + test 'call optimized filter when filter plugin that has #filter_method' do + filter = Class.new(FluentTestFilter) { + undef_method :filter + def filter_with_time(tag, time, record); end + }.new + + event_router.add_rule('test', filter) + + assert_rr do + mock(filter).filter_with_time('test', @now, @record) { [time, @record] } + event_router.emit('test', @now, @record) + end + end + + test "don't call optimized filter whenf filter plugins that has #filter_stream" do + filter = Class.new(FluentTestFilter) { + undef_method :filter + def filter_stream(tag, time, record); end + }.new + + event_router.add_rule('test', filter) + + assert_rr do + mock(filter).filter_stream('test', is_a(OneEventStream)) { OneEventStream.new(@now, @record) } + event_router.emit('test', @now, @record) + end + end + + test 'call optimized filter when filter plugins that have #filter_method and #filter' do + filter_with_time = Class.new(FluentTestFilter) { + undef_method :filter + def filter_with_time(tag, time, record); end + }.new + + event_router.add_rule('test', filter_with_time) + event_router.add_rule('test', filter) + + assert_rr do + mock(filter_with_time).filter_with_time('test', @now, @record) { [@now + 1, @record] } + mock(filter).filter('test', @now + 1, @record) { @record } + event_router.emit('test', @now, @record) + end + end + + test "don't call optimized filter whenf only one of some filters has #filter_stream method" do + filter_stream = Class.new(FluentTestFilter) { + def filter_stream(tag, record); end + }.new + + filter_with_time = Class.new(FluentTestFilter) { + undef_method :filter + def filter_with_time(tag, time, record); end + }.new + + filters = [filter_stream, filter_with_time, filter] + filters.each { |f| event_router.add_rule('test', f) } + + e = OneEventStream.new(@now, @record) + assert_rr do + mock($log).info("Filtering works with worse performance, because #{[filter_stream].map(&:class)} uses `#filter_stream` method.") + mock(filter_stream).filter_stream('test', is_a(OneEventStream)) { e } + mock(filter).filter_stream('test', is_a(OneEventStream)) { e } + mock(filter_with_time).filter_stream('test', is_a(OneEventStream)) { e } + event_router.emit('test', @now, @record) + end + end + end + sub_test_case 'emit_error_handler' do test 'call handle_emits_error when emit failed' do event_router.add_rule('test', error_output)