lumberjack_spec.rb 3.09 KB
Newer Older
Jordan Sissel's avatar
Jordan Sissel committed
1
$: << File.realpath(File.join(File.dirname(__FILE__), "..", "lib"))
2
require "tempfile"
3
require "lumberjack/server"
4 5 6 7
require "insist"
require "stud/try"

describe "lumberjack" do
8
  before :each do
9
    # TODO(sissel): Generate a self-signed SSL cert
Jordan Sissel's avatar
Jordan Sissel committed
10 11 12 13
    @file = Stud::Temporary.file("lumberjack-test-file")
    @ssl_cert = Stud::Temporary.file("lumberjack-test-file")
    @ssl_key = Stud::Temporary.file("lumberjack-test-file")
    @ssl_csr = Stud::Temporary.file("lumberjack-test-file")
14 15 16 17 18 19 20 21 22 23

    # Generate the ssl key
    system("openssl genrsa -out #{@ssl_key.path} 1024")
    system("openssl req -new -key #{@ssl_key.path} -batch -out #{@ssl_csr.path}")
    system("openssl x509 -req -days 365 -in #{@ssl_csr.path} -signkey #{@ssl_key.path} -out #{@ssl_cert.path}")

    @server = Lumberjack::Server.new(
      :ssl_certificate => @ssl_cert.path,
      :ssl_key => @ssl_key.path
    )
24 25 26 27
    @lumberjack = IO.popen("build/bin/lumberjack --host localhost " \
                           "--port #{@server.port} " \
                           "--ssl-ca-path #{@ssl_cert.path} #{@file.path}",
                           "r")
28 29 30 31 32 33 34

    @event_queue = Queue.new
    @server_thread = Thread.new do
      @server.run do |event|
        @event_queue << event
      end
    end
35
  end # before each
36

37
  after :each do
Jordan Sissel's avatar
Jordan Sissel committed
38 39 40 41
    [@file, @ssl_cert, @ssl_key, @ssl_csr].each do |f|
      f.close
      File.unlink(f.path)
    end
42 43
    Process::kill("KILL", @lumberjack.pid)
    Process::wait(@lumberjack.pid)
44 45 46 47
  end

  it "should follow a file and emit lines as events" do
    sleep 1 # let lumberjack start up.
48
    count = rand(5000) + 25000
49 50 51 52 53
    count.times do |i|
      @file.puts("hello #{i}")
    end
    @file.close

54 55
    # Wait for lumberjack to finish publishing data to us.
    Stud::try(20.times) do
56 57 58
      raise "have #{@event_queue.size}, want #{count}" if @event_queue.size < count
    end

59 60 61 62 63 64 65 66 67
    # Now verify that we have all the data and in the correct order.
    insist { @event_queue.size } == count
    host = Socket.gethostname
    count.times do |i|
      event = @event_queue.pop
      insist { event["line"] } == "hello #{i}"
      insist { event["file"] } == @file.path
      insist { event["host"] } == host
    end
68
    insist { @event_queue }.empty?
69 70 71
  end

  it "should follow a slowly-updating file and emit lines as events" do
72 73
    sleep 5 # let lumberjack start up.
    count = rand(50) + 1000
74
    count.times do |i|
75 76 77 78 79 80
      @file.puts("fizzle #{i}")

      # Start fast, then go slower after 80% of the events
      if i > (count * 0.8)
        sleep(rand * 0.200) # sleep up to 200ms
      end
81 82 83 84 85 86 87 88 89
    end
    @file.close

    # Wait for lumberjack to finish publishing data to us.
    Stud::try(20.times) do
      raise "have #{@event_queue.size}, want #{count}" if @event_queue.size < count
    end

    # Now verify that we have all the data and in the correct order.
90 91 92 93
    insist { @event_queue.size } == count
    host = Socket.gethostname
    count.times do |i|
      event = @event_queue.pop
94
      insist { event["line"] } == "fizzle #{i}"
95 96 97
      insist { event["file"] } == @file.path
      insist { event["host"] } == host
    end
98
    insist { @event_queue }.empty?
99 100
  end
end