View Javadoc
1   /**
2    * Copyright (c) 2013-2022, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package com.jcabi.github;
31  
32  import com.jcabi.aspects.RetryOnFailure;
33  import com.jcabi.log.Logger;
34  import java.io.IOException;
35  import java.net.BindException;
36  import java.net.InetSocketAddress;
37  import java.net.ServerSocket;
38  import org.junit.Assume;
39  import org.junit.rules.ExternalResource;
40  import org.junit.runner.Description;
41  import org.junit.runners.model.Statement;
42  
43  /**
44   * Test resource for skipping tests when random port is busy.
45   * @author Aleksey Popov (alopen@yandex.ru)
46   * @version $Id: 62050d514412e21513212508439ac038c03978dc $
47   */
48  public class RandomPort extends ExternalResource {
49      @Override
50      public final Statement apply(
51          final Statement base, final Description description
52      ) {
53          return new Statement() {
54              @Override
55              // @checkstyle IllegalThrowsCheck (1 line)
56              public void evaluate() throws Throwable {
57                  try {
58                      base.evaluate();
59                  } catch (final BindException ignored) {
60                      Logger.warn(
61                          base,
62                          String.format(
63                              "Test %s skipped due to no available ports",
64                              description
65                          )
66                      );
67                      Assume.assumeTrue(false);
68                  }
69              }
70          };
71      }
72  
73      /**
74       * Returns available port number.
75       * @return Available port number
76       * @throws IOException in case of IO error.
77       */
78      @RetryOnFailure
79      public final int port() throws IOException {
80          final ServerSocket socket = new ServerSocket();
81          try  {
82              socket.setReuseAddress(true);
83              socket.bind(
84                  new InetSocketAddress("localhost", 0)
85              );
86              return socket.getLocalPort();
87          } finally {
88              socket.close();
89          }
90      }
91  }