Class: TestConnection

### Home Classes Methods

### In Files

- test/test_connection.rb

### Parent

Test::Unit::TestCase

### Methods

- #setup
- #teardown
- #test_client_ack_with_symbol_10
- #test_client_ack_with_symbol_11
- #test_client_ack_with_symbol_12
- #test_closed_checks_conn
- #test_con_iss99_ok
- #test_conn10_simple
- #test_connection_closed?
- #test_connection_exists
- #test_connection_frame
- #test_connection_open?
- #test_disconnect_receipt
- #test_duplicate_subscription
- #test_embedded_null
- #test_empty_header_key
- #test_empty_header_value
- #test_explicit_receive
- #test_jruby_presence
- #test_message_to_s
- #test_messages_with_multipleLine_ends
- #test_multi_thread_poll
- #test_multi_thread_receive
- #test_nack11p_0010
- #test_nil_body
- #test_nil_connparms
- #test_no_length
- #test_poll_async
- #test_publish_two_messages
- #test_receipt
- #test_response_is_instance_of_message_class
- #test_thread_hang_one
- #test_thread_poll_one
- #test_transaction

### Included Modules

- TestBase

### Files

- CHANGELOG.rdoc
- LICENSE

### Class/Module Index

Quicksearch

- Stomp
- Stomp::Client
- Stomp::Connection
- Stomp::Error
- Stomp::Error::BrokerException
- Stomp::Error::ConnectReadTimeout
- Stomp::Error::DuplicateSubscription
- Stomp::Error::InvalidFormat
- Stomp::Error::InvalidHeartBeatHeaderError
- Stomp::Error::InvalidMessageLength
- Stomp::Error::InvalidServerCommand
- Stomp::Error::LoggerConnectionError
- Stomp::Error::MalformedFailoverOptionsError
- Stomp::Error::MaxReconnectAttempts
- Stomp::Error::MessageIDRequiredError
- Stomp::Error::NilMessageError
- Stomp::Error::NoCurrentConnection
- Stomp::Error::PacketParsingTimeout
- Stomp::Error::ProducerFlowControlException
- Stomp::Error::ProtocolError11p
- Stomp::Error::ProtocolErrorConnect
- Stomp::Error::ProtocolErrorEmptyHeaderKey
- Stomp::Error::ProtocolErrorEmptyHeaderValue
- Stomp::Error::ProtocolException
- Stomp::Error::ReadReceiptTimeoutException
- Stomp::Error::SSLClientParamsError
- Stomp::Error::SSLNoCertFileError
- Stomp::Error::SSLNoKeyFileError
- Stomp::Error::SSLNoTruststoreFileError
- Stomp::Error::SSLUnreadableCertFileError
- Stomp::Error::SSLUnreadableKeyFileError
- Stomp::Error::SSLUnreadableTruststoreFileError
- Stomp::Error::SocketOpenTimeout
- Stomp::Error::StartTimeoutException
- Stomp::Error::StompException
- Stomp::Error::StompServerError
- Stomp::Error::SubscriptionRequiredError
- Stomp::Error::UTF8ValidationError
- Stomp::Error::UnsupportedProtocolError
- Stomp::HeaderCodec
- Stomp::Message
- Stomp::NullLogger
- Stomp::SSLParams
- Client11Example1
- Client11PutGet1
- Connection11Example1
- Connection11Example2
- ExampleConsumer
- ExamplePublisher
- ExampleRubyCiphers
- ExampleSSL1
- ExampleSSL1C
- ExampleSSL2
- ExampleSSL2C
- ExampleSSL3
- ExampleSSL3C
- ExampleSSL4
- ExampleSSL4C
- ExampleSSLCtxOptions
- ExampleSSLNewParm
- ExampleTopicConsumer
- ExampleTopicPublisher
- Hash
- HeartBeatExample1
- LoggerExample
- Object
- Publish11Example1
- Receive11Example1
- Receive11Example2
- RepeatedHeadersExample
- SSLLoggerExample
- Slogger
- Stomp11Common
- TestAnonymous
- TestBase
- TestClient
- TestConnection
- TestConnection1P
- TestMessage
- TestSSL
- TestURLLogins
- Tlogger

No matching classes.

# TestConnection

Main class for testing Stomp::Connection instances.

### Public Instance Methods

setup() click to toggle source

    # File test/test_connection.rb, line 17
    def setup
      @conn = get_connection()
      # Data for multi_thread tests
      @max_threads = 20
      @max_msgs = 100
    end

