Sha256: 6a36235c3ab753c10addadb16b2daca6ec60549e931093ad5e6d1843e2a2d38e

Contents?: true

Size: 1.84 KB

Versions: 14

Compression:

Stored size: 1.84 KB

Contents

require 'spec_helper'
require 'pathname'
require 'timecop'
require 'fakefs/safe'

require 'roqua/core_ext/delayed_job/activity_monitoring'

describe DelayedJobActivityMonitoring do
  after do
    Timecop.return
  end

  def initialize_fake_rails_root
    FileUtils.mkdir_p(File.join('/app', 'tmp'))
    allow(Rails).to receive(:root).and_return(Pathname.new('/app'))
  end

  let(:monitoring_filename) { Rails.root.join('tmp', 'delayed_job_activity').to_s }

  it 'creates a file' do
    FakeFS.with_fresh do
      initialize_fake_rails_root
      expect { Delayed::Worker.new.work_off }
        .to change { File.exist?(monitoring_filename) }

      expect(File.mtime(monitoring_filename))
        .to be_within(1.second).of(Time.now)
    end
  end

  it 'updates the file modification time' do
    FakeFS.with_fresh do
      initialize_fake_rails_root

      Delayed::Worker.new.work_off

      Timecop.travel(1.minute)

      expect { Delayed::Worker.new.work_off }
        .to change { File.mtime(monitoring_filename) }

      expect(File.mtime(monitoring_filename))
        .to be_within(1.second).of(Time.now)
    end
  end

  describe 'when a job is reserved' do
    let(:max_run_time) { 2.hours }
    let(:job) do
      double(
        name: 'name',
        id: 123,
        queue: 'queue',
        max_run_time: max_run_time,
        invoke_job: nil,
        destroy: nil
      )
    end

    it 'creates a file with a modification time in the future' do
      FakeFS.with_fresh do
        initialize_fake_rails_root

        worker = Delayed::Worker.new
        expect(Delayed::Job).to receive(:reserve).and_return(job)

        expect { worker.send(:reserve_and_run_one_job) }
          .to change { File.exist?(monitoring_filename) }

        expect(File.mtime(monitoring_filename))
          .to be_within(1.second).of(max_run_time.from_now)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
roqua-support-0.4.4 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.4.3 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.4.2 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.4.1 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.4.0 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.3.5 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.3.4 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.3.3 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.3.2 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.3.1 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.3.0 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.1.34 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.1.33 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb
roqua-support-0.1.32 spec/roqua/core_ext/delayed_job/activity_monitoring_spec.rb