Sha256: 5dc9daaaf4d730e9dcfbd408f96f286bd1e078579e5c94a3be59d3f9d126c552

Contents?: true

Size: 1.97 KB

Versions: 24

Compression:

Stored size: 1.97 KB

Contents

require "spec_helper"

describe Lita::Daemon do
  let(:stdout) { StringIO.new }
  let(:stderr) { StringIO.new }
  let(:log_file) { StringIO.new }

  before do
    allow(Process).to receive(:daemon)
    allow(Process).to receive(:kill)
    allow(File).to receive(:new).and_return("log")
    allow(File).to receive(:open)
    allow(File).to receive(:read)
    stub_const("STDOUT", stdout)
    stub_const("STDERR", stderr)
  end

  subject { described_class.new("/tmp/lita_pid", "/tmp/lita_log", false) }

  describe "#daemonize" do
    it "daemonizes the running process" do
      expect(Process).to receive(:daemon).with(true)
      subject.daemonize
    end

    context "when the user has not requested that existing processes should be killed" do
      it "aborts if a Lita process is already running" do
        allow(File).to receive(:exist?).and_return(true)
        expect(subject).to receive(:abort)
        subject.daemonize
      end
    end

    context "when the user has requested that existing process be killed" do
      subject { described_class.new("/tmp/lita_pid", "/tmp/lita_log", true) }

      it "kills existing processes" do
        allow(File).to receive(:exist?).and_return(true)
        expect(Process).to receive(:kill)
        subject.daemonize
      end

      it "aborts if it can't kill an existing process" do
        allow(File).to receive(:exist?).and_return(true)
        allow(Process).to receive(:kill).and_raise(Errno::ESRCH)
        expect(subject).to receive(:abort)
        subject.daemonize
      end
    end

    it "redirects stdout to the log file" do
      allow(File).to receive(:new).with("/tmp/lita_log", "a").and_return(log_file)
      subject.daemonize
      stdout.write "foo"
      expect(log_file.string).to eq("foo")
    end

    it "redirects stderr to the log file" do
      allow(File).to receive(:new).with("/tmp/lita_log", "a").and_return(log_file)
      subject.daemonize
      stderr.write "bar"
      expect(log_file.string).to eq("bar")
    end
  end
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
lita-4.8.0 spec/lita/daemon_spec.rb
lita-4.8.0.beta1 spec/lita/daemon_spec.rb
lita-4.7.1 spec/lita/daemon_spec.rb
lita-4.7.0 spec/lita/daemon_spec.rb
lita-4.6.1 spec/lita/daemon_spec.rb
lita-4.6.0 spec/lita/daemon_spec.rb
lita-4.5.0 spec/lita/daemon_spec.rb
lita-4.4.3 spec/lita/daemon_spec.rb
lita-4.4.2 spec/lita/daemon_spec.rb
lita-4.4.1 spec/lita/daemon_spec.rb
lita-4.4.0 spec/lita/daemon_spec.rb
lita-4.3.2 spec/lita/daemon_spec.rb
lita-4.3.1 spec/lita/daemon_spec.rb
lita-4.3.0 spec/lita/daemon_spec.rb
lita-4.2.1 spec/lita/daemon_spec.rb
lita-4.2.0 spec/lita/daemon_spec.rb
lita-4.1.0 spec/lita/daemon_spec.rb
lita-4.0.4 spec/lita/daemon_spec.rb
lita-4.0.3 spec/lita/daemon_spec.rb
lita-4.0.2 spec/lita/daemon_spec.rb