teardown() click to toggle source

    # File test/test_connection.rb, line 24
    def teardown
      @conn.disconnect if @conn.open? # allow tests to disconnect
    end

test_client_ack_with_symbol_10() click to toggle source

Test ACKs for Stomp 1.0

    # File test/test_connection.rb, line 84
    def test_client_ack_with_symbol_10
      if @conn.protocol != Stomp::SPL_10
        assert true
        return
      end
      queue = make_destination()
      @conn.subscribe queue, :ack => :client
      @conn.publish queue, "test_stomp#test_client_ack_with_symbol_10"
      msg = @conn.receive
      assert_nothing_raised {
        # ACK has one required header, message-id, which must contain a value
        # matching the message-id for the MESSAGE being acknowledged.
        @conn.ack msg.headers['message-id']
      }
      checkEmsg(@conn)
    end

test_client_ack_with_symbol_11() click to toggle source

Test ACKs for Stomp 1.1

    # File test/test_connection.rb, line 102
    def test_client_ack_with_symbol_11
      if @conn.protocol != Stomp::SPL_11
        assert true
        return
      end
      sid = @conn.uuid()
      queue = make_destination()
      @conn.subscribe queue, :ack => :client, :id => sid
      @conn.publish queue, "test_stomp#test_client_ack_with_symbol_11"
      msg = @conn.receive
      assert_nothing_raised {
        # ACK has two REQUIRED headers: message-id, which MUST contain a value
        # matching the message-id for the MESSAGE being acknowledged and
        # subscription, which MUST be set to match the value of the subscription's
        # id header.
        @conn.ack msg.headers['message-id'], :subscription => msg.headers['subscription']
      }
      checkEmsg(@conn)
    end

test_client_ack_with_symbol_12() click to toggle source

Test ACKs for Stomp 1.2

    # File test/test_connection.rb, line 123
    def test_client_ack_with_symbol_12
      if @conn.protocol != Stomp::SPL_12
        assert true
        return
      end
      sid = @conn.uuid()
      queue = make_destination()
      @conn.subscribe queue, :ack => :client, :id => sid
      @conn.publish queue, "test_stomp#test_client_ack_with_symbol_11"
      msg = @conn.receive
      assert_nothing_raised {
        # The ACK frame MUST include an id header matching the ack header
        # of the MESSAGE being acknowledged.
        @conn.ack msg.headers['ack']
      }
      checkEmsg(@conn)
    end

test_closed_checks_conn() click to toggle source

Test that methods detect a closed connection.

    # File test/test_connection.rb, line 165
    def test_closed_checks_conn
      @conn.disconnect
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        @conn.ack("dummy_data")
      end
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        @conn.begin("dummy_data")
      end
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        @conn.commit("dummy_data")
      end
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        @conn.abort("dummy_data")
      end
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        conn_subscribe("dummy_data")
      end
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        @conn.unsubscribe("dummy_data")
      end
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        @conn.publish("dummy_data","dummy_data")
      end
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        @conn.unreceive("dummy_data")
      end
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        @conn.disconnect("dummy_data")
      end
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        m = @conn.receive
      end
      #
      assert_raise Stomp::Error::NoCurrentConnection do
        m = @conn.poll
      end
    end

test_con_iss99_ok() click to toggle source

test issue99, OK values

    # File test/test_connection.rb, line 611
    def test_con_iss99_ok
      return unless host() == "localhost" && port() == 61613
      #
      ok_vals = dflt_data_ok()
      ok_vals.each do |hsv|
        assert_nothing_raised {
          conn = Stomp::Connection.new(hsv)
          conn.disconnect
        }
      end
    end

test_conn10_simple() click to toggle source

Test to illustrate Issue #44. Prior to a fix for #44, these tests would fail only when connecting to a pure STOMP 1.0 server that does not return a 'version' header at all.

    # File test/test_connection.rb, line 483
    def test_conn10_simple
      @conn.disconnect
      #
      vhost = ENV['STOMP_RABBIT'] ? "/" : host
      hash = { :hosts => [
        {:login => user, :passcode => passcode, :host => host, :port => port, :ssl => false},
        ],
        :connect_headers => {"accept-version" => "1.0", "host" => vhost},
        :reliable => false,
      }
      c = nil
      assert_nothing_raised {
        c = Stomp::Connection.new(hash)
      }
      c.disconnect if c
      #
      hash = { :hosts => [
        {:login => user, :passcode => passcode, :host => host, :port => port, :ssl => false},
        ],
        :connect_headers => {"accept-version" => "3.14159,1.0,12.0", "host" => vhost},
        :reliable => false,
      }
      c = nil
      assert_nothing_raised {
        c = Stomp::Connection.new(hash)
      }
      c.disconnect if c
    end

