changeset 1055:92699ccfadca

Add more diagnostics if agent fails to bind cmd channel server. Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-April/006278.html
author Severin Gehwolf <sgehwolf@redhat.com>
date Thu, 04 Apr 2013 12:42:39 +0200
parents 1ceeba5420f2
children f82c6b24dcbd
files agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImpl.java agent/command/src/test/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImplTest.java
diffstat 2 files changed, 33 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImpl.java	Wed Apr 03 17:40:58 2013 +0200
+++ b/agent/command/src/main/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImpl.java	Thu Apr 04 12:42:39 2013 +0200
@@ -41,6 +41,7 @@
 import java.util.logging.Logger;
 
 import org.jboss.netty.bootstrap.ServerBootstrap;
+import org.jboss.netty.channel.ChannelException;
 
 import com.redhat.thermostat.agent.command.ConfigurationServer;
 import com.redhat.thermostat.common.utils.LoggingUtils;
@@ -62,8 +63,15 @@
         InetSocketAddress addr = new InetSocketAddress(host[0], Integer.parseInt(host[1]));
         
         logger.log(Level.FINE, "Starting command channel server on " + addr.toString());
-        // Bind and start to accept incoming connections.
-        bootstrap.bind(addr);
+        try {
+            // Bind and start to accept incoming connections.
+            bootstrap.bind(addr);
+        } catch (ChannelException e) {
+            logger.log(Level.SEVERE, "Failed to bind command channel server!", e);
+            // rethrow, in order to stop agent from continuing
+            throw e;
+        }
+        logger.log(Level.FINEST, "Bound command channel server to " + addr.toString());
     }
 
     @Override
--- a/agent/command/src/test/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImplTest.java	Wed Apr 03 17:40:58 2013 +0200
+++ b/agent/command/src/test/java/com/redhat/thermostat/agent/command/internal/ConfigurationServerImplTest.java	Thu Apr 04 12:42:39 2013 +0200
@@ -36,23 +36,23 @@
 
 package com.redhat.thermostat.agent.command.internal;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
 import java.net.InetSocketAddress;
 
 import org.jboss.netty.bootstrap.ServerBootstrap;
+import org.jboss.netty.channel.ChannelException;
 import org.jboss.netty.channel.group.ChannelGroup;
 import org.jboss.netty.channel.group.ChannelGroupFuture;
 import org.junit.Before;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
-import com.redhat.thermostat.agent.command.internal.ConfigurationServerImpl;
-import com.redhat.thermostat.agent.command.internal.ConfigurationServerContext;
-
-import static org.junit.Assert.assertEquals;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
 public class ConfigurationServerImplTest {
 
     private ConfigurationServerContext ctx;
@@ -81,6 +81,21 @@
         InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 123);
         assertEquals(addr, argument.getValue());
     }
+    
+    @SuppressWarnings("unchecked")
+    @Test
+    public void startListeningFailureThrowsException() {
+        ConfigurationServerImpl server = new ConfigurationServerImpl(ctx);
+
+        when(bootstrap.bind(any(InetSocketAddress.class))).thenThrow(ChannelException.class);
+        
+        try {
+            server.startListening("does-not-resolve.example.com:123");
+            fail("Should have thrown exception");
+        } catch (ChannelException e) {
+            // pass
+        }
+    }
 
     @Test
     public void testStopListening() {