From 02db9b681d5116954910bc5f0ba05938df2034f7 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 5 Mar 2019 23:12:35 +0100 Subject: [PATCH] fix MidnightTime crash + tests --- .../androidaps/utils/MidnightTime.java | 31 +++++----- .../androidaps/utils/MidnightTimeTest.java | 62 +++++++++++++++++++ 2 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java diff --git a/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java b/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java index d3d75feda3..668ca02e96 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java +++ b/app/src/main/java/info/nightscout/androidaps/utils/MidnightTime.java @@ -5,7 +5,7 @@ import android.util.LongSparseArray; import java.util.Calendar; public class MidnightTime { - private static LongSparseArray times = new LongSparseArray(); + private static final LongSparseArray times = new LongSparseArray<>(); private static long hits = 0; private static long misses = 0; @@ -20,20 +20,23 @@ public class MidnightTime { } public static long calc(long time) { - Long m = (Long) times.get(time); - if (m != null) { - ++hits; - return m; + Long m; + synchronized (times) { + m = times.get(time); + if (m != null) { + ++hits; + return m; + } + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(time); + c.set(Calendar.HOUR_OF_DAY, 0); + c.set(Calendar.MINUTE, 0); + c.set(Calendar.SECOND, 0); + c.set(Calendar.MILLISECOND, 0); + m = c.getTimeInMillis(); + times.append(time, m); + ++misses; } - Calendar c = Calendar.getInstance(); - c.setTimeInMillis(time); - c.set(Calendar.HOUR_OF_DAY, 0); - c.set(Calendar.MINUTE, 0); - c.set(Calendar.SECOND, 0); - c.set(Calendar.MILLISECOND, 0); - m = c.getTimeInMillis(); - times.append(time, m); - ++misses; return m; } diff --git a/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java b/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java new file mode 100644 index 0000000000..4770c658fc --- /dev/null +++ b/app/src/test/java/info/nightscout/androidaps/utils/MidnightTimeTest.java @@ -0,0 +1,62 @@ +package info.nightscout.androidaps.utils; + + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.util.Calendar; +import java.util.Date; + +import info.AAPSMocker; +import info.nightscout.androidaps.MainApp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by mike on 20.11.2017. + */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Calendar.class}) +public class MidnightTimeTest { + + @Test + public void calc() { + // We get real midnight + long now = DateUtil.now(); + Assert.assertTrue(now >= MidnightTime.calc()); + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(MidnightTime.calc()); + Assert.assertEquals(c.get(Calendar.HOUR_OF_DAY), 0); + Assert.assertEquals(c.get(Calendar.MINUTE), 0); + Assert.assertEquals(c.get(Calendar.SECOND), 0); + Assert.assertEquals(c.get(Calendar.MILLISECOND), 0); + } + + @Test + public void calc_time() { + // We get real midnight + long now = DateUtil.now(); + long midnight = MidnightTime.calc(now); + Assert.assertTrue(now >= midnight); + Calendar c = Calendar.getInstance(); + c.setTimeInMillis(MidnightTime.calc(now)); + Assert.assertEquals(c.get(Calendar.HOUR_OF_DAY), 0); + Assert.assertEquals(c.get(Calendar.MINUTE), 0); + Assert.assertEquals(c.get(Calendar.SECOND), 0); + Assert.assertEquals(c.get(Calendar.MILLISECOND), 0); + // Assure we get the same time from cache + Assert.assertEquals(midnight, MidnightTime.calc(now)); + } + + @Test + public void log() { + long now = DateUtil.now(); + MidnightTime.calc(now); + Assert.assertEquals(MidnightTime.log(), "Hits: 0 misses: 1 stored: 0"); + } +}