Search in sources :

Example 1 with ExecutorServiceAdapter

use of org.springframework.core.task.support.ExecutorServiceAdapter in project spring-integration by spring-projects.

the class LockRegistryLeaderInitiatorTests method testGracefulLeaderSelectorExit.

@Test
public void testGracefulLeaderSelectorExit() throws Exception {
    AtomicReference<Throwable> throwableAtomicReference = new AtomicReference<>();
    LockRegistry registry = mock(LockRegistry.class);
    Lock lock = spy(new ReentrantLock());
    willAnswer(invocation -> {
        try {
            return invocation.callRealMethod();
        } catch (Throwable e) {
            throwableAtomicReference.set(e);
            throw e;
        }
    }).given(lock).unlock();
    given(registry.obtain(anyString())).willReturn(lock);
    LockRegistryLeaderInitiator another = new LockRegistryLeaderInitiator(registry);
    willAnswer(invocation -> {
        another.stop();
        return false;
    }).given(lock).tryLock(anyLong(), eq(TimeUnit.MILLISECONDS));
    new DirectFieldAccessor(another).setPropertyValue("executorService", new ExecutorServiceAdapter(new SyncTaskExecutor()));
    another.start();
    Throwable throwable = throwableAtomicReference.get();
    assertNull(throwable);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) SyncTaskExecutor(org.springframework.core.task.SyncTaskExecutor) LockRegistry(org.springframework.integration.support.locks.LockRegistry) DefaultLockRegistry(org.springframework.integration.support.locks.DefaultLockRegistry) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutorServiceAdapter(org.springframework.core.task.support.ExecutorServiceAdapter) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 2 with ExecutorServiceAdapter

use of org.springframework.core.task.support.ExecutorServiceAdapter in project plugin-prov-azure by ligoj.

the class ProvAzurePluginResourceTest method checkStatusShudownFailed.

/**
 * Authority is valid, but the token cannot be acquired
 */
@Test
public void checkStatusShudownFailed() throws Exception {
    prepareMockAuth();
    httpServer.start();
    final TaskExecutor taskExecutor = Mockito.mock(TaskExecutor.class);
    final ProvAzurePluginResource resource = newResource(new ExecutorServiceAdapter(taskExecutor) {

        @Override
        public void shutdown() {
            throw new IllegalStateException();
        }
    });
    Assertions.assertThrows(IllegalStateException.class, () -> {
        resource.checkStatus(subscriptionResource.getParametersNoCheck(subscription));
    });
}
Also used : TaskExecutor(org.springframework.core.task.TaskExecutor) ExecutorServiceAdapter(org.springframework.core.task.support.ExecutorServiceAdapter) Test(org.junit.jupiter.api.Test) AbstractServerTest(org.ligoj.app.AbstractServerTest)

Aggregations

ExecutorServiceAdapter (org.springframework.core.task.support.ExecutorServiceAdapter)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Lock (java.util.concurrent.locks.Lock)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 AbstractServerTest (org.ligoj.app.AbstractServerTest)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 SyncTaskExecutor (org.springframework.core.task.SyncTaskExecutor)1 TaskExecutor (org.springframework.core.task.TaskExecutor)1 DefaultLockRegistry (org.springframework.integration.support.locks.DefaultLockRegistry)1 LockRegistry (org.springframework.integration.support.locks.LockRegistry)1