changeset 100:abed8154068a

Test categories in SystemBackend and friends reviewed-by: rkennke review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-March/000196.html
author Jon VanAlten <jon.vanalten@redhat.com>
date Fri, 02 Mar 2012 10:46:18 -0500
parents bedc94997add
children 3eb60cfe29b3 4acc8348d4fe
files agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatHostListener.java agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatVmListener.java agent/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java agent/src/test/java/com/redhat/thermostat/backend/system/JvmStatHostListenerTest.java agent/src/test/java/com/redhat/thermostat/backend/system/JvmStatVmListenerTest.java agent/src/test/java/com/redhat/thermostat/backend/system/SystemBackendTest.java
diffstat 6 files changed, 251 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatHostListener.java	Fri Mar 02 15:15:59 2012 +0100
+++ b/agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatHostListener.java	Fri Mar 02 10:46:18 2012 -0500
@@ -82,7 +82,7 @@
     private static final Key<Long> vmInfoStartTimeKey = new Key<>("start-time", false);
     private static final Key<Long> vmInfoStopTimeKey = new Key<>("stop-time", false);
 
-    private static final Category vmInfoCategory = new Category("vm-info",
+    static final Category vmInfoCategory = new Category("vm-info",
             vmInfoIdKey, vmInfoPidKey, vmInfoRuntimeVersionKey, vmInfoJavaHomeKey,
             vmInfoMainClassKey, vmInfoCommandLineKey,
             vmInfoVmArgumentsKey, vmInfoVmNameKey, vmInfoVmInfoKey, vmInfoVmVersionKey,
--- a/agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatVmListener.java	Fri Mar 02 15:15:59 2012 +0100
+++ b/agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatVmListener.java	Fri Mar 02 10:46:18 2012 -0500
@@ -67,7 +67,7 @@
     /** time in microseconds */
     private static final Key<Long> vmGCstatWallTimeKey = new Key<>("wall-time", false);
 
-    private static final Category vmGcStatsCategory = new Category("vm-gc-stats",
+    static final Category vmGcStatsCategory = new Category("vm-gc-stats",
             vmGcStatVmIdKey, Key.TIMESTAMP, vmGcStatCollectorKey,
             vmGcStatRunCountKey, vmGCstatWallTimeKey);
 
@@ -102,7 +102,7 @@
     private static final Key<Long> vmMemoryStatPermMaxCapacityKey = new Key<>("perm.max-capacity", false);
     private static final Key<Long> vmMemoryStatPermUsedKey = new Key<>("perm.used", false);
 
-    private static final Category vmMemoryStatsCategory = new Category("vm-memory-stats",
+    static final Category vmMemoryStatsCategory = new Category("vm-memory-stats",
             vmMemoryStatVmIdKey, Key.TIMESTAMP,
             vmMemoryStatEdenGenKey, vmMemoryStatEdenCollectorKey,
             vmMemoryStatEdenCapacityKey, vmMemoryStatEdenMaxCapacityKey,vmMemoryStatEdenUsedKey,
--- a/agent/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java	Fri Mar 02 15:15:59 2012 +0100
+++ b/agent/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java	Fri Mar 02 10:46:18 2012 -0500
@@ -85,52 +85,52 @@
 
     private Set<Integer> pidsToMonitor = new CopyOnWriteArraySet<Integer>();
 
-    private List<Category> categories = new ArrayList<Category>();
+    private static List<Category> categories = new ArrayList<Category>();
 
-    private Key<String> hostNameKey = new Key<>("hostname", true);
-    private Key<String> osNameKey = new Key<>("os_name", false);
-    private Key<String> osKernelKey = new Key<>("os_kernel", false);
-    private Key<Integer> cpuCountKey = new Key<>("cpu_num", false);
-    private Key<String> cpuModelKey = new Key<>("cpu_model", false);
-    private Key<Long> hostMemoryTotalKey = new Key<>("memory_total", false);
+    private static Key<String> hostNameKey = new Key<>("hostname", true);
+    private static Key<String> osNameKey = new Key<>("os_name", false);
+    private static Key<String> osKernelKey = new Key<>("os_kernel", false);
+    private static Key<Integer> cpuCountKey = new Key<>("cpu_num", false);
+    private static Key<String> cpuModelKey = new Key<>("cpu_model", false);
+    private static Key<Long> hostMemoryTotalKey = new Key<>("memory_total", false);
 
-    private Category hostInfoCategory = new Category("host-info",
+    static Category hostInfoCategory = new Category("host-info",
             hostNameKey, osNameKey, osKernelKey,
             cpuCountKey, cpuModelKey, hostMemoryTotalKey);
 
-    private Key<String> ifaceKey = new Key<>("iface", true);
-    private Key<String> ip4AddrKey = new Key<>("ipv4addr", false);
-    private Key<String> ip6AddrKey = new Key<>("ipv6addr", false);
+    private static Key<String> ifaceKey = new Key<>("iface", true);
+    private static Key<String> ip4AddrKey = new Key<>("ipv4addr", false);
+    private static Key<String> ip6AddrKey = new Key<>("ipv6addr", false);
 
-    private Category networkInfoCategory = new Category("network-info",
+    static Category networkInfoCategory = new Category("network-info",
             Key.TIMESTAMP, ifaceKey, ip4AddrKey, ip6AddrKey);
 
-    private Key<Double> cpu5LoadKey = new Key<>("5load", false);
-    private Key<Double> cpu10LoadKey = new Key<>("10load", false);
-    private Key<Double> cpu15LoadKey = new Key<>("15load", false);
+    private static Key<Double> cpu5LoadKey = new Key<>("5load", false);
+    private static Key<Double> cpu10LoadKey = new Key<>("10load", false);
+    private static Key<Double> cpu15LoadKey = new Key<>("15load", false);
 
-    private Category cpuStatCategory = new Category("cpu-stats",
+    static Category cpuStatCategory = new Category("cpu-stats",
             Key.TIMESTAMP, cpu5LoadKey, cpu10LoadKey, cpu15LoadKey);
 
-    private Key<Long> memoryTotalKey = new Key<>("total", false);
-    private Key<Long> memoryFreeKey = new Key<>("free", false);
-    private Key<Long> memoryBuffersKey = new Key<>("buffers", false);
-    private Key<Long> memoryCachedKey = new Key<>("cached", false);
-    private Key<Long> memorySwapTotalKey = new Key<>("swap-total", false);
-    private Key<Long> memorySwapFreeKey = new Key<>("swap-free", false);
-    private Key<Long> memoryCommitLimitKey = new Key<>("commit-limit", false);
+    private static Key<Long> memoryTotalKey = new Key<>("total", false);
+    private static Key<Long> memoryFreeKey = new Key<>("free", false);
+    private static Key<Long> memoryBuffersKey = new Key<>("buffers", false);
+    private static Key<Long> memoryCachedKey = new Key<>("cached", false);
+    private static Key<Long> memorySwapTotalKey = new Key<>("swap-total", false);
+    private static Key<Long> memorySwapFreeKey = new Key<>("swap-free", false);
+    private static Key<Long> memoryCommitLimitKey = new Key<>("commit-limit", false);
 
-    private Category memoryStatCategory = new Category("memory-stats",
+    static Category memoryStatCategory = new Category("memory-stats",
             Key.TIMESTAMP, memoryTotalKey, memoryFreeKey, memoryBuffersKey,
             memoryCachedKey, memorySwapTotalKey, memorySwapFreeKey, memoryCommitLimitKey);
 
-    private Key<Integer> vmCpuVmIdKey = new Key<>("vm-id", false);
-    private Key<Double> vmCpuLoadKey = new Key<>("processor-usage", false);
+    private static Key<Integer> vmCpuVmIdKey = new Key<>("vm-id", false);
+    private static Key<Double> vmCpuLoadKey = new Key<>("processor-usage", false);
 
-    private Category vmCpuStatCategory = new Category("vm-cpu-stats",
+    static Category vmCpuStatCategory = new Category("vm-cpu-stats",
             vmCpuLoadKey, vmCpuVmIdKey);
 
-    {
+    static {
         // Set up categories that will later be registered.
         categories.add(hostInfoCategory);
         categories.add(networkInfoCategory);
--- a/agent/src/test/java/com/redhat/thermostat/backend/system/JvmStatHostListenerTest.java	Fri Mar 02 15:15:59 2012 +0100
+++ b/agent/src/test/java/com/redhat/thermostat/backend/system/JvmStatHostListenerTest.java	Fri Mar 02 10:46:18 2012 -0500
@@ -36,18 +36,25 @@
 
 package com.redhat.thermostat.backend.system;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import java.util.Collection;
 import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import org.junit.Test;
 import org.mockito.Matchers;
 
+import com.redhat.thermostat.common.storage.Key;
+
 import sun.jvmstat.monitor.HostIdentifier;
 import sun.jvmstat.monitor.MonitoredHost;
 import sun.jvmstat.monitor.MonitoredVm;
@@ -55,8 +62,6 @@
 import sun.jvmstat.monitor.VmIdentifier;
 import sun.jvmstat.monitor.event.VmStatusChangeEvent;
 
-import com.redhat.thermostat.backend.Backend;
-
 public class JvmStatHostListenerTest {
 
     @Test
@@ -87,4 +92,25 @@
         verify(vm).addVmListener(Matchers.isA(JvmStatVmClassListener.class));
     }
 
+    @Test
+    public void testCategory() {
+        assertEquals("vm-info", JvmStatHostListener.vmInfoCategory.getName());
+        Collection<Key<?>> keys = JvmStatHostListener.vmInfoCategory.getKeys();
+        assertTrue(keys.contains(new Key<Integer>("vm-id", true)));
+        assertTrue(keys.contains(new Key<Integer>("vm-pid", false)));
+        assertTrue(keys.contains(new Key<String>("runtime-version", false)));
+        assertTrue(keys.contains(new Key<String>("java-home", false)));
+        assertTrue(keys.contains(new Key<String>("main-class", false)));
+        assertTrue(keys.contains(new Key<String>("command-line", false)));
+        assertTrue(keys.contains(new Key<String>("vm-arguments", false)));
+        assertTrue(keys.contains(new Key<String>("vm-name", false)));
+        assertTrue(keys.contains(new Key<String>("vm-info", false)));
+        assertTrue(keys.contains(new Key<String>("vm-version", false)));
+        assertTrue(keys.contains(new Key<Map<String, String>>("environment", false)));
+        assertTrue(keys.contains(new Key<List<String>>("libraries", false)));
+        assertTrue(keys.contains(new Key<Long>("start-time", false)));
+        assertTrue(keys.contains(new Key<Long>("stop-time", false)));
+        assertEquals(14, keys.size());
+
+    }
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/src/test/java/com/redhat/thermostat/backend/system/JvmStatVmListenerTest.java	Fri Mar 02 10:46:18 2012 -0500
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.backend.system;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.common.storage.Key;
+
+public class JvmStatVmListenerTest {
+
+    @Test
+    public void testCategories() {
+        assertEquals("vm-gc-stats", JvmStatVmListener.vmGcStatsCategory.getName());
+        Collection<Key<?>> keys = JvmStatVmListener.vmGcStatsCategory.getKeys();
+        assertTrue(keys.contains(new Key<Integer>("vm-id", false)));
+        assertTrue(keys.contains(new Key<Long>("timestamp", false)));
+        assertTrue(keys.contains(new Key<String>("collector", false)));
+        assertTrue(keys.contains(new Key<Long>("runtime-count", false)));
+        assertTrue(keys.contains(new Key<Long>("wall-time", false)));
+        assertEquals(5, keys.size());
+        
+        assertEquals("vm-memory-stats", JvmStatVmListener.vmMemoryStatsCategory.getName());
+        keys = JvmStatVmListener.vmMemoryStatsCategory.getKeys();
+        assertTrue(keys.contains(new Key<Integer>("vm-id", false)));
+        assertTrue(keys.contains(new Key<Long>("timestamp", false)));
+        assertTrue(keys.contains(new Key<String>("eden.gen", false)));
+        assertTrue(keys.contains(new Key<String>("eden.collector", false)));
+        assertTrue(keys.contains(new Key<Long>("eden.capacity", false)));
+        assertTrue(keys.contains(new Key<Long>("eden.max-capacity", false)));
+        assertTrue(keys.contains(new Key<Long>("eden.used", false)));
+        assertTrue(keys.contains(new Key<String>("s0.gen", false)));
+        assertTrue(keys.contains(new Key<String>("s0.collector", false)));
+        assertTrue(keys.contains(new Key<Long>("s0.capacity", false)));
+        assertTrue(keys.contains(new Key<Long>("s0.max-capacity", false)));
+        assertTrue(keys.contains(new Key<Long>("s0.used", false)));
+        assertTrue(keys.contains(new Key<String>("s1.gen", false)));
+        assertTrue(keys.contains(new Key<String>("s1.collector", false)));
+        assertTrue(keys.contains(new Key<Long>("s1.capacity", false)));
+        assertTrue(keys.contains(new Key<Long>("s1.max-capacity", false)));
+        assertTrue(keys.contains(new Key<Long>("s1.used", false)));
+        assertTrue(keys.contains(new Key<String>("old.gen", false)));
+        assertTrue(keys.contains(new Key<String>("old.collector", false)));
+        assertTrue(keys.contains(new Key<Long>("old.capacity", false)));
+        assertTrue(keys.contains(new Key<Long>("old.max-capacity", false)));
+        assertTrue(keys.contains(new Key<Long>("old.used", false)));
+        assertTrue(keys.contains(new Key<String>("perm.gen", false)));
+        assertTrue(keys.contains(new Key<String>("perm.collector", false)));
+        assertTrue(keys.contains(new Key<Long>("perm.capacity", false)));
+        assertTrue(keys.contains(new Key<Long>("perm.max-capacity", false)));
+        assertTrue(keys.contains(new Key<Long>("perm.used", false)));
+        assertEquals(27, keys.size());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/src/test/java/com/redhat/thermostat/backend/system/SystemBackendTest.java	Fri Mar 02 10:46:18 2012 -0500
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.backend.system;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.common.storage.Key;
+
+public class SystemBackendTest {
+
+    @Test
+    public void testCategories() {
+        assertEquals("host-info", SystemBackend.hostInfoCategory.getName());
+        Collection<Key<?>> keys = SystemBackend.hostInfoCategory.getKeys();
+        assertTrue(keys.contains(new Key<String>("hostname", true)));
+        assertTrue(keys.contains(new Key<String>("os_name", false)));
+        assertTrue(keys.contains(new Key<String>("os_kernel", false)));
+        assertTrue(keys.contains(new Key<Integer>("cpu_num", false)));
+        assertTrue(keys.contains(new Key<String>("cpu_model", false)));
+        assertTrue(keys.contains(new Key<Long>("memory_total", false)));
+        assertEquals(6, keys.size());
+
+
+        assertEquals("network-info", SystemBackend.networkInfoCategory.getName());
+        keys = SystemBackend.networkInfoCategory.getKeys();
+        assertTrue(keys.contains(new Key<Long>("timestamp", false)));
+        assertTrue(keys.contains(new Key<String>("iface", true)));
+        assertTrue(keys.contains(new Key<String>("ipv4addr", false)));
+        assertTrue(keys.contains(new Key<String>("ipv6addr", false)));
+        assertEquals(4, keys.size());
+
+
+        assertEquals("cpu-stats", SystemBackend.cpuStatCategory.getName());
+        keys = SystemBackend.cpuStatCategory.getKeys();
+        assertTrue(keys.contains(new Key<Long>("timestamp", false)));
+        assertTrue(keys.contains(new Key<Double>("5load", false)));
+        assertTrue(keys.contains(new Key<Double>("10load", false)));
+        assertTrue(keys.contains(new Key<Double>("15load", false)));
+        assertEquals(4, keys.size());
+
+
+        assertEquals("memory-stats", SystemBackend.memoryStatCategory.getName());
+        keys = SystemBackend.memoryStatCategory.getKeys();
+        assertTrue(keys.contains(new Key<Long>("timestamp", false)));
+        assertTrue(keys.contains(new Key<Long>("total", false)));
+        assertTrue(keys.contains(new Key<Long>("free", false)));
+        assertTrue(keys.contains(new Key<Long>("buffers", false)));
+        assertTrue(keys.contains(new Key<Long>("cached", false)));
+        assertTrue(keys.contains(new Key<Long>("swap-total", false)));
+        assertTrue(keys.contains(new Key<Long>("swap-free", false)));
+        assertTrue(keys.contains(new Key<Long>("commit-limit", false)));
+        assertEquals(8, keys.size());
+
+
+        assertEquals("vm-cpu-stats", SystemBackend.vmCpuStatCategory.getName());
+        keys = SystemBackend.vmCpuStatCategory.getKeys();
+        assertTrue(keys.contains(new Key<Integer>("vm-id", false)));
+        assertTrue(keys.contains(new Key<Integer>("processor-usage", false)));
+        assertEquals(2, keys.size());
+    }
+}