File: test_PROTON_1800_syncrequestresponse_fd_leak.py

package info (click to toggle)
qpid-proton 0.37.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,384 kB
  • sloc: ansic: 37,828; cpp: 37,140; python: 15,302; ruby: 6,018; xml: 477; sh: 320; pascal: 52; makefile: 18
file content (170 lines) | stat: -rw-r--r-- 5,747 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

"""
PROTON-1800 BlockingConnection descriptor leak
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import contextlib
import gc
import os
import subprocess
import socket
import threading
import unittest
import uuid
import warnings

from collections import namedtuple

import cproton

import proton
import proton.reactor

from proton import Message
from proton.utils import SyncRequestResponse, BlockingConnection
from proton.handlers import IncomingMessageHandler


def get_fd_set():
    # type: () -> set[str]
    return set(os.listdir('/proc/self/fd/'))


@contextlib.contextmanager
def no_fd_leaks(test):
    # type: (unittest.TestCase) -> None
    with warnings.catch_warnings(record=True) as ws:
        before = get_fd_set()
        yield
        delta = get_fd_set().difference(before)
        if len(delta) != 0:
            subprocess.check_call("ls -lF /proc/{0}/fd/".format(os.getpid()), shell=True)
            test.fail("Found {0} new fd(s) after the test".format(delta))

        if len(ws) > 0:
            test.fail([w.message for w in ws])


class Broker(proton.handlers.MessagingHandler):
    def __init__(self, acceptor_url):
        # type: (str) -> None
        super(Broker, self).__init__()
        self.acceptor_url = acceptor_url

        self.sender = None
        self.acceptor = None
        self._acceptor_opened_event = threading.Event()

    def get_acceptor_sockname(self):
        # type: () -> (str, int)
        self._acceptor_opened_event.wait()
        if hasattr(self.acceptor, '_selectable'):  # proton 0.30.0+
            sockname = self.acceptor._selectable._delegate.getsockname()
        else:  # works in proton 0.27.0
            selectable = cproton.pn_cast_pn_selectable(self.acceptor._impl)
            fd = cproton.pn_selectable_get_fd(selectable)
            s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
            sockname = s.getsockname()
        return sockname[:2]

    def on_start(self, event):
        self.acceptor = event.container.listen(self.acceptor_url)
        self._acceptor_opened_event.set()

    def on_link_opening(self, event):
        if event.link.is_sender:
            assert event.link.remote_source.dynamic
            address = str(uuid.uuid4())
            event.link.source.address = address
            self.sender = event.link
        elif event.link.remote_target.address:
            event.link.target.address = event.link.remote_target.address

    def on_message(self, event):
        message = event.message
        assert self.sender.source.address == message.reply_to
        reply = proton.Message(body=message.body.upper(), correlation_id=message.correlation_id)
        self.sender.send(reply)


@contextlib.contextmanager
def test_broker():
    broker = Broker('localhost:0')
    container = proton.reactor.Container(broker)
    threading.Thread(target=container.run).start()

    try:
        yield broker
    finally:
        container.stop()


PROC_SELF_FD_EXISTS = os.path.exists("/proc/self/fd"), "Skipped: Directory /proc/self/fd does not exist"


def skipOnFailure(reason="AssertionError ignored."):
    """Decorator for test methods that swallows AssertionErrors.

    Passing tests are reported unchanged, tests failing with AssertionError are
    reported as skipped, and tests failing with any other kind of error are
    permitted to fail.

    Use this to temporarily suppress flaky or environment-sensitive tests without
    turning them into dead code that is not being run at all.

    If a test is reliably failing, use unittest.expectedFailure instead."""

    def skip_on_assertion(old_test):
        def new_test(*args, **kwargs):
            try:
                old_test(*args, **kwargs)
            except AssertionError as e:
                raise unittest.SkipTest("Test failure '{0}' ignored: {1}".format(
                    str(e), reason))

        return new_test

    return skip_on_assertion


class Proton1800Test(unittest.TestCase):
    @unittest.skipUnless(*PROC_SELF_FD_EXISTS)
    @skipOnFailure(reason="PROTON-1800: sut is leaking one fd on Ubuntu Xenial docker image")
    def test_sync_request_response_blocking_connection_no_fd_leaks(self):
        with test_broker() as tb:
            sockname = tb.get_acceptor_sockname()
            url = "{0}:{1}".format(*sockname)
            opts = namedtuple('Opts', ['address', 'timeout'])(address=url, timeout=3)

            with no_fd_leaks(self):
                client = SyncRequestResponse(
                    BlockingConnection(url, opts.timeout, allowed_mechs="ANONYMOUS"), "somequeue")
                try:
                    request = "One Two Three Four"
                    response = client.call(Message(body=request))
                    self.assertEqual(response.body, "ONE TWO THREE FOUR")
                finally:
                    client.connection.close()

        gc.collect()