spec/spanx/actor/log_reader_spec.rb in spanx-0.1.1 vs spec/spanx/actor/log_reader_spec.rb in spanx-0.3.0

- old
+ new

@@ -1,68 +1,105 @@ require 'spec_helper' require 'file/tail' require 'timeout' require 'thread' require 'tempfile' +require 'timecop' describe Spanx::Actor::LogReader do - def test_log_file(file, expected_ip_count, expected_line_count, whitelist = nil) - counter = 0 - ip_hash = {} - reader = Spanx::Actor::LogReader.new(file, Queue.new, 1, whitelist) - reader.file.backward(1000) + subject(:reader) { Spanx::Actor::LogReader.new(files, queue, 1, whitelist) } + let(:files) { [] } + let(:queue) { [] } + let(:whitelist) { nil } + let(:read_timeout) { 0.1 } - t_reader = Thread.new do - begin - timeout(read_timeout) do - reader.read do |ip| - counter += 1 - ip_hash[ip] ||= 0 - ip_hash[ip] += 1 + describe '#read' do + let(:tempfile) { Tempfile.new('access.log') } + let!(:file) { Spanx::Actor::File.new(tempfile.path) } + + it 'yields each ip in a file as it is written' do + expect { |b| + reader_thread = Thread.new do + begin + timeout(read_timeout) do + reader.read(file, &b) + end + rescue TimeoutError end end - rescue TimeoutError - end + + ::File.open(tempfile.path, 'a') do |t| + t.puts '9.9.9.9 - some stuff' + t.puts '9.9.9.10 - some other stuff' + t.puts '9.9.9.9 - whoa moar stuff' + end + + reader_thread.join + }.to yield_successive_args('9.9.9.9', '9.9.9.10', '9.9.9.9') end - yield if block_given? + context 'if a line matches a whitelist' do + let(:whitelist) { double } - t_reader.join + before do + allow(whitelist).to receive(:match?).with("9.9.9.9 - some stuff\n").and_return(true) + allow(whitelist).to receive(:match?).with("9.9.9.10 - some other stuff\n").and_return(false) + end - counter.should eql(expected_line_count) - ip_hash.keys.size.should eql(expected_ip_count) - end + it 'skips that line' do + expect { |b| + reader_thread = Thread.new do + begin + timeout(read_timeout) do + reader.read(file, &b) + end + rescue TimeoutError + end + end - let(:file_name) { "spec/fixtures/access.log.1" } - let(:read_timeout) { 0.05 } + ::File.open(tempfile.path, 'a') do |t| + t.puts '9.9.9.9 - some stuff' + t.puts '9.9.9.10 - some other stuff' + end - context "#read" do - it "should be able to read and parse IPs from a static file" do - test_log_file(file_name, 82, 104) + reader_thread.join + }.to yield_successive_args('9.9.9.10') + end end + end - it "should be able to read and parse IPs from a file being appended to" do - tempfile = Tempfile.new("access.log") + describe '#run' do + let!(:file1) { Tempfile.new('log.log') } + let!(:file2) { Tempfile.new('loge.log') } + let!(:files) { [file1.path, file2.path] } + let(:read_timeout) { 0.1 } - contents = ::File.read(file_name) - tempfile.write(contents) - tempfile.close + after do + file1.close + file2.close + end - test_log_file(tempfile.path, 83, 105) do - t_log_appender = Thread.new do - ::File.open(tempfile.path, "a") do |t| - t.write("9.9.9.9 - content") + xit 'pushes ips from each watched file onto queue' do + Timecop.freeze(Time.at(1409270561)) + + runner = Thread.new do + begin + timeout(read_timeout) do + reader.run + reader.threads.last.join end + rescue TimeoutError end - t_log_appender.join end - end - end - context "#whitelist" do - let(:whitelist_file) { "spec/fixtures/whitelist.txt" } - it "should exclude googlebot log lines" do - test_log_file("spec/fixtures/access.log.bots", 1, 1, Spanx::Whitelist.new(whitelist_file)) + sleep 1 + + file1.puts '1.1.1.1 - everyone loves a log!' + file2.puts '2.2.2.2 - only some people love a loge' + + runner.join + + expect(queue).to match_array([['1.1.1.1', 1409270561], ['2.2.2.2', 1409270561]]) end end end