changeset 2579:4823a5a908d7

Make byteman metrics tab work for offline VMs. Reviewed-by: almac Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-February/022119.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Mon, 30 Jan 2017 18:53:09 +0100
parents acf70e9751fc
children 5f2695feaa06
files vm-byteman/client-swing/src/main/java/com/redhat/thermostat/vm/byteman/client/swing/internal/VmBytemanInformationController.java vm-byteman/client-swing/src/test/java/com/redhat/thermostat/vm/byteman/client/swing/internal/VmBytemanInformationControllerTest.java
diffstat 2 files changed, 31 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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<BytemanMetric> previousPayload = Collections.EMPTY_LIST;
+    private List<BytemanMetric> 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() {
--- 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
             }
         };
-        
     }
 }