Search in sources :

Example 1 with MockFailingConnection

use of com.adaptris.core.stubs.MockFailingConnection in project interlok by adaptris.

the class AdapterManagerTest method testInitialise_WithTimeout_InitFailure.

@Test
public void testInitialise_WithTimeout_InitFailure() throws Exception {
    TimeInterval standardTimeout = new TimeInterval(2L, TimeUnit.SECONDS);
    String adapterName = this.getClass().getSimpleName() + "." + getName();
    Adapter adapter = createAdapter(adapterName);
    MockFailingConnection conn = new MockFailingConnection(getName(), "Init");
    conn.setConnectionAttempts(3);
    conn.setConnectionRetryInterval(new TimeInterval(100L, TimeUnit.MILLISECONDS));
    adapter.getSharedComponents().addConnection(conn);
    AdapterManager adapterManager = new AdapterManager(adapter);
    try {
        adapterManager.registerMBean();
        ObjectName adapterObj = createAdapterObjectName(adapterName);
        AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, adapterObj, AdapterManagerMBean.class);
        try {
            manager.requestInit(standardTimeout.toMilliseconds());
            fail();
        } catch (CoreException expected) {
        }
    } finally {
        adapterManager.requestClose();
        adapterManager.unregisterMBean();
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) CoreException(com.adaptris.core.CoreException) MockFailingConnection(com.adaptris.core.stubs.MockFailingConnection) Adapter(com.adaptris.core.Adapter) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 2 with MockFailingConnection

use of com.adaptris.core.stubs.MockFailingConnection in project interlok by adaptris.

the class AdapterManagerTest method testForceClose_ErrorOnInit_RequestStart.

@Test
public void testForceClose_ErrorOnInit_RequestStart() throws Exception {
    final TimeInterval waitTime = new TimeInterval(5L, TimeUnit.SECONDS);
    String adapterName = this.getClass().getSimpleName() + "." + getName();
    Adapter adapter = createAdapter(adapterName);
    MockFailingConnection conn = new MockFailingConnection(getName(), "Init");
    conn.setConnectionAttempts(-1);
    conn.setConnectionRetryInterval(new TimeInterval(100L, TimeUnit.MILLISECONDS));
    adapter.getSharedComponents().addConnection(conn);
    List<BaseComponentMBean> mBeans = createJmxManagers(adapter);
    try {
        register(mBeans);
        ObjectName adapterObj = createAdapterObjectName(adapterName);
        final AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, adapterObj, AdapterManagerMBean.class);
        // Create a cyclic barrier to wait for init/start operation to finish
        final CyclicBarrier gate = new CyclicBarrier(3);
        MyExceptionHandler exceptionHandler = new MyExceptionHandler();
        ManagedThreadFactory threadFactory = new ManagedThreadFactory();
        Thread initThread = threadFactory.newThread(new Runnable() {

            @Override
            public void run() {
                try {
                    manager.requestInit();
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                }
                // This code is likely to never trigger, because of the throw above...
                try {
                    gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
                } catch (Exception gateException) {
                }
            }
        });
        initThread.setUncaughtExceptionHandler(exceptionHandler);
        initThread.start();
        Thread startThread = threadFactory.newThread(new Runnable() {

            @Override
            public void run() {
                try {
                    manager.requestStart();
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                }
                // This code is likely to never trigger, because of the throw above...
                try {
                    gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
                } catch (Exception gateException) {
                }
            }
        });
        startThread.setUncaughtExceptionHandler(exceptionHandler);
        startThread.start();
        try {
            gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
            fail("Adapter init success, when not expected");
        } catch (Exception gateException) {
            // Expected now force close it, because it took too long.
            manager.forceClose();
        }
        assertEquals(ClosedState.getInstance(), manager.getComponentState());
        assertEquals(0, conn.getInitCount());
        assertEquals(0, conn.getCloseCount());
    } finally {
        adapter.requestClose();
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) MockFailingConnection(com.adaptris.core.stubs.MockFailingConnection) Adapter(com.adaptris.core.Adapter) TimeoutException(java.util.concurrent.TimeoutException) MalformedObjectNameException(javax.management.MalformedObjectNameException) CoreException(com.adaptris.core.CoreException) ObjectName(javax.management.ObjectName) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedThreadFactory(com.adaptris.core.util.ManagedThreadFactory) CoreException(com.adaptris.core.CoreException) Test(org.junit.Test)

