-
Notifications
You must be signed in to change notification settings - Fork 31
CDSK-873 - remove builds by 'brid'. Add logging and tests #351
CDSK-873 - remove builds by 'brid'. Add logging and tests #351
Conversation
|
||
msg = "removeBuildRequest does not remove anything. breq.brid: %s" % breq.brdict['brid'] | ||
if not is_removed: | ||
klog.err_json(msg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That looks more complicated than necessary :)
I thought about something like this (unless you see something that wouldn't work):
def removeBuildRequest(self, breq):
# reset the checkMerges in case the breq still in the master cache
def remove_dict(brdicts):
for brdict in brdicts:
if brdict['brid'] == breq.brdict['brid']:
brdicts.remove(brdict)
return True
return False
if not breq.brdict:
pass # log some error here
removed = remove_dict(self.unclaimedBrdicts) or remove_dict(self.resumeBrdicts)
if removed:
breq.checkMerges = True
breq.retries = 0
breq.hasBeenMerged = False
self.breqCache.remove(breq.id)
else:
klog.err_json(
"removeBuildRequest does not remove anything. breq.brid: %s" % \
breq.brdict['brid']
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
list comprehension removes all dicts with the ['brid'] so we don't leave a possible mess in memory. In your case we remove only one dict from list. @Mowinski What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They shouldn't show up multiple times in there, but you can do it like this to feel safer:
def remove_dict(brdicts):
removed = False
for brdict in brdicts:
if brdict['brid'] == breq.brdict['brid']:
brdicts.remove(brdict)
removed = True
return removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a huge fan of replacing the entire list with a comprehension because I don't know if anyone else has a hard reference to the old list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said below, I am not a fan of inner function in this place :) and I agree with Diogo, whats more, as I see we modify the property of class so even we do not need an inner function to do it and a private method should be enough to do it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I've used Diogo's code
@@ -422,13 +422,35 @@ def claimBuildRequests(self, breqs): | |||
|
|||
def removeBuildRequest(self, breq): | |||
# reset the checkMerges in case the breq still in the master cache | |||
|
|||
def remove_dict(brdicts, breq): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that using an inner function in methods is a little bit unpractical. There are three cases when we should use the inner function:
- To separate assertion/parameters checking from recursive code
- To encapsulate function, if there is a reason to hide it from the global scope
- To create factory function, that using closure.
removeBuildRequest
isn't recursive function so the first point doesn't affect it.
This is also a method, so if you want to make it private, you can create remove_dict
as a private method in the class.
And the last point, this method isn't factory, so using closure variable is an unwanted side effect.
So in my opinion, you can create remove_dict
to a static method or a utils function and do not worry about scope :)
Here is link to great article about inner function: https://realpython.com/inner-functions-what-are-they-good-for/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course, it would be nice to add docstring and a little bit change name of this function, now it suggests that from brdicts
remove dictionary. Ensure that u want to modify parameters or just return a copy.
self.resumeBrdicts = resume_data['brdicts'] | ||
is_removed |= resume_data['is_removed'] | ||
|
||
msg = "removeBuildRequest does not remove anything. breq.brid: %s" % breq.brdict['brid'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor: this msg can be in if
statement.
chooser = self.brd.katanaBuildChooser | ||
chooser.unclaimedBrdicts = [{'brid': 1}, {'brid': 2}] | ||
chooser.resumeBrdicts = [{'brid': 3}, {'brid': 4}] | ||
breq = mock.Mock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, you do not need mock, you can use simple stub
BregClass = namedtuple('Breg', ['brdict'])
breq = BregClass(brdict={'brid': 3})
But please be aware and ensure you create all fields, which is used and checked in this test (as far as I see, checkMerges
, retries
and hasBeenMerged
has no initialization)
270e478
to
8bee438
Compare
if self.resumeBrdicts and breq.brdict and breq.brdict in self.resumeBrdicts: | ||
self.resumeBrdicts.remove(breq.brdict) | ||
self.breqCache.remove(breq.id) | ||
def __remove_brdict_by_brid(self, brdicts, buildrequest): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor: You can move it to bottom of this class, just only for readability
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
'brid'
key instead full dictremoveBuildRequest