Search in sources :

Example 1 with ArrayQueueOutOfOrderSequence

use of org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence in project neo4j by neo4j.

the class ArrayQueueOutOfOrderSequenceTest method shouldDealWithThisScenario.

@Test
public void shouldDealWithThisScenario() throws Exception {
    // GIVEN
    OutOfOrderSequence sequence = new ArrayQueueOutOfOrderSequence(0, 5, new long[1]);
    assertTrue(offer(sequence, 1, new long[] { 0 }));
    assertFalse(offer(sequence, 3, new long[] { 0 }));
    assertFalse(offer(sequence, 4, new long[] { 0 }));
    assertTrue(offer(sequence, 2, new long[] { 0 }));
    assertFalse(offer(sequence, 6, new long[] { 0 }));
    assertTrue(offer(sequence, 5, new long[] { 0 }));
    // leave out 7
    assertFalse(offer(sequence, 8, new long[] { 0 }));
    assertFalse(offer(sequence, 9, new long[] { 0 }));
    assertFalse(offer(sequence, 10, new long[] { 0 }));
    assertFalse(offer(sequence, 11, new long[] { 0 }));
    // putting 12 should need extending the backing queue array
    assertFalse(offer(sequence, 12, new long[] { 0 }));
    assertFalse(offer(sequence, 13, new long[] { 0 }));
    assertFalse(offer(sequence, 14, new long[] { 0 }));
    // WHEN finally offering nr 7
    assertTrue(offer(sequence, 7, new long[] { 0 }));
    // THEN the number should jump to 14
    assertGet(sequence, 14, new long[] { 0 });
}
Also used : ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) OutOfOrderSequence(org.neo4j.kernel.impl.util.OutOfOrderSequence) Test(org.junit.Test)

Example 2 with ArrayQueueOutOfOrderSequence

use of org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence in project neo4j by neo4j.

the class ArrayQueueOutOfOrderSequenceTest method shouldExtendArrayIfNeedBe.

@Test
public void shouldExtendArrayIfNeedBe() throws Exception {
    // GIVEN
    OutOfOrderSequence sequence = new ArrayQueueOutOfOrderSequence(0L, 5, new long[1]);
    offer(sequence, 3L, new long[] { 0 });
    offer(sequence, 2L, new long[] { 1 });
    offer(sequence, 5L, new long[] { 2 });
    offer(sequence, 4L, new long[] { 3 });
    // WHEN offering a number that should result in extending the array
    offer(sequence, 6L, new long[] { 4 });
    // and WHEN offering the missing number to fill the gap
    offer(sequence, 1L, new long[] { 5 });
    // THEN the high number should be visible
    assertGet(sequence, 6L, new long[] { 4 });
}
Also used : ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) OutOfOrderSequence(org.neo4j.kernel.impl.util.OutOfOrderSequence) Test(org.junit.Test)

Example 3 with ArrayQueueOutOfOrderSequence

use of org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence in project neo4j by neo4j.

the class ArrayQueueOutOfOrderSequenceTest method shouldKeepItsCoolWhenMultipleThreadsAreHammeringIt.

@Test
public void shouldKeepItsCoolWhenMultipleThreadsAreHammeringIt() throws Exception {
    // An interesting note is that during tests the call to sequence#offer made no difference
    // in performance, so there seems to be no visible penalty in using ArrayQueueOutOfOrderSequence.
    // GIVEN a sequence with intentionally low starting queue size
    final AtomicLong numberSource = new AtomicLong();
    final OutOfOrderSequence sequence = new ArrayQueueOutOfOrderSequence(numberSource.get(), 5, new long[1]);
    final AtomicBoolean end = new AtomicBoolean();
    // and a bunch of threads that will start offering numbers at the same time
    final CountDownLatch startSignal = new CountDownLatch(1);
    Thread[] threads = new Thread[1];
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new Thread() {

            @Override
            public void run() {
                await(startSignal);
                while (!end.get()) {
                    long number = numberSource.incrementAndGet();
                    offer(sequence, number, new long[] { number + 2 });
                }
            }
        };
    }
    // WHEN
    for (Thread thread : threads) {
        thread.start();
    }
    startSignal.countDown();
    while (numberSource.get() < 10_000_000) {
        sleep(1);
        yield();
    }
    end.set(true);
    for (Thread thread : threads) {
        thread.join();
    }
    // THEN
    long lastNumber = numberSource.get();
    assertGet(sequence, lastNumber, new long[] { lastNumber + 2 });
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) OutOfOrderSequence(org.neo4j.kernel.impl.util.OutOfOrderSequence) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with ArrayQueueOutOfOrderSequence

use of org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence in project neo4j by neo4j.

the class ArrayQueueOutOfOrderSequenceTest method shouldExposeGapFreeSequenceSingleThreaded.

@Test
public void shouldExposeGapFreeSequenceSingleThreaded() throws Exception {
    // GIVEN
    OutOfOrderSequence sequence = new ArrayQueueOutOfOrderSequence(0L, 10, new long[1]);
    // WHEN/THEN
    offer(sequence, 1, new long[] { 1 });
    assertGet(sequence, 1, new long[] { 1 });
    offer(sequence, 2, new long[] { 2 });
    assertGet(sequence, 2, new long[] { 2 });
    assertFalse(sequence.seen(4, new long[] { 3 }));
    sequence.offer(4, new long[] { 3 });
    assertGet(sequence, 2, new long[] { 2 });
    offer(sequence, 3, new long[] { 4 });
    assertGet(sequence, 4, new long[] { 3 });
    offer(sequence, 5, new long[] { 5 });
    assertGet(sequence, 5, new long[] { 5 });
    // AND WHEN/THEN
    offer(sequence, 10, new long[] { 6 });
    offer(sequence, 11, new long[] { 7 });
    offer(sequence, 8, new long[] { 8 });
    offer(sequence, 9, new long[] { 9 });
    offer(sequence, 7, new long[] { 10 });
    assertGet(sequence, 5, new long[] { 5 });
    offer(sequence, 6, new long[] { 11 });
    assertGet(sequence, 11L, new long[] { 7 });
}
Also used : ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) OutOfOrderSequence(org.neo4j.kernel.impl.util.OutOfOrderSequence) Test(org.junit.Test)

Example 5 with ArrayQueueOutOfOrderSequence

use of org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence in project neo4j by neo4j.

the class ArrayQueueOutOfOrderSequenceTest method highestEverSeenTest.

@Test
public void highestEverSeenTest() {
    final OutOfOrderSequence sequence = new ArrayQueueOutOfOrderSequence(0, 5, EMPTY_META);
    assertEquals(0L, sequence.highestEverSeen());
    sequence.offer(1L, EMPTY_META);
    assertEquals(1L, sequence.highestEverSeen());
    sequence.offer(42L, EMPTY_META);
    assertEquals(42L, sequence.highestEverSeen());
}
Also used : ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) ArrayQueueOutOfOrderSequence(org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence) OutOfOrderSequence(org.neo4j.kernel.impl.util.OutOfOrderSequence) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 ArrayQueueOutOfOrderSequence (org.neo4j.kernel.impl.util.ArrayQueueOutOfOrderSequence)5 OutOfOrderSequence (org.neo4j.kernel.impl.util.OutOfOrderSequence)5 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1