#
# 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
#

from proctest import *

class LimitedBroker(object):
    def __init__(self, test, fdlimit):
        self.test = test
        self.fdlimit = fdlimit

    def __enter__(self):
        with TestPort() as tp:
            self.port = str(tp.port)
            self.proc = self.test.proc(['prlimit', '-n%d' % self.fdlimit, 'broker', '', self.port])
            self.proc.wait_re("listening")
            return self

    def __exit__(self, *args):
        b = getattr(self, "proc")
        if b:
            if b.poll() not in [1, None]: # Broker crashed or got expected connection error
                raise ProcError(b, "broker crash")
            b.kill()

# Check if we can run prlimit to control resources
try:
    Proc(["prlimit"]).wait_exit()
    has_prlimit = True
except:
    has_prlimit = False

class FdLimitTest(ProcTestCase):

    def setUp(self):
        global has_prlimit
        if not has_prlimit:
            self.skipTest("prlimit not available")
        super(FdLimitTest, self).setUp()

    def proc(self, *args, **kwargs):
        """Skip valgrind for all processes started by this test"""
        return super(FdLimitTest, self).proc(*args, valgrind=False, **kwargs)

    def test_fd_limit_broker(self):
        """Check behaviour when running out of file descriptors on accept"""
        # Not too many FDs but not too few either, some are used for system purposes.
        fdlimit = 256
        with LimitedBroker(self, fdlimit) as b:
            receivers = []
            # Start enough receivers to use all FDs, make sure the broker logs an error
            for i in xrange(fdlimit+1):
                receivers.append(self.proc(["receive", "", b.port, str(i)]))

            # Note: libuv silently swallows EMFILE/ENFILE errors so there is no error reporting.
            # The epoll proactor will close the users connection with the EMFILE/ENFILE error
            if "TRANSPORT_CLOSED" in b.proc.out:
                self.assertIn("open files", b.proc.out)

            # All FDs are now in use, send attempt should fail or hang
            self.assertIn(self.proc(["send", "", b.port, "x"]).poll(), [1, None])

            # Kill receivers to free up FDs
            for r in receivers:
                r.kill()
            for r in receivers:
                r.wait_exit(expect=None)
            # send/receive should succeed now
            self.assertIn("10 messages sent", self.proc(["send", "", b.port]).wait_exit())
            self.assertIn("10 messages received", self.proc(["receive", "", b.port]).wait_exit())

if __name__ == "__main__":
    main()

@@ -1530,267 +1530,51 @@
 t()%0A
-    has_prlimit = True%0Aexcept:%0A    has_prlimit = False%0A%0Aclass FdLimitTest(ProcTestCase):%0A%0A    def setUp(self):%0A        global has_prlimit%0A        if not has_prlimit:%0A            self.skipTest(%22prlimit not available%22)%0A        super(FdLimitTest, self).setUp()%0A%0A
+%0A    class FdLimitTest(ProcTestCase):%0A%0A    
     
@@ -1614,16 +1614,20 @@
         
+    
 %22%22%22Skip 
@@ -1673,24 +1673,28 @@
 his test%22%22%22%0A
+    
         retu
@@ -1760,16 +1760,20 @@
 wargs)%0A%0A
+    
     def 
@@ -1808,16 +1808,20 @@
         
+    
 %22%22%22Check
@@ -1877,24 +1877,28 @@
 n accept%22%22%22%0A
+    
         # No
@@ -1971,16 +1971,20 @@
 rposes.%0A
+    
         
@@ -2001,24 +2001,28 @@
 256%0A        
+    
 with Limited
@@ -2053,32 +2053,36 @@
  b:%0A            
+    
 receivers = %5B%5D%0A 
@@ -2080,16 +2080,20 @@
 rs = %5B%5D%0A
+    
         
@@ -2180,24 +2180,28 @@
             
+    
 for i in xra
@@ -2216,16 +2216,20 @@
 mit+1):%0A
+    
         
@@ -2298,16 +2298,20 @@
 (i)%5D))%0A%0A
+    
         
@@ -2411,16 +2411,20 @@
         
+    
 # The ep
@@ -2509,16 +2509,20 @@
         
+    
 if %22TRAN
@@ -2550,16 +2550,20 @@
 oc.out:%0A
+    
         
@@ -2623,16 +2623,20 @@
         
+    
 # All FD
@@ -2686,16 +2686,20 @@
 or hang%0A
+    
         
@@ -2773,16 +2773,20 @@
 None%5D)%0A%0A
+    
         
@@ -2825,32 +2825,36 @@
 FDs%0A            
+    
 for r in receive
@@ -2873,16 +2873,20 @@
         
+    
 r.kill()
@@ -2886,16 +2886,20 @@
 .kill()%0A
+    
         
@@ -2938,16 +2938,20 @@
         
+    
 r.wait_e
@@ -2967,16 +2967,20 @@
 t=None)%0A
+    
         
@@ -3017,16 +3017,20 @@
 eed now%0A
+    
         
@@ -3116,32 +3116,36 @@
 ())%0A            
+    
 self.assertIn(%221
@@ -3215,16 +3215,20 @@
 xit())%0A%0A
+    
 if __nam
@@ -3246,16 +3246,20 @@
 ain__%22:%0A
+    
     main
@@ -3261,8 +3261,67 @@
  main()%0A
+%0Aexcept:%0A    print %22Skipping test: prlimit not available%22%0A%0A

Fix tests/fdlimit.py to work on python 2.6