Skip to content

Commit

Permalink
* Added guard against registering callbacks with the dispatcher when …
Browse files Browse the repository at this point in the history
…running outside of Rails

* Made all tests run successfully outside Rails.  Just run "rake test"
  • Loading branch information
donv committed Jun 10, 2009
1 parent d66d508 commit 551d721
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 25 deletions.
1 change: 1 addition & 0 deletions lib/activemessaging/adapters/asqs.rb
Expand Up @@ -4,6 +4,7 @@
require 'base64'
require 'cgi'
require 'time'
require 'activemessaging/adapter'

module ActiveMessaging
module Adapters
Expand Down
1 change: 1 addition & 0 deletions lib/activemessaging/adapters/test.rb
@@ -1,3 +1,4 @@
require 'activemessaging/adapter'

module ActiveMessaging
module Adapters
Expand Down
2 changes: 1 addition & 1 deletion lib/activemessaging/gateway.rb
Expand Up @@ -393,7 +393,7 @@ def matches?(message)
def subscribe
ActiveMessaging.logger.error "=> Subscribing to #{destination.value} (processed by #{processor_class})"
Gateway.connection(@destination.broker_name).subscribe(@destination.value, subscribe_headers)
# TODO (uwe): Not sure why this needs to happen here
# FIXME (uwe): Not sure why this needs to happen here
@processor = @processor_class.new
end

Expand Down
25 changes: 14 additions & 11 deletions lib/activemessaging/support.rb
@@ -1,14 +1,17 @@
require 'dispatcher' unless defined?(::Dispatcher)
::Dispatcher.class_eval do
if defined? Rails
ActiveMessaging.logger.debug "Rails available: Adding reload hooks."
require 'dispatcher' unless defined?(::Dispatcher)
::Dispatcher.class_eval do

def self.prepare_application_for_dispatch
disp = new(STDOUT)
disp.run_callbacks :before_dispatch
end

def self.reset_application_after_dispatch
disp = new(STDOUT)
disp.run_callbacks :after_dispatch, :enumerator => :reverse_each
end

def self.prepare_application_for_dispatch
disp = new(STDOUT)
disp.run_callbacks :before_dispatch
end

def self.reset_application_after_dispatch
disp = new(STDOUT)
disp.run_callbacks :after_dispatch, :enumerator => :reverse_each
end

end
20 changes: 20 additions & 0 deletions lib/activemessaging/test_helper.rb
Expand Up @@ -3,10 +3,22 @@


module ActiveMessaging #:nodoc:
@@logger = nil

# def self.reload_activemessaging
# end

def self.logger
@@logger ||= MockLogger.new
@@logger
end

class AbortMessageException < Exception #:nodoc:
end

class StopFilterException < Exception #:nodoc:
end

class Gateway

def self.reset
Expand Down Expand Up @@ -170,5 +182,13 @@ def assert_has_messages destination
assert !destination.nil? && !destination.messages.empty?, error_message
end
end

class MockLogger
def error(*args) ; end
def warn(*args) ; end
def info(*args) ; end
def debug(*args) ; end
end

end

2 changes: 1 addition & 1 deletion lib/activemessaging/trace_filter.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(options)
def process message, routing

unless ( routing[:destination].name == @queue ) then
puts "\nTrace: direction = #{routing[:direction]} publisher=#{routing[:publisher]} queue=#{routing[:destination].name} @queue=#{@queue}\n"
ActiveMessaging.logger.debug "Trace: direction = #{routing[:direction]} publisher=#{routing[:publisher]} queue=#{routing[:destination].name} @queue=#{@queue}"
if routing[:direction].to_sym==:outgoing then
"trace from outgoing"
publish @queue, "<sent>"+
Expand Down
4 changes: 4 additions & 0 deletions test/app/config/broker.yml
@@ -0,0 +1,4 @@
# Yaml config file for testing ActiveMessaging
test:
adapter: test
reliable: false
1 change: 1 addition & 0 deletions test/asqs_test.rb
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/test_helper'
require 'activemessaging/adapters/asqs'

class AsqsTest < Test::Unit::TestCase

Expand Down
1 change: 1 addition & 0 deletions test/filter_test.rb
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/test_helper'
require 'activemessaging/filter'

module ActiveMessaging #:nodoc:
def self.reload_activemessaging
Expand Down
2 changes: 1 addition & 1 deletion test/gateway_test.rb
Expand Up @@ -47,7 +47,7 @@ class TestRetryProcessor < ActiveMessaging::Processor
include ActiveMessaging::MessageSender
#subscribes_to :testqueue
def on_message(message)
puts "TestRetryProcessor - about to raise exception"
#puts "TestRetryProcessor - about to raise exception"
raise ActiveMessaging::AbortMessageException.new("Cause a retry!")
end
end
Expand Down
25 changes: 14 additions & 11 deletions test/test_helper.rb
@@ -1,16 +1,19 @@
# load the rails environment
# TODO currently requires you to run tests as a installed plugin, we should try to fix this
ENV['APP_ENV'] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
ENV['APP_ENV'] = 'test'
APP_ENV = 'test'
if defined? Rails
APP_ROOT = RAILS_ROOT
require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
else
APP_ROOT = File.dirname(__FILE__) + '/app'
require 'rubygems'
require 'activesupport'
require File.dirname(__FILE__) + '/../lib/activemessaging/message_sender'
require File.dirname(__FILE__) + '/../lib/activemessaging/processor'
require File.dirname(__FILE__) + '/../lib/activemessaging/gateway'
end

# load other libraries
require 'test/unit'

# load activemessaging
# TODO this is already loaded automatically by starting Rails
# but we may need to do this if we want to run a13g tests without Rails
#require File.dirname(__FILE__) + '/../lib/activemessaging/processor'
#require File.dirname(__FILE__) + '/../lib/activemessaging/gateway'
require File.dirname(__FILE__) + '/../lib/activemessaging/test_helper'


require 'activemessaging/adapters/test'
1 change: 1 addition & 0 deletions test/tracer_test.rb
@@ -1,4 +1,5 @@
require File.dirname(__FILE__) + '/test_helper'
require 'activemessaging/trace_filter'

module ActiveMessaging #:nodoc:
def self.reload_activemessaging
Expand Down

0 comments on commit 551d721

Please sign in to comment.