-
-
Notifications
You must be signed in to change notification settings - Fork 338
feat: Add wait strategy to check external (TCP) port availability #1495
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a2a1964
feat: add WaitUntilTcpConnectionIsSucceeded
hs-thimolimpert 8ccf475
fix: naming
hs-thimolimpert 9963856
chore: fix naming of test
hs-thimolimpert 6f94096
chore: improve docs
0e54a69
chore: implement HostPortStrategy
hs-thimolimpert d7a81fd
chore: reflect tcp only in name and reduce code duplication
hs-thimolimpert f4c85fe
test: skip to let peple know, this test was thought of
hs-thimolimpert 859b65f
fix: test probably time outs. Maybe because the listener is opened, b…
hs-thimolimpert a2b75a6
debug: verify that it is actually the wait that is broken
hs-thimolimpert 4a05fb3
fix: when a test hangs we should timeout to have a better test experi…
hs-thimolimpert 7f8abd5
fix: we need to expose the port to actually wait for it
hs-thimolimpert 4684425
fix: don't swallow exception. When we are waiting the mapping should …
hs-thimolimpert df8ff45
chore: rename to Until as it is no Wait Strategy
hs-thimolimpert 148425f
fix: we expect a throw when port not mapped
hs-thimolimpert db5e189
chore: Switch to internal and external TCP wait strategy
HofmeisterAn 460b1f1
fix: Set correcct Windows command (rm copy and past error)
HofmeisterAn 67c37f8
chore: Add remarks
HofmeisterAn 83a65a4
chore: Refactor tests
HofmeisterAn 0d70ba1
chore: Remove BOM
HofmeisterAn c50a354
docs: Add section for TCP port is available
HofmeisterAn 89dc29b
fix: Listen on public interfaces
HofmeisterAn 432a7a0
fix: Dont use privileged port
HofmeisterAn 62caa85
fix: Use correct base class UntilWindowsCommandIsCompleted
HofmeisterAn 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 hidden or 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
39 changes: 39 additions & 0 deletions
39
src/Testcontainers/Configurations/WaitStrategies/UntilExternalTcpPortIsAvailable.cs
This file contains hidden or 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,39 @@ | ||
| namespace DotNet.Testcontainers.Configurations | ||
| { | ||
| using System.Net.Sockets; | ||
| using System.Threading.Tasks; | ||
| using DotNet.Testcontainers.Containers; | ||
|
|
||
| internal class UntilExternalTcpPortIsAvailable : IWaitUntil | ||
| { | ||
| private readonly int _containerPort; | ||
|
|
||
| public UntilExternalTcpPortIsAvailable(int containerPort) | ||
| { | ||
| _containerPort = containerPort; | ||
| } | ||
|
|
||
| public async Task<bool> UntilAsync(IContainer container) | ||
| { | ||
| var hostPort = container.GetMappedPublicPort(_containerPort); | ||
|
|
||
| var tcpClient = new TcpClient(); | ||
|
|
||
| try | ||
| { | ||
| await tcpClient.ConnectAsync(container.Hostname, hostPort) | ||
| .ConfigureAwait(false); | ||
|
|
||
| return true; | ||
| } | ||
| catch | ||
| { | ||
| return false; | ||
| } | ||
| finally | ||
| { | ||
| tcpClient.Dispose(); | ||
| } | ||
| } | ||
| } | ||
| } |
6 changes: 3 additions & 3 deletions
6
...aitStrategies/UntilUnixPortIsAvailable.cs → .../UntilInternalTcpPortIsAvailableOnUnix.cs
This file contains hidden or 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
10 changes: 10 additions & 0 deletions
10
src/Testcontainers/Configurations/WaitStrategies/UntilInternalTcpPortIsAvailableOnWindows.cs
This file contains hidden or 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,10 @@ | ||
| namespace DotNet.Testcontainers.Configurations | ||
| { | ||
| internal class UntilInternalTcpPortIsAvailableOnWindows : UntilUnixCommandIsCompleted | ||
| { | ||
| public UntilInternalTcpPortIsAvailableOnWindows(int containerPort) | ||
| : base($"Exit(-Not((Test-NetConnection -ComputerName 'localhost' -Port {containerPort}).TcpTestSucceeded))") | ||
| { | ||
| } | ||
| } | ||
| } |
10 changes: 0 additions & 10 deletions
10
src/Testcontainers/Configurations/WaitStrategies/UntilWindowsPortIsAvailable.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
75 changes: 75 additions & 0 deletions
75
tests/Testcontainers.Tests/Unit/Configurations/WaitUntilExternalTcpPortIsAvailable.cs
This file contains hidden or 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,75 @@ | ||
| namespace DotNet.Testcontainers.Tests.Unit | ||
| { | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using DotNet.Testcontainers.Builders; | ||
| using DotNet.Testcontainers.Commons; | ||
| using DotNet.Testcontainers.Configurations; | ||
| using DotNet.Testcontainers.Containers; | ||
| using Xunit; | ||
|
|
||
| public sealed class WaitUntilExternalTcpPortIsAvailable : IAsyncLifetime | ||
| { | ||
| private const ushort ListeningPort = 49152; | ||
|
|
||
| private const ushort MappedPort = 49153; | ||
|
|
||
| private const ushort UnmappedPort = 49154; | ||
|
|
||
| private readonly IContainer _container = new ContainerBuilder() | ||
| .WithImage(CommonImages.Socat) | ||
| .WithCommand("-v") | ||
| .WithCommand($"TCP-LISTEN:{ListeningPort},crlf,reuseaddr,fork") | ||
| .WithCommand("EXEC:cat") | ||
| .WithPortBinding(ListeningPort, true) | ||
| .WithPortBinding(MappedPort, true) | ||
| .WithWaitStrategy(Wait.ForUnixContainer().UntilExternalTcpPortIsAvailable(ListeningPort)) | ||
| .Build(); | ||
|
|
||
| public async ValueTask InitializeAsync() | ||
| { | ||
| await _container.StartAsync() | ||
| .ConfigureAwait(false); | ||
| } | ||
|
|
||
| public ValueTask DisposeAsync() | ||
| { | ||
| return _container.DisposeAsync(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SucceedsWhenPortIsMappedAndListening() | ||
| { | ||
| // Given | ||
| var waitStrategy = new UntilExternalTcpPortIsAvailable(ListeningPort); | ||
|
|
||
| // When | ||
| var success = await waitStrategy.UntilAsync(_container) | ||
| .ConfigureAwait(true); | ||
|
|
||
| // Then | ||
| Assert.True(success); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task SucceedsWhenPortIsMappedButNotListening() | ||
| { | ||
| // Given | ||
| var waitStrategy = new UntilExternalTcpPortIsAvailable(MappedPort); | ||
|
|
||
| // When | ||
| var success = await waitStrategy.UntilAsync(_container) | ||
| .ConfigureAwait(true); | ||
|
|
||
| // Then | ||
| Assert.True(success); | ||
| } | ||
|
|
||
| [Fact] | ||
| public Task ThrowsWhenPortIsNotMapped() | ||
| { | ||
| var waitStrategy = new UntilExternalTcpPortIsAvailable(UnmappedPort); | ||
| return Assert.ThrowsAsync<InvalidOperationException>(() => waitStrategy.UntilAsync(_container)); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.