Skip to content

Commit

Permalink
refactor input specs and shutdown sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
colinsurprenant committed Mar 31, 2015
1 parent f777ab0 commit 96ad533
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 56 deletions.
7 changes: 5 additions & 2 deletions lib/logstash/inputs/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ def run(queue)

public
def teardown
@tail.sincedb_write
@tail.quit
if @tail
@tail.sincedb_write
@tail.quit
@tail = nil
end
end # def teardown
end # class LogStash::Inputs::File
104 changes: 50 additions & 54 deletions spec/inputs/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

delimiter = (LogStash::Environment.windows? ? "\r\n" : "\n")

describe "starts at the end of an existing file" do
it "should starts at the end of an existing file" do
tmpfile_path = Stud::Temporary.pathname
sincedb_path = Stud::Temporary.pathname

config <<-CONFIG
conf = <<-CONFIG
input {
file {
type => "blah"
Expand All @@ -23,48 +23,49 @@
}
CONFIG

input do |pipeline, queue|
File.open(tmpfile_path, "w") do |fd|
fd.puts("ignore me 1")
fd.puts("ignore me 2")
end
File.open(tmpfile_path, "w") do |fd|
fd.puts("ignore me 1")
fd.puts("ignore me 2")
end

Thread.new { pipeline.run }
sleep 0.1 while !pipeline.ready?
events = input(conf) do |pipeline, queue|

# at this point even if pipeline.ready? == true the plugins
# at this point the plugins
# threads might still be initializing so we cannot know when the
# file plugin will have seen the original file, it could see it
# after the first(s) hello world appends below, hence the
# retry logic.

retries = 0
loop do
insist { retries } < 20 # 2 secs should be plenty?
events = []

retries = 0
while retries < 20
File.open(tmpfile_path, "a") do |fd|
fd.puts("hello")
fd.puts("world")
end

if queue.size >= 2
events = 2.times.collect { queue.pop }
insist { events[0]["message"] } == "hello"
insist { events[1]["message"] } == "world"
break
end

sleep(0.1)
retries += 1
end

events
end

insist { events[0]["message"] } == "hello"
insist { events[1]["message"] } == "world"
end

describe "can start at the beginning of an existing file" do
it "should start at the beginning of an existing file" do
tmpfile_path = Stud::Temporary.pathname
sincedb_path = Stud::Temporary.pathname

config <<-CONFIG
conf = <<-CONFIG
input {
file {
type => "blah"
Expand All @@ -76,64 +77,59 @@
}
CONFIG

input do |pipeline, queue|
File.open(tmpfile_path, "a") do |fd|
fd.puts("hello")
fd.puts("world")
end

Thread.new { pipeline.run }
sleep 0.1 while !pipeline.ready?
File.open(tmpfile_path, "a") do |fd|
fd.puts("hello")
fd.puts("world")
end

events = 2.times.collect { queue.pop }
insist { events[0]["message"] } == "hello"
insist { events[1]["message"] } == "world"
events = input(conf) do |pipeline, queue|
2.times.collect { queue.pop }
end

insist { events[0]["message"] } == "hello"
insist { events[1]["message"] } == "world"
end

describe "restarts at the sincedb value" do
it "should restarts at the sincedb value" do
tmpfile_path = Stud::Temporary.pathname
sincedb_path = Stud::Temporary.pathname

config <<-CONFIG
conf = <<-CONFIG
input {
file {
type => "blah"
path => "#{tmpfile_path}"
start_position => "beginning"
start_position => "beginning"
sincedb_path => "#{sincedb_path}"
delimiter => "#{delimiter}"
}
}
CONFIG

input do |pipeline, queue|
File.open(tmpfile_path, "w") do |fd|
fd.puts("hello")
fd.puts("world")
end

t = Thread.new { pipeline.run }
sleep 0.1 while !pipeline.ready?

events = 2.times.collect { queue.pop }
pipeline.shutdown
t.join
File.open(tmpfile_path, "w") do |fd|
fd.puts("hello3")
fd.puts("world3")
end

File.open(tmpfile_path, "a") do |fd|
fd.puts("foo")
fd.puts("bar")
fd.puts("baz")
end
events = input(conf) do |pipeline, queue|
2.times.collect { queue.pop }
end

Thread.new { pipeline.run }
sleep 0.1 while !pipeline.ready?
insist { events[0]["message"] } == "hello3"
insist { events[1]["message"] } == "world3"

events = 3.times.collect { queue.pop }
File.open(tmpfile_path, "a") do |fd|
fd.puts("foo")
fd.puts("bar")
fd.puts("baz")
end

insist { events[0]["message"] } == "foo"
insist { events[1]["message"] } == "bar"
insist { events[2]["message"] } == "baz"
events = input(conf) do |pipeline, queue|
3.times.collect { queue.pop }
end

insist { events[0]["message"] } == "foo"
insist { events[1]["message"] } == "bar"
insist { events[2]["message"] } == "baz"
end
end

0 comments on commit 96ad533

Please sign in to comment.