Search in sources :

Example 1 with LockRegistry

use of org.springframework.integration.support.locks.LockRegistry in project spring-integration by spring-projects.

the class AggregatorParserTests method testPropertyAssignment.

@Test
public void testPropertyAssignment() throws Exception {
    EventDrivenConsumer endpoint = (EventDrivenConsumer) context.getBean("completelyDefinedAggregator");
    ReleaseStrategy releaseStrategy = (ReleaseStrategy) context.getBean("releaseStrategy");
    CorrelationStrategy correlationStrategy = (CorrelationStrategy) context.getBean("correlationStrategy");
    MessageChannel outputChannel = (MessageChannel) context.getBean("outputChannel");
    MessageChannel discardChannel = (MessageChannel) context.getBean("discardChannel");
    Object consumer = new DirectFieldAccessor(endpoint).getPropertyValue("handler");
    assertThat(consumer, is(instanceOf(AggregatingMessageHandler.class)));
    DirectFieldAccessor accessor = new DirectFieldAccessor(consumer);
    Object handlerMethods = new DirectFieldAccessor(new DirectFieldAccessor(new DirectFieldAccessor(accessor.getPropertyValue("outputProcessor")).getPropertyValue("processor")).getPropertyValue("delegate")).getPropertyValue("handlerMethods");
    assertNull(handlerMethods);
    Object handlerMethod = new DirectFieldAccessor(new DirectFieldAccessor(new DirectFieldAccessor(accessor.getPropertyValue("outputProcessor")).getPropertyValue("processor")).getPropertyValue("delegate")).getPropertyValue("handlerMethod");
    assertTrue(handlerMethod.toString().contains("createSingleMessageFromGroup"));
    assertEquals("The AggregatorEndpoint is not injected with the appropriate ReleaseStrategy instance", releaseStrategy, accessor.getPropertyValue("releaseStrategy"));
    assertEquals("The AggregatorEndpoint is not injected with the appropriate CorrelationStrategy instance", correlationStrategy, accessor.getPropertyValue("correlationStrategy"));
    assertEquals("The AggregatorEndpoint is not injected with the appropriate output channel", outputChannel, accessor.getPropertyValue("outputChannel"));
    assertEquals("The AggregatorEndpoint is not injected with the appropriate discard channel", discardChannel, accessor.getPropertyValue("discardChannel"));
    assertEquals("The AggregatorEndpoint is not set with the appropriate timeout value", 86420000L, TestUtils.getPropertyValue(consumer, "messagingTemplate.sendTimeout"));
    assertEquals("The AggregatorEndpoint is not configured with the appropriate 'send partial results on timeout' flag", true, accessor.getPropertyValue("sendPartialResultOnExpiry"));
    assertFalse(TestUtils.getPropertyValue(consumer, "expireGroupsUponTimeout", Boolean.class));
    assertTrue(TestUtils.getPropertyValue(consumer, "expireGroupsUponCompletion", Boolean.class));
    assertEquals(123L, TestUtils.getPropertyValue(consumer, "minimumTimeoutForEmptyGroups"));
    assertEquals("456", TestUtils.getPropertyValue(consumer, "groupTimeoutExpression", Expression.class).getExpressionString());
    assertSame(this.context.getBean(LockRegistry.class), TestUtils.getPropertyValue(consumer, "lockRegistry"));
    assertSame(this.context.getBean("scheduler"), TestUtils.getPropertyValue(consumer, "taskScheduler"));
    assertSame(this.context.getBean("store"), TestUtils.getPropertyValue(consumer, "messageStore"));
    assertEquals(5, TestUtils.getPropertyValue(consumer, "order"));
    assertNotNull(TestUtils.getPropertyValue(consumer, "forceReleaseAdviceChain"));
}
Also used : EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageChannel(org.springframework.messaging.MessageChannel) LockRegistry(org.springframework.integration.support.locks.LockRegistry) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) CorrelationStrategy(org.springframework.integration.aggregator.CorrelationStrategy) ExpressionEvaluatingCorrelationStrategy(org.springframework.integration.aggregator.ExpressionEvaluatingCorrelationStrategy) MethodInvokingReleaseStrategy(org.springframework.integration.aggregator.MethodInvokingReleaseStrategy) ReleaseStrategy(org.springframework.integration.aggregator.ReleaseStrategy) ExpressionEvaluatingReleaseStrategy(org.springframework.integration.aggregator.ExpressionEvaluatingReleaseStrategy) Test(org.junit.Test)

Example 2 with LockRegistry

use of org.springframework.integration.support.locks.LockRegistry 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 3 with LockRegistry

use of org.springframework.integration.support.locks.LockRegistry in project spring-integration by spring-projects.

the class LockRegistryLeaderInitiatorTests method competingWithLock.

