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; ...@@ -18,7 +18,6 @@ package org.onlab.util;
18 import com.google.common.base.Strings; 18 import com.google.common.base.Strings;
19 import com.google.common.primitives.UnsignedLongs; 19 import com.google.common.primitives.UnsignedLongs;
20 import com.google.common.util.concurrent.ThreadFactoryBuilder; 20 import com.google.common.util.concurrent.ThreadFactoryBuilder;
21 -
22 import org.slf4j.Logger; 21 import org.slf4j.Logger;
23 22
24 import java.io.BufferedReader; 23 import java.io.BufferedReader;
...@@ -64,8 +63,8 @@ public abstract class Tools { ...@@ -64,8 +63,8 @@ public abstract class Tools {
64 public static ThreadFactory namedThreads(String pattern) { 63 public static ThreadFactory namedThreads(String pattern) {
65 return new ThreadFactoryBuilder() 64 return new ThreadFactoryBuilder()
66 .setNameFormat(pattern) 65 .setNameFormat(pattern)
67 - // FIXME remove UncaughtExceptionHandler before release 66 + .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e))
68 - .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on {}", t.getName(), e)).build(); 67 + .build();
69 } 68 }
70 69
71 /** 70 /**
...@@ -84,8 +83,8 @@ public abstract class Tools { ...@@ -84,8 +83,8 @@ public abstract class Tools {
84 return new ThreadFactoryBuilder() 83 return new ThreadFactoryBuilder()
85 .setThreadFactory(groupedThreadFactory(groupName)) 84 .setThreadFactory(groupedThreadFactory(groupName))
86 .setNameFormat(groupName.replace(GroupedThreadFactory.DELIMITER, "-") + "-" + pattern) 85 .setNameFormat(groupName.replace(GroupedThreadFactory.DELIMITER, "-") + "-" + pattern)
87 - // FIXME remove UncaughtExceptionHandler before release 86 + .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on " + t.getName(), e))
88 - .setUncaughtExceptionHandler((t, e) -> log.error("Uncaught exception on {}", t.getName(), e)).build(); 87 + .build();
89 } 88 }
90 89
91 /** 90 /**
...@@ -242,6 +241,7 @@ public abstract class Tools { ...@@ -242,6 +241,7 @@ public abstract class Tools {
242 241
243 /** 242 /**
244 * Returns a human friendly time ago string for a specified system time. 243 * Returns a human friendly time ago string for a specified system time.
244 + *
245 * @param unixTime system time in millis 245 * @param unixTime system time in millis
246 * @return human friendly time ago 246 * @return human friendly time ago
247 */ 247 */
......
...@@ -21,6 +21,7 @@ import org.onlab.junit.TestTools; ...@@ -21,6 +21,7 @@ import org.onlab.junit.TestTools;
21 import java.util.concurrent.ThreadFactory; 21 import java.util.concurrent.ThreadFactory;
22 22
23 import static org.junit.Assert.*; 23 import static org.junit.Assert.*;
24 +import static org.onlab.junit.TestTools.assertAfter;
24 25
25 /** 26 /**
26 * Test of the miscellaneous tools. 27 * Test of the miscellaneous tools.
...@@ -47,18 +48,29 @@ public class ToolsTest { ...@@ -47,18 +48,29 @@ public class ToolsTest {
47 } 48 }
48 49
49 @Test 50 @Test
50 - public void namedThreads() { 51 + public void namedThreads() {
51 ThreadFactory f = Tools.namedThreads("foo-%d"); 52 ThreadFactory f = Tools.namedThreads("foo-%d");
52 Thread t = f.newThread(() -> TestTools.print("yo")); 53 Thread t = f.newThread(() -> TestTools.print("yo"));
53 assertTrue("wrong pattern", t.getName().startsWith("foo-")); 54 assertTrue("wrong pattern", t.getName().startsWith("foo-"));
54 } 55 }
55 56
56 @Test 57 @Test
57 - public void groupedThreads() { 58 + public void groupedThreads() {
58 ThreadFactory f = Tools.groupedThreads("foo/bar-me", "foo-%d"); 59 ThreadFactory f = Tools.groupedThreads("foo/bar-me", "foo-%d");
59 Thread t = f.newThread(() -> TestTools.print("yo")); 60 Thread t = f.newThread(() -> TestTools.print("yo"));
60 assertTrue("wrong pattern", t.getName().startsWith("foo-bar-me-foo-")); 61 assertTrue("wrong pattern", t.getName().startsWith("foo-bar-me-foo-"));
61 assertTrue("wrong group", t.getThreadGroup().getName().equals("foo/bar-me")); 62 assertTrue("wrong group", t.getThreadGroup().getName().equals("foo/bar-me"));
62 } 63 }
63 64
65 + @Test
66 + public void exceptionHandler() throws InterruptedException {
67 + ThreadFactory f = Tools.namedThreads("foo");
68 + Thread t = f.newThread(() -> {
69 + throw new IllegalStateException("BOOM!");
70 + });
71 + assertNotNull("thread should have exception handler", t.getUncaughtExceptionHandler());
72 + t.start();
73 + assertAfter(100, () -> assertEquals("incorrect thread state", Thread.State.TERMINATED, t.getState()));
74 + }
75 +
64 } 76 }
......