Class: Fluent::Test::TimeSlicedOutputTestDriver

Inherits:
InputTestDriver show all
Defined in:
lib/fluent/test/output_test.rb

Instance Attribute Summary collapse

Attributes inherited from InputTestDriver

#emit_streams, #event_streams, #expected_emits_length, #run_timeout

Attributes inherited from TestDriver

#config, #instance

Instance Method Summary collapse

Methods inherited from InputTestDriver

#emits, #events, #expect_emit, #expected_emits, #records, #register_run_breaking_condition, #register_run_post_condition, #run_should_stop?

Methods inherited from TestDriver

#configure

Constructor Details

#initialize(klass, tag = 'test', &block) ⇒ TimeSlicedOutputTestDriver

Returns a new instance of TimeSlicedOutputTestDriver.



104
105
106
107
108
109
# File 'lib/fluent/test/output_test.rb', line 104

def initialize(klass, tag='test', &block)
  super(klass, &block)
  @entries = []
  @expected_buffer = nil
  @tag = tag
end

Instance Attribute Details

#tagObject

Returns the value of attribute tag.



111
112
113
# File 'lib/fluent/test/output_test.rb', line 111

def tag
  @tag
end

Instance Method Details

#emit(record, time = EventTime.now) ⇒ Object



113
114
115
116
# File 'lib/fluent/test/output_test.rb', line 113

def emit(record, time=EventTime.now)
  @entries << [time, record]
  self
end

#expect_format(str) ⇒ Object



118
119
120
# File 'lib/fluent/test/output_test.rb', line 118

def expect_format(str)
  (@expected_buffer ||= '') << str
end

#run(&block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/fluent/test/output_test.rb', line 122

def run(&block)
  result = []
  super {
    block.call if block

    buffer = ''
    lines = {}
    # v0.12 TimeSlicedOutput doesn't call #format_stream
    @entries.each do |time, record|
      meta = @instance.(@tag, time, record)
      line = @instance.format(@tag, time, record)
      buffer << line
      lines[meta] ||= []
      lines[meta] << line
    end

    if @expected_buffer
      assert_equal(@expected_buffer, buffer)
    end

    lines.keys.each do |meta|
      chunk = @instance.buffer.generate_chunk(meta).staged!
      chunk.append(lines[meta])
      begin
        result.push(@instance.write(chunk))
      ensure
        chunk.purge
      end
    end
  }
  result
end