8000 bpo-25872: Add unit tests for linecache and threading by uniocto · Pull Request #25913 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-25872: Add unit tests for linecache and threading #25913

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

Merged
merged 21 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Lib/test/test_linecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,48 @@ def raise_memoryerror(*args, **kwargs):
self.assertEqual(lines3, [])
self.assertEqual(linecache.getlines(FILENAME), lines)

def test_oserror(self):
Copy link
Member

Choose a reason for hiding this comment

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

This is better. Now let's see how it should be organized. I have several comments:

  1. test_oserror is a a good name for a test that tests oserror, but this test is testing something else.
  2. It's better to split tests so that each test function is testing one thing (and its name says what).
  3. see https://docs.python.org/3/library/linecache.html -- updatecache is not part of the documented API, so I'm not sure it needs to be tested directly.
  4. There is already a test_checkcache function higher in this file, which covers some of what this test does but not all of it. It should be refactored into the new test rather than just adding a new one, so that all related tests are together.
  5. The checkcache() with no parameters API needs to be tested as well.

You could create a new test class , say class LineCacheInvalidationTests(unittest.TestCase):
Put the initialization code (what you called _oserror_helper) in this class's setUp.
Then add test functions for each test case.

def _oserror_helper():
linecache.clearcache()
be_deleted_file = os_helper.TESTFN + '.1'
be_modified_file = os_helper.TESTFN + '.2'
unchange_file = os_helper.TESTFN + '.3'
self.addCleanup(os_helper.unlink, be_deleted_file)
self.addCleanup(os_helper.unlink, be_modified_file)
self.addCleanup(os_helper.unlink, unchange_file)
with open(be_deleted_file, 'w', encoding='utf-8') as source:
source.write('print("will be deleted")')
with open(be_modified_file, 'w', encoding='utf-8') as source:
source.write('print("will be modified")')
with open(unchange_file, 'w', encoding='utf-8') as source:
source.write('print("unchange")')

_ = linecache.getlines(be_deleted_file)
_ = linecache.getlines(be_modified_file)
_ = linecache.getlines(unchange_file)
self.assertEqual(3, len(linecache.cache.keys()))

os.remove(be_deleted_file)
with open(be_modified_file, 'w', encoding='utf-8') as source:
source.write('print("was modified")')
return (be_deleted_file, be_modified_file, unchange_file)

deleted_file, modified_file, unchange_file = _oserror_helper()
_ = linecache.checkcache(deleted_file)
self.assertEqual(2, len(linecache.cache.keys()))
_ = linecache.checkcache(modified_file)
self.assertEqual(1, len(linecache.cache.keys()))
_ = linecache.checkcache(unchange_file)
self.assertEqual(1, len(linecache.cache.keys()))

deleted_file, modified_file, unchange_file = _oserror_helper()
_ = linecache.updatecache(deleted_file)
self.assertEqual(2, len(linecache.cache.keys()))
_ = linecache.updatecache(modified_file)
self.assertEqual(2, len(linecache.cache.keys()))
_ = linecache.updatecache(unchange_file)
self.assertEqual(2, len(linecache.cache.keys()))


if __name__ == "__main__":
unittest.main()
19 changes: 19 additions & 0 deletions Lib/test/test_threading.py
4023
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import subprocess
import signal
import textwrap
import traceback

from unittest import mock
from test import lock_tests
Expand Down Expand Up @@ -1338,6 +1339,24 @@ def run(self):
# explicitly break the reference cycle to not leak a dangling thread
thread.exc = None

def test_multithread_modify_file_noerror(self):
def modify_file():
with open(__file__, 'a') as fp:
fp.write(' ')
traceback.format_stack()

threads = [
threading.Thread(target=modify_file)
for i in range(100)
]
try:
Copy link
Member

Choose a reason for hiding this comment

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

Which exceptions are you trying to ignore here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nothing, sorry. I removed try.

for t in threads:
t.start()
for t in threads:
t.join()
Copy link
Member

Choose a reason for hiding this comment

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

Is there a reason why you don't do

for t in threads:
    t.start()
    t.join()

?

Copy link
Contributor Author
@uniocto uniocto May 9, 2021

Choose a reason for hiding this comment

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

Nothing, sorry.
So I fixed it to your feedback.

finally:
pass


class ThreadRunFail(threading.Thread):
def run(self):
Expand Down
0