changeset 261:0ad7fdcad47e

Thermostat GUI doesn't exit when closed, needs killing reviewed-by: vanaltj review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-April/001043.html PR 957
author Mario Torre <neugens.limasoftware@gmail.com>
date Tue, 24 Apr 2012 10:25:12 +0200
parents e5ca1ae73748
children 9a1a69250ee2
files client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java common/src/main/java/com/redhat/thermostat/common/ThreadPoolTimerFactory.java common/src/main/java/com/redhat/thermostat/common/TimerFactory.java common/src/test/java/com/redhat/thermostat/common/ThreadPoolTimerFactoryTest.java
diffstat 4 files changed, 53 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java	Mon Apr 23 16:24:41 2012 -0400
+++ b/client/src/main/java/com/redhat/thermostat/client/MainWindowControllerImpl.java	Tue Apr 24 10:25:12 2012 +0200
@@ -182,6 +182,7 @@
                 case SHUTDOWN:
                     view.hideMainWindow();
                     stop();
+                    ApplicationContext.getInstance().getTimerFactory().shutdown();
                     break;
                 default:
                     throw new IllegalStateException("unhandled action");
--- a/common/src/main/java/com/redhat/thermostat/common/ThreadPoolTimerFactory.java	Mon Apr 23 16:24:41 2012 -0400
+++ b/common/src/main/java/com/redhat/thermostat/common/ThreadPoolTimerFactory.java	Tue Apr 24 10:25:12 2012 +0200
@@ -39,12 +39,23 @@
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 
 public class ThreadPoolTimerFactory implements TimerFactory {
 
     public ThreadPoolTimerFactory(int poolSize) {
-        timerThreadPool = Executors.newScheduledThreadPool(poolSize);
+        this(poolSize, Thread.currentThread().getThreadGroup());
+    }
+
+    ThreadPoolTimerFactory(int poolSize, final ThreadGroup group) {
+        timerThreadPool = Executors.newScheduledThreadPool(poolSize, new ThreadFactory() {
+            
+            @Override
+            public Thread newThread(Runnable r) {
+                return new Thread(group, r);
+            }
+        });
     }
 
     private class ThreadPoolTimer implements Timer {
@@ -126,4 +137,9 @@
         return new ThreadPoolTimer();
     }
 
+    @Override
+    public void shutdown() {
+        timerThreadPool.shutdown();
+    }
+
 }
--- a/common/src/main/java/com/redhat/thermostat/common/TimerFactory.java	Mon Apr 23 16:24:41 2012 -0400
+++ b/common/src/main/java/com/redhat/thermostat/common/TimerFactory.java	Tue Apr 24 10:25:12 2012 +0200
@@ -39,4 +39,6 @@
 public interface TimerFactory {
 
     Timer createTimer();
+
+    void shutdown();
 }
--- a/common/src/test/java/com/redhat/thermostat/common/ThreadPoolTimerFactoryTest.java	Mon Apr 23 16:24:41 2012 -0400
+++ b/common/src/test/java/com/redhat/thermostat/common/ThreadPoolTimerFactoryTest.java	Tue Apr 24 10:25:12 2012 +0200
@@ -36,6 +36,8 @@
 
 package com.redhat.thermostat.common;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
@@ -51,6 +53,7 @@
 import org.mockito.stubbing.Answer;
 
 import com.redhat.thermostat.common.Timer.SchedulingType;
+import com.redhat.thermostat.test.Bug;
 
 public class ThreadPoolTimerFactoryTest {
 
@@ -58,15 +61,24 @@
 
     private Timer timer;
 
+    private ThreadGroup threadGroup;
+
+    private TimerFactory timerFactory;
+
     @Before
     public void setUp() {
-        ThreadPoolTimerFactory timerFactory = new ThreadPoolTimerFactory(1);
+        threadGroup = new ThreadGroup("test");
+        timerFactory = new ThreadPoolTimerFactory(1, threadGroup);
         timer = timerFactory.createTimer();
     }
 
     @After
     public void tearDown() {
+        
         timer = null;
+        timerFactory.shutdown();
+        timerFactory = null;
+        threadGroup = null;
     }
 
     @Test
@@ -187,4 +199,24 @@
         Thread.sleep(DELAY);
         verify(action, times(2)).run();
     }
+
+    @Bug(id="957",
+         summary="Thermostat GUI doesn't exit when closed, needs killing",
+         url="http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=957")
+    @Test
+    public void verifyShutdownKillsThreads() throws InterruptedException {
+
+        Runnable action = mock(Runnable.class);
+        timer.setAction(action);
+        timer.setInitialDelay(DELAY / 2);
+        timer.start();
+
+        assertTrue(threadGroup.activeCount() > 0);
+
+        timerFactory.shutdown();
+
+        Thread.sleep(DELAY);
+
+        assertEquals(0, threadGroup.activeCount());
+    }
 }