From 113ad144e04a6a05989781afc0ca05906283a5ca Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Mon, 27 Jun 2016 19:19:37 -0400 Subject: [PATCH] Add test mixins for file descriptor leaks and unclaimed temporary files. GitLab issue #309. --- mayan/apps/common/tests/base.py | 11 ++++++++ mayan/apps/common/tests/mixins.py | 42 +++++++++++++++++++++++++++++++ requirements/testing-base.txt | 2 +- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 mayan/apps/common/tests/base.py create mode 100644 mayan/apps/common/tests/mixins.py diff --git a/mayan/apps/common/tests/base.py b/mayan/apps/common/tests/base.py new file mode 100644 index 0000000000..2070ddad06 --- /dev/null +++ b/mayan/apps/common/tests/base.py @@ -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. + """ diff --git a/mayan/apps/common/tests/mixins.py b/mayan/apps/common/tests/mixins.py new file mode 100644 index 0000000000..3ed0f9638c --- /dev/null +++ b/mayan/apps/common/tests/mixins.py @@ -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() + diff --git a/requirements/testing-base.txt b/requirements/testing-base.txt index 5b7560fd79..48f9064c42 100644 --- a/requirements/testing-base.txt +++ b/requirements/testing-base.txt @@ -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