Search in sources :

Example 1 with BookStore

use of org.apache.cxf.systest.jaxrs.BookStore in project cxf by apache.

the class AbstractFailoverTest method testSequentialStrategyWithRetries.

@Test
public void testSequentialStrategyWithRetries() throws Exception {
    String address = "http://localhost:" + NON_PORT + "/non-existent";
    String address2 = "http://localhost:" + NON_PORT + "/non-existent2";
    FailoverFeature feature = new FailoverFeature();
    List<String> alternateAddresses = new ArrayList<>();
    alternateAddresses.add(address);
    alternateAddresses.add(address2);
    CustomRetryStrategy strategy = new CustomRetryStrategy();
    strategy.setMaxNumberOfRetries(5);
    strategy.setAlternateAddresses(alternateAddresses);
    feature.setStrategy(strategy);
    BookStore store = getBookStore(address, feature);
    try {
        store.getBook("1");
        fail("Exception expected");
    } catch (ProcessingException ex) {
        assertEquals(10, strategy.getTotalCount());
        assertEquals(5, strategy.getAddressCount(address));
        assertEquals(5, strategy.getAddressCount(address2));
    }
}
Also used : BookStore(org.apache.cxf.systest.jaxrs.BookStore) FailoverFeature(org.apache.cxf.clustering.FailoverFeature) ArrayList(java.util.ArrayList) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 2 with BookStore

use of org.apache.cxf.systest.jaxrs.BookStore in project cxf by apache.

the class AbstractFailoverTest method strategyTest.

protected void strategyTest(String inactiveReplica, FailoverFeature feature, String activeReplica1, String activeReplica2, boolean expectServerException, boolean expectRandom, boolean singleProxy) throws Exception {
    boolean randomized = false;
    String prevEndpoint = null;
    BookStore bookStore = null;
    if (singleProxy) {
        bookStore = getBookStore(inactiveReplica, feature);
    }
    for (int i = 0; i < 20; i++) {
        if (!singleProxy) {
            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");
            } else {
                Book book = bookStore.echoBookElementJson(new Book("CXF", 123));
                assertNotNull("expected non-null response", book);
                assertEquals("unexpected id", 123L, book.getId());
            }
        } catch (Exception error) {
            if (!expectServerException) {
                // assertTrue(currEndpoint.equals(inactiveReplica));
                throw error;
            }
            ex = error;
        }
        String currEndpoint = getCurrentEndpointAddress(bookStore);
        assertFalse(currEndpoint.equals(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;
        }
        prevEndpoint = currEndpoint;
    }
    if (!singleProxy) {
        assertEquals("unexpected random/sequential distribution of failovers", expectRandom, randomized);
    }
}
Also used : BookStore(org.apache.cxf.systest.jaxrs.BookStore) SequentialStrategy(org.apache.cxf.clustering.SequentialStrategy) RandomStrategy(org.apache.cxf.clustering.RandomStrategy) Book(org.apache.cxf.systest.jaxrs.Book) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ProcessingException(javax.ws.rs.ProcessingException)

Example 3 with BookStore

use of org.apache.cxf.systest.jaxrs.BookStore in project cxf by apache.

the class CircuitBreakerFailoverTest method testSequentialStrategyWithElapsingCircuitBreakerTimeout.

@Test
public void testSequentialStrategyWithElapsingCircuitBreakerTimeout() throws Throwable {
    FailoverFeature feature = customizeFeature(new CircuitBreakerFailoverFeature(1, 3000), false, "http://localhost:" + NON_PORT + "/non-existent", "http://localhost:" + NON_PORT + "/non-existent2");
    final BookStore bookStore = getBookStore("http://localhost:" + NON_PORT + "/non-existent", feature);
    // should reset all circuit breakers and the URLs could be tried again.
    for (int i = 0; i < 2; ++i) {
        try {
            bookStore.getBook(1);
            fail("Exception expected");
        } catch (ProcessingException ex) {
            if (!(ex.getCause() instanceof IOException)) {
                throw ex.getCause();
            }
        }
        // Let's wait a bit more than circuit breaker timeout
        Thread.sleep(4000);
    }
}
Also used : BookStore(org.apache.cxf.systest.jaxrs.BookStore) FailoverFeature(org.apache.cxf.clustering.FailoverFeature) CircuitBreakerFailoverFeature(org.apache.cxf.clustering.circuitbreaker.CircuitBreakerFailoverFeature) CircuitBreakerFailoverFeature(org.apache.cxf.clustering.circuitbreaker.CircuitBreakerFailoverFeature) IOException(java.io.IOException) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 4 with BookStore

