spec/inputs/jdbc_spec.rb in logstash-input-jdbc-1.0.2 vs spec/inputs/jdbc_spec.rb in logstash-input-jdbc-2.0.1

- old
+ new

@@ -4,12 +4,11 @@ require "sequel" require "sequel/adapters/jdbc" require "timecop" require "stud/temporary" - -describe "jdbc" do +describe LogStash::Inputs::Jdbc do let(:mixin_settings) { {"jdbc_user" => ENV['USER'], "jdbc_driver_class" => "org.apache.derby.jdbc.EmbeddedDriver", "jdbc_connection_string" => "jdbc:derby:memory:testdb;create=true"} } let(:settings) { {} } let(:plugin) { LogStash::Inputs::Jdbc.new(mixin_settings.merge(settings)) } let(:queue) { Queue.new } let (:db) do @@ -31,18 +30,27 @@ context "when registering and tearing down" do let(:settings) { {"statement" => "SELECT 1 as col1 FROM test_table"} } it "should register without raising exception" do expect { plugin.register }.to_not raise_error - plugin.teardown + plugin.stop end - it "should register with password set" do - mixin_settings['jdbc_password'] = 'pass' - expect { plugin.register }.to_not raise_error - plugin.stop + it "should stop without raising exception" do + plugin.register + expect { plugin.stop }.to_not raise_error end + + it_behaves_like "an interruptible input plugin" do + let(:settings) do + { + "statement" => "SELECT 1 FROM test_table", + "schedule" => "* * * * * UTC" + } + end + let(:config) { mixin_settings.merge(settings) } + end end context "when neither statement and statement_filepath arguments are passed" do it "should fail to register" do expect{ plugin.register }.to raise_error(LogStash::ConfigurationError) @@ -68,11 +76,11 @@ File.write(statement_file_path, statement) plugin.register end after do - plugin.teardown + plugin.stop end it "should read in statement from file" do expect(plugin.statement).to eq(statement) end @@ -89,11 +97,11 @@ before do plugin.register end after do - plugin.teardown + plugin.stop end it "should retrieve params correctly from Event" do plugin.run(queue) expect(queue.pop['num_param']).to eq(settings['parameters']['num_param']) @@ -112,11 +120,11 @@ Timecop.scale(60) runner = Thread.new do plugin.run(queue) end sleep 3 - plugin.teardown + plugin.stop runner.kill runner.join expect(queue.size).to eq(2) Timecop.return end @@ -138,11 +146,11 @@ before do plugin.register end after do - plugin.teardown + plugin.stop end it "should fetch all rows" do num_rows.times do db[:test_table].insert(:num => 1, :created_at => Time.now.utc) @@ -165,11 +173,11 @@ before do plugin.register end after do - plugin.teardown + plugin.stop end it "should successfully iterate table with respect to field values" do test_table = db[:test_table] @@ -204,11 +212,11 @@ File.write(settings["last_run_metadata_path"], YAML.dump(last_run_time)) plugin.register end after do - plugin.teardown + plugin.stop end it "should respect last run metadata" do expect(plugin.instance_variable_get("@sql_last_start")).to eq(last_run_time) end @@ -230,11 +238,11 @@ File.write(settings["last_run_metadata_path"], YAML.dump(last_run_time)) plugin.register end after do - plugin.teardown + plugin.stop end it "should ignore last run metadata if :clean_run set to true" do expect(plugin.instance_variable_get("@sql_last_start")).to eq(Time.at(0).utc) end @@ -252,11 +260,11 @@ before do plugin.register end after do - plugin.teardown + plugin.stop end it "should not save state if :record_last_run is false" do expect(File).not_to exist(settings["last_run_metadata_path"]) end @@ -280,11 +288,11 @@ plugin.register end after do - plugin.teardown + plugin.stop end it "should fetch all rows" do plugin.run(queue) expect(queue.size).to eq(num_rows) @@ -298,41 +306,8 @@ mixin_settings['jdbc_driver_class'] = "org.not.ExistsDriver" end it "should fail" do expect { plugin.register }.to raise_error(LogStash::ConfigurationError) - end - end - - context "when timing out on connection" do - let(:settings) do - { - "statement" => "SELECT * FROM test_table", - "jdbc_pool_timeout" => 0, - "jdbc_connection_string" => 'mock://localhost:1527/db', - "sequel_opts" => { - "max_connections" => 1 - } - } - end - - it "should raise PoolTimeout error" do - plugin.register - db = plugin.instance_variable_get(:@database) - expect(db.pool.instance_variable_get(:@timeout)).to eq(0) - expect(db.pool.instance_variable_get(:@max_size)).to eq(1) - - q, q1 = Queue.new, Queue.new - t = Thread.new{db.pool.hold{|c| q1.push nil; q.pop}} - q1.pop - expect{db.pool.hold {|c|}}.to raise_error(Sequel::PoolTimeout) - q.push nil - t.join - end - - it "should log error message" do - allow(Sequel).to receive(:connect).and_raise(Sequel::PoolTimeout) - expect(plugin.logger).to receive(:error).with("Failed to connect to database. 0 second timeout exceeded.") - expect { plugin.register }.to raise_error(Sequel::PoolTimeout) end end end