Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added getFirstMappedPort method #377

Merged
merged 3 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
### Fixed

### Changed
- Added `getFirstMappedPort` method

## [1.3.1] - 2017-06-22
### Fixed
Expand Down
14 changes: 14 additions & 0 deletions core/src/main/java/org/testcontainers/containers/Container.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,20 @@ default SELF withClasspathResourceMapping(final String resourcePath, final Strin
*/
Boolean isRunning();

/**
* Get the actual mapped port for a first port exposed by the container.
*
* @return the port that the exposed port is mapped to
* @throws IllegalStateException if there are no exposed ports
*/
default Integer getFirstMappedPort() {
return getExposedPorts()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 is use of streams absolutely essential here? 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, at least I wouldn't say it's a misuse :)

Also, I prefer findFirst + map over !isEmpty() + .get(0), less error prone if you ask me

.stream()
.findFirst()
.map(this::getMappedPort)
.orElseThrow(() -> new IllegalStateException("Container doesn't expose any ports"));
}

/**
* Get the actual mapped port for a given port exposed by the container.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private BufferedReader getReaderForContainerPort80(GenericContainer container) {
return Unreliables.retryUntilSuccess(10, TimeUnit.SECONDS, () -> {
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);

Socket socket = new Socket(container.getContainerIpAddress(), container.getMappedPort(80));
Socket socket = new Socket(container.getContainerIpAddress(), container.getFirstMappedPort());
return new BufferedReader(new InputStreamReader(socket.getInputStream()));
});
}
Expand Down