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

Fix next_occurrence function #1893

Merged

Conversation

jemrobinson
Copy link
Member

@jemrobinson jemrobinson commented May 16, 2024

✅ Checklist

  • You have given your pull request a meaningful title (e.g. Enable foobar integration rather than 515 foobar).
  • You are targeting the appropriate branch. If you're not certain which one this is, it should be develop.
  • Your branch is up-to-date with the target branch (it probably was when you started, but it may have changed since then).

🚦 Depends on

n/a

⤴️ Summary

Ensure that next_occurrence only adds one day when necessary, to avoid it choosing a datetime more than 1 day in the future

🌂 Related issues

Closes #1895

🔬 Tests

Check CI

…oid it choosing a datetime more than 1 day in the future
@jemrobinson jemrobinson requested a review from a team as a code owner May 16, 2024 16:38
@jemrobinson jemrobinson requested a review from a team May 16, 2024 16:39
Copy link

github-actions bot commented May 16, 2024

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  data_safe_haven/functions
  strings.py
Project Total  

This report was generated by python-coverage-comment-action

Copy link
Member

@JimMadge JimMadge 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 like this.

I've lost track of what next_occurrence is trying to do and adding minutes in odd places feels like fudge factors.

utc_now = datetime.datetime.now(pytz.utc) + datetime.timedelta(minutes=1)

This isn't true, for example, that is now plus one minute.

If the problem is the test isn't true sometimes, it would be better to make the tests deterministic by not using datetime.now() but a set of known times instead.

@jemrobinson
Copy link
Member Author

next_occurrence is supposed to give the next time that a desired local time happens in UTC. For some Azure applications the time that you pass cannot be in the past.

Previously, we took local-time-in-UTC + 1 day which is always in the future (over the lifetime of a deploy operation). However, this can break the test which assumes that the output of next_occurrence will always be within one day of current-time-in-UTC.

Possible solutions are:

  1. relax the test so that next_occurrence must be within 2 days of now
  2. update the function so that it only adds 1 day if the suggested time is in the past
  • checking for in-the-past uses now-plus-1-minute to ensure that we don't end up with a time that is currently valid, but ends up being invalid by the time it gets given to an Azure function
  • possibly this should be longer (e.g. 5 minutes) and have a better explanation as to why

I'm happy to switch to solution (1) if you prefer.

@JimMadge
Copy link
Member

Hmm, some thoughts that might help,

  • Why does the test assume next_occurrence will be within one day?
    Does it have to be? Will Azure reject times not within one day of now?
    Could next_occurrence simply being in the future be a better test?
  • The fudge sounds more reasonable given how the Azure API works (and operations are not instantaneous). Adding a comment to explain it would be good.
  • Should this really be >1 function? next_occurrence and ensure_within_1d?

@jemrobinson
Copy link
Member Author

Why does the test assume next_occurrence will be within one day?

Just because that's the intention of the function (to generate the UTC timestamp corresponding to the next time it is e.g. 3am in Perth)

Does it have to be? Will Azure reject times not within one day of now?

No, it doesn't have to be, but some of these schedulers won't start until the time occurs, so it would bad bad if this was e.g. 10 days in the future.

Could next_occurrence simply being in the future be a better test?

Potentially, but I think there should be a maximum window on how far in the future it could be (see above)

The fudge sounds more reasonable given how the Azure API works (and operations are not instantaneous). Adding a comment to explain it would be good.

Happy to remove this fudge (esp. given your comments above)

Should this really be >1 function? next_occurrence and ensure_within_1d?

I think it only needs to be one function - the important thing is to come up with a UTC value that represents when your thing will happen in the future.

data_safe_haven/functions/strings.py Outdated Show resolved Hide resolved
tests/functions/test_strings.py Outdated Show resolved Hide resolved
@JimMadge JimMadge mentioned this pull request May 20, 2024
3 tasks
@jemrobinson
Copy link
Member Author

Looks like you can't mock builtin methods. I'll have a go with freezegun.

@jemrobinson jemrobinson requested a review from JimMadge May 21, 2024 15:31
Copy link
Member

@JimMadge JimMadge left a comment

Choose a reason for hiding this comment

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

Added some extra tests with @parametrize and a test for DST.

@JimMadge JimMadge merged commit 542a1ca into alan-turing-institute:develop May 22, 2024
11 checks passed
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.

Tests for next_occurrence break at certain times of day
2 participants