[logback-dev] svn commit: r1925 - in logback/trunk: logback-access/src/test/java/ch/qos/logback/access/jetty logback-classic/src/test/java/ch/qos/logback/classic/net logback-classic/src/test/java/ch/qos/logback/classic/net/mock logback-core/src/test/java/ch/qos/logback/core/testUtil

noreply.ceki at qos.ch noreply.ceki at qos.ch
Fri Oct 31 11:07:57 CET 2008


Author: ceki
Date: Fri Oct 31 11:07:57 2008
New Revision: 1925

Added:
   logback/trunk/logback-core/src/test/java/ch/qos/logback/core/testUtil/RandomUtil.java
Modified:
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java
   logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyFixture.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java
   logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/mock/MockSyslogServer.java

Log:
- use random ports for tests to reduce the probability in LINGERING ports 
  between successive runs

Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java
==============================================================================
--- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java	(original)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyBasicTest.java	Fri Oct 31 11:07:57 2008
@@ -15,31 +15,34 @@
 import ch.qos.logback.access.spi.AccessEvent;
 import ch.qos.logback.access.spi.Util;
 import ch.qos.logback.access.testUtil.NotifyingListAppender;
+import ch.qos.logback.core.testUtil.RandomUtil;
 
 public class JettyBasicTest {
 
-  static RequestLogImpl requestLogImpl;
-  static JettyFixture fixture;
+  static RequestLogImpl REQYEST_LOG_IMPL;
+  static JettyFixture JETTY_FIXTURE;
 
+  static int RANDOM_SERVER_PORT = RandomUtil.getRandomServerPort();
+  
   @BeforeClass
   static public void startServer() throws Exception {
     // System.out.println("*** JettyBasicTest.startServer called");
-    requestLogImpl = new RequestLogImpl();
-    JettyFixture fixture = new JettyFixture(requestLogImpl);
-    fixture.start();
+    REQYEST_LOG_IMPL = new RequestLogImpl();
+    JETTY_FIXTURE = new JettyFixture(REQYEST_LOG_IMPL, RANDOM_SERVER_PORT);
+    JETTY_FIXTURE.start();
   }
 
   @AfterClass
   static public void stopServer() throws Exception {
     // System.out.println("*** JettyBasicTest.stopServer called");
-    if (fixture != null) {
-      fixture.stop();
+    if (JETTY_FIXTURE != null) {
+      JETTY_FIXTURE.stop();
     }
   }
 
   @Test
   public void getRequest() throws Exception {
-    URL url = new URL("http://localhost:" + JettyFixture.PORT + "/");
+    URL url = new URL("http://localhost:" + RANDOM_SERVER_PORT + "/");
     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
     connection.setDoInput(true);
 
@@ -47,14 +50,14 @@
 
     assertEquals("hello world", result);
 
-    NotifyingListAppender listAppender = (NotifyingListAppender) requestLogImpl
+    NotifyingListAppender listAppender = (NotifyingListAppender) REQYEST_LOG_IMPL
         .getAppender("list");
     listAppender.list.clear();
   }
 
   @Test
   public void eventGoesToAppenders() throws Exception {
-    URL url = new URL("http://localhost:" + JettyFixture.PORT + "/");
+    URL url = new URL(JETTY_FIXTURE.getUrl());
     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
     connection.setDoInput(true);
 
@@ -62,7 +65,7 @@
 
     assertEquals("hello world", result);
 
-    NotifyingListAppender listAppender = (NotifyingListAppender) requestLogImpl
+    NotifyingListAppender listAppender = (NotifyingListAppender) REQYEST_LOG_IMPL
         .getAppender("list");
     synchronized (listAppender) {
       listAppender.wait(100);
@@ -77,8 +80,7 @@
 
   @Test
   public void postContentConverter() throws Exception {
-    // System.out.println("into test");
-    URL url = new URL("http://localhost:" + JettyFixture.PORT + "/");
+    URL url = new URL(JETTY_FIXTURE.getUrl());
     String msg = "test message";
 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection();
@@ -99,7 +101,7 @@
 
     // StatusPrinter.print(requestLogImpl.getStatusManager());
 
-    NotifyingListAppender listAppender = (NotifyingListAppender) requestLogImpl
+    NotifyingListAppender listAppender = (NotifyingListAppender) REQYEST_LOG_IMPL
         .getAppender("list");
 
     synchronized (listAppender) {

Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyFixture.java
==============================================================================
--- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyFixture.java	(original)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/jetty/JettyFixture.java	Fri Oct 31 11:07:57 2008
@@ -26,25 +26,28 @@
 public class JettyFixture {
   RequestLogImpl requestLogImpl;
 
-  public static final int PORT = 1234;
+  private final int port;
+  Server server;
+  String url;
   
-  public JettyFixture(RequestLogImpl impl) {
+  public JettyFixture(RequestLogImpl impl, int port) {
     requestLogImpl = impl;
+    this.port = port;
+    url = "http://localhost:" + port + "/";
   }
 
   public String getName() {
     return "Jetty Test Setup";
   }
 
-  Server server;
-  String url = "http://localhost:" + PORT + "/";
+  public String getUrl() {
+    return url;
+  }
 
   public void start() throws Exception {
-    // System.out.println("into setUp");
-
     server = new Server();
     Connector connector = new SelectChannelConnector();
-    connector.setPort(PORT);
+    connector.setPort(port);
     server.setConnectors(new Connector[] { connector });
 
     ContextHandler context = new ContextHandler();

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SyslogAppenderTest.java	Fri Oct 31 11:07:57 2008
@@ -24,6 +24,7 @@
 import ch.qos.logback.classic.net.mock.MockSyslogServer;
 import ch.qos.logback.core.joran.spi.JoranException;
 import ch.qos.logback.core.net.SyslogConstants;
+import ch.qos.logback.core.testUtil.RandomUtil;
 import ch.qos.logback.core.util.StatusPrinter;
 
 public class SyslogAppenderTest {
@@ -38,8 +39,8 @@
 
   @Test
   public void basic() throws InterruptedException {
-    int port = MockSyslogServer.PORT + 1;
-
+    int port = RandomUtil.getRandomServerPort();
+    
     MockSyslogServer mockServer = new MockSyslogServer(1, port);
     mockServer.start();
     // give MockSyslogServer head start
@@ -83,7 +84,8 @@
 
   @Test
   public void tException() throws InterruptedException {
-    int port = MockSyslogServer.PORT + 2;
+    int port = RandomUtil.getRandomServerPort();
+    
     MockSyslogServer mockServer = new MockSyslogServer(21, port);
     mockServer.start();
     // give MockSyslogServer head start

Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/mock/MockSyslogServer.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/mock/MockSyslogServer.java	(original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/mock/MockSyslogServer.java	Fri Oct 31 11:07:57 2008
@@ -20,8 +20,6 @@
  */
 public class MockSyslogServer extends Thread {
 
-  public static final int PORT = 14500;
-
   final int loopLen;
   final int port;
   

Added: logback/trunk/logback-core/src/test/java/ch/qos/logback/core/testUtil/RandomUtil.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-core/src/test/java/ch/qos/logback/core/testUtil/RandomUtil.java	Fri Oct 31 11:07:57 2008
@@ -0,0 +1,24 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework.
+ * 
+ * Copyright (C) 2000-2008, QOS.ch
+ * 
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.core.testUtil;
+
+import java.util.Random;
+
+public class RandomUtil {
+
+  private static Random random = new Random();
+
+  public static int getRandomServerPort() {
+    int r = random.nextInt(20000);
+    // the first 1024 ports are usually reserved for the OS
+    return r + 1024;
+  }
+
+}


More information about the logback-dev mailing list