test_connection_closed?() click to toggle source

Test connection closed checking.

    # File test/test_connection.rb, line 158
    def test_connection_closed?
      assert_equal false, @conn.closed?
      @conn.disconnect
      assert_equal true, @conn.closed?
    end

test_connection_exists() click to toggle source

Test basic connection creation.

    # File test/test_connection.rb, line 29
    def test_connection_exists
      assert_not_nil @conn
    end

test_connection_frame() click to toggle source

Test that a connection frame is present.

    # File test/test_connection.rb, line 232
    def test_connection_frame
      assert_not_nil @conn.connection_frame
    end

test_connection_open?() click to toggle source

Test connection open checking.

    # File test/test_connection.rb, line 151
    def test_connection_open?
      assert_equal true , @conn.open?
      @conn.disconnect
      assert_equal false, @conn.open?
    end

test_disconnect_receipt() click to toggle source

Test asking for a receipt on disconnect.

    # File test/test_connection.rb, line 74
    def test_disconnect_receipt
      @conn.disconnect :receipt => "abc123"
      assert_nothing_raised {
        assert_not_nil(@conn.disconnect_receipt, "should have a receipt")
        assert_equal(@conn.disconnect_receipt.headers['receipt-id'],
          "abc123", "receipt sent and received should match")
      }
    end

test_duplicate_subscription() click to toggle source

Test duplicate subscriptions.

    # File test/test_connection.rb, line 417
    def test_duplicate_subscription
      @conn.disconnect # not reliable
      @conn = Stomp::Connection.open(user, passcode, host, port, true) # reliable
      dest = make_destination
      conn_subscribe dest
      #
      assert_raise Stomp::Error::DuplicateSubscription do
        conn_subscribe dest
      end
      checkEmsg(@conn)
    end

test_embedded_null() click to toggle source

Test a message with 0x00 embedded in the body.

    # File test/test_connection.rb, line 142
    def test_embedded_null
      conn_subscribe make_destination
      @conn.publish make_destination, "a\00""
      msg = @conn.receive
      assert_equal "a\00"" , msg.body
      checkEmsg(@conn)
    end

test_empty_header_key() click to toggle source

