-
Notifications
You must be signed in to change notification settings - Fork 64
Improved datetime comparison logic #170
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f209923
improve datetime comparison logic
davidmrdavid 4a312d2
fixed linting error
davidmrdavid a9d235e
added test to prevent regressions, cant validate schema
davidmrdavid f8a7bfe
Merge branch 'dev' into dajusto/timer-datetime-comparisons
davidmrdavid c312dd6
Merge branch 'dev' into dajusto/timer-datetime-comparisons
davidmrdavid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from tests.test_utils.ContextBuilder import ContextBuilder | ||
from .orchestrator_test_utils \ | ||
import get_orchestration_state_result, assert_orchestration_state_equals, assert_valid_schema | ||
from azure.durable_functions.models.actions.CreateTimerAction import CreateTimerAction | ||
from azure.durable_functions.models.OrchestratorState import OrchestratorState | ||
from azure.durable_functions.constants import DATETIME_STRING_FORMAT | ||
from datetime import datetime, timedelta, timezone | ||
|
||
|
||
def base_expected_state(output=None) -> OrchestratorState: | ||
return OrchestratorState(is_done=False, actions=[], output=output) | ||
|
||
def add_timer_fired_events(context_builder: ContextBuilder, id_: int, timestamp: str): | ||
fire_at: str = context_builder.add_timer_created_event(id_, timestamp) | ||
context_builder.add_orchestrator_completed_event() | ||
context_builder.add_orchestrator_started_event() | ||
context_builder.add_timer_fired_event(id_=id_, fire_at=fire_at) | ||
|
||
def generator_function(context): | ||
|
||
# Create a timezone aware datetime object, just like a normal | ||
# call to `context.current_utc_datetime` would create | ||
timestamp = "2020-07-23T21:56:54.936700Z" | ||
fire_at = datetime.strptime(timestamp, DATETIME_STRING_FORMAT) | ||
fire_at = fire_at.replace(tzinfo=timezone.utc) | ||
|
||
yield context.create_timer(fire_at) | ||
return "Done!" | ||
|
||
def add_timer_action(state: OrchestratorState, fire_at: datetime): | ||
action = CreateTimerAction(fire_at= fire_at) | ||
state._actions.append([action]) # Todo: brackets? | ||
|
||
def test_timers_comparison_with_relaxed_precision(): | ||
"""Test if that two `datetime` different but equivalent | ||
serializations of timer deadlines are found to be equivalent. | ||
|
||
The Durable Extension may sometimes drop redundant zeroes on | ||
a datetime object. For instance, the date | ||
2020-07-23T21:56:54.936700Z | ||
may get transformed into | ||
2020-07-23T21:56:54.9367Z | ||
This test ensures that dropping redundant zeroes does not affect | ||
our ability to recognize that a timer has been fired. | ||
""" | ||
|
||
# equivalent to 2020-07-23T21:56:54.936700Z | ||
relaxed_timestamp = "2020-07-23T21:56:54.9367Z" | ||
fire_at = datetime.strptime(relaxed_timestamp, DATETIME_STRING_FORMAT) | ||
|
||
context_builder = ContextBuilder("relaxed precision") | ||
add_timer_fired_events(context_builder, 0, relaxed_timestamp) | ||
|
||
result = get_orchestration_state_result( | ||
context_builder, generator_function) | ||
|
||
expected_state = base_expected_state(output='Done!') | ||
add_timer_action(expected_state, fire_at) | ||
|
||
expected_state._is_done = True | ||
expected = expected_state.to_json() | ||
|
||
#assert_valid_schema(result) | ||
# TODO: getting the following error when validating the schema | ||
# "Additional properties are not allowed ('fireAt', 'isCanceled' were unexpected)"> | ||
assert_orchestration_state_equals(expected, result) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So this is the new datetime format we are using. Is there anyway for us to remove the reference to the old datetime format so it isn't accidentally used?
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.
This is actually the same datetime format as before, I just needed to explicitly import it now, because I needed to use its
datettime.strptime
parser.I'll write some more thoughts on the "two datetime variants" in a comment below.