Sha256: 21e7e79ca6b18009489473ad86bc45ac84cb8f6f57bbbce895e702681bbc04d2

Contents?: true

Size: 1.54 KB

Versions: 4

Compression:

Stored size: 1.54 KB

Contents

require 'spec_helper'
require 'em-proxy'
require 'logger'

describe "Klomp reconnect logic", :acceptance => true do

  include_context :acceptance_client
  Given(:origin_server) { "127.0.0.1:61613" }
  Given(:server) { "127.0.0.1:62623" }
  Given(:options) { Hash[*%w(login passcode).zip(credentials).flatten] }

  it "reconnects after a server goes down" do
    # Publish and receive a message
    klomp.publish "/queue/greeting", "hello"
    incoming = []
    klomp.subscribe "/queue/greeting" do |msg|
      incoming << msg
    end
    sleep 2
    incoming.should_not be_empty

    # Server disappears
    stop_proxy
    expect { klomp.publish "/queue/greeting", "hello" }.to raise_error
    klomp.should_not be_connected

    # Server reappears
    start_proxy
    klomp.should be_connected

    # Subscription is re-established
    incoming = []
    klomp.publish "/queue/greeting", "hello"
    sleep 2
    incoming.should_not be_empty

    klomp.unsubscribe "/queue/greeting"
  end

  before { start_proxy }
  after { stop_proxy }

  def server_to_hash(s)
    Hash[*[:host, :port].zip(s.split(':')).flatten]
  end

  def start_proxy
    listen, forward = server_to_hash(server), server_to_hash(origin_server)
    @pid = fork do
      [STDIN, STDOUT, STDERR].each {|s| s.reopen "/dev/null" }
      Proxy.start listen do |conn|
        conn.server :origin, forward.merge(:relay_client => true, :relay_server => true)
      end
    end
    sleep 2
  end

  def stop_proxy
    if @pid
      Process.kill "TERM", @pid
      Process.waitpid @pid
      @pid = nil
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
klomp-1.0.8 spec/acceptance/reconnect_spec.rb
klomp-1.0.7 spec/acceptance/reconnect_spec.rb
klomp-1.0.6 spec/acceptance/reconnect_spec.rb
klomp-1.0.5 spec/acceptance/reconnect_spec.rb