| 1 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 2 |
# or more contributor license agreements. See the NOTICE file |
| 3 |
# distributed with this work for additional information |
| 4 |
# regarding copyright ownership. The ASF licenses this file |
| 5 |
# to you under the Apache License, Version 2.0 (the |
| 6 |
# "License"); you may not use this file except in compliance |
| 7 |
# with the License. You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, |
| 12 |
# software distributed under the License is distributed on an |
| 13 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 |
# KIND, either express or implied. See the License for the |
| 15 |
# specific language governing permissions and limitations |
| 16 |
# under the License. |
| 17 |
|
| 18 |
|
| 19 |
require 'minitest/autorun' |
| 20 |
require 'qpid_proton' |
| 21 |
require 'test_tools' |
| 22 |
include Qpid::Proton |
| 23 |
|
| 24 |
# Records every call, never provokes "on_unhandled" |
| 25 |
class RecordingHandler < Qpid::Proton::MessagingHandler |
| 26 |
|
| 27 |
def initialize(*args) super(*args); @calls = []; end |
| 28 |
|
| 29 |
attr_accessor :calls |
| 30 |
|
| 31 |
def names() @calls.collect { |c| c[0] }; end |
| 32 |
|
| 33 |
def clear() @calls.clear; end |
| 34 |
|
| 35 |
def method_missing(name, *args) |
| 36 |
respond_to_missing?(name) ? (@calls << [name, *args]) : super; |
| 37 |
end |
| 38 |
def respond_to_missing?(name, private=false); (/^on_/ =~ name); end |
| 39 |
def respond_to?(name, all=false) super || respond_to_missing?(name); end # For ruby < 1.9.2 |
| 40 |
end |
| 41 |
|
| 42 |
class NoAutoOpenClose < RecordingHandler |
| 43 |
def initialize() super; @endpoints = []; end |
| 44 |
def on_connection_open(x) @connection = x; super; raise StopAutoResponse; end |
| 45 |
def on_session_open(x) @session = x; super; raise StopAutoResponse; end |
| 46 |
def on_sender_open(x) @link = x; super; raise StopAutoResponse; end |
| 47 |
def on_receiver_open(x) @link = x; super; raise StopAutoResponse; end |
| 48 |
def on_connection_close(x) super; raise StopAutoResponse; end |
| 49 |
def on_session_close(x) super; raise StopAutoResponse; end |
| 50 |
def on_sender_close(x) super; raise StopAutoResponse; end |
| 51 |
def on_receiver_close(x) super; raise StopAutoResponse; end |
| 52 |
attr_reader :connection, :session, :link |
| 53 |
end |
| 54 |
|
| 55 |
class TestMessagingHandler < MiniTest::Test |
| 56 |
|
| 57 |
def test_auto_open_close |
| 58 |
d = DriverPair.new(RecordingHandler.new, RecordingHandler.new) |
| 59 |
d.client.connection.open; d.client.connection.open_sender; d.run |
| 60 |
assert_equal [:on_connection_open, :on_session_open, :on_sender_open, :on_sendable], d.client.handler.names |
| 61 |
assert_equal [:on_connection_open, :on_session_open, :on_receiver_open], d.server.handler.names |
| 62 |
d.clear |
| 63 |
d.client.connection.close; d.run |
| 64 |
assert_equal [:on_connection_close, :on_transport_close], d.server.handler.names |
| 65 |
assert_equal [:on_connection_close, :on_transport_close], d.client.handler.names |
| 66 |
end |
| 67 |
|
| 68 |
def test_no_auto_open_close |
| 69 |
d = DriverPair.new(NoAutoOpenClose.new, NoAutoOpenClose.new) |
| 70 |
d.client.connection.open; d.run |
| 71 |
assert_equal [:on_connection_open], d.server.handler.names |
| 72 |
assert_equal [], d.client.handler.names |
| 73 |
d.server.connection.open; d.run |
| 74 |
assert_equal [:on_connection_open], d.client.handler.names |
| 75 |
assert_equal [:on_connection_open], d.server.handler.names |
| 76 |
d.clear |
| 77 |
d.client.connection.open_session; d.run |
| 78 |
assert_equal [:on_session_open], d.server.handler.names |
| 79 |
assert_equal [], d.client.handler.names |
| 80 |
d.clear |
| 81 |
d.client.connection.close; |
| 82 |
3.times { d.process } |
| 83 |
assert_equal [:on_connection_close], d.server.handler.names |
| 84 |
assert_equal [], d.client.handler.names |
| 85 |
d.server.connection.close; d.run |
| 86 |
assert_equal [:on_connection_close, :on_transport_close], d.client.handler.names |
| 87 |
assert_equal [:on_connection_close, :on_transport_close], d.server.handler.names |
| 88 |
end |
| 89 |
|
| 90 |
def test_transport_error |
| 91 |
d = DriverPair.new(RecordingHandler.new, RecordingHandler.new) |
| 92 |
d.client.connection.open; d.run |
| 93 |
d.clear |
| 94 |
d.client.close "stop that"; d.run |
| 95 |
assert_equal [:on_transport_close], d.client.handler.names |
| 96 |
assert_equal [:on_transport_error, :on_transport_close], d.server.handler.names |
| 97 |
assert_equal Condition.new("proton:io", "stop that (connection aborted)"), d.client.transport.condition |
| 98 |
assert_equal Condition.new("amqp:connection:framing-error", "connection aborted"), d.server.transport.condition |
| 99 |
end |
| 100 |
|
| 101 |
# Close on half-open |
| 102 |
def test_connection_error |
| 103 |
d = DriverPair.new(NoAutoOpenClose.new, NoAutoOpenClose.new) |
| 104 |
d.client.connection.open; d.run |
| 105 |
d.server.connection.close "bad dog"; d.run |
| 106 |
d.client.connection.close; d.run |
| 107 |
assert_equal [:on_connection_open, :on_connection_error, :on_connection_close, :on_transport_close], d.client.handler.names |
| 108 |
assert_equal "bad dog", d.client.handler.calls[1][1].condition.description |
| 109 |
assert_equal [:on_connection_open, :on_connection_error, :on_connection_close, :on_transport_close], d.server.handler.names |
| 110 |
end |
| 111 |
|
| 112 |
def test_session_error |
| 113 |
d = DriverPair.new(RecordingHandler.new, RecordingHandler.new) |
| 114 |
d.client.connection.open |
| 115 |
s = d.client.connection.default_session; s.open; d.run |
| 116 |
assert_equal [:on_connection_open, :on_session_open], d.client.handler.names |
| 117 |
assert_equal [:on_connection_open, :on_session_open], d.server.handler.names |
| 118 |
d.clear |
| 119 |
s.close "bad dog"; d.run |
| 120 |
assert_equal [:on_session_error, :on_session_close], d.client.handler.names |
| 121 |
assert_equal [:on_session_error, :on_session_close], d.server.handler.names |
| 122 |
assert_equal "bad dog", d.server.handler.calls[0][1].condition.description |
| 123 |
end |
| 124 |
|
| 125 |
def test_sender_receiver_error |
| 126 |
d = DriverPair.new(RecordingHandler.new, RecordingHandler.new) |
| 127 |
d.client.connection.open |
| 128 |
s = d.client.connection.open_sender; d.run |
| 129 |
assert_equal [:on_connection_open, :on_session_open, :on_sender_open, :on_sendable], d.client.handler.names |
| 130 |
assert_equal [:on_connection_open, :on_session_open, :on_receiver_open], d.server.handler.names |
| 131 |
d.clear |
| 132 |
s.close "bad dog"; d.run |
| 133 |
assert_equal [:on_sender_error, :on_sender_close], d.client.handler.names |
| 134 |
assert_equal [:on_receiver_error, :on_receiver_close], d.server.handler.names |
| 135 |
assert_equal "bad dog", d.server.handler.calls[0][1].condition.description |
| 136 |
end |
| 137 |
|
| 138 |
def test_options_off |
| 139 |
linkopts = {:credit_window=>0, :auto_settle=>false, :auto_accept=>false} |
| 140 |
d = DriverPair.new(NoAutoOpenClose.new, NoAutoOpenClose.new) |
| 141 |
d.client.connection.open; d.run |
| 142 |
assert_equal [[], [:on_connection_open]], d.names |
| 143 |
d.server.connection.open; d.run |
| 144 |
assert_equal [[:on_connection_open], [:on_connection_open]], d.names |
| 145 |
d.clear |
| 146 |
s = d.client.connection.open_sender(linkopts); d.run |
| 147 |
assert_equal [[], [:on_session_open, :on_receiver_open]], d.names |
| 148 |
d.server.handler.session.open # Return session open |
| 149 |
d.server.handler.link.open(linkopts) # Return link open |
| 150 |
d.run |
| 151 |
assert_equal [[:on_session_open, :on_sender_open], [:on_session_open, :on_receiver_open]], d.names |
| 152 |
d.clear |
| 153 |
d.server.handler.link.flow(1); d.run |
| 154 |
assert_equal [[:on_sendable], []], d.names |
| 155 |
assert_equal 1, s.credit |
| 156 |
d.clear |
| 157 |
s.send Message.new("foo"); d.run |
| 158 |
assert_equal [[], [:on_message]], d.names |
| 159 |
end |
| 160 |
|
| 161 |
def test_message |
| 162 |
handler_class = Class.new(MessagingHandler) do |
| 163 |
def on_message(delivery, message) @message = message; end |
| 164 |
def on_tracker_accept(event) @accepted = true; end |
| 165 |
attr_accessor :message, :accepted |
| 166 |
end |
| 167 |
d = DriverPair.new(handler_class.new, handler_class.new) |
| 168 |
d.client.connection.open; |
| 169 |
s = d.client.connection.open_sender; d.run |
| 170 |
assert_equal 10, s.credit # Default prefetch |
| 171 |
s.send(Message.new("foo")); d.run |
| 172 |
assert_equal "foo", d.server.handler.message.body |
| 173 |
assert d.client.handler.accepted |
| 174 |
end |
| 175 |
|
| 176 |
# Verify on_unhandled is called |
| 177 |
def test_unhandled |
| 178 |
handler_class = Class.new(MessagingHandler) do |
| 179 |
def initialize() super; @unhandled = []; end |
| 180 |
def on_unhandled(method, *args) @unhandled << method; end |
| 181 |
attr_accessor :unhandled |
| 182 |
end |
| 183 |
d = DriverPair.new(handler_class.new, handler_class.new) |
| 184 |
d.client.connection.open; d.run |
| 185 |
assert_equal [:on_connection_open], d.client.handler.unhandled |
| 186 |
assert_equal [:on_connection_open], d.server.handler.unhandled |
| 187 |
end |
| 188 |
|
| 189 |
# Verify on_error is called |
| 190 |
def test_on_error |
| 191 |
handler_class = Class.new(MessagingHandler) do |
| 192 |
def initialize() super; @error = []; @unhandled = []; end |
| 193 |
def on_error(condition) @error << condition; end |
| 194 |
def on_unhandled(method, *args) @unhandled << method; end |
| 195 |
attr_accessor :error, :unhandled |
| 196 |
end |
| 197 |
d = DriverPair.new(handler_class.new, handler_class.new) |
| 198 |
d.client.connection.open |
| 199 |
r = d.client.connection.open_receiver; d.run |
| 200 |
assert_equal [:on_connection_open, :on_session_open, :on_receiver_open], d.client.handler.unhandled |
| 201 |
assert_equal [:on_connection_open, :on_session_open, :on_sender_open, :on_sendable], d.server.handler.unhandled |
| 202 |
r.close Condition.new("goof", "oops"); d.run |
| 203 |
|
| 204 |
assert_equal [Condition.new("goof", "oops")], d.client.handler.error |
| 205 |
assert_equal [:on_connection_open, :on_session_open, :on_sender_open, :on_sendable, :on_sender_close], d.server.handler.unhandled |
| 206 |
assert_equal [Condition.new("goof", "oops")], d.server.handler.error |
| 207 |
|
| 208 |
end |
| 209 |
|
| 210 |
# Verify on_unhandled is called for errors if there is no on_error |
| 211 |
def test_unhandled_error |
| 212 |
handler_class = Class.new(MessagingHandler) do |
| 213 |
def on_unhandled(method, *args) |
| 214 |
@error = args[0].condition if method == :on_connection_error; |
| 215 |
end |
| 216 |
attr_accessor :error |
| 217 |
end |
| 218 |
d = DriverPair.new(handler_class.new, handler_class.new) |
| 219 |
d.client.connection.open; d.run |
| 220 |
d.client.connection.close "oops"; d.run |
| 221 |
assert_equal [Condition.new("error", "oops")]*2, d.collect { |x| x.handler.error } |
| 222 |
end |
| 223 |
end |