public void testWrapID() throws Throwable
{
final ConcurrentHashSet<Long> hashSet = new org.hornetq.utils.ConcurrentHashSet<Long>();
TimeAndCounterIDGenerator seq = new TimeAndCounterIDGenerator();
System.out.println("Current Time = " + TimeAndCounterIDGeneratorTest.hex(System.currentTimeMillis()) + " " + seq);
seq.setInternalDate(System.currentTimeMillis() + 10000l); // 10 seconds in the future
seq.setInternalID(TimeAndCounterIDGenerator.ID_MASK); // 1 ID about to explode
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();
seq.setInternalDate(System.currentTimeMillis() - 10000l); // 10 seconds in the past
long timeMark = seq.getInternalTimeMark();
seq.setInternalID(TimeAndCounterIDGenerator.ID_MASK); // 1 ID about to explode
// 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());
}