Skip to content

Commit

Permalink
Fix java 21 build (#421)
Browse files Browse the repository at this point in the history
* Update common files

* build plugin to 6.6.0

* Revert "add import"

This reverts commit e9a0b15.

* Ensure lock is unlocked

* Poke CI

* Fix smells

---------

Co-authored-by: micronaut-build <[email protected]>
Co-authored-by: Tim Yates <[email protected]>
  • Loading branch information
3 people authored Oct 20, 2023
1 parent e62a711 commit cd86849
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 68 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ end_of_line = lf
[{*.bat,*.cmd}]
end_of_line = crlf

[{*.mustache,*.ftl}]
insert_final_newline = false

[*.java]
indent_size = 4
tab_width = 4
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/graalvm-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
max-parallel: 6
matrix: ${{ fromJson(needs.build_matrix.outputs.matrix) }}
matrix:
java: ['17', '21']
native_test_task: ${{ fromJson(needs.build_matrix.outputs.matrix).native_test_task }}
env:
GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
GRADLE_ENTERPRISE_CACHE_USERNAME: ${{ secrets.GRADLE_ENTERPRISE_CACHE_USERNAME }}
Expand All @@ -46,7 +48,7 @@ jobs:
id: pre-build
with:
distribution: 'graalvm'
java: '17'
java: ${{ matrix.java }}
- name: Build Steps
uses: micronaut-projects/github-actions/graalvm/build@master
id: build
Expand All @@ -60,4 +62,4 @@ jobs:
uses: micronaut-projects/github-actions/graalvm/post-build@master
id: post-build
with:
java: '17'
java: ${{ matrix.java }}
4 changes: 2 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: ['17']
java: ['17', '21']
env:
GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}
GRADLE_ENTERPRISE_CACHE_USERNAME: ${{ secrets.GRADLE_ENTERPRISE_CACHE_USERNAME }}
Expand Down Expand Up @@ -64,7 +64,7 @@ jobs:
./gradlew check --no-daemon --continue
- name: "🔎 Run static analysis"
if: env.SONAR_TOKEN != ''
if: env.SONAR_TOKEN != '' && matrix.java == '17'
run: |
./gradlew sonar
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pluginManagement {
}

plugins {
id 'io.micronaut.build.shared.settings' version '6.5.6'
id 'io.micronaut.build.shared.settings' version '6.6.0'
}
rootProject.name = 'testresources-parent'

Expand Down
2 changes: 2 additions & 0 deletions test-resources-server/src/test/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@
<root level="info">
<appender-ref ref="STDOUT" />
</root>
<logger name="io.micronaut.testresources.server" level="debug" />
<logger name="io.micronaut.testresources.testcontainers" level="trace" />
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ private TestContainers() {

}

private static <B> B withLock(String description, Supplier<B> supplier) {
LOCK.lock();
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Locked for {}", description);
}
try {
return supplier.get();
} finally {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Unlocked for {}", description);
}
LOCK.unlock();
}
}

