changeset 391:be4863c455f7

Avoid exceptions when reading /proc/ for dead processes reviewed-by: neugens review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-June/001848.html PR1051
author Jon VanAlten <jon.vanalten@redhat.com>
date Tue, 19 Jun 2012 13:35:57 -0400
parents 7a5ea4137f04
children cb22cde2bd47
files agent/core/src/main/java/com/redhat/thermostat/backend/system/VmCpuStatBuilder.java agent/core/src/test/java/com/redhat/thermostat/backend/system/VmCpuStatBuilderTest.java
diffstat 2 files changed, 24 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/agent/core/src/main/java/com/redhat/thermostat/backend/system/VmCpuStatBuilder.java	Tue Jun 19 19:11:24 2012 +0200
+++ b/agent/core/src/main/java/com/redhat/thermostat/backend/system/VmCpuStatBuilder.java	Tue Jun 19 13:35:57 2012 -0400
@@ -115,6 +115,7 @@
         ProcessStatusInfo info = statusBuilder.build(pid);
         if (info == null) {
             logger.log(Level.WARNING, "can not learn about pid " + pid + " : statusBuilder returned null");
+            return;
         }
 
         lastProcessTickTime.put(pid, time);
--- a/agent/core/src/test/java/com/redhat/thermostat/backend/system/VmCpuStatBuilderTest.java	Tue Jun 19 19:11:24 2012 +0200
+++ b/agent/core/src/test/java/com/redhat/thermostat/backend/system/VmCpuStatBuilderTest.java	Tue Jun 19 13:35:57 2012 -0400
@@ -39,6 +39,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
@@ -47,6 +48,7 @@
 
 import com.redhat.thermostat.common.Clock;
 import com.redhat.thermostat.common.model.VmCpuStat;
+import com.redhat.thermostat.test.Bug;
 
 public class VmCpuStatBuilderTest {
 
@@ -142,4 +144,25 @@
         assertEquals(CPU_LOAD_PERCENT, stat.getCpuLoad(), 0.0001);
     }
 
+    @Bug(id="1051",
+            summary="Avoid exceptions when reading /proc/ for dead processes",
+            url="http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=1051")
+    @Test
+    public void testNoExceptionForBuilderLearningAboutDeadProcess() {
+        Clock clock = mock(Clock.class);
+        when(clock.getMonotonicTimeNanos()).thenReturn((long) (10000 * 1E6));
+        ProcessStatusInfoBuilder procBuilder = mock(ProcessStatusInfoBuilder.class);
+        // This thing returns null if the /proc entry goes away.  Rather than try to
+        // 'guess' at a pid that will not be present during test, just mock this.
+        when(procBuilder.build(any(Integer.class))).thenReturn(null);
+        VmCpuStatBuilder builder = new VmCpuStatBuilder(clock, 3, 100, procBuilder);
+        // If we can't handle a process' /proc entry disappearing, the next line
+        // will throw exception.  If it does not, then we are okay.
+        try {
+            builder.learnAbout(0);
+        } catch (Exception e) {
+            // Shouldn't happen.
+            assertTrue(false);
+        }
+    }
 }