Search in sources :

Example 1 with TimeAndCounterIDGenerator

use of org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator in project activemq-artemis by apache.

the class TimeAndCounterIDGeneratorTest method testCalculation.

// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testCalculation() {
    TimeAndCounterIDGenerator seq = new TimeAndCounterIDGenerator();
    long max = 11000;
    long lastNr = 0;
    for (long i = 0; i < max; i++) {
        long seqNr = seq.generateID();
        Assert.assertTrue("The sequence generator should aways generate crescent numbers", seqNr > lastNr);
        lastNr = seqNr;
    }
}
Also used : TimeAndCounterIDGenerator(org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator) Test(org.junit.Test)

Example 2 with TimeAndCounterIDGenerator

use of org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator in project activemq-artemis by apache.

the class TimeAndCounterIDGeneratorTest method testCalculationOnMultiThread.

@Test
public void testCalculationOnMultiThread() throws Throwable {
    final ConcurrentHashSet<Long> hashSet = new ConcurrentHashSet<>();
    final TimeAndCounterIDGenerator seq = new TimeAndCounterIDGenerator();
    System.out.println("Time = " + TimeAndCounterIDGeneratorTest.hex(System.currentTimeMillis()) + ", " + seq);
    final int NUMBER_OF_THREADS = 100;
    final int NUMBER_OF_IDS = 10;
    final CountDownLatch latchAlign = new CountDownLatch(NUMBER_OF_THREADS);
    final CountDownLatch latchStart = new CountDownLatch(1);
    class T1 extends Thread {

        Throwable e;

        @Override
        public void run() {
            try {
                latchAlign.countDown();
                assertTrue("Latch has got to return within a minute", latchStart.await(1, TimeUnit.MINUTES));
                long lastValue = 0L;
                for (int i = 0; i < NUMBER_OF_IDS; i++) {
                    long value = seq.generateID();
                    Assert.assertTrue(TimeAndCounterIDGeneratorTest.hex(value) + " should be greater than " + TimeAndCounterIDGeneratorTest.hex(lastValue) + " on seq " + seq.toString(), value > lastValue);
                    lastValue = value;
                    hashSet.add(value);
                }
            } catch (Throwable e) {
                this.e = e;
            }
        }
    }
    T1[] arrays = new T1[NUMBER_OF_THREADS];
    for (int i = 0; i < arrays.length; i++) {
        arrays[i] = new T1();
        arrays[i].start();
    }
    assertTrue("Latch has got to return within a minute", latchAlign.await(1, TimeUnit.MINUTES));
    latchStart.countDown();
    for (T1 t : arrays) {
        t.join();
        if (t.e != null) {
            throw t.e;
        }
    }
    Assert.assertEquals(NUMBER_OF_THREADS * NUMBER_OF_IDS, hashSet.size());
    hashSet.clear();
}
Also used : TimeAndCounterIDGenerator(org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator) ConcurrentHashSet(org.apache.activemq.artemis.utils.collections.ConcurrentHashSet) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with TimeAndCounterIDGenerator

use of org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator in project activemq-artemis by apache.

the class StorageManagerTestBase method createJMSStorage.

/**
 * @throws Exception
 */
protected void createJMSStorage() throws Exception {
    jmsJournal = new JMSJournalStorageManagerImpl(null, new TimeAndCounterIDGenerator(), createDefaultInVMConfig(), null);
    addActiveMQComponent(jmsJournal);
    jmsJournal.start();
    jmsJournal.load();
}
Also used : JMSJournalStorageManagerImpl(org.apache.activemq.artemis.jms.persistence.impl.journal.JMSJournalStorageManagerImpl) TimeAndCounterIDGenerator(org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator)

Example 4 with TimeAndCounterIDGenerator

use of org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator in project activemq-artemis by apache.

the class TimeAndCounterIDGeneratorTest method testCalculationRefresh.

@Test
public void testCalculationRefresh() {
    TimeAndCounterIDGenerator seq = new TimeAndCounterIDGenerator();
    long id1 = seq.generateID();
    Assert.assertEquals(1, id1 & 0xffff);
    Assert.assertEquals(2, seq.generateID() & 0xffff);
    seq.refresh();
    long id2 = seq.generateID();
    Assert.assertTrue(id2 > id1);
    Assert.assertEquals(1, id2 & 0xffff);
}
Also used : TimeAndCounterIDGenerator(org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator) Test(org.junit.Test)

Example 5 with TimeAndCounterIDGenerator

use of org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator in project activemq-artemis by apache.

the class TimeAndCounterIDGeneratorTest method testWrapID.

@Test
public void testWrapID() throws Throwable {
    TimeAndCounterIDGenerator seq = new TimeAndCounterIDGenerator();
    System.out.println("Current Time = " + TimeAndCounterIDGeneratorTest.hex(System.currentTimeMillis()) + " " + seq);
    // 10 seconds in the future
    seq.setInternalDate(System.currentTimeMillis() + 10000L);
    // 1 ID about to explode
    seq.setInternalID(TimeAndCounterIDGenerator.ID_MASK);
    try {
        // This is simulating a situation where we generated more than 268 million messages on the same time interval
        seq.generateID();
        Assert.fail("It was supposed to throw an exception, as the counter was set to explode on this test");
    } catch (Exception e) {
    }
    seq = new TimeAndCounterIDGenerator();
    // 10 seconds in the past
    seq.setInternalDate(System.currentTimeMillis() - 10000L);
    long timeMark = seq.getInternalTimeMark();
    // 1 ID about to explode
    seq.setInternalID(TimeAndCounterIDGenerator.ID_MASK);
    // This is ok... the time portion would be added to the next one generated 10 seconds ago
    seq.generateID();
    Assert.assertTrue(TimeAndCounterIDGeneratorTest.hex(timeMark) + " < " + TimeAndCounterIDGeneratorTest.hex(seq.getInternalTimeMark()), timeMark < seq.getInternalTimeMark());
}
Also used : TimeAndCounterIDGenerator(org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator) Test(org.junit.Test)

Aggregations

TimeAndCounterIDGenerator (org.apache.activemq.artemis.utils.TimeAndCounterIDGenerator)5 Test (org.junit.Test)4 CountDownLatch (java.util.concurrent.CountDownLatch)1 JMSJournalStorageManagerImpl (org.apache.activemq.artemis.jms.persistence.impl.journal.JMSJournalStorageManagerImpl)1 ConcurrentHashSet (org.apache.activemq.artemis.utils.collections.ConcurrentHashSet)1