From a406a46fa10536a955494abc5d331573bb88f28e Mon Sep 17 00:00:00 2001 From: marckchr Date: Mon, 5 Jul 2021 14:57:19 +0200 Subject: [PATCH 1/2] Fix duration to microseconds conversion See gh-27149 --- .../java/org/springframework/boot/convert/DurationStyle.java | 2 +- .../org/springframework/boot/convert/DurationStyleTests.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationStyle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationStyle.java index 36fd244ecf..c606e45304 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationStyle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationStyle.java @@ -183,7 +183,7 @@ public enum DurationStyle { /** * Microseconds. */ - MICROS(ChronoUnit.MICROS, "us", (duration) -> duration.toMillis() * 1000L), + MICROS(ChronoUnit.MICROS, "us", duration -> duration.toNanos() / 1000L), /** * Milliseconds. diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/DurationStyleTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/DurationStyleTests.java index 0e160b2a83..9e009fd748 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/DurationStyleTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/DurationStyleTests.java @@ -234,6 +234,7 @@ class DurationStyleTests { void printSimpleWithUnitShouldPrintInUnit() { Duration duration = Duration.ofMillis(1000); assertThat(DurationStyle.SIMPLE.print(duration, ChronoUnit.SECONDS)).isEqualTo("1s"); + assertThat(DurationStyle.SIMPLE.print(Duration.ofNanos(2000), ChronoUnit.MICROS)).isEqualTo("2us"); } } From 5ec0c7ed12686d0e1794b6a9e8c40f8ff955230f Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 6 Jul 2021 09:07:36 +0200 Subject: [PATCH 2/2] Polish "Fix duration to microseconds conversion" See gh-27149 --- .../springframework/boot/convert/DurationStyle.java | 4 ++-- .../boot/convert/DurationStyleTests.java | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationStyle.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationStyle.java index c606e45304..70992bc17e 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationStyle.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/convert/DurationStyle.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -183,7 +183,7 @@ public enum DurationStyle { /** * Microseconds. */ - MICROS(ChronoUnit.MICROS, "us", duration -> duration.toNanos() / 1000L), + MICROS(ChronoUnit.MICROS, "us", (duration) -> duration.toNanos() / 1000L), /** * Milliseconds. diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/DurationStyleTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/DurationStyleTests.java index 9e009fd748..9dc23b1c14 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/DurationStyleTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/convert/DurationStyleTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -231,10 +231,15 @@ class DurationStyleTests { } @Test - void printSimpleWithUnitShouldPrintInUnit() { + void printSimpleWithSecondsUnitShouldPrintInUnit() { Duration duration = Duration.ofMillis(1000); assertThat(DurationStyle.SIMPLE.print(duration, ChronoUnit.SECONDS)).isEqualTo("1s"); - assertThat(DurationStyle.SIMPLE.print(Duration.ofNanos(2000), ChronoUnit.MICROS)).isEqualTo("2us"); + } + + @Test + void printSimpleWithMicrosUnitShouldPrintInUnit() { + Duration duration = Duration.ofNanos(2000); + assertThat(DurationStyle.SIMPLE.print(duration, ChronoUnit.MICROS)).isEqualTo("2us"); } }