| 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 |
# Tools for tests. Only minitest is used. |
| 20 |
|
| 21 |
require 'minitest/autorun' |
| 22 |
require 'qpid_proton' |
| 23 |
require 'socket' |
| 24 |
|
| 25 |
begin |
| 26 |
MiniTest::Test |
| 27 |
rescue NameError # For older versions of MiniTest |
| 28 |
MiniTest::Test = MiniTest::Unit::TestCase |
| 29 |
end |
| 30 |
|
| 31 |
class TestError < RuntimeError; end # Normal error |
| 32 |
class TestException < Exception; end # Not caught by default rescue |
| 33 |
|
| 34 |
def wait_port(port, timeout=5) |
| 35 |
deadline = Time.now + timeout |
| 36 |
begin # Wait for the port to be connectible |
| 37 |
TCPSocket.open("", $port).close |
| 38 |
rescue Errno::ECONNREFUSED |
| 39 |
if Time.now > deadline then |
| 40 |
raise TestError, "timed out waiting for port #{port}" |
| 41 |
end |
| 42 |
sleep(0.1) |
| 43 |
retry |
| 44 |
end |
| 45 |
end |
| 46 |
|
| 47 |
# Handler that records some common events that are checked by tests |
| 48 |
class TestHandler < Qpid::Proton::MessagingHandler |
| 49 |
attr_reader :errors, :connections, :sessions, :links, :messages |
| 50 |
|
| 51 |
# Pass optional extra handlers and options to the Container |
| 52 |
# @param raise_errors if true raise an exception for error events, if false, store them in #errors |
| 53 |
def initialize(raise_errors=true) |
| 54 |
super() |
| 55 |
@raise_errors = raise_errors |
| 56 |
@errors, @connections, @sessions, @links, @messages = 5.times.collect { [] } |
| 57 |
end |
| 58 |
|
| 59 |
# If the handler has errors, raise a TestError with all the error text |
| 60 |
def raise_errors() |
| 61 |
return if @errors.empty? |
| 62 |
text = "" |
| 63 |
while @errors.size > 0 |
| 64 |
text << @errors.pop + "\n" |
| 65 |
end |
| 66 |
raise TestError.new("TestHandler has errors:\n #{text}") |
| 67 |
end |
| 68 |
|
| 69 |
def on_error(condition) |
| 70 |
@errors.push "#{condition}" |
| 71 |
raise_errors if @raise_errors |
| 72 |
end |
| 73 |
|
| 74 |
def endpoint_open(queue, endpoint) |
| 75 |
queue.push(endpoint) |
| 76 |
end |
| 77 |
|
| 78 |
def on_connection_open(c) |
| 79 |
endpoint_open(@connections, c) |
| 80 |
end |
| 81 |
|
| 82 |
def on_session_open(s) |
| 83 |
endpoint_open(@sessions, s) |
| 84 |
end |
| 85 |
|
| 86 |
def on_receiver_open(l) |
| 87 |
endpoint_open(@links, l) |
| 88 |
end |
| 89 |
|
| 90 |
def on_sender_open(l) |
| 91 |
endpoint_open(@links, l) |
| 92 |
end |
| 93 |
|
| 94 |
def on_message(d, m) |
| 95 |
@messages.push(m) |
| 96 |
end |
| 97 |
end |
| 98 |
|
| 99 |
# ListenHandler that closes the Listener after first (or n) accepts |
| 100 |
class ListenOnceHandler < Qpid::Proton::Listener::Handler |
| 101 |
def initialize(opts, n=1) super(opts); @n=n; end |
| 102 |
def on_error(l, e) raise e; end |
| 103 |
def on_accept(l) l.close if (@n -= 1).zero?; super; end |
| 104 |
end |
| 105 |
|
| 106 |
# Add port/url to Listener, assuming a TCP socket |
| 107 |
class Qpid::Proton::Listener |
| 108 |
def url() "amqp://:#{port}"; end |
| 109 |
end |
| 110 |
|
| 111 |
# A client/server pair of ConnectionDrivers linked by a socket pair |
| 112 |
DriverPair = Struct.new(:client, :server) do |
| 113 |
|
| 114 |
def initialize(client_handler, server_handler) |
| 115 |
s = Socket.pair(:LOCAL, :STREAM, 0) |
| 116 |
self.client = HandlerDriver.new(s[0], client_handler) |
| 117 |
self.server = HandlerDriver.new(s[1], server_handler) |
| 118 |
server.transport.set_server |
| 119 |
end |
| 120 |
|
| 121 |
# Process each driver once, return time of next timed event |
| 122 |
def process(now = Time.now, max_time=nil) |
| 123 |
t = collect { |d| d.process(now) }.compact.min |
| 124 |
t = max_time if max_time && t > max_time |
| 125 |
t |
| 126 |
end |
| 127 |
|
| 128 |
def active() |
| 129 |
can_read = self.select { |d| d.can_read? } |
| 130 |
can_write = self.select {|d| d.can_write? } |
| 131 |
IO.select(can_read, can_write, [], 0) |
| 132 |
end |
| 133 |
|
| 134 |
def names() collect { |x| x.handler.names }; end |
| 135 |
|
| 136 |
def clear() each { |x| x.handler.clear; } end |
| 137 |
|
| 138 |
# Run till there is nothing else to do - not handle waiting for timed events |
| 139 |
# but does pass +now+ to process and returns the min returned timed event time |
| 140 |
def run(now=Time.now) |
| 141 |
t = nil |
| 142 |
begin |
| 143 |
t = process(now, t) |
| 144 |
end while active |
| 145 |
t |
| 146 |
end |
| 147 |
end |
| 148 |
|
| 149 |
# Container that listens on a random port |
| 150 |
class ServerContainer < Qpid::Proton::Container |
| 151 |
include Qpid::Proton |
| 152 |
|
| 153 |
def initialize(id=nil, listener_opts=nil, n=1, handler=nil) |
| 154 |
super handler, id |
| 155 |
@listener = listen_io(TCPServer.open(0), ListenOnceHandler.new(listener_opts, n)) |
| 156 |
end |
| 157 |
|
| 158 |
attr_reader :listener |
| 159 |
|
| 160 |
def port() @listener.port; end |
| 161 |
def url() "amqp://:#{port}"; end |
| 162 |
end |
| 163 |
|
| 164 |
class ServerContainerThread < ServerContainer |
| 165 |
def initialize(*args) |
| 166 |
super |
| 167 |
@thread = Thread.new { run } |
| 168 |
end |
| 169 |
|
| 170 |
attr_reader :thread |
| 171 |
def join() @thread.join; end |
| 172 |
end |