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, #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.



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

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.



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

def tag
  @tag
end

Instance Method Details

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



120
121
122
123
124
125
126
# File 'lib/fluent/test/output_test.rb', line 120

def emit(record, time=Time.now)
  slicer = @instance.instance_eval{@time_slicer}
  key = slicer.call(time.to_i)
  @entries[key] = [] unless @entries.has_key?(key)
  @entries[key] << [time.to_i, record]
  self
end

#expect_format(str) ⇒ Object



128
129
130
# File 'lib/fluent/test/output_test.rb', line 128

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

#run(&block) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fluent/test/output_test.rb', line 132

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

    buffer = ''
    @entries.keys.each {|key|
      es = ArrayEventStream.new(@entries[key])
      @instance.emit(@tag, es, NullOutputChain.instance)
      buffer << @instance.format_stream(@tag, es)
    }

    if @expected_buffer
      assert_equal(@expected_buffer, buffer)
    end

    chunks = []
    @instance.instance_eval do
      @buffer.instance_eval{ @map.keys }.each do |key|
        @buffer.push(key)
        chunks << @buffer.instance_eval{ @queue.pop }
      end
    end
    chunks.each { |chunk|
      begin
        result.push(@instance.write(chunk))
      ensure
        chunk.purge
      end
    }
  }
  result
end