diff --git a/CHANGELOG.md b/CHANGELOG.md index 34748391..0aab445a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## Release Notes +`1.3.3` + +--- + `1.3.2` - Added `Strings.nonBlankOrDefault(String, String)` to return a default when the value is blank. diff --git a/README.md b/README.md index 2b53316d..bd0fdada 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Support is being developed for other HTTP clients like Netty, Jersey, etc. If yo ### Releases -Current release `1.3.2` +Current release `1.3.3` ### Getting Started @@ -62,7 +62,7 @@ Maven: add this dependency to your `pom.xml` io.github.raduking apiphany - 1.3.2 + 1.3.3 ``` diff --git a/apiphany-core/pom.xml b/apiphany-core/pom.xml index 401b4494..206ae825 100644 --- a/apiphany-core/pom.xml +++ b/apiphany-core/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 apiphany diff --git a/apiphany-core/src/main/java/org/apiphany/security/oauth2/OAuth2TokenProviderRegistry.java b/apiphany-core/src/main/java/org/apiphany/security/oauth2/OAuth2TokenProviderRegistry.java index 3b7f9c39..4f2fbe00 100644 --- a/apiphany-core/src/main/java/org/apiphany/security/oauth2/OAuth2TokenProviderRegistry.java +++ b/apiphany-core/src/main/java/org/apiphany/security/oauth2/OAuth2TokenProviderRegistry.java @@ -158,6 +158,9 @@ public static OAuth2TokenProviderRegistry of(final OAuth2Properties oAuth2Proper /** * Adds a new OAuth2 token provider to the registry. + *

