-
Notifications
You must be signed in to change notification settings - Fork 71
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
test: select random free server ports with 0 #960
Merged
nosahama
merged 1 commit into
main
from
jjaakola-aiven-select-server-ports-with-0-in-tests
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,10 @@ | |
Copyright (c) 2023 Aiven Ltd | ||
See LICENSE for details | ||
""" | ||
from contextlib import ExitStack | ||
from contextlib import closing, contextmanager, ExitStack | ||
from karapace.config import set_config_defaults | ||
from pathlib import Path | ||
from tests.integration.utils.kafka_server import KafkaServers | ||
from tests.integration.utils.network import PortRangeInclusive | ||
from tests.integration.utils.process import stop_process | ||
from tests.utils import popen_karapace_all | ||
from typing import Iterator | ||
|
@@ -15,8 +14,15 @@ | |
import socket | ||
|
||
|
||
@contextmanager | ||
def allocate_port_no_reuse() -> Iterator[int]: | ||
"""Allocate random free port and do not allow reuse.""" | ||
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as sock: | ||
sock.bind(("127.0.0.1", 0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Simpler and nice 👍 |
||
yield sock.getsockname()[1] | ||
|
||
|
||
def test_regression_server_must_exit_on_exception( | ||
port_range: PortRangeInclusive, | ||
tmp_path: Path, | ||
kafka_servers: Iterator[KafkaServers], | ||
) -> None: | ||
|
@@ -25,10 +31,10 @@ def test_regression_server_must_exit_on_exception( | |
Karapace was not closing all its background threads, so when an exception | ||
was raised an reached the top-level, the webserver created by asyncio would | ||
be stopped but the threads would keep the server running. | ||
Karapace exit on exception is done by setting a reserved port as server port. | ||
""" | ||
with ExitStack() as stack: | ||
port = stack.enter_context(port_range.allocate_port()) | ||
sock = stack.enter_context(socket.socket()) | ||
port = stack.enter_context(allocate_port_no_reuse()) | ||
|
||
config = set_config_defaults( | ||
{ | ||
|
@@ -42,7 +48,6 @@ def test_regression_server_must_exit_on_exception( | |
logfile = stack.enter_context((tmp_path / "karapace.log").open("w")) | ||
errfile = stack.enter_context((tmp_path / "karapace.err").open("w")) | ||
config_path.write_text(json.dumps(config)) | ||
sock.bind(("127.0.0.1", port)) | ||
process = popen_karapace_all(config_path, logfile, errfile) | ||
stack.callback(stop_process, process) # make sure to stop the process if the test fails | ||
assert process.wait(timeout=10) != 0, "Process should have exited with an error, port is already is use" |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit-pick]
Maybe
Generator[int]
as the return type, since you already did earlier, but doesn't hurt as generators as also iterators.