Skip to content

Commit

Permalink
util-jackson: Don't include fractions of a second in test
Browse files Browse the repository at this point in the history
Problem

The LocalDateTimeSerdeTest#serialize as text test is highly sensitive
as it tries to match serialized/deserialized LocalDateTime instances
including fractions of a second.

Solution

Remove the fractions of a second from the comparison when testing to
reduce the chance for non-determinism.

JIRA Issues: CSL-11031

Differential Revision: https://phabricator.twitter.biz/D684685
  • Loading branch information
cacoco authored and jenkins committed Jun 9, 2021
1 parent 6f9e112 commit e04b0a0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
1 change: 0 additions & 1 deletion util-jackson/src/test/scala/com/twitter/util/jackson/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ junit_tests(
"caseclass/*.scala",
"serde/*.scala",
],
tags = ["known-to-fail-jira:CSL-11031"],
dependencies = [
"3rdparty/jvm/com/fasterxml/jackson/core:jackson-annotations",
"3rdparty/jvm/com/fasterxml/jackson/core:jackson-core",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ class LocalDateTimeSerdeTest extends AnyFunSuite with Matchers {
val mapper = new JacksonObjectMapper()
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
mapper.registerModule(new JavaTimeModule)
val actual = mapper.writeValueAsString(NowUtc)
val expected = quote(NowUtc.toString)
// occasionally Jackson does not include the last 0 when writing the nano seconds
expected.startsWith(actual) should be(true)
val actualStr = mapper.writeValueAsString(NowUtc)
val actualStrUnquoted =
actualStr.substring(1, actualStr.length - 1) // drop beginning and end quotes
val actual =
actualStrUnquoted.substring(
0,
actualStrUnquoted.lastIndexOf('.')
) // // drop fractions of second
val expectStr = NowUtc.toString
val expected = expectStr.substring(0, expectStr.lastIndexOf('.')) // drop fractions of second
expected should be(actual)
}

test("deserialize from text") {
Expand Down

0 comments on commit e04b0a0

Please sign in to comment.