Mercurial > hg > release > thermostat-1.6
changeset 1940:6c8f7bce641f
Don't throw NPE when there is no host CPU data
Reviewed by: jkang
Review Thread: http://icedtea.classpath.org/pipermail/thermostat/2016-June/019747.html
PR3032
author | Joshua Matsuoka <jmatsuok@redhat.com> |
---|---|
date | Thu, 23 Jun 2016 12:04:07 -0400 |
parents | a09beb448a72 |
children | 1da17b854973 |
files | host-cpu/client-core/src/main/java/com/redhat/thermostat/host/cpu/client/core/internal/HostCpuController.java host-cpu/client-core/src/test/java/com/redhat/thermostat/host/cpu/client/core/internal/HostCpuControllerTest.java |
diffstat | 2 files changed, 29 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/host-cpu/client-core/src/main/java/com/redhat/thermostat/host/cpu/client/core/internal/HostCpuController.java Thu Jun 23 11:17:14 2016 -0400 +++ b/host-cpu/client-core/src/main/java/com/redhat/thermostat/host/cpu/client/core/internal/HostCpuController.java Thu Jun 23 12:04:07 2016 -0400 @@ -139,6 +139,10 @@ private void doCpuChartUpdate() { CpuStat oldest = cpuStatDAO.getOldest(ref); CpuStat newest = cpuStatDAO.getNewest(ref); + // Do nothing if there is no data + if (oldest == null || newest == null) { + return; + } final List<CpuStat> cpuStats = new ArrayList<>();
--- a/host-cpu/client-core/src/test/java/com/redhat/thermostat/host/cpu/client/core/internal/HostCpuControllerTest.java Thu Jun 23 11:17:14 2016 -0400 +++ b/host-cpu/client-core/src/test/java/com/redhat/thermostat/host/cpu/client/core/internal/HostCpuControllerTest.java Thu Jun 23 12:04:07 2016 -0400 @@ -52,7 +52,6 @@ import java.util.concurrent.TimeUnit; import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -86,8 +85,7 @@ private Runnable timerAction; @SuppressWarnings({ "rawtypes", "unchecked" }) - @Before - public void setUp() { + public void setUpWithCpuDAO(CpuStatDAO cpuStatDAO) { // Setup timer. timer = mock(Timer.class); ArgumentCaptor<Runnable> actionCaptor = ArgumentCaptor.forClass(Runnable.class); @@ -103,15 +101,6 @@ HostInfoDAO hostInfoDAO = mock(HostInfoDAO.class); when(hostInfoDAO.getHostInfo(any(HostRef.class))).thenReturn(hostInfo); - CpuStat cpuStat1 = new CpuStat("foo", 1l, new double[] {10.0, 20.0, 30.0}); - CpuStat cpuStat2 = new CpuStat("foo", 2l, new double[] {15.0, 25.0, 35.0}); - CpuStatDAO cpuStatDAO = mock(CpuStatDAO.class); - - when(cpuStatDAO.getOldest(any(HostRef.class))).thenReturn(cpuStat1); - when(cpuStatDAO.getNewest(any(HostRef.class))).thenReturn(cpuStat2); - - when(cpuStatDAO.getCpuStats(any(HostRef.class), anyLong(), anyLong())).thenReturn(Arrays.asList(cpuStat1, cpuStat2)); - // Set up View view = mock(HostCpuView.class); when(view.getUserDesiredDuration()).thenReturn(new Duration(10, TimeUnit.MINUTES)); @@ -138,6 +127,8 @@ @Test public void testTimer() { + setUpWithCpuDAO(mock(CpuStatDAO.class)); + viewListener.actionPerformed(new ActionEvent<>(view, HostCpuView.Action.VISIBLE)); verify(timer).setAction(isNotNull(Runnable.class)); @@ -155,6 +146,17 @@ @SuppressWarnings("unchecked") @Test public void testTimerAction() { + CpuStat cpuStat1 = new CpuStat("foo", 1l, new double[] {10.0, 20.0, 30.0}); + CpuStat cpuStat2 = new CpuStat("foo", 2l, new double[] {15.0, 25.0, 35.0}); + CpuStatDAO cpuStatDAO = mock(CpuStatDAO.class); + + when(cpuStatDAO.getOldest(any(HostRef.class))).thenReturn(cpuStat1); + when(cpuStatDAO.getNewest(any(HostRef.class))).thenReturn(cpuStat2); + + when(cpuStatDAO.getCpuStats(any(HostRef.class), anyLong(), anyLong())).thenReturn(Arrays.asList(cpuStat1, cpuStat2)); + + setUpWithCpuDAO(cpuStatDAO); + timerAction.run(); verify(view).setCpuModel("fluffCpu1"); verify(view).setCpuCount("12345"); @@ -168,5 +170,16 @@ assertEquals(2, cpuLoadData.get(1).getTimeInMillis()); assertEquals(15.0, cpuLoadData.get(1).getData().doubleValue(), 0.0001); } + + /** + * Verifies that chart update does not throw a NPE due to no CPU stats + * available. + */ + @Test + public void testViewUpdateNoCPUDatat() { + // will return null on getlattest/getOldest + setUpWithCpuDAO(mock(CpuStatDAO.class)); + timerAction.run(); + } }