This is an automated email from the ASF dual-hosted git repository.
ffang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new 16c63e353df Close the previous client in the failover strategy tests
(#3656)
16c63e353df is described below
commit 16c63e353df25407d228b61ad5ed126860df16ee
Author: Freeman(Yue) Fang <[email protected]>
AuthorDate: Fri Oct 30 21:55:19 2026 -0400
Close the previous client in the failover strategy tests (#3656)
* Close the previous client in the failover strategy tests
AbstractFailoverTest.strategyTest() and strategyTestWebClient() create 20
clients from one FailoverFeature and never close them. A feature hands the
same target selector to every client it is applied to, so when the garbage
collector finalizes one of the earlier clients, AbstractClient.close()
closes
that shared selector, and with it the conduit the current client is using.
The
request in flight then fails with "IOException: shutdownNow", or the
failover
strategy moves on to another address, and CircuitBreakerFailoverTest,
FailoverTest and LoadDistributorTest fail intermittently.
Close the previous client before creating the next one, so that no client is
left for the finalizer to close.
* Close the WebClient of each failover strategy test iteration with
try-with-resources
* Close the proxy of each failover strategy test iteration with
try-with-resources
---
.../jaxrs/failover/AbstractFailoverTest.java | 131 +++++++++++----------
.../jaxrs/failover/LoadDistributorTest.java | 27 +++--
2 files changed, 83 insertions(+), 75 deletions(-)
diff --git
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/AbstractFailoverTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/AbstractFailoverTest.java
index 0ed8a266244..79703625919 100644
---
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/AbstractFailoverTest.java
+++
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/AbstractFailoverTest.java
@@ -36,6 +36,7 @@ import org.apache.cxf.clustering.RandomStrategy;
import org.apache.cxf.clustering.RetryStrategy;
import org.apache.cxf.clustering.SequentialStrategy;
import org.apache.cxf.endpoint.ConduitSelector;
+import org.apache.cxf.jaxrs.client.Client;
import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.provider.ProviderFactory;
@@ -241,44 +242,47 @@ public abstract class AbstractFailoverTest extends
AbstractBusClientServerTestBa
feature.getTargetSelector().close();
bookStore = getBookStore(inactiveReplica, feature);
}
- verifyStrategy(bookStore, expectRandom
- ? RandomStrategy.class
- : SequentialStrategy.class);
- Exception ex = null;
- try {
- if (expectServerException) {
- bookStore.getBook("9999");
- fail("Exception expected");
+ // a single proxy is used by all iterations, so only a proxy of
this iteration is closed
+ try (Client client = singleProxy ? null :
WebClient.client(bookStore)) {
+ verifyStrategy(bookStore, expectRandom
+ ? RandomStrategy.class
+ : SequentialStrategy.class);
+ Exception ex = null;
+ try {
+ if (expectServerException) {
+ bookStore.getBook("9999");
+ fail("Exception expected");
+ } else {
+ final long bookId = 123L;
+ Book book = bookStore.echoBookElementJson(new
Book("CXF", bookId));
+ assertNotNull("expected non-null response", book);
+ assertEquals("unexpected id", bookId, book.getId());
+ }
+ } catch (Exception error) {
+ if (!expectServerException) {
+ //String currEndpoint =
getCurrentEndpointAddress(bookStore);
+ //assertEquals(currEndpoint, inactiveReplica);
+ throw error;
+ }
+ ex = error;
+ }
+ String currEndpoint = getCurrentEndpointAddress(bookStore);
+ assertNotEquals(currEndpoint, inactiveReplica);
+ if (expectRandom) {
+ assertTrue(currEndpoint.equals(activeReplica1) ||
currEndpoint.equals(activeReplica2));
} else {
- final long bookId = 123L;
- Book book = bookStore.echoBookElementJson(new Book("CXF",
bookId));
- assertNotNull("expected non-null response", book);
- assertEquals("unexpected id", bookId, book.getId());
+ assertEquals(activeReplica1, currEndpoint);
}
- } catch (Exception error) {
- if (!expectServerException) {
- //String currEndpoint =
getCurrentEndpointAddress(bookStore);
- //assertEquals(currEndpoint, inactiveReplica);
- throw error;
+ if (expectServerException) {
+ assertNotNull(ex);
+ throw ex;
}
- ex = error;
- }
- String currEndpoint = getCurrentEndpointAddress(bookStore);
- assertNotEquals(currEndpoint, inactiveReplica);
- if (expectRandom) {
- assertTrue(currEndpoint.equals(activeReplica1) ||
currEndpoint.equals(activeReplica2));
- } else {
- assertEquals(activeReplica1, currEndpoint);
- }
- if (expectServerException) {
- assertNotNull(ex);
- throw ex;
- }
- if (!(prevEndpoint == null || currEndpoint.equals(prevEndpoint))) {
- randomized = true;
+ if (!(prevEndpoint == null ||
currEndpoint.equals(prevEndpoint))) {
+ randomized = true;
+ }
+ prevEndpoint = currEndpoint;
}
- prevEndpoint = currEndpoint;
}
if (!singleProxy) {
assertEquals("unexpected random/sequential distribution of
failovers",
@@ -297,39 +301,40 @@ public abstract class AbstractFailoverTest extends
AbstractBusClientServerTestBa
String prevEndpoint = null;
for (int i = 0; i < 20; i++) {
feature.getTargetSelector().close();
- WebClient bookStore = getWebClient(inactiveReplica, feature);
- verifyStrategy(bookStore, expectRandom
- ? RandomStrategy.class
- : SequentialStrategy.class);
- String bookId = expectServerException ? "9999" : "123";
- bookStore.path("bookstore/books").path(bookId);
- Exception ex = null;
- try {
- Book book = bookStore.get(Book.class);
- assertNotNull("expected non-null response", book);
- assertEquals("unexpected id", 123L, book.getId());
- } catch (Exception error) {
- if (!expectServerException) {
- throw error;
+ try (WebClient bookStore = getWebClient(inactiveReplica, feature))
{
+ verifyStrategy(bookStore, expectRandom
+ ? RandomStrategy.class
+ : SequentialStrategy.class);
+ String bookId = expectServerException ? "9999" : "123";
+ bookStore.path("bookstore/books").path(bookId);
+ Exception ex = null;
+ try {
+ Book book = bookStore.get(Book.class);
+ assertNotNull("expected non-null response", book);
+ assertEquals("unexpected id", 123L, book.getId());
+ } catch (Exception error) {
+ if (!expectServerException) {
+ throw error;
+ }
+ ex = error;
+ }
+ String currEndpoint = getCurrentEndpointAddress(bookStore);
+ assertNotEquals(currEndpoint, inactiveReplica);
+ if (expectRandom) {
+ assertTrue(currEndpoint.equals(activeReplica1) ||
currEndpoint.equals(activeReplica2));
+ } else {
+ assertEquals(currEndpoint, activeReplica1);
+ }
+ if (expectServerException) {
+ assertNotNull(ex);
+ throw ex;
}
- ex = error;
- }
- String currEndpoint = getCurrentEndpointAddress(bookStore);
- assertNotEquals(currEndpoint, inactiveReplica);
- if (expectRandom) {
- assertTrue(currEndpoint.equals(activeReplica1) ||
currEndpoint.equals(activeReplica2));
- } else {
- assertEquals(currEndpoint, activeReplica1);
- }
- if (expectServerException) {
- assertNotNull(ex);
- throw ex;
- }
- if (!(prevEndpoint == null || currEndpoint.equals(prevEndpoint))) {
- randomized = true;
+ if (!(prevEndpoint == null ||
currEndpoint.equals(prevEndpoint))) {
+ randomized = true;
+ }
+ prevEndpoint = currEndpoint;
}
- prevEndpoint = currEndpoint;
}
assertEquals("unexpected random/sequential distribution of failovers",
expectRandom,
diff --git
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/LoadDistributorTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/LoadDistributorTest.java
index cb83f476e29..bcd94db0320 100644
---
a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/LoadDistributorTest.java
+++
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/failover/LoadDistributorTest.java
@@ -32,6 +32,7 @@ import
org.apache.cxf.clustering.LoadDistributorTargetSelector;
import org.apache.cxf.clustering.SequentialStrategy;
import org.apache.cxf.endpoint.ConduitSelector;
import org.apache.cxf.feature.AbstractFeature;
+import org.apache.cxf.jaxrs.client.Client;
import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.provider.ProviderFactory;
@@ -152,18 +153,20 @@ public class LoadDistributorTest extends
AbstractBusClientServerTestBase {
int address3Count = 0;
for (int i = 0; i < 20; i++) {
BookStore bookStore = getBookStore(initialAddress, feature);
- verifyStrategy(bookStore, SequentialStrategy.class);
- String bookId = "123";
-
- Book book = bookStore.getBook(bookId);
- assertNotNull("expected non-null response", book);
- assertEquals("unexpected id", 123L, book.getId());
-
- String address = getCurrentEndpointAddress(bookStore);
- if (Server.ADDRESS2.equals(address)) {
- address2Count++;
- } else if (Server.ADDRESS3.equals(address)) {
- address3Count++;
+ try (Client client = WebClient.client(bookStore)) {
+ verifyStrategy(bookStore, SequentialStrategy.class);
+ String bookId = "123";
+
+ Book book = bookStore.getBook(bookId);
+ assertNotNull("expected non-null response", book);
+ assertEquals("unexpected id", 123L, book.getId());
+
+ String address = getCurrentEndpointAddress(bookStore);
+ if (Server.ADDRESS2.equals(address)) {
+ address2Count++;
+ } else if (Server.ADDRESS3.equals(address)) {
+ address3Count++;
+ }
}
}
assertEquals(10, address2Count);