Skip to content

Commit

Permalink
Use HttpConnectionProvider mutate method (reactor#2463)
Browse files Browse the repository at this point in the history
  • Loading branch information
yangbongsoo committed Sep 4, 2022
1 parent 8691df2 commit ddc92c1
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
Expand Up @@ -262,9 +262,8 @@ default Map<SocketAddress, Integer> maxConnectionsPerHost() {
* @return a builder to mutate properties of this {@link ConnectionProvider}
* @since 1.0.14
*/
@Nullable
default Builder mutate() {
return null;
return new Builder("");
}

/**
Expand Down
Expand Up @@ -56,6 +56,15 @@ else if (http1ConnectionProvider == null) {
}
}

@Override
public Builder mutate() {
if (this.http1ConnectionProvider == null) {
return ConnectionProvider.super.mutate();
}

return this.http1ConnectionProvider.mutate();
}

@Override
public void disposeWhen(SocketAddress address) {
http1ConnectionProvider().disposeWhen(address);
Expand Down
Expand Up @@ -92,4 +92,64 @@ void maxConnectionsPerHostDefaultConnectionProvider() {
.isEqualTo(HttpResources.get().getOrCreateHttp2ConnectionProvider(HTTP2_CONNECTION_PROVIDER_FACTORY)
.maxConnectionsPerHost());
}

@Test
void configuredConnectionProviderShouldReturnTheOriginalConnectionProvider() {
ConnectionProvider provider = ConnectionProvider.create("provider");
try {
HttpClient client = HttpClient.create(provider);
HttpConnectionProvider configuredConnectionProvider =
(HttpConnectionProvider) client.configuration().connectionProvider();
assertThat(provider.getClass().getName())
.isEqualTo(configuredConnectionProvider.http1ConnectionProvider.getClass().getName());
}
finally {
provider.disposeLater()
.block(Duration.ofSeconds(5));
}
}

@Test
void mergeConnectionProviderUsingMutate() {
ConnectionProvider beforeProvider = ConnectionProvider
.builder("beforeProvider")
.maxConnections(1)
.disposeTimeout(Duration.ofSeconds(1L))
.pendingAcquireTimeout(Duration.ofSeconds(1L))
.maxIdleTime(Duration.ofSeconds(1L))
.maxLifeTime(Duration.ofSeconds(10L))
.lifo()
.build();
HttpClient client1 = HttpClient.create(beforeProvider);

ConnectionProvider mergedProvider = client1.configuration().connectionProvider()
.mutate()
.pendingAcquireTimeout(Duration.ofSeconds(2L))
.maxIdleTime(Duration.ofSeconds(2L))
.maxLifeTime(Duration.ofSeconds(20L))
.build();

assertThat(beforeProvider.maxConnections()).isEqualTo(mergedProvider.maxConnections());
assertThat(beforeProvider.name()).isEqualTo(mergedProvider.name());

beforeProvider.disposeLater().block(Duration.ofSeconds(5));
mergedProvider.disposeLater().block(Duration.ofSeconds(5));
}

@Test
void mergeDefaultConnectionProviderUsingMutate() {
HttpClient client = HttpClient.create();

ConnectionProvider mergedProvider = client.configuration().connectionProvider()
.mutate()
.name("mergedProvider")
.pendingAcquireTimeout(Duration.ofSeconds(2L))
.maxIdleTime(Duration.ofSeconds(2L))
.maxLifeTime(Duration.ofSeconds(20L))
.build();

assertThat(mergedProvider.name()).isEqualTo("mergedProvider");

mergedProvider.disposeLater().block(Duration.ofSeconds(5));
}
}

0 comments on commit ddc92c1

Please sign in to comment.