-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix deadlock when ryuk does not acknowledge filters (#843)
* Fix deadlock when ryuk does not acknowledge filters * Extract FilterRegistry in ResourceReaper for unit-testability * Use explicit scoping for FilterRegistry#register * Restructure tests * Update CHANGELOG.md
- Loading branch information
Showing
2 changed files
with
126 additions
and
20 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
70 changes: 70 additions & 0 deletions
70
core/src/test/java/org/testcontainers/utility/FilterRegistryTest.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,70 @@ | ||
package org.testcontainers.utility; | ||
|
||
import org.junit.Test; | ||
import org.testcontainers.utility.ResourceReaper.FilterRegistry; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.AbstractMap.SimpleEntry; | ||
import java.util.List; | ||
import java.util.Map.Entry; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class FilterRegistryTest { | ||
|
||
private static final List<Entry<String, String>> FILTERS = asList( | ||
new SimpleEntry<>("key1!", "value2?"), new SimpleEntry<>("key2#", "value2%") | ||
); | ||
private static final String URL_ENCODED_FILTERS = "key1%21=value2%3F&key2%23=value2%25"; | ||
private static final byte[] ACKNOWLEDGEMENT = FilterRegistry.ACKNOWLEDGMENT.getBytes(); | ||
private static final byte[] NO_ACKNOWLEDGEMENT = "".getBytes(); | ||
private static final String NEW_LINE = "\n"; | ||
|
||
@Test | ||
public void registerReturnsTrueIfAcknowledgementIsReadFromInputStream() throws IOException { | ||
FilterRegistry registry = new FilterRegistry(inputStream(ACKNOWLEDGEMENT), anyOutputStream()); | ||
|
||
boolean successful = registry.register(FILTERS); | ||
|
||
assertTrue(successful); | ||
} | ||
|
||
@Test | ||
public void registerReturnsFalseIfNoAcknowledgementIsReadFromInputStream() throws IOException { | ||
FilterRegistry registry = new FilterRegistry(inputStream(NO_ACKNOWLEDGEMENT), anyOutputStream()); | ||
|
||
boolean successful = registry.register(FILTERS); | ||
|
||
assertFalse(successful); | ||
} | ||
|
||
@Test | ||
public void registerWritesUrlEncodedFiltersAndNewlineToOutputStream() throws IOException { | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
FilterRegistry registry = new FilterRegistry(anyInputStream(), outputStream); | ||
|
||
registry.register(FILTERS); | ||
|
||
assertEquals(URL_ENCODED_FILTERS + NEW_LINE, new String(outputStream.toByteArray())); | ||
} | ||
|
||
private static InputStream inputStream(byte[] bytes) { | ||
return new ByteArrayInputStream(bytes); | ||
} | ||
|
||
private static InputStream anyInputStream() { | ||
return inputStream(ACKNOWLEDGEMENT); | ||
} | ||
|
||
private static OutputStream anyOutputStream() { | ||
return new ByteArrayOutputStream(); | ||
} | ||
|
||
} |