Skip to content

Commit

Permalink
Isolate test helper classes in their own namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
guyboertje authored and suyograo committed Dec 30, 2015
1 parent 4a59f3b commit baed94e
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 58 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.1.2
- Isolate test helper class in their own namespace

## 2.1.1
- Correct LS core dependency version

Expand Down
56 changes: 56 additions & 0 deletions lib/logstash/inputs/identity_map_codec_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# encoding: utf-8
require "logstash/inputs/component"
require "logstash/codecs/identity_map_codec"

module LogStash module Inputs class IdentityMapCodecComponent
include Component

attr_reader :codec

def add_codec(codec)
@codec = LogStash::Codecs::IdentityMapCodec.new(codec)
self
end

def stop
@codec.close
end

def do_work(context, data)
do_line(context, data) || do_eviction(context, data)
end

def process(context, data)
# data should be an event
deliver(context, data)
end

private

def do_line(context, data)
return false unless line?(context)
@codec.decode_accept(context, data, self)
# above should call back on #process
true
end

def do_eviction(context, data)
return false unless evicting?(context)
path = context[:path]
@codec.evict(path) if path
true
end

def line?(ctx)
action(ctx) == "line"
end

def evicting?(ctx)
_action = action(ctx)
_action == "timed_out" || _action == "deleted"
end

def action(ctx)
ctx[:action]
end
end end end
2 changes: 1 addition & 1 deletion spec/inputs/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
let(:mlconf) { Hash.new }
let(:events) { Array.new }
let(:mlcodec) { LogStash::Codecs::Multiline.new(mlconf) }
let(:codec) { CodecTracer.new }
let(:codec) { FileInput::CodecTracer.new }
let(:tmpfile_path) { Stud::Temporary.pathname }
let(:sincedb_path) { Stud::Temporary.pathname }
let(:tmpdir_path) { Stud::Temporary.directory }
Expand Down
121 changes: 64 additions & 57 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,85 @@

require "logstash/devutils/rspec/spec_helper"

class TracerBase
def initialize() @tracer = []; end
module FileInput
class TracerBase
def initialize() @tracer = []; end

def trace_for(symbol)
params = @tracer.map {|k,v| k == symbol ? v : nil}.compact
params.empty? ? false : params
end
def trace_for(symbol)
params = @tracer.map {|k,v| k == symbol ? v : nil}.compact
params.empty? ? false : params
end

def clear()
@tracer.clear()
def clear()
@tracer.clear()
end
end
end

class FileLogTracer < TracerBase
def warn(*args) @tracer.push [:warn, args]; end
def error(*args) @tracer.push [:error, args]; end
def debug(*args) @tracer.push [:debug, args]; end
def info(*args) @tracer.push [:info, args]; end

def info?() true; end
def debug?() true; end
def warn?() true; end
def error?() true; end
end

class ComponentTracer < TracerBase
def accept(*args) @tracer.push [:accept, args]; end
def deliver(*args) @tracer.push [:deliver, args]; end
end
class FileLogTracer < TracerBase
def warn(*args) @tracer.push [:warn, args]; end
def error(*args) @tracer.push [:error, args]; end
def debug(*args) @tracer.push [:debug, args]; end
def info(*args) @tracer.push [:info, args]; end

class CodecTracer < TracerBase
def decode_accept(ctx, data, listener)
@tracer.push [:decode_accept, [ctx, data]]
listener.process(ctx, {"message" => data})
end
def accept(listener)
@tracer.push [:accept, true]
end
def auto_flush()
@tracer.push [:auto_flush, true]
def info?() true; end
def debug?() true; end
def warn?() true; end
def error?() true; end
end
def close
@tracer.push [:close, true]

class ComponentTracer < TracerBase
def accept(*args) @tracer.push [:accept, args]; end
def deliver(*args) @tracer.push [:deliver, args]; end
end
def clone
self.class.new

class CodecTracer < TracerBase
def decode_accept(ctx, data, listener)
@tracer.push [:decode_accept, [ctx, data]]
listener.process(ctx, {"message" => data})
end
def accept(listener)
@tracer.push [:accept, true]
end
def auto_flush()
@tracer.push [:auto_flush, true]
end
def close
@tracer.push [:close, true]
end
def clone
self.class.new
end
end
end

module Kernel
def pause_until(nap = 5, &block)
sq = SizedQueue.new(1)
th1 = Thread.new(sq) {|q| sleep nap; q.push(false) }
th2 = Thread.new(sq) do |q|
success = false
iters = nap * 5 + 1
iters.times do
break if !!(success = block.call)
sleep(0.2)
unless Kernel.method_defined?(:pause_until)
module Kernel
def pause_until(nap = 5, &block)
sq = SizedQueue.new(1)
th1 = Thread.new(sq) {|q| sleep nap; q.push(false) }
th2 = Thread.new(sq) do |q|
success = false
iters = nap * 5 + 1
iters.times do
break if !!(success = block.call)
sleep(0.2)
end
q.push(success)
end
q.push(success)
sq.pop
end
sq.pop
end
end

RSpec::Matchers.define(:receive_call_and_args) do |m, args|
match do |actual|
actual.trace_for(m) == args
end
unless RSpec::Matchers.method_defined?(:receive_call_and_args)
RSpec::Matchers.define(:receive_call_and_args) do |m, args|
match do |actual|
actual.trace_for(m) == args
end

failure_message do
"Expecting method #{m} to receive: #{args} but got: #{actual.trace_for(m)}"
failure_message do
"Expecting method #{m} to receive: #{args} but got: #{actual.trace_for(m)}"
end
end
end

0 comments on commit baed94e

Please sign in to comment.