forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve OutputDelegator implementation and specs.
Backwards compatibility is now implemented for existing workers_not_supported uses. This clears up a few bugs in the initial pass as well. Fixes elastic#4391
- Loading branch information
Showing
4 changed files
with
168 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,122 @@ | ||
# encoding: utf-8 | ||
require 'spec_helper' | ||
|
||
|
||
|
||
describe LogStash::OutputDelegator do | ||
let(:logger) { double("logger") } | ||
let(:out_klass) { double("output klass") } | ||
let(:out_inst) { double("output instance") } | ||
let(:events) { 7.times.map { LogStash::Event.new }} | ||
let(:default_worker_count) { 1 } | ||
|
||
subject { described_class.new(logger, out_klass, 1) } | ||
subject { described_class.new(logger, out_klass, default_worker_count) } | ||
|
||
before do | ||
allow(out_klass).to receive(:new).with(any_args).and_return(out_inst) | ||
allow(out_klass).to receive(:threadsafe?).and_return(false) | ||
allow(out_klass).to receive(:workers_not_supported?).and_return(false) | ||
allow(out_inst).to receive(:register) | ||
allow(logger).to receive(:debug).with(any_args) | ||
end | ||
|
||
it "should initialize cleanly" do | ||
expect { subject }.not_to raise_error | ||
end | ||
|
||
context "after having received a batch of events" do | ||
let(:events) { 7.times.map { LogStash::Event.new }} | ||
context "with a plain output plugin" do | ||
let(:out_klass) { double("output klass") } | ||
let(:out_inst) { double("output instance") } | ||
|
||
before do | ||
allow(out_klass).to receive(:new).with(any_args).and_return(out_inst) | ||
allow(out_klass).to receive(:threadsafe?).and_return(false) | ||
allow(out_klass).to receive(:workers_not_supported?).and_return(false) | ||
allow(out_inst).to receive(:register) | ||
allow(out_inst).to receive(:multi_receive) | ||
subject.multi_receive(events) | ||
allow(logger).to receive(:debug).with(any_args) | ||
end | ||
|
||
it "should initialize cleanly" do | ||
expect { subject }.not_to raise_error | ||
end | ||
|
||
it "should pass the events through" do | ||
expect(out_inst).to have_received(:multi_receive).with(events) | ||
context "after having received a batch of events" do | ||
before do | ||
subject.multi_receive(events) | ||
end | ||
|
||
it "should pass the events through" do | ||
expect(out_inst).to have_received(:multi_receive).with(events) | ||
end | ||
|
||
it "should increment the number of events received" do | ||
expect(subject.events_received).to eql(events.length) | ||
end | ||
end | ||
|
||
it "should increment the number of events received" do | ||
expect(subject.events_received).to eql(events.length) | ||
it "should register all workers on register" do | ||
expect(out_inst).to receive(:register) | ||
subject.register | ||
end | ||
|
||
it "should close all workers when closing" do | ||
expect(out_inst).to receive(:do_close) | ||
subject.do_close | ||
end | ||
|
||
describe "concurrency and worker support" do | ||
describe "non-threadsafe outputs that allow workers" do | ||
let(:default_worker_count) { 3 } | ||
|
||
before do | ||
allow(out_klass).to receive(:threadsafe?).and_return(false) | ||
allow(out_klass).to receive(:workers_not_supported?).and_return(false) | ||
end | ||
|
||
it "should instantiate multiple workers" do | ||
expect(subject.workers.length).to eql(default_worker_count) | ||
end | ||
|
||
it "should send received events to the worker" do | ||
expect(out_inst).to receive(:multi_receive).with(events) | ||
subject.multi_receive(events) | ||
end | ||
end | ||
|
||
describe "threadsafe outputs" do | ||
before do | ||
allow(out_klass).to receive(:threadsafe?).and_return(true) | ||
allow(out_klass).to receive(:workers_not_supported?).and_return(false) | ||
end | ||
|
||
it "should return true when threadsafe? is invoked" do | ||
expect(subject.threadsafe?).to eql(true) | ||
end | ||
|
||
it "should define a threadsafe_worker" do | ||
expect(subject.send(:threadsafe_worker)).to eql(out_inst) | ||
end | ||
|
||
it "should utilize threadsafe_multi_receive" do | ||
expect(subject.send(:threadsafe_worker)).to receive(:multi_receive).with(events) | ||
subject.multi_receive(events) | ||
end | ||
|
||
it "should not utilize the worker queue" do | ||
expect(subject.send(:worker_queue)).not_to receive(:pop) | ||
subject.multi_receive(events) | ||
end | ||
|
||
it "should send received events to the worker" do | ||
expect(out_inst).to receive(:multi_receive).with(events) | ||
subject.multi_receive(events) | ||
end | ||
end | ||
end | ||
end | ||
|
||
it "should register all workers on register" do | ||
expect(out_inst).to receive(:register) | ||
subject.register | ||
# This may seem suspiciously similar to the class in outputs/base_spec | ||
# but, in fact, we need a whole new class because using this even once | ||
# will immutably modify the base class | ||
class LogStash::Outputs::NOOPDelLegacyNoWorkers < ::LogStash::Outputs::Base | ||
LEGACY_WORKERS_NOT_SUPPORTED_REASON = "legacy reason" | ||
|
||
def register | ||
workers_not_supported(LEGACY_WORKERS_NOT_SUPPORTED_REASON) | ||
end | ||
end | ||
|
||
it "should close all workers when closing" do | ||
expect(out_inst).to receive(:do_close) | ||
subject.do_close | ||
describe "legacy output workers_not_supported" do | ||
let(:default_worker_count) { 2 } | ||
let(:out_klass) { LogStash::Outputs::NOOPDelLegacyNoWorkers } | ||
|
||
it "should only setup one worker" do | ||
expect(subject.worker_count).to eql(1) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters