-
Notifications
You must be signed in to change notification settings - Fork 19
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
Fix some flaky tests sorting the parsers by name also #184
Conversation
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 understand why this is useful but I feel like we should explain what we are after with the change. It is not "fix some flaky tests" (which tests? why do they need fixing? are there situations where tests would pass where they should have failed?) but "always apply parsers in the same, deterministic order". Maybe we could find a way to autogenerate the priority value for this, instead of doing a (to me - not very obvious) namesort?
Also we need to apply autoformatting so that CI passes.
lib/format_parser.rb
Outdated
if @parser_priorities[parser_a] != @parser_priorities[parser_b] | ||
@parser_priorities[parser_a] <=> @parser_priorities[parser_b] | ||
else | ||
parser_a.class.name <=> parser_b.class.name |
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.
We really need to explain why we are doing this in a comment
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 added a comment, could you take a look on if it's good pls?
@julik I tried to reproduce this failure in my laptop but I couldn't! I'm using rbenv, ruby 2.2.10p489 and rubocop 0.52.1 Do you have any idea of why I can't reproduce it? |
Forget about it, I was in the wrong branch as it shows the picture! |
@julik I updated the description with more details. Could you take a look pls? |
@linkyndy the current sorting sorts by the priority. Each parser has a priority as follows:
But, when the priority is the same, the method sort does not guarantee that the order of the result will always be the same:
In fact, I think this explains almost everything! |
That's where I wanted to get 😊 |
Still on the fence about this sorting using a class name. We specify in the documentation for example that a parser can be a Proc. A Proc will not give you a meaningful class name you can sort on, you also pretty much mandate that a parser must be an instance of a fairly unique class :thinking_face: while we can change the library to work this way it does make the API more complicated. How could we solve this differently I wonder? For example, we add parsers in a certain priority order if a priority value is given. What if we were to assign a better priority value if none is specified, based on the number of parsers already configured? |
What if we change the method Example: FormatParser.register_parser(:a, 1)
FormatParser.register_parser(:b, 2)
FormatParser.register_parser(:c, 2)
FormatParser.register_parser(:d, 2)
FormatParser.register_parser(:e, 3) result: [
[:a, 1],
[:b, 2],
[:c, 2.0001],
[:d, 2.0002],
[:e, 3]
] |
So the issue that we have is that also parsers with the same priority get sorted undeterministically right? |
yes, it is! |
Ok, what if we record the order in which parsers get registered (and when doing a |
@julik could you take a look if this is the change that you expected pls? |
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.
🔥 Sweet
I used |
I think it made total sense given ordering was not important. Now that it is, an ordered set would make more sense. But it is good to be cautious with regards to this aspect 🙂 |
@linkyndy I think I replied to your question and now it's ready to be approved. Is there anything else that worries you? |
fix #183
Motivation
Some specs, like this one, calls the method
FormatParser.parse
with the optionresults: :first
, which is the default value, and checks if the returned parser is the expected one.In some cases,
FormatParser.parse
recognizes a file with multiple formats, and usingresults: :first
, it returns only the first one.The problem is that the order of these results varies.
In this method, there is a logic to sort the parsers by priority, but since there are many with the same priority, the order varies on each execution.
Because of this, some tests fail as the following example:
I don't know exactly why the order sometimes is different, but I could prove it by adding this line:
When I run on my laptop:
When I run in production:
I think it's related to the following MUTEX, which sets a few class variables when registering the parsers:
I also did a test to see if this depend on the order the files are loaded, but the results are the same in staging and also in my laptop:
Proposal
This PR changes the way the registered parsers are sorted.
It can help us to fix the flaky tests and also getting different results when running
FormatParser.parse
to get only the first result.