use of org.apache.cxf.systest.jaxrs.BookStore in project cxf by apache.

the class LoadDistributorTest method strategyTest.

protected void strategyTest(String initialAddress, FailoverFeature feature) throws Exception {
    assertEquals(Server.ADDRESS1, initialAddress);
    int address2Count = 0;
    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++;
        }
    }
    assertEquals(10, address2Count);
    assertEquals(10, address3Count);
}
Also used : BookStore(org.apache.cxf.systest.jaxrs.BookStore) Book(org.apache.cxf.systest.jaxrs.Book)

Example 5 with BookStore

use of org.apache.cxf.systest.jaxrs.BookStore in project cxf by apache.

the class JAXRSHttpsBookTest method doTestGetBook123ProxyFromSpring.

private void doTestGetBook123ProxyFromSpring(String cfgFile) throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { cfgFile });
    Object bean = ctx.getBean("bookService.proxyFactory");
    assertNotNull(bean);
    JAXRSClientFactoryBean cfb = (JAXRSClientFactoryBean) bean;
    Bus bus = cfb.getBus();
    ClientLifeCycleManager manager = bus.getExtension(ClientLifeCycleManager.class);
    TestClientLifeCycleListener listener = new TestClientLifeCycleListener();
    manager.registerListener(listener);
    BookStore bs = cfb.create(BookStore.class);
    assertNotNull(listener.getEp());
    assertEquals("{http://service.rs}BookService", listener.getEp().getEndpointInfo().getName().toString());
    assertEquals("https://localhost:" + PORT, WebClient.client(bs).getBaseURI().toString());
    Book b = bs.getSecureBook("123");
    assertEquals(b.getId(), 123);
    b = bs.getSecureBook("123");
    assertEquals(b.getId(), 123);
    ctx.close();
}
Also used : Bus(org.apache.cxf.Bus) BookStore(org.apache.cxf.systest.jaxrs.BookStore) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Book(org.apache.cxf.systest.jaxrs.Book) ClientLifeCycleManager(org.apache.cxf.endpoint.ClientLifeCycleManager)

Aggregations

BookStore (org.apache.cxf.systest.jaxrs.BookStore)14 Book (org.apache.cxf.systest.jaxrs.Book)8 Test (org.junit.Test)8 ProcessingException (javax.ws.rs.ProcessingException)4 FailoverFeature (org.apache.cxf.clustering.FailoverFeature)3 JAXRSClientFactoryBean (org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean)3 WebClient (org.apache.cxf.jaxrs.client.WebClient)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 ArrayList (java.util.ArrayList)2 SequentialStrategy (org.apache.cxf.clustering.SequentialStrategy)2 CircuitBreakerFailoverFeature (org.apache.cxf.clustering.circuitbreaker.CircuitBreakerFailoverFeature)2 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)2 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)2 IOException (java.io.IOException)1 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)1 Bus (org.apache.cxf.Bus)1 FailoverFailedException (org.apache.cxf.clustering.FailoverFailedException)1 LoadDistributorFeature (org.apache.cxf.clustering.LoadDistributorFeature)1 RandomStrategy (org.apache.cxf.clustering.RandomStrategy)1 ClientLifeCycleManager (org.apache.cxf.endpoint.ClientLifeCycleManager)1