Test that methods detect an empty header key.

    # File test/test_connection.rb, line 522
    def test_empty_header_key
      #
      bad_headers = {"a" => "11", "" => "emptykey", :c => "ccc"}
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderKey do
        @conn.ack("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderKey do
        @conn.nack("dummy_data", bad_headers)
      end if @conn.protocol != Stomp::SPL_10
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderKey do
        @conn.begin("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderKey do
        @conn.commit("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderKey do
        @conn.abort("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderKey do
        @conn.subscribe("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderKey do
        @conn.unsubscribe("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderKey do
        @conn.publish("dummy_data","dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderKey do
        @conn.disconnect(bad_headers)
      end
    end

test_empty_header_value() click to toggle source

Test that methods detect an empty header value. STOMP 1.0 only.

    # File test/test_connection.rb, line 565
    def test_empty_header_value
      if @conn.protocol != Stomp::SPL_10
        assert true
        return
      end
      #
      bad_headers = {"a" => "11", "hdra" => "", :c => "ccc"}
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderValue do
        @conn.ack("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderValue do
        @conn.nack("dummy_data", bad_headers)
      end if @conn.protocol != Stomp::SPL_10
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderValue do
        @conn.begin("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderValue do
        @conn.commit("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderValue do
        @conn.abort("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderValue do
        @conn.subscribe("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderValue do
        @conn.unsubscribe("dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderValue do
        @conn.publish("dummy_data","dummy_data", bad_headers)
      end
      #
      assert_raise Stomp::Error::ProtocolErrorEmptyHeaderValue do
        @conn.disconnect(bad_headers)
      end
    end

test_explicit_receive() click to toggle source

Test direct / explicit receive.

    # File test/test_connection.rb, line 58
    def test_explicit_receive
      conn_subscribe make_destination
      @conn.publish make_destination, "test_stomp#test_explicit_receive"
      msg = @conn.receive
      assert_equal "test_stomp#test_explicit_receive", msg.body
    end

test_jruby_presence() click to toggle source

test JRuby detection

    # File test/test_connection.rb, line 513
    def test_jruby_presence
      if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
        assert @conn.jruby
      else
        assert !@conn.jruby
      end
    end

test_message_to_s() click to toggle source

Test converting a Message to a string.

    # File test/test_connection.rb, line 223
    def test_message_to_s
      conn_subscribe make_destination
      @conn.publish make_destination, "a\00""
      msg = @conn.receive
      assert_match /^<Stomp::Message headers=/ , msg.to_s
      checkEmsg(@conn)
    end

test_messages_with_multipleLine_ends() click to toggle source

Test messages with multiple line ends.

    # File test/test_connection.rb, line 237
    def test_messages_with_multipleLine_ends
      conn_subscribe make_destination
      @conn.publish make_destination, "a\n\n"
      @conn.publish make_destination, "b\n\na\n\n"

      msg_a = @conn.receive
      msg_b = @conn.receive

      assert_equal "a\n\n", msg_a.body
      assert_equal "b\n\na\n\n", msg_b.body
      checkEmsg(@conn)
    end

test_multi_thread_poll() click to toggle source

Test polling with multiple threads.

    # File test/test_connection.rb, line 342
    def test_multi_thread_poll
      #
      lock = Mutex.new
      msg_ctr = 0
      dest = make_destination
      #
      1.upto(@max_threads) do |tnum|
        Thread.new(@conn) do |amq|
          while true
            received = amq.poll
            if received
              lock.synchronize do
                msg_ctr += 1
              end
              # Simulate message processing
              sleep 0.05
            else
              # Wait a bit for more work
              sleep 0.05
            end
          end
        end
      end
      #
      conn_subscribe( dest )
      1.upto(@max_msgs) do |mnum|
        msg = Time.now.to_s + " #{mnum}"
        @conn.publish(dest, msg)
      end
      #
      max_sleep = (RUBY_VERSION =~ /1\.8\.6/) ? 30 : 5
      max_sleep = 30 if RUBY_ENGINE =~ /mingw/
      sleep_incr = 0.10
      total_slept = 0
      while true
        break if @max_msgs == msg_ctr
        total_slept += sleep_incr
        break if total_slept > max_sleep
        sleep sleep_incr
      end
      assert_equal @max_msgs, msg_ctr
      checkEmsg(@conn)
    end

test_multi_thread_receive() click to toggle source

Test receiving with multiple threads.

    # File test/test_connection.rb, line 303
    def test_multi_thread_receive
      lock = Mutex.new
      msg_ctr = 0
      dest = make_destination
      #
      1.upto(@max_threads) do |tnum|
        Thread.new(@conn) do |amq|
          while true
            received = amq.receive
            lock.synchronize do
              msg_ctr += 1
            end
            # Simulate message processing
            sleep 0.05
          end
        end
      end
      #
      conn_subscribe( dest )
      1.upto(@max_msgs) do |mnum|
        msg = Time.now.to_s + " #{mnum}"
        @conn.publish(dest, msg)
      end
      #
      max_sleep = (RUBY_VERSION =~ /1\.8/) ? 30 : 5
      max_sleep = 30 if RUBY_ENGINE =~ /mingw/
      sleep_incr = 0.10
      total_slept = 0
      while true
        break if @max_msgs == msg_ctr
        total_slept += sleep_incr
        break if total_slept > max_sleep
        sleep sleep_incr
      end
      assert_equal @max_msgs, msg_ctr
      checkEmsg(@conn)
    end

test_nack11p_0010() click to toggle source

Basic NAK test.

    # File test/test_connection.rb, line 440
    def test_nack11p_0010
      if @conn.protocol == Stomp::SPL_10
        assert_raise Stomp::Error::UnsupportedProtocolError do
          @conn.nack "dummy msg-id"
        end
      else
        dest = make_destination
        smsg = "test_stomp#test_nack01: #{Time.now.to_f}"
        @conn.publish dest, smsg
        #
        sid = @conn.uuid()
        @conn.subscribe dest, :ack => :client, :id => sid
        msg = @conn.receive
        assert_equal smsg, msg.body
        case @conn.protocol
          when Stomp::SPL_12
            assert_nothing_raised {
              @conn.nack msg.headers["ack"]
              sleep 0.05 # Give racy brokers a chance to handle the last nack before unsubscribe
              @conn.unsubscribe dest, :id => sid
            }
          else # Stomp::SPL_11
            assert_nothing_raised {
              @conn.nack msg.headers["message-id"], :subscription => sid
              sleep 0.05 # Give racy brokers a chance to handle the last nack before unsubscribe
              @conn.unsubscribe dest, :id => sid
            }
        end

        # phase 2
        teardown()
        setup()
        sid = @conn.uuid()
        @conn.subscribe dest, :ack => :auto, :id => sid
        msg2 = @conn.receive
        assert_equal smsg, msg2.body
        checkEmsg(@conn)
      end
    end

test_nil_body() click to toggle source

Test using a nil body.

    # File test/test_connection.rb, line 387
    def test_nil_body
      dest = make_destination
      assert_nothing_raised {
        @conn.publish dest, nil
      }
      conn_subscribe dest
      msg = @conn.receive
      assert_equal "", msg.body
      checkEmsg(@conn)
    end

test_nil_connparms() click to toggle source

Test nil 1.1 connection parameters.

    # File test/test_connection.rb, line 430
    def test_nil_connparms
      @conn.disconnect
      #
      assert_nothing_raised do
        @conn = Stomp::Connection.open(user, passcode, host, port, false, 5, nil)
      end
      checkEmsg(@conn)
    end

test_no_length() click to toggle source

Test suppression of content length header.

    # File test/test_connection.rb, line 42
    def test_no_length
      conn_subscribe make_destination
      #
      @conn.publish make_destination, "test_stomp#test_no_length",
        { :suppress_content_length => true }
      msg = @conn.receive
      assert_equal "test_stomp#test_no_length", msg.body
      #
      @conn.publish make_destination, "test_stomp#test_\0000_length",
        { :suppress_content_length => true }
      msg2 = @conn.receive
      assert_equal "test_stomp#test_", msg2.body
      checkEmsg(@conn)
    end

test_poll_async() click to toggle source

Test asynchronous polling.

    # File test/test_connection.rb, line 34
    def test_poll_async
      @conn.subscribe("/queue/do.not.put.messages.on.this.queue", :id => "a.no.messages.queue")
      # If the test 'hangs' here, Connection#poll is broken.
      m = @conn.poll
      assert m.nil?
    end

test_publish_two_messages() click to toggle source

Test publishing multiple messages.

    # File test/test_connection.rb, line 251
    def test_publish_two_messages
      conn_subscribe make_destination
      @conn.publish make_destination, "a\00""
      @conn.publish make_destination, "b\00""
      msg_a = @conn.receive
      msg_b = @conn.receive

      assert_equal "a\00"", msg_a.body
      assert_equal "b\00"", msg_b.body
      checkEmsg(@conn)
    end

test_receipt() click to toggle source

Test asking for a receipt.

    # File test/test_connection.rb, line 66
    def test_receipt
      conn_subscribe make_destination, :receipt => "abc"
      msg = @conn.receive
      assert_equal "abc", msg.headers['receipt-id']
      checkEmsg(@conn)
    end

test_response_is_instance_of_message_class() click to toggle source

Test that we receive a Stomp::Message.

    # File test/test_connection.rb, line 214
    def test_response_is_instance_of_message_class
      conn_subscribe make_destination
      @conn.publish make_destination, "a\00""
      msg = @conn.receive
      assert_instance_of Stomp::Message , msg
      checkEmsg(@conn)
    end

test_thread_hang_one() click to toggle source

    # File test/test_connection.rb, line 263
    def test_thread_hang_one
      received = nil
      Thread.new(@conn) do |amq|
          while true
              received = amq.receive
          end
      end
      #
      conn_subscribe( make_destination )
      message = Time.now.to_s
      @conn.publish(make_destination, message)
      sleep 1
      assert_not_nil received
      assert_equal message, received.body
      checkEmsg(@conn)
    end

test_thread_poll_one() click to toggle source

Test polling with a single thread.

    # File test/test_connection.rb, line 281
    def test_thread_poll_one
      received = nil
      max_sleep = (RUBY_VERSION =~ /1\.8/) ? 10 : 1
      Thread.new(@conn) do |amq|
          while true
            received = amq.poll
            # One message is needed
            Thread.exit if received
            sleep max_sleep
          end
      end
      #
      conn_subscribe( make_destination )
      message = Time.now.to_s
      @conn.publish(make_destination, message)
      sleep max_sleep+1
      assert_not_nil received
      assert_equal message, received.body
      checkEmsg(@conn)
    end

test_transaction() click to toggle source

Test transaction message sequencing.

    # File test/test_connection.rb, line 399
    def test_transaction
      conn_subscribe make_destination

      @conn.begin "txA"
      @conn.publish make_destination, "txn message", 'transaction' => "txA"

      @conn.publish make_destination, "first message"

      msg = @conn.receive
      assert_equal "first message", msg.body

      @conn.commit "txA"
      msg = @conn.receive
      assert_equal "txn message", msg.body
      checkEmsg(@conn)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.