spec/outputs/redis_spec.rb in logstash-output-redis-2.0.2 vs spec/outputs/redis_spec.rb in logstash-output-redis-2.0.4

- old
+ new

@@ -1,128 +1,75 @@ require "logstash/devutils/rspec/spec_helper" require "logstash/outputs/redis" require "logstash/json" require "redis" +require "flores/random" describe LogStash::Outputs::Redis, :redis => true do - - describe "ship lots of events to a list" do - key = 10.times.collect { rand(10).to_s }.join("") - event_count = 10000 + rand(500) - - config <<-CONFIG - input { - generator { - message => "hello world" - count => #{event_count} - type => "generator" - } + shared_examples_for "writing to redis list" do |extra_config| + let(:key) { 10.times.collect { rand(10).to_s }.join("") } + let(:event_count) { Flores::Random.integer(0..10000) } + let(:message) { Flores::Random.text(0..100) } + let(:default_config) { + { + "key" => key, + "data_type" => "list", + "host" => "localhost" } - output { - redis { - host => "127.0.0.1" - key => "#{key}" - data_type => list - } - } - CONFIG + } + let(:redis_config) { + default_config.merge(extra_config || {}) + } + let(:redis_output) { described_class.new(redis_config) } - agent do - # Query redis directly and inspect the goodness. - redis = Redis.new(:host => "127.0.0.1") - - # The list should contain the number of elements our agent pushed up. - insist { redis.llen(key) } == event_count - - # Now check all events for order and correctness. - event_count.times do |value| - id, element = redis.blpop(key, 0) - event = LogStash::Event.new(LogStash::Json.load(element)) - insist { event["sequence"] } == value - insist { event["message"] } == "hello world" + before do + redis_output.register + event_count.times do |i| + event = LogStash::Event.new("sequence" => i, "message" => message) + redis_output.receive(event) end + redis_output.close + end - # The list should now be empty - insist { redis.llen(key) } == 0 - end # agent - end - - describe "batch mode" do - key = 10.times.collect { rand(10).to_s }.join("") - event_count = 200000 - - config <<-CONFIG - input { - generator { - message => "hello world" - count => #{event_count} - type => "generator" - } - } - output { - redis { - host => "127.0.0.1" - key => "#{key}" - data_type => list - batch => true - batch_timeout => 5 - timeout => 5 - } - } - CONFIG - - agent do - # we have to wait for close to execute & flush the last batch. - # otherwise we might start doing assertions before everything has been - # sent out to redis. - sleep 2 - + it "should successfully send all events to redis" do redis = Redis.new(:host => "127.0.0.1") - + # The list should contain the number of elements our agent pushed up. insist { redis.llen(key) } == event_count # Now check all events for order and correctness. event_count.times do |value| id, element = redis.blpop(key, 0) event = LogStash::Event.new(LogStash::Json.load(element)) insist { event["sequence"] } == value - insist { event["message"] } == "hello world" + insist { event["message"] } == message end # The list should now be empty insist { redis.llen(key) } == 0 - end # agent + end end - describe "converts US-ASCII to utf-8 without failures" do - key = 10.times.collect { rand(10).to_s }.join("") + context "when batch_mode is false" do + include_examples "writing to redis list" + end - config <<-CONFIG - input { - generator { - charset => "US-ASCII" - message => "\xAD\u0000" - count => 1 - type => "generator" - } - } - output { - redis { - host => "127.0.0.1" - key => "#{key}" - data_type => list - } - } - CONFIG + context "when batch_mode is true" do + batch_events = Flores::Random.integer(1..1000) + batch_settings = { + "batch" => true, + "batch_events" => batch_events + } - agent do - # Query redis directly and inspect the goodness. - redis = Redis.new(:host => "127.0.0.1") + include_examples "writing to redis list", batch_settings do - # The list should contain no elements. - insist { redis.llen(key) } == 1 - end # agent + # A canary to make sure we're actually enabling batch mode + # in this shared example. + it "should have batch mode enabled" do + expect(redis_config).to include("batch") + expect(redis_config["batch"]).to be_truthy + end + end end end