-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integration tests for JMX Scraper (#1480)
Co-authored-by: Sylvain Juge <763082+SylvainJuge@users.noreply.github.com>
- Loading branch information
1 parent
ec5b648
commit b0aae23
Showing
5 changed files
with
86 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/PortSelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.contrib.jmxscraper; | ||
|
||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.util.Random; | ||
|
||
/** Class used for finding random free network port from range 1024-65535 */ | ||
public class PortSelector { | ||
private static final Random random = new Random(System.currentTimeMillis()); | ||
|
||
private static final int MIN_PORT = 1024; | ||
private static final int MAX_PORT = 65535; | ||
|
||
private PortSelector() {} | ||
|
||
/** | ||
* @return random available TCP port | ||
*/ | ||
public static synchronized int getAvailableRandomPort() { | ||
int port; | ||
|
||
do { | ||
port = random.nextInt(MAX_PORT - MIN_PORT + 1) + MIN_PORT; | ||
} while (!isPortAvailable(port)); | ||
|
||
return port; | ||
} | ||
|
||
private static boolean isPortAvailable(int port) { | ||
// see https://stackoverflow.com/a/13826145 for the chosen implementation | ||
try (Socket s = new Socket("localhost", port)) { | ||
return false; | ||
} catch (IOException e) { | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters