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

Tickets/dm 10767 #59

Merged
merged 4 commits into from Jun 5, 2017
Merged

Tickets/dm 10767 #59

merged 4 commits into from Jun 5, 2017

Conversation

n8pease
Copy link
Contributor

@n8pease n8pease commented Jun 2, 2017

No description provided.

fileType = file
except NameError:
from io import IOBase
fileType = IOBase
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use use io.IOBase in in Python 2? It is part of the standard library. If it works, I think using it would be a lot cleaner than the above. Just import it and use it.

If that does not work for some reason, then I think this would be significantly cleaner if your new code was put with the other import statements at the top of the file.

Copy link
Contributor

Choose a reason for hiding this comment

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

file does not inherit from io.IOBase in python 2.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if you do need to still assign the class to a variable, please use a variable name that starts with a capital letter.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 on putting it in the imports though

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious whether it matters that file inherits from io.IOBase. I realize that it may mean a tiny change to one test, but is that a bad thing?

if os.path.exists(os.path.join(ROOT, 'root/out')):
shutil.rmtree(os.path.join(ROOT, 'root/out'))
if os.path.exists(self.outputDir):
shutil.rmtree(self.outputDir)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it safe to not manually delete the butler? If it holds onto any objects that inherit from Citizen then failure to do so will trigger an error in the memory test.

Copy link
Contributor

Choose a reason for hiding this comment

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

It seems risky to have setUp call tearDown, since normally users are free to add things to tearDown without worrying about breaking other methods such as setUp. Instead, I suggest that you make a new method deleteOutputDir that does what tearDown does above, then have setUp and tearDown call that.

Note that it would then be safe to have tearDown delete the butler again, which demonstrates my point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

del doesn't actually do anything to destroy the object, it makes it available for garbage collection. when the test class is destroyed, the butler goes out of scope, same as if del had been called.
(and, the memory tests pass.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes the memory test passes now, but if butler ever is modified to include an object that inherits from Citizen it will stop passing and folks may be surprised at this. An explicit delete is safer.

Copy link
Contributor

Choose a reason for hiding this comment

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

In any case I think refactoring the code as suggested is significantly cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see how allowing the butler instance to be GC'd will not delete a contained Citizen object whereas explicitly handing the butler to the GC will delete the contained Citizen...?

Copy link
Contributor

@r-owen r-owen Jun 2, 2017

Choose a reason for hiding this comment

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

Just to clarify: it is definitely true that we must explicitly delete test case attributes that inherit from Citizen in tearDown. If we don't, the memory test fails. Allowing such objects to be garbage collected definitely does not work. It is not a question of whether the objects eventually go out of scope -- of course they do -- it's whether they go out of scope fast enough that the memory test doesn't think memory has leaked.

Also, I thought CPython did delete immediately when the ref count dropped to 0 (unless there are cycles, which take longer to clean up?) -- though I agree the language specification does not require that. If this was not the case, then I'm surprised our memory test is usable; I would expect random failures in most of our packages.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suspect that the test case instance is not deleted until after the memory test has run, so by then it's too late for automatic garbage collection to help us. (I can't think why else we'd have to explicitly delete test case instances to avoid false memory leak errors).

from io import IOBase
fileType = IOBase

isinstance(f, fileType)
Copy link
Contributor

Choose a reason for hiding this comment

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

You can probably get rid of this line, since the next line does the assert

@RobertLuptonTheGood
Copy link
Contributor

I think that the test object remains in scope, so the GC doesn't happen until the process exists.

@n8pease
Copy link
Contributor Author

n8pease commented Jun 2, 2017

so @r-owen is right, and the memory test fails. It does seem to be timing or platform related (passed on centos-6, failed on centos-7).

@n8pease n8pease merged commit 3ef808b into master Jun 5, 2017
@ktlim ktlim deleted the tickets/DM-10767 branch August 25, 2018 06:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants