| 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 |
from __future__ import absolute_import |
| 20 |
from __future__ import division |
| 21 |
from __future__ import print_function |
| 22 |
|
| 23 |
import os |
| 24 |
import subprocess |
| 25 |
import time |
| 26 |
import unittest |
| 27 |
|
| 28 |
import test_subprocess |
| 29 |
|
| 30 |
# Check if we can run prlimit to control resources |
| 31 |
try: |
| 32 |
assert subprocess.check_call(["prlimit"], stdout=open(os.devnull, 'w')) == 0, 'prlimit is present, but broken' |
| 33 |
prlimit_available = True |
| 34 |
except OSError: |
| 35 |
prlimit_available = False |
| 36 |
|
| 37 |
|
| 38 |
class PRLimitedBroker(test_subprocess.Server): |
| 39 |
def __init__(self, fdlimit, *args, **kwargs): |
| 40 |
super(PRLimitedBroker, self).__init__( |
| 41 |
['prlimit', '-n{0:d}:'.format(fdlimit), "broker", "", "0"], # `-n 256:` sets only soft limit to 256 |
| 42 |
stdout=subprocess.PIPE, universal_newlines=True, *args, **kwargs) |
| 43 |
self.fdlimit = fdlimit |
| 44 |
|
| 45 |
|
| 46 |
class FdLimitTest(unittest.TestCase): |
| 47 |
devnull = open(os.devnull, 'w') |
| 48 |
|
| 49 |
@classmethod |
| 50 |
def tearDownClass(cls): |
| 51 |
if cls.devnull: |
| 52 |
cls.devnull.close() |
| 53 |
|
| 54 |
@unittest.skipUnless(prlimit_available, "prlimit not available") |
| 55 |
def test_fd_limit_broker(self): |
| 56 |
"""Check behaviour when running out of file descriptors on accept""" |
| 57 |
# Not too many FDs but not too few either, some are used for system purposes. |
| 58 |
fdlimit = 256 |
| 59 |
with PRLimitedBroker(fdlimit, kill_me=True) as b: |
| 60 |
receivers = [] |
| 61 |
|
| 62 |
# Start enough receivers to use all FDs |
| 63 |
# NOTE: broker does not log a file descriptor related error at any point in the test, only |
| 64 |
# PN_TRANSPORT_CLOSED: amqp:connection:framing-error: connection aborted |
| 65 |
# PN_TRANSPORT_CLOSED: proton:io: Connection reset by peer - disconnected :5672 (connection aborted) |
| 66 |
for i in range(fdlimit): |
| 67 |
receiver = subprocess.Popen(["receive", "", b.port, str(i)], stdout=self.devnull, stderr=subprocess.STDOUT) |
| 68 |
receivers.append(receiver) |
| 69 |
# Allow these subprocesses time to establish ahead of the upcoming test sender. |
| 70 |
time.sleep(1) |
| 71 |
|
| 72 |
# All FDs are now in use, new send should not succeed. May fail by hanging (epoll) or by |
| 73 |
# immediate failure (libuv). But poll() should never be 0. |
| 74 |
sender = subprocess.Popen(["send", "", b.port, "x"], |
| 75 |
stdout=self.devnull, stderr=subprocess.STDOUT) |
| 76 |
time.sleep(1) # polling for None immediately would always succeed, regardless whether send hangs or not |
| 77 |
self.assertNotEqual(sender.poll(), 0) |
| 78 |
|
| 79 |
# Kill receivers to free up FDs |
| 80 |
for r in receivers: |
| 81 |
r.kill() |
| 82 |
for r in receivers: |
| 83 |
r.wait() |
| 84 |
|
| 85 |
# Sender completes on its own |
| 86 |
sender.wait() |
| 87 |
|
| 88 |
# Additional send/receive should succeed now |
| 89 |
self.assertIn("10 messages sent", test_subprocess.check_output( |
| 90 |
["send", "", b.port], universal_newlines=True)) |
| 91 |
self.assertIn("10 messages received", test_subprocess.check_output( |
| 92 |
["receive", "", b.port], universal_newlines=True)) |
| 93 |
|
| 94 |
|
| 95 |
if __name__ == "__main__": |
| 96 |
unittest.main() |