@Test
public void competingWithLock() throws Exception {
    // switch used to toggle which registry obtains lock
    AtomicBoolean firstLocked = new AtomicBoolean(true);
    // set up first registry instance - this one will be able to obtain lock initially
    LockRegistry firstRegistry = mock(LockRegistry.class);
    Lock firstLock = mock(Lock.class);
    given(firstRegistry.obtain(anyString())).willReturn(firstLock);
    given(firstLock.tryLock(anyLong(), any(TimeUnit.class))).willAnswer(i -> firstLocked.get());
    // set up first initiator instance using first LockRegistry
    LockRegistryLeaderInitiator first = new LockRegistryLeaderInitiator(firstRegistry, new DefaultCandidate());
    CountDownLatch firstGranted = new CountDownLatch(1);
    CountDownLatch firstRevoked = new CountDownLatch(1);
    CountDownLatch firstAquireLockFailed = new CountDownLatch(1);
    first.setHeartBeatMillis(10);
    first.setBusyWaitMillis(1);
    first.setLeaderEventPublisher(new CountingPublisher(firstGranted, firstRevoked, firstAquireLockFailed));
    // set up second registry instance - this one will NOT be able to obtain lock initially
    LockRegistry secondRegistry = mock(LockRegistry.class);
    Lock secondLock = mock(Lock.class);
    given(secondRegistry.obtain(anyString())).willReturn(secondLock);
    given(secondLock.tryLock(anyLong(), any(TimeUnit.class))).willAnswer(i -> !firstLocked.get());
    // set up second initiator instance using second LockRegistry
    LockRegistryLeaderInitiator second = new LockRegistryLeaderInitiator(secondRegistry, new DefaultCandidate());
    CountDownLatch secondGranted = new CountDownLatch(1);
    CountDownLatch secondRevoked = new CountDownLatch(1);
    CountDownLatch secondAquireLockFailed = new CountDownLatch(1);
    second.setHeartBeatMillis(10);
    second.setBusyWaitMillis(1);
    second.setLeaderEventPublisher(new CountingPublisher(secondGranted, secondRevoked, secondAquireLockFailed));
    // start initiators
    first.start();
    second.start();
    // first initiator should lead and publish granted event
    assertThat(firstGranted.await(10, TimeUnit.SECONDS), is(true));
    assertThat(first.getContext().isLeader(), is(true));
    assertThat(second.getContext().isLeader(), is(false));
    // simulate first registry instance unable to obtain lock, for example due to lock timeout
    firstLocked.set(false);
    // second initiator should take lead and publish granted event, first initiator should publish revoked event
    assertThat(secondGranted.await(10, TimeUnit.SECONDS), is(true));
    assertThat(firstRevoked.await(10, TimeUnit.SECONDS), is(true));
    assertThat(second.getContext().isLeader(), is(true));
    assertThat(first.getContext().isLeader(), is(false));
    first.stop();
    second.stop();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LockRegistry(org.springframework.integration.support.locks.LockRegistry) DefaultLockRegistry(org.springframework.integration.support.locks.DefaultLockRegistry) DefaultCandidate(org.springframework.integration.leader.DefaultCandidate) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Example 4 with LockRegistry

use of org.springframework.integration.support.locks.LockRegistry in project spring-integration by spring-projects.

the class LockRegistryLeaderInitiatorTests method testExceptionFromLock.

@Test
public void testExceptionFromLock() throws Exception {
    Lock mockLock = mock(Lock.class);
    AtomicBoolean exceptionThrown = new AtomicBoolean();
    willAnswer(invocation -> {
        if (!exceptionThrown.getAndSet(true)) {
            throw new RuntimeException("lock is broken");
        } else {
            return true;
        }
    }).given(mockLock).tryLock(anyLong(), any(TimeUnit.class));
    LockRegistry registry = lockKey -> mockLock;
    CountDownLatch onGranted = new CountDownLatch(1);
    LockRegistryLeaderInitiator another = new LockRegistryLeaderInitiator(registry);
    another.setLeaderEventPublisher(new CountingPublisher(onGranted));
    another.start();
    assertTrue(onGranted.await(10, TimeUnit.SECONDS));
    assertTrue(another.getContext().isLeader());
    assertTrue(exceptionThrown.get());
    another.stop();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ExecutorServiceAdapter(org.springframework.core.task.support.ExecutorServiceAdapter) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeaderEventPublisher(org.springframework.integration.leader.event.LeaderEventPublisher) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) Mockito.spy(org.mockito.Mockito.spy) TestUtils(org.springframework.integration.test.util.TestUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) Context(org.springframework.integration.leader.Context) Assert.assertThat(org.junit.Assert.assertThat) LockRegistry(org.springframework.integration.support.locks.LockRegistry) BDDMockito.given(org.mockito.BDDMockito.given) DefaultLockRegistry(org.springframework.integration.support.locks.DefaultLockRegistry) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Executors(java.util.concurrent.Executors) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) SyncTaskExecutor(org.springframework.core.task.SyncTaskExecutor) DefaultLeaderEventPublisher(org.springframework.integration.leader.event.DefaultLeaderEventPublisher) Lock(java.util.concurrent.locks.Lock) Assert.assertNull(org.junit.Assert.assertNull) DefaultCandidate(org.springframework.integration.leader.DefaultCandidate) Assert.assertFalse(org.junit.Assert.assertFalse) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LockRegistry(org.springframework.integration.support.locks.LockRegistry) DefaultLockRegistry(org.springframework.integration.support.locks.DefaultLockRegistry) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 LockRegistry (org.springframework.integration.support.locks.LockRegistry)4 Lock (java.util.concurrent.locks.Lock)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)3 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)3 DefaultLockRegistry (org.springframework.integration.support.locks.DefaultLockRegistry)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 SyncTaskExecutor (org.springframework.core.task.SyncTaskExecutor)2 ExecutorServiceAdapter (org.springframework.core.task.support.ExecutorServiceAdapter)2 DefaultCandidate (org.springframework.integration.leader.DefaultCandidate)2 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 CoreMatchers.is (org.hamcrest.CoreMatchers.is)1 Assert.assertFalse (org.junit.Assert.assertFalse)1 Assert.assertNull (org.junit.Assert.assertNull)1 Assert.assertThat (org.junit.Assert.assertThat)1 Assert.assertTrue (org.junit.Assert.assertTrue)1