Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -62,7 +62,7 @@ Maven: add this dependency to your `pom.xml`
<dependency>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion apiphany-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</parent>

<artifactId>apiphany</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public static OAuth2TokenProviderRegistry of(final OAuth2Properties oAuth2Proper

/**
* Adds a new OAuth2 token provider to the registry.
* <p>
* 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
Expand All @@ -178,6 +181,13 @@ public void addProvider(final String name, final ScopedResource<OAuth2TokenProvi
e -> 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.");
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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";
Expand All @@ -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();
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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<String, ScopedResource<OAuth2TokenProvider>> pausingProviders = new ConcurrentHashMap<>() {
@Override
public ScopedResource<OAuth2TokenProvider> putIfAbsent(final String key, final ScopedResource<OAuth2TokenProvider> value) {
addingGate.countDown();
Threads.safeWait(continueAddGate, Duration.ofSeconds(WAIT_SECONDS));
return super.putIfAbsent(key, value);
}
};
Fields.IgnoreAccess.set(registry, "providers", pausingProviders);

ScopedResource<OAuth2TokenProvider> 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 {
Expand Down
2 changes: 1 addition & 1 deletion apiphany-httpclient5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</parent>

<artifactId>apiphany-httpclient5</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion apiphany-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</parent>

<artifactId>apiphany-integration-tests</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion apiphany-okhttp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</parent>

<artifactId>apiphany-okhttp</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion apiphany-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</parent>

<artifactId>apiphany-spring</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion apiphany-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
</parent>

<artifactId>apiphany-test</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion integration/apiphany-httpclient5-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion integration/apiphany-json-jackson2-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion integration/apiphany-json-jackson3-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion integration/apiphany-json-no-dependency-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion integration/apiphany-okhttp-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion integration/apiphany-spring-no-client-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion integration/apiphany-spring-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<groupId>io.github.raduking</groupId>
<artifactId>apiphany-parent</artifactId>
<version>1.3.2</version>
<version>1.3.3</version>
<packaging>pom</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down