+ * If the registry is closed concurrently with this call, the provider is either closed by {@link #close()} or by this + * method (and the call throws), but it can be closed by both, so closing a token provider must be idempotent. * * @param name the name of the OAuth2 token provider * @param provider the OAuth2 token provider @@ -178,6 +181,13 @@ public void addProvider(final String name, final ScopedResource LOGGER.error("Error closing candidate OAuth2 token provider: '{}' when a provider with the same name exists.", name, e)); throw new IllegalStateException("An OAuth2 token provider with name '" + name + "' is already registered."); } + // close() could have finished iterating between the check above and the put, in which case nobody would ever close + // this provider, if close() also saw the provider it is closed twice, which is fine because close must be idempotent + if (closing.get()) { + provider.closeIfManaged( + e -> LOGGER.error("Error closing OAuth2 token provider: '{}' when adding to a closing registry.", name, e)); + throw new IllegalStateException("Cannot add new OAuth2 token provider " + name + " to a closing registry."); + } } /** diff --git a/apiphany-core/src/test/java/org/apiphany/security/oauth2/OAuth2TokenProviderRegistryTest.java b/apiphany-core/src/test/java/org/apiphany/security/oauth2/OAuth2TokenProviderRegistryTest.java index aea2950d..d234182e 100644 --- a/apiphany-core/src/test/java/org/apiphany/security/oauth2/OAuth2TokenProviderRegistryTest.java +++ b/apiphany-core/src/test/java/org/apiphany/security/oauth2/OAuth2TokenProviderRegistryTest.java @@ -15,22 +15,30 @@ import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; +import java.time.Duration; import java.time.Instant; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import java.util.function.BiConsumer; import java.util.function.Consumer; import org.apiphany.lang.Strings; +import org.apiphany.logging.slf4j.Slf4jLoggerAdapter; import org.apiphany.security.AuthenticationToken; import org.apiphany.security.AuthenticationTokenProvider; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.morphix.lang.function.LoggerAdapter; +import org.morphix.lang.function.ThrowingRunnable; import org.morphix.lang.resource.ScopedResource; +import org.morphix.lang.thread.Threads; +import org.morphix.reflection.Fields; /** * Test class for {@link OAuth2TokenProviderRegistry}. @@ -39,6 +47,8 @@ */ class OAuth2TokenProviderRegistryTest { + private static final LoggerAdapter LOGGER = Slf4jLoggerAdapter.of(OAuth2ClientRegistrationTest.class); + private static final String PROVIDER_NAME_MY_PROVIDER = "myProvider"; private static final String PROVIDER_NAME_GOOD = "good"; private static final String PROVIDER_NAME_BAD = "bad"; @@ -57,6 +67,7 @@ class OAuth2TokenProviderRegistryTest { private static final int THREADS = 200; private static final int CLOSE_COUNT = 10; + private static final long WAIT_SECONDS = 5; private static final long EXPIRES_IN = 300; private static final Instant DEFAULT_EXPIRATION = Instant.now(); @@ -818,6 +829,7 @@ void shouldBeThreadSafeForConcurrentAddAndClose() throws Exception { registry.addProvider(PROVIDER_NAME_MY_PROVIDER, resource); } catch (Exception ignored) { // expected exception when duplicate or closing + LOGGER.info("Expected: {}: {}", ignored.getClass().getName(), ignored.getMessage()); } finally { doneTasksGate.countDown(); } @@ -860,6 +872,46 @@ void shouldBeThreadSafeForConcurrentAddAndClose() throws Exception { } } + @Test + @SuppressWarnings({ "unchecked", "resource" }) + void shouldCloseProviderWhenCloseIsCalledWhileTheProviderIsBeingAdded() throws Exception { + OAuth2Registry mockRegistry = mock(OAuth2Registry.class); + OAuth2TokenProviderRegistry registry = OAuth2TokenProviderRegistry.of(mockRegistry); + + CountDownLatch addingGate = new CountDownLatch(1); + CountDownLatch continueAddGate = new CountDownLatch(1); + + // pauses the adding thread right after it checked that the registry is not closing + Map> pausingProviders = new ConcurrentHashMap<>() { + @Override + public ScopedResource putIfAbsent(final String key, final ScopedResource value) { + addingGate.countDown(); + Threads.safeWait(continueAddGate, Duration.ofSeconds(WAIT_SECONDS)); + return super.putIfAbsent(key, value); + } + }; + Fields.IgnoreAccess.set(registry, "providers", pausingProviders); + + ScopedResource tokenProvider = mock(ScopedResource.class); + doReturn(mock(OAuth2TokenProvider.class)).when(tokenProvider).unwrap(); + + Thread addThread = Thread.ofPlatform() + .start(() -> registry.addProvider(PROVIDER_NAME_MY_PROVIDER, tokenProvider)); + addingGate.await(WAIT_SECONDS, TimeUnit.SECONDS); + + Thread closeThread = Thread.ofPlatform() + .start(ThrowingRunnable.unchecked(() -> registry.close())); + // wait for close to finish iterating over an empty map before the provider is put in the map + closeThread.join(TimeUnit.SECONDS.toMillis(WAIT_SECONDS)); + continueAddGate.countDown(); + + addThread.join(TimeUnit.SECONDS.toMillis(WAIT_SECONDS)); + closeThread.join(TimeUnit.SECONDS.toMillis(WAIT_SECONDS)); + + assertThat(registry.getProviderNames(), contains(PROVIDER_NAME_MY_PROVIDER)); + verify(tokenProvider).closeIfManaged(any(Consumer.class)); + } + @Test @SuppressWarnings({ "unchecked", "resource" }) void shouldNotCallCloseOnAllProvidersEvenIfRegistryCloseIsCalledMultipleTimes() throws Exception { diff --git a/apiphany-httpclient5/pom.xml b/apiphany-httpclient5/pom.xml index 0bf01a48..00ef5ada 100644 --- a/apiphany-httpclient5/pom.xml +++ b/apiphany-httpclient5/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 apiphany-httpclient5 diff --git a/apiphany-integration-tests/pom.xml b/apiphany-integration-tests/pom.xml index b9e52eeb..e1eb1b77 100644 --- a/apiphany-integration-tests/pom.xml +++ b/apiphany-integration-tests/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 apiphany-integration-tests diff --git a/apiphany-okhttp/pom.xml b/apiphany-okhttp/pom.xml index cacfcfaa..e308bf9f 100644 --- a/apiphany-okhttp/pom.xml +++ b/apiphany-okhttp/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 apiphany-okhttp diff --git a/apiphany-spring/pom.xml b/apiphany-spring/pom.xml index 11195f8d..71622485 100644 --- a/apiphany-spring/pom.xml +++ b/apiphany-spring/pom.xml @@ -7,7 +7,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 apiphany-spring diff --git a/apiphany-test/pom.xml b/apiphany-test/pom.xml index df2f5dfb..da585969 100644 --- a/apiphany-test/pom.xml +++ b/apiphany-test/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 apiphany-test diff --git a/integration/apiphany-httpclient5-tests/pom.xml b/integration/apiphany-httpclient5-tests/pom.xml index 1efc9ecb..7c121ff1 100644 --- a/integration/apiphany-httpclient5-tests/pom.xml +++ b/integration/apiphany-httpclient5-tests/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 ../../pom.xml diff --git a/integration/apiphany-json-jackson2-tests/pom.xml b/integration/apiphany-json-jackson2-tests/pom.xml index 0336b8ee..485728f7 100644 --- a/integration/apiphany-json-jackson2-tests/pom.xml +++ b/integration/apiphany-json-jackson2-tests/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 ../../pom.xml diff --git a/integration/apiphany-json-jackson3-tests/pom.xml b/integration/apiphany-json-jackson3-tests/pom.xml index f9707b76..7b087765 100644 --- a/integration/apiphany-json-jackson3-tests/pom.xml +++ b/integration/apiphany-json-jackson3-tests/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 ../../pom.xml diff --git a/integration/apiphany-json-no-dependency-tests/pom.xml b/integration/apiphany-json-no-dependency-tests/pom.xml index 5e0803a9..a6ac5aa4 100644 --- a/integration/apiphany-json-no-dependency-tests/pom.xml +++ b/integration/apiphany-json-no-dependency-tests/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 ../../pom.xml diff --git a/integration/apiphany-okhttp-tests/pom.xml b/integration/apiphany-okhttp-tests/pom.xml index 90b6acb7..e283d937 100644 --- a/integration/apiphany-okhttp-tests/pom.xml +++ b/integration/apiphany-okhttp-tests/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 ../../pom.xml diff --git a/integration/apiphany-spring-no-client-tests/pom.xml b/integration/apiphany-spring-no-client-tests/pom.xml index 72f85be2..e89e443c 100644 --- a/integration/apiphany-spring-no-client-tests/pom.xml +++ b/integration/apiphany-spring-no-client-tests/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 ../../pom.xml diff --git a/integration/apiphany-spring-tests/pom.xml b/integration/apiphany-spring-tests/pom.xml index 19c7a847..278f9a8b 100644 --- a/integration/apiphany-spring-tests/pom.xml +++ b/integration/apiphany-spring-tests/pom.xml @@ -8,7 +8,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 ../../pom.xml diff --git a/pom.xml b/pom.xml index 313f3dae..01f21c45 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ io.github.raduking apiphany-parent - 1.3.2 + 1.3.3 pom ${project.groupId}:${project.artifactId}