# HG changeset patch # User Severin Gehwolf # Date 1485798789 -3600 # Node ID 4823a5a908d7a026720e7f56178301d73ed26b0b # Parent acf70e9751fc3995934746eaa25026f83fc58a9b Make byteman metrics tab work for offline VMs. Reviewed-by: almac Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-February/022119.html diff -r acf70e9751fc -r 4823a5a908d7 vm-byteman/client-swing/src/main/java/com/redhat/thermostat/vm/byteman/client/swing/internal/VmBytemanInformationController.java --- a/vm-byteman/client-swing/src/main/java/com/redhat/thermostat/vm/byteman/client/swing/internal/VmBytemanInformationController.java Wed Feb 01 09:34:49 2017 -0500 +++ b/vm-byteman/client-swing/src/main/java/com/redhat/thermostat/vm/byteman/client/swing/internal/VmBytemanInformationController.java Mon Jan 30 18:53:09 2017 +0100 @@ -99,7 +99,7 @@ private final VmBytemanDAO bytemanDao; private final RequestQueue requestQueue; private Timer timer; - private List previousPayload = Collections.EMPTY_LIST; + private List previousPayload = Collections.emptyList(); private boolean comboBoxSelected = false; private boolean isPolling = false; @@ -310,7 +310,9 @@ void stopPolling() { isPolling = false; - timer.cancel(); + if (timer != null) { // Dead VMs might never have had the timer started + timer.cancel(); + } } void updateGraph() { diff -r acf70e9751fc -r 4823a5a908d7 vm-byteman/client-swing/src/test/java/com/redhat/thermostat/vm/byteman/client/swing/internal/VmBytemanInformationControllerTest.java --- a/vm-byteman/client-swing/src/test/java/com/redhat/thermostat/vm/byteman/client/swing/internal/VmBytemanInformationControllerTest.java Wed Feb 01 09:34:49 2017 -0500 +++ b/vm-byteman/client-swing/src/test/java/com/redhat/thermostat/vm/byteman/client/swing/internal/VmBytemanInformationControllerTest.java Mon Jan 30 18:53:09 2017 +0100 @@ -37,6 +37,7 @@ package com.redhat.thermostat.vm.byteman.client.swing.internal; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; import static org.mockito.Matchers.any; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doNothing; @@ -295,6 +296,22 @@ BytemanMetric m = metrics.get(0); assertEquals(VM_ID, m.getVmId()); } + + /** + * Verifies no exceptions are thrown when the VM is dead and + * updateMetrics() is called. + */ + @Test + public void testUpdateMetricsDeadVm() { + boolean isAliveVm = false; + VmBytemanInformationController controller = createController(isAliveVm); + try { + controller.updateMetrics(); + // pass + } catch (Exception e) { + fail("Must not throw exception when VM is dead: " + e); + } + } @Test public void testPollingState() { @@ -340,17 +357,25 @@ } private VmBytemanInformationController createController() { + return createController(true); + } + + private VmBytemanInformationController createController(boolean isVmAlive) { VmBytemanView view = mock(VmBytemanView.class); ref = mock(VmRef.class); when(ref.getVmId()).thenReturn(VM_ID); when(ref.getHostRef()).thenReturn(new HostRef(AGENT_ID, HOST_NAME)); AgentInfoDAO agentInfoDao = mock(AgentInfoDAO.class); AgentInformation agentInfo = mock(AgentInformation.class); - when(agentInfo.isAlive()).thenReturn(true); + when(agentInfo.isAlive()).thenReturn(isVmAlive); when(agentInfoDao.getAgentInformation(any(AgentId.class))).thenReturn(agentInfo); VmInfoDAO vmInfoDao = mock(VmInfoDAO.class); VmInfo vmInfo = mock(VmInfo.class); - when(vmInfo.isAlive(agentInfo)).thenReturn(AliveStatus.RUNNING); + if (isVmAlive) { + when(vmInfo.isAlive(agentInfo)).thenReturn(AliveStatus.RUNNING); + } else { + when(vmInfo.isAlive(agentInfo)).thenReturn(AliveStatus.EXITED); + } when(vmInfoDao.getVmInfo(any(VmRef.class))).thenReturn(vmInfo); vmBytemanDao = mock(VmBytemanDAO.class); requestQueue = mock(RequestQueue.class); @@ -361,6 +386,5 @@ // nothing, return immediately for tests } }; - } }