| 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 |
OldMessagingHandler = Qpid::Proton::Handler::MessagingHandler #Use the old handler. |
| 25 |
|
| 26 |
# Records every call |
| 27 |
class AllHandler < OldMessagingHandler |
| 28 |
def initialize(*args) |
| 29 |
super(*args) |
| 30 |
@calls = [] |
| 31 |
end |
| 32 |
|
| 33 |
attr_accessor :calls |
| 34 |
|
| 35 |
def names; @calls.map { |c| c[0] }; end |
| 36 |
def events; @calls.map { |c| c[1] }; end |
| 37 |
|
| 38 |
def method_missing(name, *args) (/^on_/ =~ name) ? (@calls << [name] + args) : super; end |
| 39 |
def respond_to_missing?(name, private=false); (/^on_/ =~ name); end |
| 40 |
def respond_to?(name, all=false) super || respond_to_missing?(name); end # For ruby < 1.9.2 |
| 41 |
end |
| 42 |
|
| 43 |
# Tests with Mock handler that handles all methods, expect both old and new calls |
| 44 |
class TestOldHandler < MiniTest::Test |
| 45 |
def setup |
| 46 |
@h = [AllHandler.new, AllHandler.new] |
| 47 |
@ch, @sh = *@h |
| 48 |
@d = DriverPair.new(*@h) |
| 49 |
end |
| 50 |
|
| 51 |
def clear; @d.each { |d| h = d.handler; h.calls.clear }; end |
| 52 |
|
| 53 |
def test_handler_defaults |
| 54 |
want = { :prefetch => 10, :auto_settle => true, :auto_accept => true, :auto_open => true, :auto_close => true, :peer_close_is_error => false } |
| 55 |
assert_equal want, @ch.options |
| 56 |
assert_equal want, @sh.options |
| 57 |
end |
| 58 |
|
| 59 |
def test_auto_open_close |
| 60 |
@d.client.connection.open; @d.client.connection.open_sender; @d.run |
| 61 |
assert_equal [:on_connection_opened, :on_session_opened, :on_link_opened, :on_sendable], @ch.names |
| 62 |
assert_equal [:on_connection_opening, :on_session_opening, :on_link_opening, :on_connection_opened, :on_session_opened, :on_link_opened], @sh.names |
| 63 |
clear |
| 64 |
@d.client.connection.close; @d.run |
| 65 |
assert_equal [:on_connection_closed, :on_transport_closed], @ch.names |
| 66 |
assert_equal [:on_connection_closing, :on_connection_closed, :on_transport_closed], @sh.names |
| 67 |
end |
| 68 |
|
| 69 |
def test_no_auto_open_close |
| 70 |
[:auto_close, :auto_open].each { |k| @ch.options[k] = @sh.options[k] = false } |
| 71 |
@d.client.connection.open; @d.run |
| 72 |
assert_equal [:on_connection_opening], @sh.names |
| 73 |
assert_equal [], @ch.names |
| 74 |
@d.server.connection.open; @d.run |
| 75 |
assert_equal [:on_connection_opened], @ch.names |
| 76 |
assert_equal [:on_connection_opening, :on_connection_opened], @sh.names |
| 77 |
clear |
| 78 |
@d.client.connection.session.open; @d.run |
| 79 |
assert_equal [:on_session_opening], @sh.names |
| 80 |
assert_equal [], @ch.names |
| 81 |
clear |
| 82 |
@d.client.connection.close; |
| 83 |
3.times { @d.process } |
| 84 |
assert_equal [:on_connection_closing], @sh.names |
| 85 |
assert_equal [], @ch.names |
| 86 |
@d.server.connection.close; @d.run |
| 87 |
assert_equal [:on_connection_closed, :on_transport_closed], @ch.names |
| 88 |
assert_equal [:on_connection_closing, :on_connection_closed, :on_transport_closed], @sh.names |
| 89 |
end |
| 90 |
|
| 91 |
def test_transport_error |
| 92 |
@d.client.connection.open; @d.run |
| 93 |
clear |
| 94 |
@d.client.close "stop that"; @d.run |
| 95 |
assert_equal [:on_transport_closed], @ch.names |
| 96 |
assert_equal [:on_transport_error, :on_transport_closed], @sh.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 |
def test_connection_error |
| 102 |
@ch.options[:auto_open] = @sh.options[:auto_open] = false |
| 103 |
@d.client.connection.open; @d.run |
| 104 |
@d.server.connection.close "bad dog"; @d.run |
| 105 |
assert_equal [:on_connection_opened, :on_connection_error, :on_connection_closed, :on_transport_closed], @ch.names |
| 106 |
assert_equal "bad dog", @ch.calls[2][1].condition.description |
| 107 |
assert_equal [:on_connection_opening, :on_connection_closed, :on_transport_closed], @sh.names |
| 108 |
end |
| 109 |
|
| 110 |
def test_session_error |
| 111 |
@d.client.connection.open |
| 112 |
s = @d.client.connection.session; s.open; @d.run |
| 113 |
s.close "bad dog"; @d.run |
| 114 |
assert_equal [:on_connection_opened, :on_session_opened, :on_session_closed], @ch.names |
| 115 |
assert_equal [:on_connection_opening, :on_session_opening, :on_connection_opened, :on_session_opened, :on_session_error, :on_session_closed], @sh.names |
| 116 |
assert_equal "bad dog", @sh.calls[-3][1].condition.description |
| 117 |
end |
| 118 |
|
| 119 |
def test_link_error |
| 120 |
@d.client.connection.open |
| 121 |
s = @d.client.connection.open_sender; @d.run |
| 122 |
s.close "bad dog"; @d.run |
| 123 |
assert_equal [:on_connection_opened, :on_session_opened, :on_link_opened, :on_sendable, :on_link_closed], @ch.names |
| 124 |
assert_equal [:on_connection_opening, :on_session_opening, :on_link_opening, |
| 125 |
:on_connection_opened, :on_session_opened, :on_link_opened, |
| 126 |
:on_link_error, :on_link_closed], @sh.names |
| 127 |
assert_equal "bad dog", @sh.calls[-3][1].condition.description |
| 128 |
end |
| 129 |
|
| 130 |
def test_options_off |
| 131 |
off = {:prefetch => 0, :auto_settle => false, :auto_accept => false, :auto_open => false, :auto_close => false} |
| 132 |
@ch.options.replace(off) |
| 133 |
@sh.options.replace(off) |
| 134 |
@d.client.connection.open; @d.run |
| 135 |
assert_equal [[], [:on_connection_opening]], [@ch.names, @sh.names] |
| 136 |
@d.server.connection.open; @d.run |
| 137 |
assert_equal [[:on_connection_opened], [:on_connection_opening, :on_connection_opened]], [@ch.names, @sh.names] |
| 138 |
clear |
| 139 |
s = @d.client.connection.open_sender; @d.run |
| 140 |
assert_equal [[], [:on_session_opening, :on_link_opening]], [@ch.names, @sh.names] |
| 141 |
@sh.events[1].session.open |
| 142 |
r = @sh.events[1].link |
| 143 |
r.open; @d.run |
| 144 |
assert_equal [[:on_session_opened, :on_link_opened], [:on_session_opening, :on_link_opening, :on_session_opened, :on_link_opened]], [@ch.names, @sh.names] |
| 145 |
clear |
| 146 |
r.flow(1); @d.run |
| 147 |
assert_equal [[:on_sendable], []], [@ch.names, @sh.names] |
| 148 |
assert_equal 1, s.credit |
| 149 |
clear |
| 150 |
s.send Message.new("foo"); @d.run |
| 151 |
assert_equal [[], [:on_message]], [@ch.names, @sh.names] |
| 152 |
end |
| 153 |
|
| 154 |
def test_peer_close_is_error |
| 155 |
@ch.options[:peer_close_is_error] = true |
| 156 |
@d.client.connection.open; @d.run |
| 157 |
@d.server.connection.close; @d.run |
| 158 |
assert_equal [:on_connection_opened, :on_connection_error, :on_connection_closed, :on_transport_closed], @ch.names |
| 159 |
assert_equal [:on_connection_opening, :on_connection_opened, :on_connection_closed, :on_transport_closed], @sh.names |
| 160 |
end |
| 161 |
end |
| 162 |
|
| 163 |
# Test with real handlers that implement a few methods |
| 164 |
class TestOldUnhandled < MiniTest::Test |
| 165 |
|
| 166 |
def test_message |
| 167 |
handler_class = Class.new(OldMessagingHandler) do |
| 168 |
def on_message(event) @message = event.message; end |
| 169 |
def on_accepted(event) @accepted = true; end |
| 170 |
attr_accessor :message, :accepted, :sender |
| 171 |
end |
| 172 |
d = DriverPair.new(handler_class.new, handler_class.new) |
| 173 |
d.client.connection.open; |
| 174 |
s = d.client.connection.open_sender; d.run |
| 175 |
assert_equal 10, s.credit # Default prefetch |
| 176 |
s.send(Message.new("foo")); d.run |
| 177 |
assert_equal "foo", d.server.handler.message.body |
| 178 |
assert d.client.handler.accepted |
| 179 |
end |
| 180 |
|
| 181 |
# Verify on_unhandled is called |
| 182 |
def test_unhandled |
| 183 |
handler_class = Class.new(OldMessagingHandler) do |
| 184 |
def initialize() super; @unhandled = []; end |
| 185 |
def on_unhandled(event) @unhandled << event.method; end |
| 186 |
attr_accessor :unhandled |
| 187 |
end |
| 188 |
d = DriverPair.new(handler_class.new, handler_class.new) |
| 189 |
d.client.connection.open; d.run |
| 190 |
assert_equal [:on_connection_opened], d.client.handler.unhandled |
| 191 |
assert_equal [:on_connection_opening, :on_connection_opened], d.server.handler.unhandled |
| 192 |
end |
| 193 |
|
| 194 |
# Verify on_error is called |
| 195 |
def test_on_error |
| 196 |
handler_class = Class.new(OldMessagingHandler) do |
| 197 |
def initialize() super; @error = []; @unhandled = []; end |
| 198 |
def on_error(event) @error << event.method; end |
| 199 |
def on_unhandled(event) @unhandled << event.method; end |
| 200 |
attr_accessor :error, :unhandled |
| 201 |
end |
| 202 |
d = DriverPair.new(handler_class.new, handler_class.new) |
| 203 |
d.client.connection.open |
| 204 |
r = d.client.connection.open_receiver; d.run |
| 205 |
r.close "oops"; d.run |
| 206 |
assert_equal [:on_connection_opened, :on_session_opened, :on_link_opened, |
| 207 |
:on_link_closed], d.client.handler.unhandled |
| 208 |
assert_equal [:on_connection_opening, :on_session_opening, :on_link_opening, |
| 209 |
:on_connection_opened, :on_session_opened, :on_link_opened, :on_sendable, |
| 210 |
:on_link_closed], d.server.handler.unhandled |
| 211 |
assert_equal [:on_link_error], d.server.handler.error |
| 212 |
|
| 213 |
end |
| 214 |
|
| 215 |
# Verify on_unhandled is called even for errors if there is no on_error |
| 216 |
def test_unhandled_error |
| 217 |
handler_class = Class.new(OldMessagingHandler) do |
| 218 |
def initialize() super; @unhandled = []; end |
| 219 |
def on_unhandled(event) @unhandled << event.method; end |
| 220 |
attr_accessor :unhandled |
| 221 |
end |
| 222 |
d = DriverPair.new(handler_class.new, handler_class.new) |
| 223 |
d.client.connection.open; d.run |
| 224 |
d.client.connection.close "oops"; d.run |
| 225 |
assert_equal [:on_connection_opened, :on_connection_closed, :on_transport_closed], d.client.handler.unhandled |
| 226 |
assert_equal [:on_connection_opening, :on_connection_opened, :on_connection_error, :on_connection_closed, :on_transport_closed], d.server.handler.unhandled |
| 227 |
end |
| 228 |
end |