Thomas Vachuska
Committed by Gerrit Code Review

Fixing uncaught exception handler and adding a test.

Change-Id: I0861b0200bb39a14c39746ae7b72a1e21f957e35
......@@ -18,7 +18,6 @@ package org.onlab.util;
import com.google.common.base.Strings;
import com.google.common.primitives.UnsignedLongs;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import java.io.BufferedReader;
......@@ -64,8 +63,8 @@ public abstract class Tools {
public static ThreadFactory namedThreads(String pattern) {
return new ThreadFactoryBuilder()
.setNameFormat(pattern)
// FIXME remove UncaughtExceptionHandler before release
.setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on {}", t.getName(), e)).build();
.setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e))
.build();
}
/**
......@@ -84,8 +83,8 @@ public abstract class Tools {
return new ThreadFactoryBuilder()
.setThreadFactory(groupedThreadFactory(groupName))
.setNameFormat(groupName.replace(GroupedThreadFactory.DELIMITER, "-") + "-" + pattern)
// FIXME remove UncaughtExceptionHandler before release
.setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on {}", t.getName(), e)).build();
.setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e))
.build();
}
/**
......@@ -242,6 +241,7 @@ public abstract class Tools {
/**
* Returns a human friendly time ago string for a specified system time.
*
* @param unixTime system time in millis
* @return human friendly time ago
*/
......
......@@ -21,6 +21,7 @@ import org.onlab.junit.TestTools;
import java.util.concurrent.ThreadFactory;
import static org.junit.Assert.*;
import static org.onlab.junit.TestTools.assertAfter;
/**
* Test of the miscellaneous tools.
......@@ -47,18 +48,29 @@ public class ToolsTest {
}
@Test
public void namedThreads() {
public void namedThreads() {
ThreadFactory f = Tools.namedThreads("foo-%d");
Thread t = f.newThread(() -> TestTools.print("yo"));
assertTrue("wrong pattern", t.getName().startsWith("foo-"));
}
@Test
public void groupedThreads() {
public void groupedThreads() {
ThreadFactory f = Tools.groupedThreads("foo/bar-me", "foo-%d");
Thread t = f.newThread(() -> TestTools.print("yo"));
assertTrue("wrong pattern", t.getName().startsWith("foo-bar-me-foo-"));
assertTrue("wrong group", t.getThreadGroup().getName().equals("foo/bar-me"));
}
@Test
public void exceptionHandler() throws InterruptedException {
ThreadFactory f = Tools.namedThreads("foo");
Thread t = f.newThread(() -> {
throw new IllegalStateException("BOOM!");
});
assertNotNull("thread should have exception handler", t.getUncaughtExceptionHandler());
t.start();
assertAfter(100, () -> assertEquals("incorrect thread state", Thread.State.TERMINATED, t.getState()));
}
}
......