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"));
}
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);
}
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();
}
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();
}
Aggregations