| 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 'test_tools' |
| 20 |
require 'minitest/unit' |
| 21 |
require 'socket' |
| 22 |
|
| 23 |
# Since ruby 2.5 the default is true, turn it off since we have tests that deliberately |
| 24 |
# leak exceptions from threads to very they are caught properly from Container#run() |
| 25 |
(Thread.report_on_exception = false) rescue nil |
| 26 |
|
| 27 |
# MessagingHandler that raises in on_error to catch unexpected errors |
| 28 |
class ExceptionMessagingHandler |
| 29 |
def on_error(e) raise e; end |
| 30 |
end |
| 31 |
|
| 32 |
class ContainerTest < MiniTest::Test |
| 33 |
include Qpid::Proton |
| 34 |
|
| 35 |
def test_simple() |
| 36 |
send_handler = Class.new(ExceptionMessagingHandler) do |
| 37 |
attr_reader :accepted, :sent |
| 38 |
|
| 39 |
def initialize() @sent, @accepted = nil; end |
| 40 |
|
| 41 |
def on_sendable(sender) |
| 42 |
unless @sent |
| 43 |
m = Message.new("hello") |
| 44 |
m[:foo] = :bar |
| 45 |
sender.send m |
| 46 |
end |
| 47 |
@sent = true |
| 48 |
end |
| 49 |
|
| 50 |
def on_tracker_accept(tracker) |
| 51 |
@accepted = true |
| 52 |
tracker.connection.close |
| 53 |
end |
| 54 |
end.new |
| 55 |
|
| 56 |
receive_handler = Class.new(ExceptionMessagingHandler) do |
| 57 |
attr_reader :message, :link |
| 58 |
def on_receiver_open(link) |
| 59 |
@link = link |
| 60 |
@link.open |
| 61 |
@link.flow(1) |
| 62 |
end |
| 63 |
|
| 64 |
def on_message(delivery, message) |
| 65 |
@message = message; |
| 66 |
delivery.update Disposition::ACCEPTED |
| 67 |
delivery.settle |
| 68 |
end |
| 69 |
end.new |
| 70 |
|
| 71 |
c = ServerContainer.new(__method__, {:handler => receive_handler}) |
| 72 |
c.connect(c.url, {:handler => send_handler}).open_sender({:name => "testlink"}) |
| 73 |
c.run |
| 74 |
|
| 75 |
assert send_handler.accepted |
| 76 |
assert_equal "testlink", receive_handler.link.name |
| 77 |
assert_equal "hello", receive_handler.message.body |
| 78 |
assert_equal :bar, receive_handler.message[:foo] |
| 79 |
assert_equal "test_simple", receive_handler.link.connection.container_id |
| 80 |
end |
| 81 |
|
| 82 |
class CloseOnOpenHandler < TestHandler |
| 83 |
def on_connection_open(c) super; c.close; end |
| 84 |
end |
| 85 |
|
| 86 |
def test_auto_stop_one |
| 87 |
# A listener and a connection |
| 88 |
start_stop_handler = Class.new do |
| 89 |
def on_container_start(c) @start = c; end |
| 90 |
def on_container_stop(c) @stop = c; end |
| 91 |
attr_reader :start, :stop |
| 92 |
end.new |
| 93 |
c = Container.new(start_stop_handler, __method__) |
| 94 |
threads = 3.times.collect { Thread.new { c.run } } |
| 95 |
sleep(0.01) while c.running < 3 |
| 96 |
assert_equal c, start_stop_handler.start |
| 97 |
l = c.listen_io(TCPServer.new(0), ListenOnceHandler.new({ :handler => CloseOnOpenHandler.new})) |
| 98 |
c.connect("amqp://:#{l.to_io.addr[1]}", { :handler => CloseOnOpenHandler.new} ) |
| 99 |
threads.each { |t| assert t.join(1) } |
| 100 |
assert_equal c, start_stop_handler.stop |
| 101 |
assert_raises(Container::StoppedError) { c.run } |
| 102 |
end |
| 103 |
|
| 104 |
def test_auto_stop_two |
| 105 |
# Connect between different containers |
| 106 |
c1, c2 = Container.new("#{__method__}-1"), Container.new("#{__method__}-2") |
| 107 |
threads = [ Thread.new {c1.run }, Thread.new {c2.run } ] |
| 108 |
l = c2.listen_io(TCPServer.new(0), ListenOnceHandler.new({ :handler => CloseOnOpenHandler.new})) |
| 109 |
c1.connect(l.url, { :handler => CloseOnOpenHandler.new} ) |
| 110 |
assert threads.each { |t| t.join(1) } |
| 111 |
assert_raises(Container::StoppedError) { c1.run } |
| 112 |
assert_raises(Container::StoppedError) { c2.connect("") } |
| 113 |
end |
| 114 |
|
| 115 |
def test_auto_stop_listener_only |
| 116 |
c = Container.new(__method__) |
| 117 |
# Listener only, external close |
| 118 |
t = Thread.new { c.run } |
| 119 |
l = c.listen_io(TCPServer.new(0)) |
| 120 |
l.close |
| 121 |
assert t.join(1) |
| 122 |
end |
| 123 |
|
| 124 |
def test_stop_empty |
| 125 |
c = Container.new(__method__) |
| 126 |
threads = 3.times.collect { Thread.new { c.run } } |
| 127 |
sleep(0.01) while c.running < 3 |
| 128 |
assert_nil threads[0].join(0.001) # Not stopped |
| 129 |
c.stop |
| 130 |
assert c.stopped |
| 131 |
assert_raises(Container::StoppedError) { c.connect("") } |
| 132 |
assert_raises(Container::StoppedError) { c.run } |
| 133 |
threads.each { |t| assert t.join(1) } |
| 134 |
end |
| 135 |
|
| 136 |
def test_stop |
| 137 |
c = Container.new(__method__) |
| 138 |
c.auto_stop = false |
| 139 |
|
| 140 |
l = c.listen_io(TCPServer.new(0)) |
| 141 |
threads = 3.times.collect { Thread.new { c.run } } |
| 142 |
sleep(0.01) while c.running < 3 |
| 143 |
l.close |
| 144 |
assert_nil threads[0].join(0.001) # Not stopped, no auto_stop |
| 145 |
|
| 146 |
l = c.listen_io(TCPServer.new(0)) # New listener |
| 147 |
conn = c.connect("amqp://:#{l.to_io.addr[1]}") |
| 148 |
c.stop |
| 149 |
assert c.stopped |
| 150 |
threads.each { |t| assert t.join(1) } |
| 151 |
|
| 152 |
assert_raises(Container::StoppedError) { c.run } |
| 153 |
assert_equal 0, c.running |
| 154 |
assert_nil l.condition |
| 155 |
assert_nil conn.condition |
| 156 |
end |
| 157 |
|
| 158 |
def test_bad_host |
| 159 |
cont = Container.new(__method__) |
| 160 |
assert_raises (SocketError) { cont.listen("badlisten.example.com:999") } |
| 161 |
assert_raises (SocketError) { cont.connect("badconnect.example.com:999") } |
| 162 |
end |
| 163 |
|
| 164 |
# Verify that connection options are sent to the peer |
| 165 |
def test_connection_options |
| 166 |
# Note: user, password and sasl_xxx options are tested by ContainerSASLTest below |
| 167 |
server_handler = Class.new(ExceptionMessagingHandler) do |
| 168 |
def on_connection_open(c) |
| 169 |
@connection = c |
| 170 |
c.open({ |
| 171 |
:virtual_host => "server.to.client", |
| 172 |
:properties => { :server => :client }, |
| 173 |
:offered_capabilities => [ :s1 ], |
| 174 |
:desired_capabilities => [ :s2 ], |
| 175 |
:container_id => "box", |
| 176 |
}) |
| 177 |
c.close |
| 178 |
end |
| 179 |
attr_reader :connection |
| 180 |
end.new |
| 181 |
# Transport options set by listener, by Connection#open it is too late |
| 182 |
cont = ServerContainer.new(__method__, { |
| 183 |
:handler => server_handler, |
| 184 |
:idle_timeout => 88, |
| 185 |
:max_sessions =>1000, |
| 186 |
:max_frame_size => 8888, |
| 187 |
}) |
| 188 |
client = cont.connect(cont.url, |
| 189 |
{:virtual_host => "client.to.server", |
| 190 |
:properties => { "foo" => :bar, "str" => "str" }, |
| 191 |
:offered_capabilities => [:c1 ], |
| 192 |
:desired_capabilities => [:c2 ], |
| 193 |
:idle_timeout => 42, |
| 194 |
:max_sessions =>100, |
| 195 |
:max_frame_size => 4096, |
| 196 |
:container_id => "bowl" |
| 197 |
}) |
| 198 |
cont.run |
| 199 |
|
| 200 |
c = server_handler.connection |
| 201 |
assert_equal "client.to.server", c.virtual_host |
| 202 |
assert_equal({ "foo" => :bar, "str" => "str" }, c.properties) |
| 203 |
assert_equal([:c1], c.offered_capabilities) |
| 204 |
assert_equal([:c2], c.desired_capabilities) |
| 205 |
assert_equal 21, c.idle_timeout # Proton divides by 2 |
| 206 |
assert_equal 100, c.max_sessions |
| 207 |
assert_equal 4096, c.max_frame_size |
| 208 |
assert_equal "bowl", c.container_id |
| 209 |
|
| 210 |
c = client |
| 211 |
assert_equal "server.to.client", c.virtual_host |
| 212 |
assert_equal({ :server => :client }, c.properties) |
| 213 |
assert_equal([:s1], c.offered_capabilities) |
| 214 |
assert_equal([:s2], c.desired_capabilities) |
| 215 |
assert_equal "box", c.container_id |
| 216 |
assert_equal 8888, c.max_frame_size |
| 217 |
assert_equal 44, c.idle_timeout # Proton divides by 2 |
| 218 |
assert_equal 100, c.max_sessions |
| 219 |
end |
| 220 |
|
| 221 |
def test_link_options |
| 222 |
server_handler = Class.new(ExceptionMessagingHandler) do |
| 223 |
def initialize() @links = []; end |
| 224 |
attr_reader :links |
| 225 |
def on_sender_open(l) @links << l; end |
| 226 |
def on_receiver_open(l) @links << l; end |
| 227 |
end.new |
| 228 |
|
| 229 |
client_handler = Class.new(ExceptionMessagingHandler) do |
| 230 |
def on_connection_open(c) |
| 231 |
@links = []; |
| 232 |
@links << c.open_sender("s1") |
| 233 |
@links << c.open_sender({:name => "s2-n", :target => "s2-t", :source => "s2-s"}) |
| 234 |
@links << c.open_receiver("r1") |
| 235 |
@links << c.open_receiver({:name => "r2-n", :target => "r2-t", :source => "r2-s"}) |
| 236 |
c.close |
| 237 |
end |
| 238 |
attr_reader :links |
| 239 |
end.new |
| 240 |
|
| 241 |
cont = ServerContainer.new(__method__, {:handler => server_handler }, 1) |
| 242 |
cont.connect(cont.url, :handler => client_handler) |
| 243 |
cont.run |
| 244 |
|
| 245 |
expect = ["test_link_options/1", "s2-n", "test_link_options/2", "r2-n"] |
| 246 |
assert_equal expect, server_handler.links.map(&:name) |
| 247 |
assert_equal expect, client_handler.links.map(&:name) |
| 248 |
|
| 249 |
expect = [[nil,"s1"], ["s2-s","s2-t"], ["r1",nil], ["r2-s","r2-t"]] |
| 250 |
assert_equal expect, server_handler.links.map { |l| [l.remote_source.address, l.remote_target.address] } |
| 251 |
assert_equal expect, client_handler.links.map { |l| [l.source.address, l.target.address] } |
| 252 |
end |
| 253 |
|
| 254 |
def extract_terminus_options(t) |
| 255 |
opts = Hash[[:address, :distribution_mode, :durability_mode, :timeout, :expiry_policy].map { |m| [m, t.send(m)] }] |
| 256 |
opts[:filter] = t.filter.map |
| 257 |
opts[:capabilities] = t.capabilities.map |
| 258 |
opts[:dynamic] = t.dynamic? |
| 259 |
opts |
| 260 |
end |
| 261 |
|
| 262 |
def test_terminus_options |
| 263 |
opts = { |
| 264 |
:distribution_mode => Terminus::DIST_MODE_COPY, |
| 265 |
:durability_mode => Terminus::DELIVERIES, |
| 266 |
:timeout => 5, |
| 267 |
:expiry_policy => Terminus::EXPIRE_WITH_LINK, |
| 268 |
:filter => { :try => 'me' }, |
| 269 |
:capabilities => { :cap => 'len' }, |
| 270 |
} |
| 271 |
src_opts = { :address => "src", :dynamic => true }.update(opts) |
| 272 |
tgt_opts = { :address => "tgt", :dynamic => false }.update(opts) |
| 273 |
|
| 274 |
cont = ServerContainer.new(__method__, {}, 1) |
| 275 |
c = cont.connect(cont.url) |
| 276 |
s = c.open_sender({:target => tgt_opts, :source => src_opts }) |
| 277 |
assert_equal src_opts, extract_terminus_options(s.source) |
| 278 |
assert_equal tgt_opts, extract_terminus_options(s.target) |
| 279 |
assert s.source.dynamic? |
| 280 |
assert !s.target.dynamic? |
| 281 |
end |
| 282 |
|
| 283 |
# Test for time out on connecting to an unresponsive server |
| 284 |
def test_idle_timeout_server_no_open |
| 285 |
s = TCPServer.new(0) |
| 286 |
cont = Container.new(__method__) |
| 287 |
cont.connect(":#{s.addr[1]}", {:idle_timeout => 0.1, :handler => ExceptionMessagingHandler.new }) |
| 288 |
ex = assert_raises(Qpid::Proton::Condition) { cont.run } |
| 289 |
assert_match(/resource-limit-exceeded/, ex.to_s) |
| 290 |
ensure |
| 291 |
s.close if s |
| 292 |
end |
| 293 |
|
| 294 |
# Test for time out on unresponsive client |
| 295 |
def test_idle_timeout_client |
| 296 |
server = ServerContainerThread.new("#{__method__}.server", {:idle_timeout => 0.1}) |
| 297 |
client_handler = Class.new(ExceptionMessagingHandler) do |
| 298 |
def initialize() @ready, @block = Queue.new, Queue.new; end |
| 299 |
attr_reader :ready, :block |
| 300 |
def on_connection_open(c) |
| 301 |
@ready.push nil # Tell the main thread we are now open |
| 302 |
@block.pop # Block the client so the server will time it out |
| 303 |
end |
| 304 |
end.new |
| 305 |
|
| 306 |
client = Container.new(nil, "#{__method__}.client") |
| 307 |
client.connect(server.url, {:handler => client_handler}) |
| 308 |
client_thread = Thread.new { client.run } |
| 309 |
client_handler.ready.pop # Wait till the client has connected |
| 310 |
server.join # Exits when the connection closes from idle-timeout |
| 311 |
client_handler.block.push nil # Unblock the client |
| 312 |
ex = assert_raises(Qpid::Proton::Condition) { client_thread.join } |
| 313 |
assert_match(/resource-limit-exceeded/, ex.to_s) |
| 314 |
end |
| 315 |
|
| 316 |
# Make sure we stop and clean up if an aborted connection causes a handler to raise. |
| 317 |
# https://issues.apache.org/jira/browse/PROTON-1791 |
| 318 |
def test_handler_raise |
| 319 |
cont = ServerContainer.new(__method__, {}, 0) # Don't auto-close the listener |
| 320 |
client_handler = Class.new(MessagingHandler) do |
| 321 |
# TestException is < Exception so not handled by default rescue clause |
| 322 |
def on_connection_open(c) raise TestException.new("Bad Dog"); end |
| 323 |
end.new |
| 324 |
threads = 3.times.collect { Thread.new { cont.run } } |
| 325 |
sleep 0.01 while cont.running < 3 # Wait for all threads to be running |
| 326 |
sockets = 2.times.collect { TCPSocket.new("", cont.port) } |
| 327 |
cont.connect_io(sockets[1]) # No exception |
| 328 |
cont.connect_io(sockets[0], {:handler => client_handler}) # Should stop container |
| 329 |
|
| 330 |
threads.each { |t| assert_equal("Bad Dog", assert_raises(TestException) {t.join}.message) } |
| 331 |
sockets.each { |s| assert s.closed? } |
| 332 |
assert cont.listener.to_io.closed? |
| 333 |
assert_raises(Container::StoppedError) { cont.run } |
| 334 |
assert_raises(Container::StoppedError) { cont.listen "" } |
| 335 |
end |
| 336 |
|
| 337 |
# Check if two time values are "close enough" to be reasonable. |
| 338 |
def assert_equalish(x, y, delta=0.1) |
| 339 |
assert_in_delta(x, y, delta) |
| 340 |
end |
| 341 |
|
| 342 |
# Test container doesn't stops only when schedule work is done |
| 343 |
def test_container_work_queue |
| 344 |
c = Container.new __method__ |
| 345 |
delays = [0.1, 0.03, 0.02] |
| 346 |
a = [] |
| 347 |
delays.each { |d| c.schedule(d) { a << [d, Time.now] } } |
| 348 |
start = Time.now |
| 349 |
c.run |
| 350 |
delays.sort.each do |d| |
| 351 |
x = a.shift |
| 352 |
assert_equal d, x[0] |
| 353 |
end |
| 354 |
assert_equalish delays.reduce(:+), Time.now-start |
| 355 |
end |
| 356 |
|
| 357 |
# Test container work queue finishes due tasks on external stop, drops future tasks |
| 358 |
def test_container_work_queue_stop |
| 359 |
q = Queue.new |
| 360 |
c = Container.new __method__ |
| 361 |
thread = Thread.new { c.run } |
| 362 |
time = Time.now + 0.01 |
| 363 |
# Mix good scheduled tasks at time and bad tasks scheduled after 10 secs |
| 364 |
10.times do |
| 365 |
c.schedule(time) { q << true } |
| 366 |
c.schedule(10) { q << false } |
| 367 |
end |
| 368 |
assert_same true, q.pop # First task processed, all others at same time are due |
| 369 |
# Mix in some immediate tasks |
| 370 |
5.times do |
| 371 |
c.work_queue.add { q << true } # Immediate |
| 372 |
c.schedule(time) { q << true } |
| 373 |
c.schedule(10) { q << false } |
| 374 |
end |
| 375 |
c.stop |
| 376 |
thread.join |
| 377 |
19.times { assert_same true, q.pop } |
| 378 |
assert_equal 0, q.size # Tasks with 10 sec delay should be dropped |
| 379 |
end |
| 380 |
|
| 381 |
# Chain schedule calls from other schedule calls |
| 382 |
def test_container_schedule_chain |
| 383 |
c = Container.new(__method__) |
| 384 |
delays = [0.05, 0.02, 0.04] |
| 385 |
i = delays.each |
| 386 |
a = [] |
| 387 |
p = Proc.new { c.schedule(i.next) { a << Time.now; p.call } rescue nil } |
| 388 |
p.call # Schedule the first, which schedules the second etc. |
| 389 |
start = Time.now |
| 390 |
c.run |
| 391 |
assert_equal 3, a.size |
| 392 |
assert_equalish delays.reduce(:+), Time.now-start |
| 393 |
end |
| 394 |
|
| 395 |
# Schedule calls from handlers |
| 396 |
def test_container_schedule_handler |
| 397 |
h = Class.new() do |
| 398 |
def initialize() @got = []; end |
| 399 |
attr_reader :got |
| 400 |
def record(m) @got << m; end |
| 401 |
def on_container_start(c) c.schedule(0) {record __method__}; end |
| 402 |
def on_connection_open(c) c.close; c.container.schedule(0) {record __method__}; end |
| 403 |
def on_connection_close(c) c.container.schedule(0) {record __method__}; end |
| 404 |
end.new |
| 405 |
t = ServerContainerThread.new(__method__, nil, 1, h) |
| 406 |
t.connect(t.url) |
| 407 |
t.join |
| 408 |
assert_equal [:on_container_start, :on_connection_open, :on_connection_open, :on_connection_close, :on_connection_close], h.got |
| 409 |
end |
| 410 |
|
| 411 |
# Raising from container handler should stop container |
| 412 |
def test_container_handler_raise |
| 413 |
h = Class.new() do |
| 414 |
def on_container_start(c) raise "BROKEN"; end |
| 415 |
end.new |
| 416 |
c = Container.new(h, __method__) |
| 417 |
assert_equal("BROKEN", (assert_raises(RuntimeError) { c.run }).to_s) |
| 418 |
end |
| 419 |
|
| 420 |
# Raising from connection handler should stop container |
| 421 |
def test_connection_handler_raise |
| 422 |
h = Class.new() do |
| 423 |
def on_connection_open(c) raise "BROKEN"; end |
| 424 |
end.new |
| 425 |
c = ServerContainer.new(__method__, nil, 1, h) |
| 426 |
c.connect(c.url) |
| 427 |
assert_equal("BROKEN", (assert_raises(RuntimeError) { c.run }).to_s) |
| 428 |
end |
| 429 |
|
| 430 |
# Raising from container schedule should stop container |
| 431 |
def test_container_schedule_raise |
| 432 |
c = Container.new(__method__) |
| 433 |
c.schedule(0) { raise "BROKEN" } |
| 434 |
assert_equal("BROKEN", (assert_raises(RuntimeError) { c.run }).to_s) |
| 435 |
end |
| 436 |
|
| 437 |
def test_connection_work_queue |
| 438 |
cont = ServerContainer.new(__method__, {}, 1) |
| 439 |
c = cont.connect(cont.url) |
| 440 |
t = Thread.new { cont.run } |
| 441 |
q = Queue.new |
| 442 |
|
| 443 |
start = Time.now |
| 444 |
c.work_queue.schedule(0.02) { q << [3, Thread.current] } |
| 445 |
c.work_queue.add { q << [1, Thread.current] } |
| 446 |
c.work_queue.schedule(0.04) { q << [4, Thread.current] } |
| 447 |
c.work_queue.add { q << [2, Thread.current] } |
| 448 |
|
| 449 |
assert_equal [1, t], q.pop |
| 450 |
assert_equal [2, t], q.pop |
| 451 |
assert_equalish 0.0, Time.now-start |
| 452 |
assert_equal [3, t], q.pop |
| 453 |
assert_equal [4, t], q.pop |
| 454 |
assert_equalish 0.02 + 0.04, Time.now-start |
| 455 |
|
| 456 |
c.work_queue.add { c.close } |
| 457 |
t.join |
| 458 |
assert_raises(WorkQueue::StoppedError) { c.work_queue.add { } } |
| 459 |
end |
| 460 |
|
| 461 |
# Raising from connection schedule should stop container |
| 462 |
def test_connection_work_queue_raise |
| 463 |
c = ServerContainer.new(__method__) |
| 464 |
c.connect(c.url) |
| 465 |
c.work_queue.add { raise "BROKEN" } |
| 466 |
assert_equal("BROKEN", (assert_raises(RuntimeError) { c.run }).to_s) |
| 467 |
end |
| 468 |
end |