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

controller: replaced parse_duration package with custom parser #607

Merged
merged 1 commit into from
Oct 24, 2022

Conversation

mjsterckx
Copy link
Contributor

Issue number:

Closes #596

Description of changes:

The parse_duration package is no longer being maintained. Since we used the package for a very specific use-case (parsing the timeout string, which is already validated by regex), it was easier to remove the package and implement a short function to replace it. This function takes a string that conforms to "^((([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?|\d+)$" and converts it into a Duration object.

Testing done:

Created Test object with various valid values for timeout ("123", "1d2h3m4s", "500s", "3h100s", "20d", etc.) which all resulted in the correct Duration. When a value was used for timeout that didn't conform to the regex, the object could not be created (as is expected).

Terms of contribution:

By submitting this pull request, I agree that this contribution is dual-licensed under the terms of both the Apache License, version 2.0, and the MIT license.

Copy link
Contributor

@webern webern left a comment

Choose a reason for hiding this comment

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

Nice. Please add unit tests.

controller/src/constants.rs Outdated Show resolved Hide resolved
@mjsterckx
Copy link
Contributor Author

Since the new function is only called with values that are previously checked with regex, running the function with a value that doesn't conform to the regex is undefined behavior. Should I "define" this behavior and add some unit tests for those cases, or only add tests for values that conform to the regex?

@webern
Copy link
Contributor

webern commented Oct 14, 2022

Since the new function is only called with values that are previously checked with regex, running the function with a value that doesn't conform to the regex is undefined behavior. Should I "define" this behavior and add some unit tests for those cases, or only add tests for values that conform to the regex?

It's not super important in this case, so not worth losing a bunch of time on, but...

It probably wouldn't hurt to use ? instead of unwrap_or_else([default]). If you use anyhow::Error (via crate::Error) instead of ParseIntError then I think it's pretty easy to define the behavior of this function such that it errors on unexpected input. And it's always good to have unit tests that cover these "bad input" cases.

controller/Cargo.toml Show resolved Hide resolved
controller/src/constants.rs Outdated Show resolved Hide resolved
@@ -15,3 +17,20 @@ pub(crate) fn requeue_slow() -> Action {
pub(crate) fn no_requeue() -> Action {
Action::await_change()
}

/// Parse a duration string into a Duration object.
pub(crate) fn parse_duration(input: &str) -> Result<Duration, ParseIntError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should return an error type from this crate.

controller/src/constants.rs Outdated Show resolved Hide resolved
@mjsterckx
Copy link
Contributor Author

mjsterckx commented Oct 17, 2022

^ Added unit tests and removed parse_duration and its dependencies from Cargo.lock

@mjsterckx mjsterckx marked this pull request as ready for review October 17, 2022 19:08
Copy link
Contributor

@webern webern left a comment

Choose a reason for hiding this comment

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

I don't quite follow the parsing logic, but the tests convince me that it works.

@mjsterckx mjsterckx requested a review from etungsten October 17, 2022 21:17
Copy link
Contributor

@stmcginnis stmcginnis left a comment

Choose a reason for hiding this comment

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

Looks like unit tests cover each case, including invalid cases. Might be nice to have the error message contain something more detailed as to why the parsing failed, but hopefully that should be pretty obvious to future us.

Copy link
Contributor

@etungsten etungsten left a comment

Choose a reason for hiding this comment

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

Not sure if the parsing function belongs in the constants module. Maybe a new utils.rs is more appropriate.

@mjsterckx mjsterckx merged commit ce4fe17 into bottlerocket-os:develop Oct 24, 2022
@mjsterckx mjsterckx deleted the parse_duration branch October 24, 2022 20:58
@etungsten etungsten mentioned this pull request Nov 1, 2022
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.

Replace parse_duration package
5 participants