use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.
the class TimedBufferTest method testTimeOnTimedBuffer.
@Test
public void testTimeOnTimedBuffer() throws Exception {
final ReusableLatch latchFlushed = new ReusableLatch(0);
final AtomicInteger flushes = new AtomicInteger(0);
class TestObserver implements TimedBufferObserver {
@Override
public void flushBuffer(final ByteBuffer buffer, final boolean sync, final List<IOCallback> callbacks) {
for (IOCallback callback : callbacks) {
callback.done();
}
}
/* (non-Javadoc)
* @see org.apache.activemq.artemis.utils.timedbuffer.TimedBufferObserver#newBuffer(int, int)
*/
@Override
public ByteBuffer newBuffer(final int minSize, final int maxSize) {
return ByteBuffer.allocate(maxSize);
}
@Override
public int getRemainingBytes() {
return 1024 * 1024;
}
}
TimedBuffer timedBuffer = new TimedBuffer(null, 100, TimedBufferTest.ONE_SECOND_IN_NANOS / 2, false);
timedBuffer.start();
TestObserver observer = new TestObserver();
timedBuffer.setObserver(observer);
int x = 0;
byte[] bytes = new byte[10];
for (int j = 0; j < 10; j++) {
bytes[j] = ActiveMQTestBase.getSamplebyte(x++);
}
ActiveMQBuffer buff = ActiveMQBuffers.wrappedBuffer(bytes);
IOCallback callback = new IOCallback() {
@Override
public void done() {
System.out.println("done");
latchFlushed.countDown();
}
@Override
public void onError(int errorCode, String errorMessage) {
}
};
try {
latchFlushed.setCount(2);
// simulating a low load period
timedBuffer.addBytes(buff, true, callback);
Thread.sleep(1000);
timedBuffer.addBytes(buff, true, callback);
Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS));
latchFlushed.setCount(5);
flushes.set(0);
// Sending like crazy... still some wait (1 millisecond) between each send..
long time = System.currentTimeMillis();
for (int i = 0; i < 5; i++) {
timedBuffer.addBytes(buff, true, callback);
Thread.sleep(1);
}
Assert.assertTrue(latchFlushed.await(5, TimeUnit.SECONDS));
// The purpose of the timed buffer is to batch writes up to a millisecond.. or up to the size of the buffer.
Assert.assertTrue("Timed Buffer is not batching accordingly, it was expected to take at least 500 seconds batching multiple writes while it took " + (System.currentTimeMillis() - time) + " milliseconds", System.currentTimeMillis() - time >= 450);
// ^^ there are some discounts that can happen inside the timed buffer that are still considered valid (like discounting the time it took to perform the operation itself
// for that reason the test has been failing (before this commit) at 499 or 480 milliseconds. So, I'm using a reasonable number close to 500 milliseconds that would still be valid for the test
// it should be in fact only writing once..
// i will set for 3 just in case there's a GC or anything else happening on the test
Assert.assertTrue("Too many writes were called", flushes.get() <= 3);
} finally {
timedBuffer.stop();
}
}
use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.
the class FileStoreMonitorTest method testScheduler.
@Test
public void testScheduler() throws Exception {
FileStoreMonitor storeMonitor = new FileStoreMonitor(scheduledExecutorService, executorService, 20, TimeUnit.MILLISECONDS, 0.9, null);
final ReusableLatch latch = new ReusableLatch(5);
storeMonitor.addStore(getTestDirfile());
storeMonitor.addCallback(new FileStoreMonitor.Callback() {
@Override
public void tick(FileStore store, double usage) {
System.out.println("TickS::" + usage);
latch.countDown();
}
@Override
public void over(FileStore store, double usage) {
}
@Override
public void under(FileStore store, double usage) {
}
});
storeMonitor.start();
Assert.assertTrue(latch.await(1, TimeUnit.SECONDS));
storeMonitor.stop();
latch.setCount(1);
Assert.assertFalse(latch.await(100, TimeUnit.MILLISECONDS));
// FileStoreMonitor monitor = new FileStoreMonitor()
}
use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.
the class MixupCompactorTestBase method setUp.
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Override
@Before
public void setUp() throws Exception {
super.setUp();
tCompact = null;
startedCompactingLatch = new ReusableLatch(1);
releaseCompactingLatch = new ReusableLatch(1);
setup(2, 60 * 1024, false);
}
use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.
the class ReusableLatchTest method testTimeout.
@Test
public void testTimeout() throws Exception {
ReusableLatch latch = new ReusableLatch();
latch.countUp();
long start = System.currentTimeMillis();
Assert.assertFalse(latch.await(1000));
long end = System.currentTimeMillis();
Assert.assertTrue("Timeout didn't work correctly", end - start >= 1000 && end - start < 2000);
}
use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.
the class RedeployTest method testRedeployAddressQueue.
@Test
public void testRedeployAddressQueue() throws Exception {
Path brokerXML = getTestDirfile().toPath().resolve("broker.xml");
URL url1 = RedeployTest.class.getClassLoader().getResource("reload-address-queues.xml");
URL url2 = RedeployTest.class.getClassLoader().getResource("reload-address-queues-updated.xml");
Files.copy(url1.openStream(), brokerXML);
EmbeddedJMS embeddedJMS = new EmbeddedJMS();
embeddedJMS.setConfigResourcePath(brokerXML.toUri().toString());
embeddedJMS.start();
final ReusableLatch latch = new ReusableLatch(1);
Runnable tick = new Runnable() {
@Override
public void run() {
latch.countDown();
}
};
embeddedJMS.getActiveMQServer().getReloadManager().setTick(tick);
try {
latch.await(10, TimeUnit.SECONDS);
Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_address_removal_no_queue"));
Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_address_removal"));
Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_queue_removal"));
Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_removal").contains("config_test_queue_removal_queue_1"));
Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_removal").contains("config_test_queue_removal_queue_2"));
Assert.assertNotNull(getAddressInfo(embeddedJMS, "permanent_test_address_removal"));
Assert.assertNotNull(getAddressInfo(embeddedJMS, "permanent_test_queue_removal"));
Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "permanent_test_queue_removal").contains("permanent_test_queue_removal_queue_1"));
Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "permanent_test_queue_removal").contains("permanent_test_queue_removal_queue_2"));
Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_queue_change"));
Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_change").contains("config_test_queue_change_queue"));
Assert.assertEquals(10, getQueue(embeddedJMS, "config_test_queue_change_queue").getMaxConsumers());
Assert.assertEquals(false, getQueue(embeddedJMS, "config_test_queue_change_queue").isPurgeOnNoConsumers());
Files.copy(url2.openStream(), brokerXML, StandardCopyOption.REPLACE_EXISTING);
brokerXML.toFile().setLastModified(System.currentTimeMillis() + 1000);
latch.setCount(1);
embeddedJMS.getActiveMQServer().getReloadManager().setTick(tick);
latch.await(10, TimeUnit.SECONDS);
Assert.assertNull(getAddressInfo(embeddedJMS, "config_test_address_removal_no_queue"));
Assert.assertNull(getAddressInfo(embeddedJMS, "config_test_address_removal"));
Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_queue_removal"));
Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_removal").contains("config_test_queue_removal_queue_1"));
Assert.assertFalse(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_removal").contains("config_test_queue_removal_queue_2"));
Assert.assertNotNull(getAddressInfo(embeddedJMS, "permanent_test_address_removal"));
Assert.assertNotNull(getAddressInfo(embeddedJMS, "permanent_test_queue_removal"));
Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "permanent_test_queue_removal").contains("permanent_test_queue_removal_queue_1"));
Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "permanent_test_queue_removal").contains("permanent_test_queue_removal_queue_2"));
Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_queue_change"));
Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_change").contains("config_test_queue_change_queue"));
Assert.assertEquals(1, getQueue(embeddedJMS, "config_test_queue_change_queue").getMaxConsumers());
Assert.assertEquals(true, getQueue(embeddedJMS, "config_test_queue_change_queue").isPurgeOnNoConsumers());
} finally {
embeddedJMS.stop();
}
}
Aggregations