Debian Codegraph @

python/tests/integration/broker_PROTON_2116_blocking_connection_object_leak.py

raw
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 from __future__ import absolute_import
21 from __future__ import division
22 from __future__ import print_function
23
24 import optparse
25 import socket
26 import sys
27 import threading
28
29 import cproton
30
31 import proton.handlers
32 import proton.reactor
33 import proton.utils
34
35
36 class Broker(proton.handlers.MessagingHandler):
37 def __init__(self, acceptor_url):
38 # type: (str) -> None
39 super(Broker, self).__init__()
40 self.acceptor_url = acceptor_url
41
42 self.acceptor = None
43 self._acceptor_opened_event = threading.Event()
44
45 def get_acceptor_sockname(self):
46 # type: () -> (str, int)
47 self._acceptor_opened_event.wait()
48 if hasattr(self.acceptor, '_selectable'): # proton 0.30.0+
49 sockname = self.acceptor._selectable._delegate.getsockname()
50 else: # works in proton 0.27.0
51 selectable = cproton.pn_cast_pn_selectable(self.acceptor._impl)
52 fd = cproton.pn_selectable_get_fd(selectable)
53 s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
54 sockname = s.getsockname()
55 return sockname[:2]
56
57 def on_start(self, event):
58 self.acceptor = event.container.listen(self.acceptor_url)
59 self._acceptor_opened_event.set()
60
61 def on_link_opening(self, event):
62 if event.link.is_sender:
63 assert not event.link.remote_source.dynamic, "This cannot happen"
64 event.link.source.address = event.link.remote_source.address
65 elif event.link.remote_target.address:
66 event.link.target.address = event.link.remote_target.address
67
68
69 def main():
70 parser = optparse.OptionParser()
71 parser.add_option("-b", dest="hostport", default="localhost:0", type="string",
72 help="port number to use")
73 options, args = parser.parse_args()
74
75 broker = Broker(options.hostport)
76 container = proton.reactor.Container(broker)
77 threading.Thread(target=container.run).start()
78 print("{0}:{1}".format(*broker.get_acceptor_sockname()))
79 sys.stdout.flush()
80
81
82 if __name__ == '__main__':
83 main()