Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to confirm that rotation handler is working as intended. #8197

Merged
merged 2 commits into from Jul 14, 2021

Conversation

rtibbles
Copy link
Member

@rtibbles rtibbles commented Jul 10, 2021

Summary

  • Adds test to assert that rotating handler is indeed rotating correctly

References

Provides a test to show that #5989 is not a real issue and so closes #5989

Reviewer guidance

Does the test make sense?


Testing checklist

  • Contributor has fully tested the PR manually
  • If there are any front-end changes, before/after screenshots are included
  • Critical user journeys are covered by Gherkin stories
  • Critical and brittle code paths are covered by unit tests

PR process

  • PR has the correct target branch and milestone
  • PR has 'needs review' or 'work-in-progress' label
  • If PR is ready for review, a reviewer has been added. (Don't use 'Assignees')
  • If this is an important user-facing change, PR or related issue has a 'changelog' label
  • If this includes an internal dependency change, a link to the diff is provided

Reviewer checklist

  • Automated test coverage is satisfactory
  • PR is fully functional
  • PR has been tested for accessibility regressions
  • External dependency files were updated if necessary (yarn and pip)
  • Documentation is updated
  • Contributor is in AUTHORS.md

@rtibbles rtibbles added the TODO: needs review Waiting for review label Jul 10, 2021
@rtibbles rtibbles added this to the 0.15.0 milestone Jul 10, 2021

def test_getFilesToDelete(self):
temp_dir = tempfile.mkdtemp()
file_handle, log_file = tempfile.mkstemp(suffix=".txt", dir=temp_dir)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a pitty we don't have TemporaryDirectory in Python 2.7, but, for the purpose of this code, could it be done using with tempfile.TemporaryFile() as log_file , so you don't need to import and use shutil?
i.e.

@@ -1,5 +1,4 @@
 import os
-import shutil
 import tempfile
 from time import sleep
 
@@ -33,21 +32,14 @@ class KolibriTimedRotatingFileHandlerTestCase(TestCase):
         self.assertNotEqual(os.listdir(archive_dir), [])
 
     def test_getFilesToDelete(self):
-        temp_dir = tempfile.mkdtemp()
-        file_handle, log_file = tempfile.mkstemp(suffix=".txt", dir=temp_dir)
-        os.close(file_handle)
-        handler = KolibriTimedRotatingFileHandler(log_file, backupCount=3, when="s")
-        sleep(1)
-        handler.doRollover()
-        sleep(1)
-        handler.doRollover()
-        sleep(1)
-        handler.doRollover()
-        sleep(1)
-        handler.doRollover()
-        self.assertEqual(len(handler.getFilesToDelete()), 1)
-        try:
-            os.remove(log_file)
-        except OSError:
-            pass
-        shutil.rmtree(temp_dir, ignore_errors=True)
+        with tempfile.NamedTemporaryFile(suffix=".txt") as log_file:
+            handler = KolibriTimedRotatingFileHandler(log_file.name, backupCount=3, when="s")
+            sleep(1)
+            handler.doRollover()
+            sleep(1)
+            handler.doRollover()
+            sleep(1)
+            handler.doRollover()
+            sleep(1)
+            handler.doRollover()
+            self.assertEqual(len(handler.getFilesToDelete()), 1)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I've had to remove our use of NamedTemporaryFile in general, because it breaks on Windows due to permissions errors.

Copy link
Member

@jredrejo jredrejo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see it can't be used as the file is not closed so the directory can't be created. There's no alternative for Python 2.7 without tempfile.TemporaryDirectory

@rtibbles rtibbles merged commit 07d1a71 into learningequality:develop Jul 14, 2021
@rtibbles rtibbles deleted the handler_count branch July 14, 2021 16:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TODO: needs review Waiting for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

log archive directory doesn't have the correct number of files
2 participants