Example 3 with MockFailingConnection

use of com.adaptris.core.stubs.MockFailingConnection in project interlok by adaptris.

the class AdapterManagerTest method testForceClose_ErrorOnStart_RequestStart.

@Test
public void testForceClose_ErrorOnStart_RequestStart() throws Exception {
    final TimeInterval waitTime = new TimeInterval(5L, TimeUnit.SECONDS);
    String adapterName = this.getClass().getSimpleName() + "." + getName();
    Adapter adapter = createAdapter(adapterName);
    MockFailingConnection conn = new MockFailingConnection(getName(), "Start");
    conn.setConnectionAttempts(-1);
    conn.setConnectionRetryInterval(new TimeInterval(100L, TimeUnit.MILLISECONDS));
    adapter.getSharedComponents().addConnection(conn);
    List<BaseComponentMBean> mBeans = createJmxManagers(adapter);
    try {
        register(mBeans);
        ObjectName adapterObj = createAdapterObjectName(adapterName);
        final AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, adapterObj, AdapterManagerMBean.class);
        // Create a cyclic barrier to wait for init operation to finish
        final CyclicBarrier gate = new CyclicBarrier(2);
        MyExceptionHandler exceptionHandler = new MyExceptionHandler();
        Thread startThread = new ManagedThreadFactory().newThread(new Runnable() {

            @Override
            public void run() {
                try {
                    manager.requestStart();
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                }
                // This code is likely to never trigger, because of the throw above...
                try {
                    gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
                } catch (Exception gateException) {
                }
            }
        });
        startThread.setUncaughtExceptionHandler(exceptionHandler);
        startThread.start();
        try {
            gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
            fail("Adapter Start success, when not expected");
        } catch (Exception gateException) {
            // Expected now force close it, because it took too long.
            manager.forceClose();
        }
        assertEquals(ClosedState.getInstance(), manager.getComponentState());
        assertEquals(1, conn.getInitCount());
        assertEquals(0, conn.getStartCount());
        assertEquals(1, conn.getCloseCount());
    } finally {
        adapter.requestClose();
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) MockFailingConnection(com.adaptris.core.stubs.MockFailingConnection) Adapter(com.adaptris.core.Adapter) TimeoutException(java.util.concurrent.TimeoutException) MalformedObjectNameException(javax.management.MalformedObjectNameException) CoreException(com.adaptris.core.CoreException) ObjectName(javax.management.ObjectName) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedThreadFactory(com.adaptris.core.util.ManagedThreadFactory) CoreException(com.adaptris.core.CoreException) Test(org.junit.Test)

Example 4 with MockFailingConnection

use of com.adaptris.core.stubs.MockFailingConnection in project interlok by adaptris.

the class AdapterManagerTest method testStart_WithTimeout_StartFailure.

@Test
public void testStart_WithTimeout_StartFailure() throws Exception {
    TimeInterval standardTimeout = new TimeInterval(3L, TimeUnit.SECONDS);
    String adapterName = this.getClass().getSimpleName() + "." + getName();
    Adapter adapter = createAdapter(adapterName);
    MockFailingConnection conn = new MockFailingConnection(getName(), "Start");
    conn.setConnectionAttempts(3);
    conn.setConnectionRetryInterval(new TimeInterval(100L, TimeUnit.MILLISECONDS));
    adapter.getSharedComponents().addConnection(conn);
    AdapterManager adapterManager = new AdapterManager(adapter);
    try {
        adapterManager.registerMBean();
        ObjectName adapterObj = createAdapterObjectName(adapterName);
        AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, adapterObj, AdapterManagerMBean.class);
        try {
            manager.requestStart(standardTimeout.toMilliseconds());
            fail();
        } catch (CoreException expected) {
        }
    } finally {
        adapterManager.requestClose();
        adapterManager.unregisterMBean();
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) CoreException(com.adaptris.core.CoreException) MockFailingConnection(com.adaptris.core.stubs.MockFailingConnection) Adapter(com.adaptris.core.Adapter) ObjectName(javax.management.ObjectName) Test(org.junit.Test)

