Add test mixins for file descriptor leaks and unclaimed temporary files. GitLab issue #309.
This commit is contained in:
11
mayan/apps/common/tests/base.py
Normal file
11
mayan/apps/common/tests/base.py
Normal 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.
|
||||
"""
|
||||
42
mayan/apps/common/tests/mixins.py
Normal file
42
mayan/apps/common/tests/mixins.py
Normal 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()
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user