forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bug]: duct-tape:1.0.8 contains a Thread Leak
1st step: move code from the archived repository Related to testcontainers#9227.
- Loading branch information
Showing
45 changed files
with
455 additions
and
57 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
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
4 changes: 2 additions & 2 deletions
4
core/src/main/java/org/testcontainers/containers/wait/strategy/AbstractWaitStrategy.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
4 changes: 2 additions & 2 deletions
4
.../main/java/org/testcontainers/containers/wait/strategy/DockerHealthcheckWaitStrategy.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
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
4 changes: 2 additions & 2 deletions
4
core/src/main/java/org/testcontainers/containers/wait/strategy/ShellStrategy.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
2 changes: 1 addition & 1 deletion
2
core/src/main/java/org/testcontainers/containers/wait/strategy/WaitAllStrategy.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
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
27 changes: 27 additions & 0 deletions
27
core/src/main/java/org/testcontainers/utility/ducttape/ConstantThroughputRateLimiter.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,27 @@ | ||
package org.testcontainers.utility.ducttape; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* A rate limiter that uses a simple 'run every n millis' strategy to achieve constant throughput. | ||
* This code comes from <a href="https://github.com/rnorth/duct-tape/">rnorth/duct-tape</a> | ||
*/ | ||
class ConstantThroughputRateLimiter extends RateLimiter { | ||
|
||
private final long timeBetweenInvocations; | ||
|
||
ConstantThroughputRateLimiter(@NotNull Integer rate, @NotNull TimeUnit perTimeUnit) { | ||
this.timeBetweenInvocations = perTimeUnit.toMillis(1) / rate; | ||
} | ||
|
||
@Override | ||
protected long getWaitBeforeNextInvocation() { | ||
|
||
long timeToNextAllowed = (lastInvocation + timeBetweenInvocations) - System.currentTimeMillis(); | ||
|
||
// Clamp wait time to 0< | ||
return Math.max(timeToNextAllowed, 0); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
core/src/main/java/org/testcontainers/utility/ducttape/Preconditions.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,19 @@ | ||
package org.testcontainers.utility.ducttape; | ||
|
||
/** | ||
* Simple Preconditions check implementation. | ||
* This code comes from <a href="https://github.com/rnorth/duct-tape/">rnorth/duct-tape</a> | ||
*/ | ||
public class Preconditions { | ||
|
||
/** | ||
* Check that a given condition is true. Will throw an IllegalArgumentException otherwise. | ||
* @param message message to display if the precondition check fails | ||
* @param condition the result of evaluating the condition | ||
*/ | ||
public static void check(String message, boolean condition) { | ||
if (!condition) { | ||
throw new IllegalArgumentException("Precondition failed: " + message); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
core/src/main/java/org/testcontainers/utility/ducttape/RateLimiter.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,59 @@ | ||
package org.testcontainers.utility.ducttape; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.concurrent.Callable; | ||
|
||
/** | ||
* Base class for rate limiters. Use RateLimiterBuilder to build new instances. | ||
* This code comes from <a href="https://github.com/rnorth/duct-tape/">rnorth/duct-tape</a> | ||
*/ | ||
public abstract class RateLimiter { | ||
|
||
protected long lastInvocation; | ||
|
||
/** | ||
* Invoke a lambda function, with Thread.sleep() being called to limit the execution rate if needed. | ||
* @param lambda a Runnable lamda function to invoke | ||
*/ | ||
public void doWhenReady(@NotNull final Runnable lambda) { | ||
|
||
// Wait before proceeding, if needed | ||
long waitBeforeNextInvocation = getWaitBeforeNextInvocation(); | ||
try { | ||
Thread.sleep(waitBeforeNextInvocation); | ||
} catch (InterruptedException ignored) { } | ||
|
||
try { | ||
lambda.run(); | ||
} finally { | ||
lastInvocation = System.currentTimeMillis(); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* Invoke a lambda function and get the result, with Thread.sleep() being called to limit the execution rate | ||
* if needed. | ||
* @param lambda a Callable lamda function to invoke | ||
* @param <T> return type of the lamda | ||
* @throws Exception rethrown from lambda | ||
* @return result of the lambda call | ||
*/ | ||
public <T> T getWhenReady(@NotNull final Callable<T> lambda) throws Exception { | ||
|
||
// Wait before proceeding, if needed | ||
long waitBeforeNextInvocation = getWaitBeforeNextInvocation(); | ||
try { | ||
Thread.sleep(waitBeforeNextInvocation); | ||
} catch (InterruptedException ignored) { } | ||
|
||
try { | ||
return lambda.call(); | ||
} finally { | ||
lastInvocation = System.currentTimeMillis(); | ||
} | ||
} | ||
|
||
protected abstract long getWaitBeforeNextInvocation(); | ||
} |
68 changes: 68 additions & 0 deletions
68
core/src/main/java/org/testcontainers/utility/ducttape/RateLimiterBuilder.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,68 @@ | ||
package org.testcontainers.utility.ducttape; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.testcontainers.utility.ducttape.Preconditions.check; | ||
|
||
/** | ||
* Builder for rate limiters. | ||
* This code comes from <a href="https://github.com/rnorth/duct-tape/">rnorth/duct-tape</a> | ||
*/ | ||
public class RateLimiterBuilder { | ||
|
||
private Integer invocations; | ||
private TimeUnit perTimeUnit; | ||
private RateLimiterStrategy strategy; | ||
|
||
private RateLimiterBuilder() { } | ||
|
||
/** | ||
* Obtain a new builder instance. | ||
* @return a new builder | ||
*/ | ||
public static RateLimiterBuilder newBuilder() { | ||
return new RateLimiterBuilder(); | ||
} | ||
|
||
/** | ||
* Set the maximum rate that the limiter should allow, expressed as the number of invocations | ||
* allowed in a given time period. | ||
* @param invocations number of invocations | ||
* @param perTimeUnit the time period in which this number of invocations are allowed | ||
* @return the builder | ||
*/ | ||
public RateLimiterBuilder withRate(final int invocations, final TimeUnit perTimeUnit) { | ||
this.invocations = invocations; | ||
this.perTimeUnit = perTimeUnit; | ||
return this; | ||
} | ||
|
||
/** | ||
* Configure the rate limiter to use a constant throughput strategy for rate limiting. | ||
* @return the builder | ||
*/ | ||
public RateLimiterBuilder withConstantThroughput() { | ||
this.strategy = RateLimiterStrategy.CONSTANT_THROUGHPUT; | ||
return this; | ||
} | ||
|
||
/** | ||
* Build and obtain a configured rate limiter. A rate and rate limiting strategy must have been selected. | ||
* @return the configured rate limiter instance | ||
*/ | ||
public RateLimiter build() { | ||
check("A rate must be set", invocations != null); | ||
check("A rate must be set", perTimeUnit != null); | ||
check("A rate limit strategy must be set", strategy != null); | ||
|
||
if (strategy == RateLimiterStrategy.CONSTANT_THROUGHPUT) { | ||
return new ConstantThroughputRateLimiter(invocations, perTimeUnit); | ||
} else { | ||
throw new IllegalStateException(); | ||
} | ||
} | ||
|
||
private enum RateLimiterStrategy { | ||
CONSTANT_THROUGHPUT | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
core/src/main/java/org/testcontainers/utility/ducttape/RetryCountExceededException.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,12 @@ | ||
package org.testcontainers.utility.ducttape; | ||
|
||
/** | ||
* Indicates repeated failure of an UnreliableSupplier | ||
* This code comes from <a href="https://github.com/rnorth/duct-tape/">rnorth/duct-tape</a> | ||
*/ | ||
public class RetryCountExceededException extends RuntimeException { | ||
|
||
public RetryCountExceededException(String message, Exception exception) { | ||
super(message, exception); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
core/src/main/java/org/testcontainers/utility/ducttape/TimeoutException.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,16 @@ | ||
package org.testcontainers.utility.ducttape; | ||
|
||
/** | ||
* Indicates timeout of an UnreliableSupplier | ||
* This code comes from <a href="https://github.com/rnorth/duct-tape/">rnorth/duct-tape</a> | ||
*/ | ||
public class TimeoutException extends RuntimeException { | ||
|
||
public TimeoutException(String message, Exception exception) { | ||
super(message, exception); | ||
} | ||
|
||
public TimeoutException(Exception e) { | ||
super(e); | ||
} | ||
} |
Oops, something went wrong.