Sha256: dfa14d63216ce7f5a125a8609a70de15edfb859c658d9b8fea03df40d6ed95cb

Contents?: true

Size: 1.37 KB

Versions: 7

Compression:

Stored size: 1.37 KB

Contents

require 'spec_helper'

describe Klomp::Sentinel do

  Given(:connection) { double "Connection", connected?:false, reconnect:nil }
  Given(:sentinel) { Klomp::Sentinel.new connection }
  Given(:thread) { double Thread }
  Given do
    Thread.stub!(:new).and_return {|*args,&blk| thread.stub!(:block => blk); thread }
  end

  context "does nothing if the connection is already connected" do

    Given { connection.stub!(connected?: true) }

    When { sentinel }

    Then { Thread.should_not have_received(:new) }

  end

  context "reconnects the connection" do

    When { sentinel ; thread.block.call }

    Then { connection.should have_received(:reconnect) }

  end

  context "does fibonacci backoff if reconnection fails" do

    Given(:number_of_reconnects) { 7 }
    Given do
      count = 0
      connection.stub!(:reconnect).and_return { count += 1; raise Klomp::Error if count < number_of_reconnects }
    end

    When do
      sentinel.stub!(:sleep)
      thread.block.call
    end

    Then do
      connection.should have_received(:reconnect).exactly(number_of_reconnects).times
    end

    Then do
      sentinel.should have_received(:sleep).with(1).twice
      sentinel.should have_received(:sleep).with(2)
      sentinel.should have_received(:sleep).with(3)
      sentinel.should have_received(:sleep).with(5)
      sentinel.should have_received(:sleep).with(8)
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
klomp-1.0.6 spec/klomp/sentinel_spec.rb
klomp-1.0.5 spec/klomp/sentinel_spec.rb
klomp-1.0.4 spec/klomp/sentinel_spec.rb
klomp-1.0.3 spec/klomp/sentinel_spec.rb
klomp-1.0.2 spec/klomp/sentinel_spec.rb
klomp-1.0.1 spec/klomp/sentinel_spec.rb
klomp-1.0.0 spec/klomp/sentinel_spec.rb