diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java index b0dc77d95..222e4570d 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingExec.java @@ -143,7 +143,7 @@ public class CachingExec implements ClientExecChain { this.responseCachingPolicy = new ResponseCachingPolicy( this.cacheConfig.getMaxObjectSize(), this.cacheConfig.isSharedCache(), this.cacheConfig.isNeverCacheHTTP10ResponsesWithQuery()); - this.asynchRevalidator = asynchRevalidator != null ? asynchRevalidator : makeAsynchronousValidator(config); + this.asynchRevalidator = asynchRevalidator; } public CachingExec( @@ -169,7 +169,8 @@ public class CachingExec implements ClientExecChain { final ConditionalRequestBuilder conditionalRequestBuilder, final ResponseProtocolCompliance responseCompliance, final RequestProtocolCompliance requestCompliance, - final CacheConfig config) { + final CacheConfig config, + final AsynchronousValidator asynchRevalidator) { this.cacheConfig = config != null ? config : CacheConfig.DEFAULT; this.backend = backend; this.responseCache = responseCache; @@ -181,15 +182,7 @@ public class CachingExec implements ClientExecChain { this.conditionalRequestBuilder = conditionalRequestBuilder; this.responseCompliance = responseCompliance; this.requestCompliance = requestCompliance; - this.asynchRevalidator = makeAsynchronousValidator(config); - } - - private AsynchronousValidator makeAsynchronousValidator( - final CacheConfig config) { - if (config.getAsynchronousWorkersMax() > 0) { - return new AsynchronousValidator(config); - } - return null; + this.asynchRevalidator = asynchRevalidator; } /** diff --git a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java index e3aa6e31c..cbaa38384 100644 --- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java +++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/CachingHttpClientBuilder.java @@ -103,9 +103,12 @@ public class CachingHttpClientBuilder extends HttpClientBuilder { } private AsynchronousValidator createAsynchronousRevalidator(final CacheConfig config) { - final AsynchronousValidator revalidator = new AsynchronousValidator(config); - addCloseable(revalidator); - return revalidator; + if (config.getAsynchronousWorkersMax() > 0) { + final AsynchronousValidator revalidator = new AsynchronousValidator(config); + addCloseable(revalidator); + return revalidator; + } + return null; } } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java index c82601f8d..b197e94d7 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingExec.java @@ -120,6 +120,7 @@ public class TestCachingExec { private ResponseProtocolCompliance mockResponseProtocolCompliance; private RequestProtocolCompliance mockRequestProtocolCompliance; private CacheConfig config; + private AsynchronousValidator asyncValidator; private Date requestDate; private Date responseDate; @@ -151,6 +152,7 @@ public class TestCachingExec { mockRequestProtocolCompliance = createNiceMock(RequestProtocolCompliance.class); mockStorage = createNiceMock(HttpCacheStorage.class); config = CacheConfig.DEFAULT; + asyncValidator = new AsynchronousValidator(config); requestDate = new Date(System.currentTimeMillis() - 1000); responseDate = new Date(); @@ -171,7 +173,8 @@ public class TestCachingExec { mockConditionalRequestBuilder, mockResponseProtocolCompliance, mockRequestProtocolCompliance, - config); + config, + asyncValidator); } private void replayMocks() { @@ -1916,7 +1919,8 @@ public class TestCachingExec { mockConditionalRequestBuilder, mockResponseProtocolCompliance, mockRequestProtocolCompliance, - config).addMockedMethods(methods).createNiceMock(); + config, + asyncValidator).addMockedMethods(methods).createNiceMock(); } } diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClientBuilder.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClientBuilder.java new file mode 100755 index 000000000..1ec666835 --- /dev/null +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestCachingHttpClientBuilder.java @@ -0,0 +1,41 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ +package org.apache.http.impl.client.cache; + +import org.junit.Test; + +public class TestCachingHttpClientBuilder { + + @Test + public void testAsynchronousWorkersMax0() throws Exception { + CacheConfig cacheConfig = CacheConfig.custom().setAsynchronousWorkersMax(0).build(); + // Asynchronous validation should be disabled but we should not get an + // Exception + CachingHttpClientBuilder.create().setCacheConfig(cacheConfig).build(); + } + +} diff --git a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java index 78245667f..ff35f639f 100644 --- a/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java +++ b/httpclient-cache/src/test/java/org/apache/http/impl/client/cache/TestRFC5861Compliance.java @@ -307,7 +307,7 @@ public class TestRFC5861Compliance extends AbstractProtocolTest { .setAsynchronousWorkersMax(1) .build(); - impl = new CachingExec(mockBackend, cache, config); + impl = new CachingExec(mockBackend, cache, config, new AsynchronousValidator(config)); final HttpRequestWrapper req1 = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1)); @@ -351,7 +351,7 @@ public class TestRFC5861Compliance extends AbstractProtocolTest { .setAsynchronousWorkersMax(1) .setSharedCache(false) .build(); - impl = new CachingExec(mockBackend, cache, config); + impl = new CachingExec(mockBackend, cache, config, new AsynchronousValidator(config)); final HttpRequestWrapper req1 = HttpRequestWrapper.wrap( new BasicHttpRequest("GET", "/", HttpVersion.HTTP_1_1));