Permalink
Browse files

working ping of rabbitmq server

  • Loading branch information...
1 parent bd493b3 commit 1d634a1d2c2e5d90295d0f59111cc3b1521e872a @rick committed Nov 26, 2008
Showing with 61 additions and 6 deletions.
  1. +39 −5 spec/rake/rabbitmq.rake_spec.rb
  2. +22 −1 tasks/rabbitmq.rake
@@ -1,8 +1,9 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
require 'rake'
+require 'rubygems'
+require 'mq'
describe 'rake tasks to control rabbitmq server' do
-
before :each do
Rake.application = @rake = Rake::Application.new
load File.expand_path(File.dirname(__FILE__) + '/../../tasks/rabbitmq.rake')
@@ -12,7 +13,30 @@
Rake.application = nil
end
+ describe 'pinging the rabbitmq server' do
+ before :each do
+ stub(EM).run
+ end
+
+ it 'should work without arguments' do
+ lambda { RabbitMQ.ping }.should_not raise_error(ArgumentError)
+ end
+
+ it 'should not allow arguments' do
+ lambda { RabbitMQ.ping(:foo) }.should raise_error(ArgumentError)
+ end
+
+ it 'should set up an eventmachine loop to contact the server' do
+ mock(EM).run
+ RabbitMQ.ping
+ end
+ end
+
describe 'determining if rabbitmq is running' do
+ before :each do
+ stub(RabbitMQ).ping
+ end
+
it 'should work without arguments' do
lambda { RabbitMQ.running? }.should_not raise_error(ArgumentError)
end
@@ -21,11 +45,21 @@
lambda { RabbitMQ.running?(:foo) }.should raise_error(ArgumentError)
end
- it 'should attempt to connect to rabbitmq'
+ it 'should attempt to connect to rabbitmq' do
+ mock(RabbitMQ).ping
+ RabbitMQ.running?
+ end
- it 'should return true if rabbitmq is running'
+ it 'should return true if rabbitmq is running' do
+ stub(RabbitMQ).ping { raise RuntimeError }
+ RabbitMQ.should be_running
+ end
- it 'should return false if rabbitmq is not running'
+ it 'should return false if rabbitmq is not running' do
+ stub(RabbitMQ).ping { raise AMQP::Error }
+ RabbitMQ.should_not be_running
+ end
+
end
describe 'finding the erlang home path' do
@@ -198,7 +232,7 @@
end
it 'should start the rabbitmq server' do
- mock(RabbitMQ).system("./rabbitmq-server -detached")
+ mock(RabbitMQ).system("./rabbitmq-server")
RabbitMQ.start
end
end
View
@@ -1,10 +1,31 @@
require File.expand_path(File.dirname(__FILE__)) + '/rake_install'
+require 'rubygems'
+require 'mq'
class RabbitMQ
class << self
def running?
+ ping
+ rescue AMQP::Error
+ return false
+ rescue RuntimeError
+ return true
end
+ def ping
+ EM.run do
+ # This is basically the minimal working set I could derive
+ # from the AMQP examples to do a "ping" of the server.
+ # Note that we're going to raise an exception either way, and
+ # the exceptions ensure that we can exit the eventmachine loop
+ # and can use rescue clauses in the caller to see what happened.
+ ping = MQ.new.fanout('ping')
+ EM.add_periodic_timer(1) { ping.publish('ping'); raise "done" }
+ amq = MQ.new
+ amq.queue('pong').bind(amq.fanout('ping')).subscribe {|packet| puts packet }
+ end
+ end
+
def setup_environment
server = File.expand_path(File.dirname(__FILE__) + '/../run/')
ENV['ERLANG_HOME'] = find_erlang_home
@@ -29,7 +50,7 @@ class RabbitMQ
def start
setup_environment
Dir.chdir(File.expand_path(File.dirname(__FILE__) + '/../run/rabbitmq/sbin'))
- system("./rabbitmq-server -detached")
+ system("./rabbitmq-server")
end
def stop

0 comments on commit 1d634a1

Please sign in to comment.