def testCountOpenFileDescriptors(self): # CNY-2536 startCount = util.countOpenFileDescriptors() fdarr = [ open('/dev/null') for x in range(200) ] endCount = util.countOpenFileDescriptors() self.assertEqual(startCount + len(fdarr), endCount) fdarr = None endCount = util.countOpenFileDescriptors() self.assertEqual(startCount, endCount)
def run(self, *args, **kw): from conary.lib import util fdCount = util.countOpenFileDescriptors() if fdCount != len(self.openFds): self.openFds = self._openFdSet() prePid = os.getpid() try: unittest.TestCase.run(self, *args, **kw) finally: # Make sure some test didn't accidentally fork the # testsuite. postPid = os.getpid() if prePid != postPid: sys.stderr.write("\n*** CHILD RE-ENTERED TESTSUITE ***\n") sys.stderr.write("A forked process was allowed to return to " "the testsuite handler, probably due\nto an exception. " "Find it and kill it!\n") sys.stderr.write("PID was: %d now: %d\n" % (prePid, postPid)) os._exit(2) self.unmock() mock.unmockAll() # ask gc to run to see if we can avoid "leaked -1 file descriptors" gc.collect() fdCount = util.countOpenFileDescriptors() if False and fdCount != len(self.openFds): try: methodName = self.__testMethodName except AttributeError: methodName = self._testMethodName sys.stderr.write("\nTest %s.%s leaked %s file descriptors\n" % (self.__module__, methodName, fdCount - len(self.openFds))) newOpenFds = self._openFdSet() for (fd, contents) in newOpenFds - self.openFds: if contents and ((fd, contents) not in self.openFds): print '%s: %s' % (fd, contents) self.openFds = newOpenFds
def testExtractRpmPayloadFdLeak(self): fdCount0 = util.countOpenFileDescriptors() self.testExtractRpmPayload() fdCount1 = util.countOpenFileDescriptors() self.assertEqual(fdCount1 - fdCount0, 0)