use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.
the class PagingStoreImplTest method testAddAndRemoveMessages.
@Test
public void testAddAndRemoveMessages() {
long id1 = RandomUtil.randomLong();
long id2 = RandomUtil.randomLong();
PageTransactionInfo trans = new PageTransactionInfoImpl(id2);
trans.setRecordID(id1);
// anything between 2 and 100
int nr1 = RandomUtil.randomPositiveInt() % 98 + 2;
for (int i = 0; i < nr1; i++) {
trans.increment(1, 0);
}
Assert.assertEquals(nr1, trans.getNumberOfMessages());
ActiveMQBuffer buffer = ActiveMQBuffers.fixedBuffer(trans.getEncodeSize());
trans.encode(buffer);
PageTransactionInfo trans2 = new PageTransactionInfoImpl(id1);
trans2.decode(buffer);
Assert.assertEquals(id2, trans2.getTransactionID());
Assert.assertEquals(nr1, trans2.getNumberOfMessages());
}
use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.
the class PagingStoreImplTest method testDepageMultiplePages.
@Test
public void testDepageMultiplePages() throws Exception {
SequentialFileFactory factory = new FakeSequentialFileFactory();
SimpleString destination = new SimpleString("test");
PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
PagingStoreImpl store = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE), getExecutorFactory().getExecutor(), true);
store.start();
Assert.assertEquals(0, store.getNumberOfPages());
store.startPaging();
Assert.assertEquals(1, store.getNumberOfPages());
List<ActiveMQBuffer> buffers = new ArrayList<>();
for (int i = 0; i < 10; i++) {
ActiveMQBuffer buffer = createRandomBuffer(i + 1L, 10);
buffers.add(buffer);
if (i == 5) {
store.forceAnotherPage();
}
Message msg = createMessage(i, store, destination, buffer);
final RoutingContextImpl ctx = new RoutingContextImpl(null);
Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock));
}
Assert.assertEquals(2, store.getNumberOfPages());
store.sync();
int sequence = 0;
for (int pageNr = 0; pageNr < 2; pageNr++) {
Page page = store.depage();
System.out.println("numberOfPages = " + store.getNumberOfPages());
page.open();
List<PagedMessage> msg = page.read(new NullStorageManager());
page.close();
Assert.assertEquals(5, msg.size());
for (int i = 0; i < 5; i++) {
Assert.assertEquals(sequence++, msg.get(i).getMessage().getMessageID());
ActiveMQTestBase.assertEqualsBuffers(18, buffers.get(pageNr * 5 + i), msg.get(i).getMessage().toCore().getBodyBuffer());
}
}
Assert.assertEquals(1, store.getNumberOfPages());
Assert.assertTrue(store.isPaging());
Message msg = createMessage(1, store, destination, buffers.get(0));
final RoutingContextImpl ctx = new RoutingContextImpl(null);
Assert.assertTrue(store.page(msg, ctx.getTransaction(), ctx.getContextListing(store.getStoreName()), lock));
Page newPage = store.depage();
newPage.open();
Assert.assertEquals(1, newPage.read(new NullStorageManager()).size());
newPage.delete(null);
Assert.assertEquals(1, store.getNumberOfPages());
Assert.assertTrue(store.isPaging());
Assert.assertNull(store.depage());
Assert.assertFalse(store.isPaging());
{
final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
Assert.assertFalse(store.page(msg, ctx2.getTransaction(), ctx2.getContextListing(store.getStoreName()), lock));
}
store.startPaging();
{
final RoutingContextImpl ctx2 = new RoutingContextImpl(null);
Assert.assertTrue(store.page(msg, ctx2.getTransaction(), ctx2.getContextListing(store.getStoreName()), lock));
}
Page page = store.depage();
page.open();
List<PagedMessage> msgs = page.read(new NullStorageManager());
Assert.assertEquals(1, msgs.size());
Assert.assertEquals(1L, msgs.get(0).getMessage().getMessageID());
ActiveMQTestBase.assertEqualsBuffers(18, buffers.get(0), msgs.get(0).getMessage().toCore().getBodyBuffer());
Assert.assertEquals(1, store.getNumberOfPages());
Assert.assertTrue(store.isPaging());
Assert.assertNull(store.depage());
Assert.assertEquals(0, store.getNumberOfPages());
page.open();
}
use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.
the class PagingStoreImplTest method testStore.
@Test
public void testStore() throws Exception {
SequentialFileFactory factory = new FakeSequentialFileFactory();
PagingStoreFactory storeFactory = new FakeStoreFactory(factory);
AddressSettings addressSettings = new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE);
PagingStore storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, addressSettings, getExecutorFactory().getExecutor(), true);
storeImpl.start();
Assert.assertEquals(0, storeImpl.getNumberOfPages());
storeImpl.startPaging();
Assert.assertEquals(1, storeImpl.getNumberOfPages());
List<ActiveMQBuffer> buffers = new ArrayList<>();
ActiveMQBuffer buffer = createRandomBuffer(0, 10);
buffers.add(buffer);
SimpleString destination = new SimpleString("test");
Message msg = createMessage(1, storeImpl, destination, buffer);
Assert.assertTrue(storeImpl.isPaging());
final RoutingContextImpl ctx = new RoutingContextImpl(null);
Assert.assertTrue(storeImpl.page(msg, ctx.getTransaction(), ctx.getContextListing(storeImpl.getStoreName()), lock));
Assert.assertEquals(1, storeImpl.getNumberOfPages());
storeImpl.sync();
storeImpl = new PagingStoreImpl(PagingStoreImplTest.destinationTestName, null, 100, createMockManager(), createStorageManagerMock(), factory, storeFactory, PagingStoreImplTest.destinationTestName, addressSettings, getExecutorFactory().getExecutor(), true);
storeImpl.start();
Assert.assertEquals(1, storeImpl.getNumberOfPages());
}
use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.
the class NettyAcceptorTest method testStartStop.
@Test
public void testStartStop() throws Exception {
BufferHandler handler = new BufferHandler() {
@Override
public void bufferReceived(final Object connectionID, final ActiveMQBuffer buffer) {
}
};
Map<String, Object> params = new HashMap<>();
ServerConnectionLifeCycleListener listener = new ServerConnectionLifeCycleListener() {
@Override
public void connectionException(final Object connectionID, final ActiveMQException me) {
}
@Override
public void connectionDestroyed(final Object connectionID) {
}
@Override
public void connectionCreated(final ActiveMQComponent component, final Connection connection, final ProtocolManager protocol) {
}
@Override
public void connectionReadyForWrites(Object connectionID, boolean ready) {
}
};
pool2 = Executors.newScheduledThreadPool(ActiveMQDefaultConfiguration.getDefaultScheduledThreadPoolMaxSize(), ActiveMQThreadFactory.defaultThreadFactory());
NettyAcceptor acceptor = new NettyAcceptor("netty", null, params, handler, listener, pool2, new HashMap<String, ProtocolManager>());
addActiveMQComponent(acceptor);
acceptor.start();
Assert.assertTrue(acceptor.isStarted());
acceptor.stop();
Assert.assertFalse(acceptor.isStarted());
ActiveMQTestBase.checkFreePort(TransportConstants.DEFAULT_PORT);
acceptor.start();
Assert.assertTrue(acceptor.isStarted());
acceptor.stop();
Assert.assertFalse(acceptor.isStarted());
ActiveMQTestBase.checkFreePort(TransportConstants.DEFAULT_PORT);
pool2.shutdown();
pool2.awaitTermination(1, TimeUnit.SECONDS);
}
use of org.apache.activemq.artemis.api.core.ActiveMQBuffer in project activemq-artemis by apache.
the class SequentialFileFactoryTestBase method testWriteandRead.
@Test
public void testWriteandRead() throws Exception {
SequentialFile sf = factory.createSequentialFile("write.amq");
sf.open();
String s1 = "aardvark";
byte[] bytes1 = s1.getBytes(StandardCharsets.UTF_8);
ActiveMQBuffer bb1 = wrapBuffer(bytes1);
String s2 = "hippopotamus";
byte[] bytes2 = s2.getBytes(StandardCharsets.UTF_8);
ActiveMQBuffer bb2 = wrapBuffer(bytes2);
String s3 = "echidna";
byte[] bytes3 = s3.getBytes(StandardCharsets.UTF_8);
ActiveMQBuffer bb3 = wrapBuffer(bytes3);
long initialPos = sf.position();
sf.write(bb1, true);
long bytesWritten = sf.position() - initialPos;
Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesWritten);
initialPos = sf.position();
sf.write(bb2, true);
bytesWritten = sf.position() - initialPos;
Assert.assertEquals(calculateRecordSize(bytes2.length, factory.getAlignment()), bytesWritten);
initialPos = sf.position();
sf.write(bb3, true);
bytesWritten = sf.position() - initialPos;
Assert.assertEquals(calculateRecordSize(bytes3.length, factory.getAlignment()), bytesWritten);
sf.position(0);
ByteBuffer rb1 = factory.newBuffer(bytes1.length);
ByteBuffer rb2 = factory.newBuffer(bytes2.length);
ByteBuffer rb3 = factory.newBuffer(bytes3.length);
int bytesRead = sf.read(rb1);
Assert.assertEquals(calculateRecordSize(bytes1.length, factory.getAlignment()), bytesRead);
for (int i = 0; i < bytes1.length; i++) {
Assert.assertEquals(bytes1[i], rb1.get(i));
}
bytesRead = sf.read(rb2);
Assert.assertEquals(calculateRecordSize(bytes2.length, factory.getAlignment()), bytesRead);
for (int i = 0; i < bytes2.length; i++) {
Assert.assertEquals(bytes2[i], rb2.get(i));
}
bytesRead = sf.read(rb3);
Assert.assertEquals(calculateRecordSize(bytes3.length, factory.getAlignment()), bytesRead);
for (int i = 0; i < bytes3.length; i++) {
Assert.assertEquals(bytes3[i], rb3.get(i));
}
sf.close();
}
Aggregations