Search in sources :

Example 46 with SettableFuture

use of com.google.common.util.concurrent.SettableFuture in project apollo by ctripcorp.

the class ReleaseMessageScannerTest method testScanMessageWithGapAndNotifyMessageListener.

@Test
public void testScanMessageWithGapAndNotifyMessageListener() throws Exception {
    String someMessage = "someMessage";
    long someId = 1;
    ReleaseMessage someReleaseMessage = assembleReleaseMessage(someId, someMessage);
    String someMissingMessage = "someMissingMessage";
    long someMissingId = 2;
    ReleaseMessage someMissingReleaseMessage = assembleReleaseMessage(someMissingId, someMissingMessage);
    String anotherMessage = "anotherMessage";
    long anotherId = 3;
    ReleaseMessage anotherReleaseMessage = assembleReleaseMessage(anotherId, anotherMessage);
    String anotherMissingMessage = "anotherMissingMessage";
    long anotherMissingId = 4;
    ReleaseMessage anotherMissingReleaseMessage = assembleReleaseMessage(anotherMissingId, anotherMissingMessage);
    long someRolledBackId = 5;
    String yetAnotherMessage = "yetAnotherMessage";
    long yetAnotherId = 6;
    ReleaseMessage yetAnotherReleaseMessage = assembleReleaseMessage(yetAnotherId, yetAnotherMessage);
    ArrayList<ReleaseMessage> receivedMessage = Lists.newArrayList();
    SettableFuture<ReleaseMessage> someListenerFuture = SettableFuture.create();
    ReleaseMessageListener someListener = (message, channel) -> receivedMessage.add(message);
    releaseMessageScanner.addMessageListener(someListener);
    when(releaseMessageRepository.findFirst500ByIdGreaterThanOrderByIdAsc(0L)).thenReturn(Lists.newArrayList(someReleaseMessage));
    await().untilAsserted(() -> {
        assertEquals(1, receivedMessage.size());
        assertSame(someReleaseMessage, receivedMessage.get(0));
    });
    when(releaseMessageRepository.findFirst500ByIdGreaterThanOrderByIdAsc(someId)).thenReturn(Lists.newArrayList(anotherReleaseMessage));
    await().untilAsserted(() -> {
        assertEquals(2, receivedMessage.size());
        assertSame(someReleaseMessage, receivedMessage.get(0));
        assertSame(anotherReleaseMessage, receivedMessage.get(1));
    });
    when(releaseMessageRepository.findAllById(Sets.newHashSet(someMissingId))).thenReturn(Lists.newArrayList(someMissingReleaseMessage));
    await().untilAsserted(() -> {
        assertEquals(3, receivedMessage.size());
        assertSame(someReleaseMessage, receivedMessage.get(0));
        assertSame(anotherReleaseMessage, receivedMessage.get(1));
        assertSame(someMissingReleaseMessage, receivedMessage.get(2));
    });
    when(releaseMessageRepository.findFirst500ByIdGreaterThanOrderByIdAsc(anotherId)).thenReturn(Lists.newArrayList(yetAnotherReleaseMessage));
    await().untilAsserted(() -> {
        assertEquals(4, receivedMessage.size());
        assertSame(someReleaseMessage, receivedMessage.get(0));
        assertSame(anotherReleaseMessage, receivedMessage.get(1));
        assertSame(someMissingReleaseMessage, receivedMessage.get(2));
        assertSame(yetAnotherReleaseMessage, receivedMessage.get(3));
    });
    when(releaseMessageRepository.findAllById(Sets.newHashSet(anotherMissingId, someRolledBackId))).thenReturn(Lists.newArrayList(anotherMissingReleaseMessage));
    await().untilAsserted(() -> {
        assertEquals(5, receivedMessage.size());
        assertSame(someReleaseMessage, receivedMessage.get(0));
        assertSame(anotherReleaseMessage, receivedMessage.get(1));
        assertSame(someMissingReleaseMessage, receivedMessage.get(2));
        assertSame(yetAnotherReleaseMessage, receivedMessage.get(3));
        assertSame(anotherMissingReleaseMessage, receivedMessage.get(4));
    });
}
Also used : ReleaseMessageRepository(com.ctrip.framework.apollo.biz.repository.ReleaseMessageRepository) Awaitility.await(org.awaitility.Awaitility.await) Mock(org.mockito.Mock) ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SettableFuture(com.google.common.util.concurrent.SettableFuture) Sets(com.google.common.collect.Sets) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) Assert.assertSame(org.junit.Assert.assertSame) BizConfig(com.ctrip.framework.apollo.biz.config.BizConfig) Lists(com.google.common.collect.Lists) Awaitility(org.awaitility.Awaitility) AbstractUnitTest(com.ctrip.framework.apollo.biz.AbstractUnitTest) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.biz.AbstractUnitTest)

Aggregations

SettableFuture (com.google.common.util.concurrent.SettableFuture)46 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)15 List (java.util.List)15 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)14 File (java.io.File)12 IOException (java.io.IOException)10 TimeUnit (java.util.concurrent.TimeUnit)10 Futures (com.google.common.util.concurrent.Futures)9 ImmutableMap (com.google.common.collect.ImmutableMap)8 ExecutionException (java.util.concurrent.ExecutionException)8 FutureCallback (com.google.common.util.concurrent.FutureCallback)7 Before (org.junit.Before)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Throwables (com.google.common.base.Throwables)5 ImmutableList (com.google.common.collect.ImmutableList)5 Assert.assertSame (org.junit.Assert.assertSame)5 Mock (org.mockito.Mock)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5