| 1 |
# |
| 2 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
# or more contributor license agreements. See the NOTICE file |
| 4 |
# distributed with this work for additional information |
| 5 |
# regarding copyright ownership. The ASF licenses this file |
| 6 |
# to you under the Apache License, Version 2.0 (the |
| 7 |
# "License"); you may not use this file except in compliance |
| 8 |
# with the License. You may obtain a copy of the License at |
| 9 |
# |
| 10 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
# |
| 12 |
# Unless required by applicable law or agreed to in writing, |
| 13 |
# software distributed under the License is distributed on an |
| 14 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
# KIND, either express or implied. See the License for the |
| 16 |
# specific language governing permissions and limitations |
| 17 |
# under the License. |
| 18 |
# |
| 19 |
|
| 20 |
""" |
| 21 |
PROTON-1800 BlockingConnection descriptor leak |
| 22 |
""" |
| 23 |
from __future__ import absolute_import |
| 24 |
from __future__ import division |
| 25 |
from __future__ import print_function |
| 26 |
|
| 27 |
import contextlib |
| 28 |
import gc |
| 29 |
import os |
| 30 |
import subprocess |
| 31 |
import socket |
| 32 |
import threading |
| 33 |
import unittest |
| 34 |
import uuid |
| 35 |
import warnings |
| 36 |
|
| 37 |
from collections import namedtuple |
| 38 |
|
| 39 |
import cproton |
| 40 |
|
| 41 |
import proton |
| 42 |
import proton.reactor |
| 43 |
|
| 44 |
from proton import Message |
| 45 |
from proton.utils import SyncRequestResponse, BlockingConnection |
| 46 |
from proton.handlers import IncomingMessageHandler |
| 47 |
|
| 48 |
|
| 49 |
def get_fd_set(): |
| 50 |
# type: () -> set[str] |
| 51 |
return set(os.listdir('/proc/self/fd/')) |
| 52 |
|
| 53 |
|
| 54 |
@contextlib.contextmanager |
| 55 |
def no_fd_leaks(test): |
| 56 |
# type: (unittest.TestCase) -> None |
| 57 |
with warnings.catch_warnings(record=True) as ws: |
| 58 |
before = get_fd_set() |
| 59 |
yield |
| 60 |
delta = get_fd_set().difference(before) |
| 61 |
if len(delta) != 0: |
| 62 |
subprocess.check_call("ls -lF /proc/{0}/fd/".format(os.getpid()), shell=True) |
| 63 |
test.fail("Found {0} new fd(s) after the test".format(delta)) |
| 64 |
|
| 65 |
if len(ws) > 0: |
| 66 |
test.fail([w.message for w in ws]) |
| 67 |
|
| 68 |
|
| 69 |
class Broker(proton.handlers.MessagingHandler): |
| 70 |
def __init__(self, acceptor_url): |
| 71 |
# type: (str) -> None |
| 72 |
super(Broker, self).__init__() |
| 73 |
self.acceptor_url = acceptor_url |
| 74 |
|
| 75 |
self.sender = None |
| 76 |
self.acceptor = None |
| 77 |
self._acceptor_opened_event = threading.Event() |
| 78 |
|
| 79 |
def get_acceptor_sockname(self): |
| 80 |
# type: () -> (str, int) |
| 81 |
self._acceptor_opened_event.wait() |
| 82 |
if hasattr(self.acceptor, '_selectable'): # proton 0.30.0+ |
| 83 |
sockname = self.acceptor._selectable._delegate.getsockname() |
| 84 |
else: # works in proton 0.27.0 |
| 85 |
selectable = cproton.pn_cast_pn_selectable(self.acceptor._impl) |
| 86 |
fd = cproton.pn_selectable_get_fd(selectable) |
| 87 |
s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) |
| 88 |
sockname = s.getsockname() |
| 89 |
return sockname[:2] |
| 90 |
|
| 91 |
def on_start(self, event): |
| 92 |
self.acceptor = event.container.listen(self.acceptor_url) |
| 93 |
self._acceptor_opened_event.set() |
| 94 |
|
| 95 |
def on_link_opening(self, event): |
| 96 |
if event.link.is_sender: |
| 97 |
assert event.link.remote_source.dynamic |
| 98 |
address = str(uuid.uuid4()) |
| 99 |
event.link.source.address = address |
| 100 |
self.sender = event.link |
| 101 |
elif event.link.remote_target.address: |
| 102 |
event.link.target.address = event.link.remote_target.address |
| 103 |
|
| 104 |
def on_message(self, event): |
| 105 |
message = event.message |
| 106 |
assert self.sender.source.address == message.reply_to |
| 107 |
reply = proton.Message(body=message.body.upper(), correlation_id=message.correlation_id) |
| 108 |
self.sender.send(reply) |
| 109 |
|
| 110 |
|
| 111 |
@contextlib.contextmanager |
| 112 |
def test_broker(): |
| 113 |
broker = Broker('localhost:0') |
| 114 |
container = proton.reactor.Container(broker) |
| 115 |
threading.Thread(target=container.run).start() |
| 116 |
|
| 117 |
try: |
| 118 |
yield broker |
| 119 |
finally: |
| 120 |
container.stop() |
| 121 |
|
| 122 |
|
| 123 |
PROC_SELF_FD_EXISTS = os.path.exists("/proc/self/fd"), "Skipped: Directory /proc/self/fd does not exist" |
| 124 |
|
| 125 |
|
| 126 |
def skipOnFailure(reason="AssertionError ignored."): |
| 127 |
"""Decorator for test methods that swallows AssertionErrors. |
| 128 |
|
| 129 |
Passing tests are reported unchanged, tests failing with AssertionError are |
| 130 |
reported as skipped, and tests failing with any other kind of error are |
| 131 |
permitted to fail. |
| 132 |
|
| 133 |
Use this to temporarily suppress flaky or environment-sensitive tests without |
| 134 |
turning them into dead code that is not being run at all. |
| 135 |
|
| 136 |
If a test is reliably failing, use unittest.expectedFailure instead.""" |
| 137 |
|
| 138 |
def skip_on_assertion(old_test): |
| 139 |
def new_test(*args, **kwargs): |
| 140 |
try: |
| 141 |
old_test(*args, **kwargs) |
| 142 |
except AssertionError as e: |
| 143 |
raise unittest.SkipTest("Test failure '{0}' ignored: {1}".format( |
| 144 |
str(e), reason)) |
| 145 |
|
| 146 |
return new_test |
| 147 |
|
| 148 |
return skip_on_assertion |
| 149 |
|
| 150 |
|
| 151 |
class Proton1800Test(unittest.TestCase): |
| 152 |
@unittest.skipUnless(*PROC_SELF_FD_EXISTS) |
| 153 |
@skipOnFailure(reason="PROTON-1800: sut is leaking one fd on Ubuntu Xenial docker image") |
| 154 |
def test_sync_request_response_blocking_connection_no_fd_leaks(self): |
| 155 |
with test_broker() as tb: |
| 156 |
sockname = tb.get_acceptor_sockname() |
| 157 |
url = "{0}:{1}".format(*sockname) |
| 158 |
opts = namedtuple('Opts', ['address', 'timeout'])(address=url, timeout=3) |
| 159 |
|
| 160 |
with no_fd_leaks(self): |
| 161 |
client = SyncRequestResponse( |
| 162 |
BlockingConnection(url, opts.timeout, allowed_mechs="ANONYMOUS"), "somequeue") |
| 163 |
try: |
| 164 |
request = "One Two Three Four" |
| 165 |
response = client.call(Message(body=request)) |
| 166 |
self.assertEqual(response.body, "ONE TWO THREE FOUR") |
| 167 |
finally: |
| 168 |
client.connection.close() |
| 169 |
|
| 170 |
gc.collect() |