Sha256: b296c744750fd5993749360155bd6e1bead19d2da15afc0d77d5f9801ed6023e

Contents?: true

Size: 1.76 KB

Versions: 4

Compression:

Stored size: 1.76 KB

Contents

require 'spec_helper'

class WaitForService < Cistern::Service
  model :wait_for_model, require: false
  collection :wait_for_models, require: false

  class Real
    def initialize(*args)
    end
  end
end

class WaitForService::WaitForModel < Cistern::Model
  identity :id

  attribute :name
end

class WaitForService::WaitForModels < Cistern::Collection
  model WaitForService::WaitForModel

  def get(identity)
    self
  end
end

describe 'Cistern#wait_for' do
  it "should return false if timeout exceeded" do
    expect(Cistern.wait_for(0, 0) { false }).to be_false
  end
end

describe 'Cistern#wait_for!' do
  it "should raise if timeout exceeded" do
    expect { Cistern.wait_for!(0, 0) { false } }.to raise_exception(Cistern::Timeout)
  end
end

describe 'Cistern::Model#wait_for!' do
  let(:service) { WaitForService.new }
  let(:model)   { service.wait_for_models.new(identity: 1) }

  it "should raise if timeout exceeded" do
    expect { model.wait_for!(0, 0) { false } }.to raise_exception(WaitForService::Timeout)
  end
end


describe "WaitForModel#timeout" do
  let(:service) { WaitForService.new }
  let(:model)   { service.wait_for_models.new(identity: 1) }

  it "should use service-specific timeout in #wait_for" do
    service.class.timeout = 0.1
    service.class.poll_interval = 0

    elapsed = 0

    timeout(2) do
      expect do
        model.wait_for! { sleep(0.2); elapsed += 0.2; elapsed > 0.2 }
      end.to raise_exception(WaitForService::Timeout)
    end
  end

  it "should favor explicit timeout" do
    service.class.timeout = 1
    service.class.poll_interval = 0

    elapsed = 0

    timeout(2) do
      expect do
        model.wait_for!(0.1) { sleep(0.2); elapsed += 0.2; elapsed > 0.2 }
      end.to raise_exception(WaitForService::Timeout)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
cistern-0.9.0 spec/wait_for_spec.rb
cistern-0.8.0 spec/wait_for_spec.rb
cistern-0.7.1 spec/wait_for_spec.rb
cistern-0.7.0 spec/wait_for_spec.rb