Skip to content

Commit

Permalink
Add optimized filter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ganmacs committed Aug 18, 2016
1 parent bd50807 commit e1de49e
Showing 1 changed file with 85 additions and 16 deletions.
101 changes: 85 additions & 16 deletions test/test_event_router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 the filter plugin implements #filter without #filter_stream' 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 the filter plugin implements #filter_with_time without #filter_stream' 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 when filter plugins implement #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 have #filter_with_time instead of #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 when just a filter of some filters implements #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)
Expand Down

0 comments on commit e1de49e

Please sign in to comment.