Example 5 with MockFailingConnection

use of com.adaptris.core.stubs.MockFailingConnection in project interlok by adaptris.

the class AdapterManagerTest method testForceClose_ErrorOnInit_RequestInit.

@Test
public void testForceClose_ErrorOnInit_RequestInit() throws Exception {
    final TimeInterval waitTime = new TimeInterval(5L, TimeUnit.SECONDS);
    String adapterName = this.getClass().getSimpleName() + "." + getName();
    Adapter adapter = createAdapter(adapterName);
    MockFailingConnection conn = new MockFailingConnection(getName(), "Init");
    conn.setConnectionAttempts(-1);
    conn.setConnectionRetryInterval(new TimeInterval(100L, TimeUnit.MILLISECONDS));
    adapter.getSharedComponents().addConnection(conn);
    List<BaseComponentMBean> mBeans = createJmxManagers(adapter);
    try {
        register(mBeans);
        ObjectName adapterObj = createAdapterObjectName(adapterName);
        final AdapterManagerMBean manager = JMX.newMBeanProxy(mBeanServer, adapterObj, AdapterManagerMBean.class);
        // Create a cyclic barrier to wait for init operation to finish
        final CyclicBarrier gate = new CyclicBarrier(2);
        MyExceptionHandler exceptionHandler = new MyExceptionHandler();
        Thread initThread = new ManagedThreadFactory().newThread(new Runnable() {

            @Override
            public void run() {
                try {
                    manager.requestInit();
                } catch (CoreException e) {
                    throw new RuntimeException(e);
                }
                // This code is likely to never trigger, because of the throw above...
                try {
                    gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
                } catch (Exception gateException) {
                }
            }
        });
        initThread.setUncaughtExceptionHandler(exceptionHandler);
        initThread.start();
        try {
            gate.await(waitTime.toMilliseconds(), TimeUnit.MILLISECONDS);
            fail("Adapter init success, when not expected");
        } catch (Exception gateException) {
            // Expected now force close it, because it took too long.
            manager.forceClose();
        }
        assertEquals(ClosedState.getInstance(), manager.getComponentState());
        assertEquals(0, conn.getInitCount());
        assertEquals(0, conn.getCloseCount());
    } finally {
        adapter.requestClose();
    }
}
Also used : TimeInterval(com.adaptris.util.TimeInterval) MockFailingConnection(com.adaptris.core.stubs.MockFailingConnection) Adapter(com.adaptris.core.Adapter) TimeoutException(java.util.concurrent.TimeoutException) MalformedObjectNameException(javax.management.MalformedObjectNameException) CoreException(com.adaptris.core.CoreException) ObjectName(javax.management.ObjectName) CyclicBarrier(java.util.concurrent.CyclicBarrier) ManagedThreadFactory(com.adaptris.core.util.ManagedThreadFactory) CoreException(com.adaptris.core.CoreException) Test(org.junit.Test)

Aggregations

Adapter (com.adaptris.core.Adapter)5 CoreException (com.adaptris.core.CoreException)5 MockFailingConnection (com.adaptris.core.stubs.MockFailingConnection)5 TimeInterval (com.adaptris.util.TimeInterval)5 ObjectName (javax.management.ObjectName)5 Test (org.junit.Test)5 ManagedThreadFactory (com.adaptris.core.util.ManagedThreadFactory)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 TimeoutException (java.util.concurrent.TimeoutException)3 MalformedObjectNameException (javax.management.MalformedObjectNameException)3