Uploaded image for project: 'Maven Surefire'
  1. Maven Surefire
  2. SUREFIRE-1587

Forked execution prevents correct ServerSocket closing

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.22.1
    • None
    • None
    • None
    • Linux, problem becomes visible on archlinux and also on Travis CI

    Description

      The default mode of forked unit test execution (with JUnit) seems to interfere with socket operations. In our case, we see that with a surefire execution in forked mode, ServerSocket instances, despite returning from a close() call, are sometimes not correctly closed and a subsequent try to acquire a server socket on the same port then fails. This does not happen outside of surefire or when forkMode is set to none.

      Here is a simple test case to try this. First the maven project pom.xml:

      pom.xml
      <?xml version="1.0" encoding="UTF-8"?>
      
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
      
        <groupId>testing</groupId>
        <artifactId>testit</artifactId>
        <version>1.0-SNAPSHOT</version>
      
        <name>testit</name>
        <description>A simple testit.</description>
        <url>http://www.example.com</url>
      
        <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
          <maven.compiler.source>1.7</maven.compiler.source>
          <maven.compiler.target>1.7</maven.compiler.target>
        </properties>
      
        <dependencies>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
          </dependency>
        </dependencies>
      
        <build>
          <plugins>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
          </plugins>
        </build>
      
      </project>
      

      And here a test file to be placed into the test source tree:

      AppTest.java
      package testing;
      
      import org.junit.Test;
      
      import java.net.ServerSocket;
      
      public class AppTest {
      
          @Test
          public void testSocketStuff() throws Exception {
              while (true) {
                  System.out.println("Iteration");
                  final ServerSocket socket = new ServerSocket(55444);
                  socket.close();
              }
          }
      
      }
      

      Executing this with the default options (forking mode enabled) will pretty soon end up in the following exception after some iterations:

      [ERROR] testSocketStuff(testing.AppTest)  Time elapsed: 1.376 s  <<< ERROR!
      java.net.BindException: Address already in use (Bind failed)
              at testing.AppTest.testSocketStuff(AppTest.java:17)
      

      Executing the same test with -DforkMode=none does not result in this failure and the loop runs endlessly.

      Attachments

        Activity

          People

            Unassigned Unassigned
            languitar Johannes Wienke
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: