# HG changeset patch # User Andrew Azores # Date 1444398712 14400 # Node ID b1ec95a6f5b60f59cdc2c12b4e04d44fc40889da # Parent 1240d8f621f360ec93d39a86a1fbe0dd598c6e88 Disable "Perform GC" button in GC tab when selected VM is not running Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-October/016647.html PR2668 diff -r 1240d8f621f3 -r b1ec95a6f5b6 vm-gc/client-core/src/main/java/com/redhat/thermostat/vm/gc/client/core/internal/Activator.java --- a/vm-gc/client-core/src/main/java/com/redhat/thermostat/vm/gc/client/core/internal/Activator.java Thu Oct 08 10:54:59 2015 -0400 +++ b/vm-gc/client-core/src/main/java/com/redhat/thermostat/vm/gc/client/core/internal/Activator.java Fri Oct 09 09:51:52 2015 -0400 @@ -43,6 +43,7 @@ import com.redhat.thermostat.gc.remote.common.GCRequest; import com.redhat.thermostat.storage.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.dao.VmInfoDAO; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; @@ -68,6 +69,7 @@ Class[] deps = new Class[] { VmMemoryStatDAO.class, VmGcStatDAO.class, + VmInfoDAO.class, AgentInfoDAO.class, GCRequest.class, ApplicationService.class, @@ -82,6 +84,8 @@ Objects.requireNonNull(vmMemoryStatDAO); VmGcStatDAO vmGcStatDAO = (VmGcStatDAO) services.get(VmGcStatDAO.class.getName()); Objects.requireNonNull(vmGcStatDAO); + VmInfoDAO vmInfoDAO = (VmInfoDAO) services.get(VmInfoDAO.class.getName()); + Objects.requireNonNull(vmInfoDAO); AgentInfoDAO agentInfoDAO = (AgentInfoDAO) services.get(AgentInfoDAO.class.getName()); Objects.requireNonNull(agentInfoDAO); GCRequest gcRequest = (GCRequest) services.get(GCRequest.class.getName()); @@ -91,7 +95,7 @@ VmGcViewProvider viewProvider = (VmGcViewProvider) services.get(VmGcViewProvider.class.getName()); Objects.requireNonNull(viewProvider); - VmGcService service = new VmGcServiceImpl(appSvc, vmMemoryStatDAO, vmGcStatDAO, agentInfoDAO, viewProvider, gcRequest); + VmGcService service = new VmGcServiceImpl(appSvc, vmMemoryStatDAO, vmGcStatDAO, vmInfoDAO, agentInfoDAO, viewProvider, gcRequest); Dictionary properties = new Hashtable<>(); properties.put(Constants.GENERIC_SERVICE_CLASSNAME, VmRef.class.getName()); properties.put(InformationService.KEY_SERVICE_ID, VmGcService.SERVICE_ID); diff -r 1240d8f621f3 -r b1ec95a6f5b6 vm-gc/client-core/src/main/java/com/redhat/thermostat/vm/gc/client/core/internal/VmGcController.java --- a/vm-gc/client-core/src/main/java/com/redhat/thermostat/vm/gc/client/core/internal/VmGcController.java Thu Oct 08 10:54:59 2015 -0400 +++ b/vm-gc/client-core/src/main/java/com/redhat/thermostat/vm/gc/client/core/internal/VmGcController.java Fri Oct 09 09:51:52 2015 -0400 @@ -65,6 +65,7 @@ import com.redhat.thermostat.shared.locale.Translate; import com.redhat.thermostat.storage.core.VmRef; import com.redhat.thermostat.storage.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.storage.model.IntervalTimeData; import com.redhat.thermostat.storage.model.TimeStampedPojoComparator; import com.redhat.thermostat.vm.gc.client.core.VmGcView; @@ -89,6 +90,7 @@ private final VmGcStatDAO gcDao; private final VmMemoryStatDAO memDao; private final AgentInfoDAO agentDAO; + private final VmInfoDAO infoDAO; private final Set addedCollectors = new TreeSet<>(); // the last value seen for each collector @@ -98,7 +100,7 @@ private long lastSeenTimeStamp; - public VmGcController(ApplicationService appSvc, VmMemoryStatDAO vmMemoryStatDao, VmGcStatDAO vmGcStatDao, AgentInfoDAO agentInfoDAO, VmRef ref, VmGcViewProvider provider, final GCRequest gcRequest) { + public VmGcController(ApplicationService appSvc, VmMemoryStatDAO vmMemoryStatDao, VmGcStatDAO vmGcStatDao, VmInfoDAO vmInfoDAO, AgentInfoDAO agentInfoDAO, final VmRef ref, VmGcViewProvider provider, final GCRequest gcRequest) { this.ref = ref; this.view = provider.createView(); this.timer = appSvc.getTimerFactory().createTimer(); @@ -106,6 +108,7 @@ gcDao = vmGcStatDao; memDao = vmMemoryStatDao; agentDAO = agentInfoDAO; + infoDAO = vmInfoDAO; view.addActionListener(new ActionListener() { @Override @@ -115,6 +118,7 @@ stop(); break; case VISIBLE: + view.setEnableGCAction(infoDAO.getVmInfo(ref).isAlive()); start(); break; default: @@ -176,6 +180,8 @@ timer.setInitialDelay(0); timer.setDelay(5); timer.setTimeUnit(TimeUnit.SECONDS); + + view.setEnableGCAction(infoDAO.getVmInfo(ref).isAlive()); } private void start() { diff -r 1240d8f621f3 -r b1ec95a6f5b6 vm-gc/client-core/src/main/java/com/redhat/thermostat/vm/gc/client/core/internal/VmGcServiceImpl.java --- a/vm-gc/client-core/src/main/java/com/redhat/thermostat/vm/gc/client/core/internal/VmGcServiceImpl.java Thu Oct 08 10:54:59 2015 -0400 +++ b/vm-gc/client-core/src/main/java/com/redhat/thermostat/vm/gc/client/core/internal/VmGcServiceImpl.java Fri Oct 09 09:51:52 2015 -0400 @@ -43,6 +43,8 @@ import com.redhat.thermostat.gc.remote.common.GCRequest; import com.redhat.thermostat.storage.core.VmRef; import com.redhat.thermostat.storage.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.dao.VmInfoDAO; +import com.redhat.thermostat.storage.model.VmInfo; import com.redhat.thermostat.vm.gc.client.core.VmGcService; import com.redhat.thermostat.vm.gc.client.core.VmGcViewProvider; import com.redhat.thermostat.vm.gc.common.VmGcStatDAO; @@ -52,20 +54,22 @@ private static final int ORDER = ORDER_MEMORY_GROUP; private static final Filter FILTER = new NameMatchingRefFilter<>(); + private ApplicationService appSvc; - private ApplicationService appSvc; private VmMemoryStatDAO vmMemoryStatDAO; private VmGcStatDAO vmGcStatDAO; + private VmInfoDAO vmInfoDAO; private AgentInfoDAO agentInfoDAO; private VmGcViewProvider viewProvider; private GCRequest gcRequest; public VmGcServiceImpl(ApplicationService appSvc, - VmMemoryStatDAO vmMemoryStatDAO, VmGcStatDAO vmGcStatDAO, AgentInfoDAO agentInfoDAO, + VmMemoryStatDAO vmMemoryStatDAO, VmGcStatDAO vmGcStatDAO, VmInfoDAO vmInfoDAO, AgentInfoDAO agentInfoDAO, VmGcViewProvider viewProvider, GCRequest gcRequest) { this.appSvc = appSvc; this.vmMemoryStatDAO = vmMemoryStatDAO; this.vmGcStatDAO = vmGcStatDAO; + this.vmInfoDAO = vmInfoDAO; this.agentInfoDAO = agentInfoDAO; this.viewProvider = viewProvider; this.gcRequest = gcRequest; @@ -74,7 +78,7 @@ @Override public InformationServiceController getInformationServiceController( VmRef ref) { - return new VmGcController(appSvc, vmMemoryStatDAO, vmGcStatDAO, agentInfoDAO, ref, viewProvider, gcRequest); + return new VmGcController(appSvc, vmMemoryStatDAO, vmGcStatDAO, vmInfoDAO, agentInfoDAO, ref, viewProvider, gcRequest); } @Override diff -r 1240d8f621f3 -r b1ec95a6f5b6 vm-gc/client-core/src/test/java/com/redhat/thermostat/vm/gc/client/core/internal/ActivatorTest.java --- a/vm-gc/client-core/src/test/java/com/redhat/thermostat/vm/gc/client/core/internal/ActivatorTest.java Thu Oct 08 10:54:59 2015 -0400 +++ b/vm-gc/client-core/src/test/java/com/redhat/thermostat/vm/gc/client/core/internal/ActivatorTest.java Fri Oct 09 09:51:52 2015 -0400 @@ -43,6 +43,7 @@ import com.redhat.thermostat.gc.remote.common.GCRequest; import com.redhat.thermostat.storage.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.dao.VmInfoDAO; import org.junit.Test; import com.redhat.thermostat.client.core.InformationService; @@ -75,6 +76,7 @@ StubBundleContext context = new StubBundleContext(); VmMemoryStatDAO vmMemoryStatDAO = mock(VmMemoryStatDAO.class); VmGcStatDAO vmGcStatDAO = mock(VmGcStatDAO.class); + VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); GCRequest gcRequest = mock(GCRequest.class); ApplicationService appSvc = mock(ApplicationService.class); @@ -82,6 +84,7 @@ context.registerService(VmMemoryStatDAO.class, vmMemoryStatDAO, null); context.registerService(VmGcStatDAO.class, vmGcStatDAO, null); + context.registerService(VmInfoDAO.class, vmInfoDAO, null); context.registerService(AgentInfoDAO.class, agentInfoDAO, null); context.registerService(GCRequest.class, gcRequest, null); context.registerService(ApplicationService.class, appSvc, null); @@ -96,7 +99,7 @@ activator.stop(context); assertEquals(0, context.getServiceListeners().size()); - assertEquals(6, context.getAllServices().size()); + assertEquals(7, context.getAllServices().size()); } } diff -r 1240d8f621f3 -r b1ec95a6f5b6 vm-gc/client-core/src/test/java/com/redhat/thermostat/vm/gc/client/core/internal/VmGcControllerTest.java --- a/vm-gc/client-core/src/test/java/com/redhat/thermostat/vm/gc/client/core/internal/VmGcControllerTest.java Thu Oct 08 10:54:59 2015 -0400 +++ b/vm-gc/client-core/src/test/java/com/redhat/thermostat/vm/gc/client/core/internal/VmGcControllerTest.java Fri Oct 09 09:51:52 2015 -0400 @@ -39,6 +39,7 @@ import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isA; import static org.mockito.Matchers.isNotNull; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -53,7 +54,9 @@ import com.redhat.thermostat.gc.remote.common.GCRequest; import com.redhat.thermostat.storage.core.HostRef; import com.redhat.thermostat.storage.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.storage.model.AgentInformation; +import com.redhat.thermostat.storage.model.VmInfo; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -81,6 +84,7 @@ private Runnable timerAction; private VmGcView view; private ActionListener viewListener; + private VmInfoDAO vmInfoDAO; @SuppressWarnings({ "rawtypes", "unchecked" }) @Before @@ -109,12 +113,16 @@ gen.setCollector("collector1"); VmMemoryStat memoryStat = new VmMemoryStat("foo-agent", 1, "vmId", new Generation[] { gen }); + VmInfo vmInfo = new VmInfo("foo", "vm1", 1, 0, -1, "1.8.0_45", "", "", "", "", "", "", "", null, null, null, -1, null); + AgentInformation agentInfo = new AgentInformation("foo"); // Setup DAO VmGcStatDAO vmGcStatDAO = mock(VmGcStatDAO.class); when(vmGcStatDAO.getLatestVmGcStats(isA(VmRef.class), isA(Long.class))).thenReturn(stats); VmMemoryStatDAO vmMemoryStatDAO = mock(VmMemoryStatDAO.class); + vmInfoDAO = mock(VmInfoDAO.class); + when(vmInfoDAO.getVmInfo(isA(VmRef.class))).thenReturn(vmInfo); when(vmMemoryStatDAO.getNewestMemoryStat(isA(VmRef.class))).thenReturn(memoryStat); AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); when(agentInfoDAO.getAgentInformation(isA(HostRef.class))).thenReturn(agentInfo); @@ -141,7 +149,7 @@ // Now start the controller VmRef ref = mock(VmRef.class); - new VmGcController(appSvc, vmMemoryStatDAO, vmGcStatDAO, agentInfoDAO, ref, viewProvider, gcRequest); + new VmGcController(appSvc, vmMemoryStatDAO, vmGcStatDAO, vmInfoDAO, agentInfoDAO, ref, viewProvider, gcRequest); // Extract relevant objects viewListener = viewArgumentCaptor.getValue(); @@ -170,6 +178,22 @@ verify(timer).stop(); } + @Test + public void verifyGcEnabled() { + viewListener.actionPerformed(new ActionEvent<>(view, VmGcView.Action.VISIBLE)); + verify(view, atLeastOnce()).setEnableGCAction(true); + } + + @Test + public void verifyGcDisabledWhenVmDead() { + VmInfo stoppedVmInfo = mock(VmInfo.class); + when(stoppedVmInfo.isAlive()).thenReturn(false); + when(vmInfoDAO.getVmInfo(isA(VmRef.class))).thenReturn(stoppedVmInfo); + + viewListener.actionPerformed(new ActionEvent<>(view, VmGcView.Action.VISIBLE)); + verify(view, atLeastOnce()).setEnableGCAction(false); + } + @SuppressWarnings("unchecked") @Test public void verifyAction() { diff -r 1240d8f621f3 -r b1ec95a6f5b6 vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsController.java --- a/vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsController.java Thu Oct 08 10:54:59 2015 -0400 +++ b/vm-memory/client-core/src/main/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsController.java Fri Oct 09 09:51:52 2015 -0400 @@ -235,6 +235,7 @@ break; case VISIBLE: + view.setEnableGCAction(vmInfoDao.getVmInfo(ref).isAlive()); start(); break; @@ -284,9 +285,7 @@ } }); - if (!vmInfoDao.getVmInfo(ref).isAlive()) { - view.setEnableGCAction(false); - } + view.setEnableGCAction(vmInfoDao.getVmInfo(ref).isAlive()); timeRangeController = new TimeRangeController<>(); } diff -r 1240d8f621f3 -r b1ec95a6f5b6 vm-memory/client-core/src/test/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsControllerTest.java --- a/vm-memory/client-core/src/test/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsControllerTest.java Thu Oct 08 10:54:59 2015 -0400 +++ b/vm-memory/client-core/src/test/java/com/redhat/thermostat/vm/memory/client/core/internal/MemoryStatsControllerTest.java Fri Oct 09 09:51:52 2015 -0400 @@ -42,6 +42,7 @@ import static org.mockito.Matchers.anyLong; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isA; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -300,6 +301,21 @@ assertTimeStampIsAround(DATA_TIMESTAMP, end2); } + @Test + public void verifyGcEnabled() { + viewListener.actionPerformed(new ActionEvent<>(view, MemoryStatsView.Action.VISIBLE)); + verify(view, atLeastOnce()).setEnableGCAction(true); + } + + @Test + public void verifyGcDisabledWhenVmDead() { + VmInfo vmInfo = mock(VmInfo.class); + when(vmInfo.isAlive()).thenReturn(false); + when(infoDao.getVmInfo(any(VmRef.class))).thenReturn(vmInfo); + + viewListener.actionPerformed(new ActionEvent<>(view, MemoryStatsView.Action.VISIBLE)); + verify(view, atLeastOnce()).setEnableGCAction(false); + } private void assertTimeStampIsAround(long expected, long actual) { assertTrue(actual <= expected + 500);