Skip to content

Commit

Permalink
use plugin.stop to return from run
Browse files Browse the repository at this point in the history
  • Loading branch information
jsvd committed Sep 21, 2015
1 parent 255fe09 commit 955fef5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
6 changes: 5 additions & 1 deletion lib/logstash/inputs/heartbeat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ def register

def run(queue)
sequence = 0
@thread = Thread.current

Stud.interval(@interval) do
sequence += 1
event = generate_message(sequence)
decorate(event)
queue << event
break if sequence == @count
break if sequence == @count || stop?
end # loop

end # def run
Expand All @@ -67,4 +68,7 @@ def generate_message(sequence)
end
end

def stop
Stud.stop!(@thread)
end
end # class LogStash::Inputs::Heartbeat
29 changes: 13 additions & 16 deletions spec/inputs/heartbeat_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
require "logstash/inputs/heartbeat"

describe LogStash::Inputs::Heartbeat do

it_behaves_like "an interruptible input plugin" do
let(:config) { { "interval" => 100 } }
end

sequence = 1
context "Default message test" do
subject { LogStash::Inputs::Heartbeat.new({}) }
Expand Down Expand Up @@ -37,22 +42,14 @@
end # it "should return an event with the current time (as epoch)"
end # context "Epoch test"

it "should generate a fixed number of events then stop" do
count = 4
config = <<-CONFIG
input {
heartbeat {
interval => 1
message => "sequence"
count => #{count}
}
}
CONFIG

events = input(config) do |pipeline, queue|
count.times.map{queue.pop}
end
context "with a fixed count" do
let(:events) { [] }
let(:count) { 4 }
subject { LogStash::Inputs::Heartbeat.new("interval" => 1, "message" => "sequence", "count" => count) }

events.each_with_index{|event, i| expect(event['clock']).to eq(i + 1)}
it "should generate a fixed number of events then stop" do
subject.run(events)
events.each_with_index{|event, i| expect(event['clock']).to eq(i + 1)}
end
end
end

0 comments on commit 955fef5

Please sign in to comment.