/**
* Returns a test container and caches it, so that if the same owner
* and properties are requested, we can return an existing container.
Expand All @@ -71,19 +86,18 @@ static <T extends GenericContainer<? extends T>> T getOrCreate(String requestedP
Map<String, Object> query,
Supplier<T> creator) {
Key key = Key.of(owner, name, Scope.from(query), query);
LOCK.lock();
@SuppressWarnings("unchecked")
T container = (T) CONTAINERS_BY_KEY.get(key);
if (container == null) {
container = creator.get();
LOGGER.info("Starting test container {}", name);
container.start();
CONTAINERS_BY_KEY.put(key, container);
}
CONTAINERS_BY_PROPERTY.computeIfAbsent(requestedProperty, e -> new LinkedHashSet<>())
.add(container);
LOCK.unlock();
return container;
return withLock("getOrCreate", () -> {
T container = (T) CONTAINERS_BY_KEY.get(key);
if (container == null) {
container = creator.get();
LOGGER.info("Starting test container {}", name);
container.start();
CONTAINERS_BY_KEY.put(key, container);
}
CONTAINERS_BY_PROPERTY.computeIfAbsent(requestedProperty, e -> new LinkedHashSet<>())
.add(container);
return container;
});
}

/**
Expand All @@ -101,9 +115,8 @@ public static Map<Scope, List<GenericContainer<?>>> listByScope(String id) {
}

private static Map<Scope, List<GenericContainer<?>>> listByScope(Scope scope) {
LOCK.lock();
try {
return CONTAINERS_BY_KEY.entrySet()
return withLock("listByScope", () ->
CONTAINERS_BY_KEY.entrySet()
.stream()
.filter(entry -> scope.includes(entry.getKey().scope))
.collect(Collectors.groupingBy(
Expand All @@ -112,45 +125,41 @@ private static Map<Scope, List<GenericContainer<?>>> listByScope(Scope scope) {
Map.Entry::getValue,
Collectors.toList()
)
));
} finally {
LOCK.unlock();
}
))
);
}

@SuppressWarnings("java:S6204") // toList() breaks the return type
private static List<GenericContainer<?>> filterByScope(Scope scope, Set<GenericContainer<?>> containers) {
if (containers.isEmpty()) {
return Collections.emptyList();
}
LOCK.lock();
try {
return CONTAINERS_BY_KEY.entrySet()
return withLock("filterByScope", () ->
CONTAINERS_BY_KEY.entrySet()
.stream()
.filter(entry -> containers.contains(entry.getValue()) && scope.includes(entry.getKey().scope))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
} finally {
LOCK.unlock();
}
.collect(Collectors.toList())
);
}

public static Network network(String name) {
return NETWORKS_BY_KEY.computeIfAbsent(name, k -> Network.newNetwork());
}

public static boolean closeAll() {
LOCK.lock();
boolean closed = false;
for (GenericContainer<?> container : CONTAINERS_BY_KEY.values()) {
container.close();
closed = true;
}
CONTAINERS_BY_KEY.clear();
CONTAINERS_BY_PROPERTY.clear();
NETWORKS_BY_KEY.values().forEach(Network::close);
NETWORKS_BY_KEY.clear();
LOCK.unlock();
return closed;
return withLock("closeAll", () -> {
boolean closed = false;
for (GenericContainer<?> container : CONTAINERS_BY_KEY.values()) {
container.close();
closed = true;
}
CONTAINERS_BY_KEY.clear();
CONTAINERS_BY_PROPERTY.clear();
NETWORKS_BY_KEY.values().forEach(Network::close);
NETWORKS_BY_KEY.clear();
return closed;
});
}

public static Map<String, Network> getNetworks() {
Expand All @@ -159,44 +168,41 @@ public static Map<String, Network> getNetworks() {

public static boolean closeScope(String id) {
Scope scope = Scope.of(id);
LOCK.lock();
boolean closed = false;
Iterator<Map.Entry<Key, GenericContainer<?>>> iterator = CONTAINERS_BY_KEY.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Key, GenericContainer<?>> entry = iterator.next();
var existingScope = entry.getKey().scope;
if (scope.includes(existingScope)) {
iterator.remove();
GenericContainer<?> container = entry.getValue();
LOGGER.debug("Stopping container {}", container.getContainerId());
container.close();
closed = true;
for (Set<GenericContainer<?>> value : CONTAINERS_BY_PROPERTY.values()) {
value.remove(container);
return withLock("closeScope", () -> {
boolean closed = false;
Iterator<Map.Entry<Key, GenericContainer<?>>> iterator = CONTAINERS_BY_KEY.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<Key, GenericContainer<?>> entry = iterator.next();
var existingScope = entry.getKey().scope;
if (scope.includes(existingScope)) {
iterator.remove();
GenericContainer<?> container = entry.getValue();
LOGGER.debug("Stopping container {}", container.getContainerId());
container.close();
closed = true;
for (Set<GenericContainer<?>> value : CONTAINERS_BY_PROPERTY.values()) {
value.remove(container);
}
}
}
}
LOCK.unlock();
return closed;
return closed;
});
}

public static List<GenericContainer<?>> findByRequestedProperty(Scope scope, String property) {
LOCK.lock();
try {
return withLock("findByRequestedProperty", () -> {
Set<GenericContainer<?>> byProperty = CONTAINERS_BY_PROPERTY.getOrDefault(property, Collections.emptySet());
LOGGER.debug("Found {} containers for property {}. All properties: {}", byProperty.size(), property, CONTAINERS_BY_PROPERTY.keySet());
return filterByScope(scope, byProperty);
} finally {
LOCK.unlock();
}
});
}

private static final class Key {
private final Class<?> type;
private final String name;
private final Scope scope;
private final Map<String, String> properties;
private final int hashCode;
final Scope scope;

private Key(Class<?> type, String name, Scope scope, Map<String, String> properties) {
this.type = type;
Expand Down

0 comments on commit cd86849

Please sign in to comment.