Search in sources :

Example 1 with SegmentPublisherSupplier

use of org.infinispan.reactive.publisher.impl.SegmentPublisherSupplier in project infinispan by infinispan.

the class DistributedStreamIteratorTest method waitUntilProcessingResults.

@Test
public void waitUntilProcessingResults() throws TimeoutException, InterruptedException, ExecutionException {
    Cache<Object, String> cache0 = cache(0, CACHE_NAME);
    Cache<Object, String> cache1 = cache(1, CACHE_NAME);
    Map<Object, String> values = new HashMap<>();
    for (int i = 0; i < 9; ++i) {
        MagicKey key = new MagicKey(cache1);
        cache1.put(key, key.toString());
        values.put(key, key.toString());
    }
    CheckPoint checkPoint = new CheckPoint();
    checkPoint.triggerForever(Mocks.AFTER_RELEASE);
    ClusterPublisherManager<Object, String> spy = Mocks.replaceComponentWithSpy(cache0, ClusterPublisherManager.class);
    doAnswer(invocation -> {
        SegmentPublisherSupplier<?> result = (SegmentPublisherSupplier<?>) invocation.callRealMethod();
        return Mocks.blockingPublisher(result, checkPoint);
    }).when(spy).entryPublisher(any(), any(), any(), anyLong(), any(), anyInt(), any());
    final BlockingQueue<Map.Entry<Object, String>> returnQueue = new LinkedBlockingQueue<>();
    Future<Void> future = fork(() -> {
        Iterator<Map.Entry<Object, String>> iter = cache0.entrySet().stream().iterator();
        while (iter.hasNext()) {
            Map.Entry<Object, String> entry = iter.next();
            returnQueue.add(entry);
        }
        return null;
    });
    // Now wait for them to send back first results but don't let them process
    checkPoint.awaitStrict(Mocks.BEFORE_INVOCATION, 10, TimeUnit.SECONDS);
    // Now let them process the results
    checkPoint.triggerForever(Mocks.BEFORE_RELEASE);
    // Now kill the cache - we should recover and get appropriate values
    killMember(1, CACHE_NAME);
    future.get(10, TimeUnit.SECONDS);
    KeyPartitioner keyPartitioner = TestingUtil.extractComponent(cache0, KeyPartitioner.class);
    Map<Integer, Set<Map.Entry<Object, String>>> expected = generateEntriesPerSegment(keyPartitioner, values.entrySet());
    Map<Integer, Set<Map.Entry<Object, String>>> answer = generateEntriesPerSegment(keyPartitioner, returnQueue);
    for (Map.Entry<Integer, Set<Map.Entry<Object, String>>> entry : expected.entrySet()) {
        Integer segment = entry.getKey();
        Set<Map.Entry<Object, String>> answerForSegment = answer.get(segment);
        if (answerForSegment != null) {
            for (Map.Entry<Object, String> exp : entry.getValue()) {
                if (!answerForSegment.contains(exp)) {
                    log.errorf("Segment %d, missing %s", segment, exp);
                }
            }
            for (Map.Entry<Object, String> ans : answerForSegment) {
                if (!entry.getValue().contains(ans)) {
                    log.errorf("Segment %d, extra %s", segment, ans);
                }
            }
            assertEquals(entry.getValue().size(), answerForSegment.size());
        }
        assertEquals("Segment " + segment + " had a mismatch", entry.getValue(), answerForSegment);
    }
}
Also used : SegmentPublisherSupplier(org.infinispan.reactive.publisher.impl.SegmentPublisherSupplier) Set(java.util.Set) IntSet(org.infinispan.commons.util.IntSet) HashSet(java.util.HashSet) HashMap(java.util.HashMap) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) CheckPoint(org.infinispan.test.fwk.CheckPoint) CheckPoint(org.infinispan.test.fwk.CheckPoint) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MagicKey(org.infinispan.distribution.MagicKey) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test)

Aggregations

HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IntSet (org.infinispan.commons.util.IntSet)1 MagicKey (org.infinispan.distribution.MagicKey)1 KeyPartitioner (org.infinispan.distribution.ch.KeyPartitioner)1 SegmentPublisherSupplier (org.infinispan.reactive.publisher.impl.SegmentPublisherSupplier)1 CheckPoint (org.infinispan.test.fwk.CheckPoint)1 Test (org.testng.annotations.Test)1