services/ticker: fix flakey spec comparing time with postgres stored time #1774
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.
PR Checklist
PR Structure
otherwise).
services/friendbot
, orall
ordoc
if the changes are broad or impact manypackages.
Thoroughness
This PR adds tests for the most critical parts of the new functionality or fixes.I've updated any docs (developer docs,.md
files, etc... affected by this change). Take a look in the
docs
folder for a given service,like this one.
Release planning
I've updated the relevant CHANGELOG (here for Horizon) ifneeded with deprecations, added features, breaking changes, and DB schema changes.
semver, or if it's mainly a patch change. The PR is targeted at the next
release branch if it's not a patch change.
Summary
Change the
TestInsertOrUpdateAsset
test toRound
instead ofTruncate
time values that are being compared with time that was stored in Postgres.Goal and scope
Intermittently the
TestInsertOrUpdateAsset
test will fail because a time value that's been stored in Postgres doesn't match the time value we gave to Postgres to store. We attempt to do our best to make them match by using theTruncate
value on both the input and output values, but that doesn't work for some cases.I managed to reproduce the failure 100% of the time by using a specific input time of
12:08:50.9419997
where themilliseconds
component will be rounded up because of the values in thenanosecond
component by Postgres when it reduces the value tomicroseconds
. To see the exact failure case, take a look at the first commit where I hardcoded it for consistent failures. This isn't the only failure case though. When I ran the tests thousands of times I had plenty of times where it failed. Because we use theTruncate
function, we are comparing a rounded down value with a rounded up value in any cases where the millisecond component gets rounded up when the microsecond component is rounded up.To illustrate exactly what is happening here, this is our original time:
When Postgres is truncating it to microseconds, which is six decimal places, it is rounding to the nearest, which in this case is up:
In our test we use the truncate function to truncate to milliseconds:
Using
Round
instead causes us to round up to the same value.Close #1733
Summary of changes
Truncate
withRound
.Known limitations & issues
The fix in this change isn't perfect according to discussion at lib/pq#227 (comment), where a commenter states that Postgres' rounding function rounds to nearest and in the event of a tie rounds to even, which is different to Go's
Round
function which rounds to nearest and in the event of a tie rounds away from zero. This would cause a problem in this situation:However, in the tests I ran with time
12:08:50.9419995
and rounding to the nearestmicrosecond
, both Postgres and Go had rounded in the same directions, so I don't see evidence that this is an issue.Even if this was still an issue I think this change is the best simple change we can make to the test and it would significantly reduce the number of cases this test fails.
What shouldn't be reviewed
N/A