Add test mixins for file descriptor leaks and unclaimed temporary files. GitLab issue #309.

This commit is contained in:
Roberto Rosario
2016-06-27 19:19:37 -04:00
parent c531631a37
commit 113ad144e0
3 changed files with 54 additions and 1 deletions

View File

@@ -0,0 +1,11 @@
from __future__ import unicode_literals
from django.test import TestCase
from .mixins import FileDescriptorCheckMixin, TempfileCheckMixin
class BaseTestCase(FileDescriptorCheckMixin, TempfileCheckMixin, TestCase):
"""
This is the most basic test case class any test in the project should use.
"""

View File

@@ -0,0 +1,42 @@
import os
import psutil
from ..settings import setting_temporary_directory
class TempfileCheckMixin(object):
def _get_temporary_entries_count(self):
return len(os.listdir(setting_temporary_directory.value))
def setUp(self):
super(TempfileCheckMixin, self).setUp()
self._temporary_items = self._get_temporary_entries_count()
def tearDown(self):
self.assertEqual(
self._temporary_items, self._get_temporary_entries_count(),
msg='Orphan temporary file. The number of temporary file and '
'directories at the start and at the end of the test are not the '
'same.'
)
super(TempfileCheckMixin, self).tearDown()
class FileDescriptorCheckMixin(object):
def _get_descriptor_count(self):
process = psutil.Process()
return process.num_fds()
def setUp(self):
super(FileDescriptorCheckMixin, self).setUp()
self._descriptor_count = self._get_descriptor_count()
def tearDown(self):
self.assertEqual(
self._descriptor_count, self._get_descriptor_count(),
msg='File descriptor leak. The number of file descriptors at '
'the start and at the end of the test are not the same.'
)
super(FileDescriptorCheckMixin, self).tearDown()

View File

@@ -4,4 +4,4 @@ coveralls==0.5
django-test-without-migrations==0.2
mock==2.0.0
tox==2.1.1
psutil==4.3.0