Permalink
Browse files

Allow URL style connections descriptors

git-svn-id: http://svn.codehaus.org/stomp/ruby/trunk@44 fd4e7336-3dff-0310-b68a-b6615a75f13b
  • Loading branch information...
1 parent 2703b1b commit ae09910f3843c5f2636a5451aa6e644a150d2c7b brianm committed Jan 15, 2007
Showing with 413 additions and 5 deletions.
  1. +1 −1 Rakefile
  2. +18 −4 lib/stomp.rb
  3. +197 −0 test/test_url_connections.rb
  4. +197 −0 test/test_url_connections_noauth.rb
View
@@ -24,7 +24,7 @@ task :default => ['test']
spec = Gem::Specification.new do |s|
s.name = "stomp"
- s.version = "1.0.3"
+ s.version = "1.0.4"
s.author = "Brian McCallister"
s.email = "brianm@apache.org"
s.homepage = "http://stomp.codehaus.org/"
View
@@ -24,7 +24,7 @@ module Stomp
class Connection
def Connection.open(login = "", passcode = "", host='localhost', port=61613, reliable=FALSE, reconnectDelay=5)
- Connection.new login, passcode, host, port, reliable, reconnectDelay
+ Connection.new login, passcode, host, port, reliable, reconnectDelay
end
# Create a connection, requires a login and passcode.
@@ -260,7 +260,21 @@ class Client
# Accepts a username (default ""), password (default ""),
# host (default localhost), and port (default 61613)
- def initialize user="", pass="", host="localhost", port=61613, reliable=FALSE
+ def initialize user="", pass="", host="localhost", port=61613, reliable=false
+ if user =~ /stomp:\/\/(\w+):(\d+)/
+ user = ""
+ pass = ""
+ host = $1
+ port = $2
+ reliable = false
+ elsif user =~ /stomp:\/\/(\w+):(\w+)@(\w+):(\d+)/
+ user = $1
+ pass = $2
+ host = $3
+ port = $4
+ reliable = false
+ end
+
@id_mutex = Mutex.new
@ids = 1
@connection = Connection.open user, pass, host, port, reliable
@@ -295,8 +309,8 @@ def join
# Accepts a username (default ""), password (default ""),
# host (default localhost), and port (default 61613)
- def self.open user="", pass="", host="localhost", port=61613
- Client.new user, pass, host, port
+ def self.open user="", pass="", host="localhost", port=61613, reliable=false
+ Client.new user, pass, host, port, reliable
end
# Begin a transaction by name
@@ -0,0 +1,197 @@
+# Copyright 2005-2006 Brian McCallister
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
+require 'test/unit'
+require 'timeout'
+require 'stomp'
+
+class TestClient < Test::Unit::TestCase
+
+ def setup
+ @client = Stomp::Client.open "stomp://fred:secret@localhost:61613"
+ end
+
+ def teardown
+ @client.close
+ end
+
+ def message_text
+ "test_client#" + name()
+ end
+
+ def destination
+ "/queue/test/ruby/client/" + name()
+ end
+
+ def test_subscribe_requires_block
+ assert_raise(RuntimeError) do
+ @client.subscribe destination
+ end
+ end
+
+ def test_asynch_subscribe
+ received = false
+ @client.subscribe(destination) {|msg| received = msg}
+ @client.send destination, message_text
+ sleep 0.01 until received
+
+ assert_equal message_text, received.body
+ end
+
+ def test_ack_api_works
+ @client.send destination, message_text
+
+ received = nil
+ @client.subscribe(destination, :ack => 'client') {|msg| received = msg}
+ sleep 0.01 until received
+ assert_equal message_text, received.body
+
+ receipt = nil
+ @client.acknowledge(received) {|r| receipt = r}
+ sleep 0.01 until receipt
+ assert_not_nil receipt.headers['receipt-id']
+ end
+
+ # BROKEN
+ def test_noack
+ @client.send destination, message_text
+
+ received = nil
+ @client.subscribe(destination, :ack => :client) {|msg| received = msg}
+ sleep 0.01 until received
+ assert_equal message_text, received.body
+ @client.close
+
+ # was never acked so should be resent to next client
+
+ @client = Stomp::Client.new "test", "user", "localhost", 61613
+ received = nil
+ @client.subscribe(destination) {|msg| received = msg}
+ sleep 0.01 until received
+
+ assert_equal message_text, received.body
+ end
+
+ def test_receipts
+ receipt = false
+ @client.send(destination, message_text) {|r| receipt = r}
+ sleep 0.1 until receipt
+
+ message = nil
+ @client.subscribe(destination) {|m| message = m}
+ sleep 0.1 until message
+ assert_equal message_text, message.body
+ end
+
+ def test_send_then_sub
+ @client.send destination, message_text
+ message = nil
+ @client.subscribe(destination) {|m| message = m}
+ sleep 0.01 until message
+
+ assert_equal message_text, message.body
+ end
+
+ def test_transactional_send
+ @client.begin 'tx1'
+ @client.send destination, message_text, :transaction => 'tx1'
+ @client.commit 'tx1'
+
+ message = nil
+ @client.subscribe(destination) {|m| message = m}
+ sleep 0.01 until message
+
+ assert_equal message_text, message.body
+ end
+
+ def test_transaction_send_then_rollback
+ @client.begin 'tx1'
+ @client.send destination, "first_message", :transaction => 'tx1'
+ @client.abort 'tx1'
+
+ @client.begin 'tx1'
+ @client.send destination, "second_message", :transaction => 'tx1'
+ @client.commit 'tx1'
+
+ message = nil
+ @client.subscribe(destination) {|m| message = m}
+ sleep 0.01 until message
+ assert_equal "second_message", message.body
+ end
+
+ def test_transaction_ack_rollback_with_new_client
+ @client.send destination, message_text
+
+ @client.begin 'tx1'
+ message = nil
+ @client.subscribe(destination, :ack => 'client') {|m| message = m}
+ sleep 0.01 until message
+ assert_equal message_text, message.body
+ @client.acknowledge message, :transaction => 'tx1'
+ message = nil
+ @client.abort 'tx1'
+
+ # lets recreate the connection
+ teardown
+ setup
+ @client.subscribe(destination, :ack => 'client') {|m| message = m}
+
+ Timeout::timeout(4) do
+ sleep 0.01 until message
+ end
+ assert_not_nil message
+ assert_equal message_text, message.body
+
+ @client.begin 'tx2'
+ @client.acknowledge message, :transaction => 'tx2'
+ @client.commit 'tx2'
+ end
+
+ def test_unsubscribe
+ message = nil
+ client = Stomp::Client.new("test", "user", "localhost", 61613, true)
+ client.subscribe(destination, :ack => 'client') { |m| message = m }
+ @client.send destination, message_text
+ Timeout::timeout(4) do
+ sleep 0.01 until message
+ end
+ client.unsubscribe destination # was throwing exception on unsub at one point
+
+ end
+
+ def test_transaction_with_client_side_redelivery
+ @client.send destination, message_text
+
+ @client.begin 'tx1'
+ message = nil
+ @client.subscribe(destination, :ack => 'client') { |m| message = m }
+
+ sleep 0.1 while message == nil
+
+ assert_equal message_text, message.body
+ @client.acknowledge message, :transaction => 'tx1'
+ message = nil
+ @client.abort 'tx1'
+
+ sleep 0.1 while message == nil
+
+ assert_not_nil message
+ assert_equal message_text, message.body
+
+ @client.begin 'tx2'
+ @client.acknowledge message, :transaction => 'tx2'
+ @client.commit 'tx2'
+ end
+end
Oops, something went wrong.

0 comments on commit ae09910

Please sign in to comment.