Permalink
Browse files

Add a HA Singleton integ test under spec-domain

  • Loading branch information...
1 parent 3ba8de8 commit a4bae45195d4e8e5a5f87b193acbfbd0baf26170 @bbrowning bbrowning committed Jul 18, 2012
@@ -0,0 +1,29 @@
+require 'java'
+require 'torquebox'
+
+class NodeNameService
+ include TorqueBox::Injectors
+
+ def initialize(options={})
+ @queue = inject('/queues/node_name')
+ @done = false
+ end
+
+ def start
+ @thread = Thread.new do
+ loop_once until @done
+ end
+ end
+
+ def loop_once
+ @queue.receive_and_publish(:timeout => 500) do |message|
+ java.lang.System.getProperty('jboss.node.name')
+ end
+ end
+
+ def stop
+ @done = true
+ @thread.join
+ end
+
+end
@@ -0,0 +1,7 @@
+services:
+ NodeNameService:
+ config:
+ queue: /queues/node_name
+queues:
+ /queues/node_name:
+ durable: false
@@ -1,4 +1,4 @@
-require 'spec_helper'
+require 'spec_helper_domain'
shared_examples_for "basic rack" do
@@ -1,4 +1,4 @@
-require 'spec_helper'
+require 'spec_helper_domain'
require 'torquebox-cache'
@@ -0,0 +1,38 @@
+require 'spec_helper_domain'
+require 'torquebox-messaging'
+
+describe 'ha singleton' do
+
+ deploy <<-END.gsub(/^ {4}/,'')
+
+ application:
+ root: #{File.dirname(__FILE__)}/../apps/alacarte/ha-services
+ env: development
+ ruby:
+ version: #{RUBY_VERSION[0,3]}
+
+ END
+
+ it 'should failover services' do
+ stop_server(:server1)
+ ensure_service_master(:server2)
+ start_server(:server1)
+ ensure_service_master(:server2)
+ stop_server(:server2)
+ ensure_service_master(:server1)
+ start_server(:server2)
+ ensure_service_master(:server1)
+ end
+
+ def ensure_service_master(server)
+ queue = TorqueBox::Messaging::Queue.new('/queues/node_name',
+ :host => domain_host_for(server),
+ :port => domain_port_for(server, 5445))
+ condition = lambda { |message| message != nil }
+ message = wait_for(30, 1, condition) do
+ queue.publish_and_receive('node_name', :timeout => 5000)
+ end
+ message.should == "master:#{domain_server_config_for(server)}"
+ end
+
+end
@@ -1,4 +1,4 @@
-require 'spec_helper'
+require 'spec_helper_domain'
require 'torquebox-messaging'
describe 'messaging clustering' do
@@ -0,0 +1,53 @@
+require 'spec_helper'
+
+def domain_server
+ Thread.current[:app_server]
+end
+
+def domain_host_for(server)
+ 'localhost'
+end
+
+def domain_port_for(server, base_port)
+ port_offset = 100
+ server == :server1 ? base_port : base_port + port_offset
+end
+
+def domain_server_config_for(server)
+ server == :server1 ? 'server-01' : 'server-02'
+end
+
+def start_server(server)
+ server_config = domain_server_config_for(server)
+ domain_server.send(:api, :operation => 'start',
+ :address => [{ :host => 'master' },
+ { 'server-config' => server_config }])
+ wait_for_status(server_config, 'STARTED', 30)
+ # Sleep to give the application time to deploy after the server
+ # has started
+ sleep 20
+end
+
+def stop_server(server)
+ server_config = domain_server_config_for(server)
+ domain_server.send(:api, :operation => 'stop',
+ :address => [{ :host => 'master' },
+ { 'server-config' => server_config }])
+ wait_for_status(server_config, 'STOPPED', 30)
+end
+
+def wait_for_status(server_config, expected_status, timeout)
+ condition = lambda { |status| status == expected_status }
+ wait_for(timeout, 2, condition) do
+ server_status(server_config)
+ end
+end
+
+def server_status(server_config)
+ JSON.parse(domain_server.send(:api, :operation => 'read-resource',
+ :address => [{ :host => 'master' },
+ { 'server-config' => server_config }],
+ 'include-runtime' => true))['result']['status']
+rescue
+ nil
+end
@@ -73,13 +73,14 @@ def assert_paths_are_equal(actual, expected)
normalize_path(actual).should eql(normalize_path(expected))
end
-def domain_host_for(server)
- 'localhost'
-end
-
-def domain_port_for(server, base_port)
- port_offset = 100
- server == :server1 ? base_port : base_port + port_offset
+def wait_for(timeout, interval, condition)
+ start_time = Time.now
+ while (Time.now - start_time < timeout) do
+ value = yield
+ return value if condition.call(value)
+ sleep(interval)
+ end
+ nil
end
# JRuby 1.6.7.2 in 1.9 mode has a bug where it needs ObjectSpace for

0 comments on commit a4bae45

Please sign in to comment.