[jboss-cvs] JBoss Messaging SVN: r3414 - in branches/Branch_New_Persistence/src/main/org/jboss: messaging/util and 24 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Dec 5 14:44:34 EST 2007
Author: timfox
Date: 2007-12-05 14:44:34 -0500 (Wed, 05 Dec 2007)
New Revision: 3414
Added:
branches/Branch_New_Persistence/src/main/org/jboss/messaging/util/Pair.java
branches/Branch_New_Persistence/src/main/org/jboss/test/
branches/Branch_New_Persistence/src/main/org/jboss/test/concurrent/
branches/Branch_New_Persistence/src/main/org/jboss/test/concurrent/messaging/
branches/Branch_New_Persistence/src/main/org/jboss/test/concurrent/messaging/newcore/
branches/Branch_New_Persistence/src/main/org/jboss/test/concurrent/messaging/newcore/impl/
branches/Branch_New_Persistence/src/main/org/jboss/test/concurrent/messaging/newcore/impl/QueueTest.java
branches/Branch_New_Persistence/src/main/org/jboss/test/integration/
branches/Branch_New_Persistence/src/main/org/jboss/test/integration/messaging/
branches/Branch_New_Persistence/src/main/org/jboss/test/integration/messaging/newcore/
branches/Branch_New_Persistence/src/main/org/jboss/test/integration/messaging/newcore/impl/
branches/Branch_New_Persistence/src/main/org/jboss/test/integration/messaging/newcore/impl/bdbje/
branches/Branch_New_Persistence/src/main/org/jboss/test/integration/messaging/newcore/impl/bdbje/RealBDBJEEnvironmentTest.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/FakeConsumer.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/FakeFilter.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/bdbje/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/bdbje/FakeBDBJEEnvironment.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/UnitTestCase.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/BDBJEPersistenceManagerTest.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/MessageTest.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/QueueTest.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/BDBJEEnvironmentTestBase.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/BDBSpeedTest.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/FakeBDBJEEnvironmentTest.java
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/timing/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/timing/messaging/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/timing/messaging/newcore/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/timing/messaging/newcore/impl/
branches/Branch_New_Persistence/src/main/org/jboss/test/unit/timing/messaging/newcore/impl/QueueTest.java
Log:
missing files
Added: branches/Branch_New_Persistence/src/main/org/jboss/messaging/util/Pair.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/messaging/util/Pair.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/messaging/util/Pair.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,43 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.messaging.util;
+
+/**
+ *
+ * A Pair
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class Pair<A, B>
+{
+ public Pair(A a, B b)
+ {
+ this.a = a;
+
+ this.b = b;
+ }
+
+ public A a;
+
+ public B b;
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/concurrent/messaging/newcore/impl/QueueTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/concurrent/messaging/newcore/impl/QueueTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/concurrent/messaging/newcore/impl/QueueTest.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,200 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.concurrent.messaging.newcore.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.intf.HandleStatus;
+import org.jboss.messaging.newcore.intf.MessageReference;
+import org.jboss.messaging.newcore.intf.Queue;
+import org.jboss.test.unit.fakes.messaging.newcore.impl.FakeConsumer;
+import org.jboss.test.unit.messaging.UnitTestCase;
+
+/**
+ *
+ * A concurrent QueueTest
+ *
+ * All the concurrent queue tests go in here
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class QueueTest extends UnitTestCase
+{
+ // The tests ----------------------------------------------------------------
+
+ /*
+ * Concurrent set consumer not busy, busy then, call deliver while messages are being added and consumed
+ */
+ public void testConcurrentAddsDeliver() throws Exception
+ {
+ Queue queue = new QueueImpl(1);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ final long testTime = 5000;
+
+ Sender sender = new Sender(queue, testTime);
+
+ Toggler toggler = new Toggler(queue, consumer, testTime);
+
+ sender.start();
+
+ toggler.start();
+
+ sender.join();
+
+ toggler.join();
+
+ consumer.setStatusImmediate(HandleStatus.HANDLED);
+
+ queue.deliver();
+
+ if (sender.getException() != null)
+ {
+ throw sender.getException();
+ }
+
+ if (toggler.getException() != null)
+ {
+ throw toggler.getException();
+ }
+
+ assertRefListsIdenticalRefs(sender.getReferences(), consumer.getReferences());
+
+ System.out.println("num refs: " + sender.getReferences().size());
+
+ System.out.println("num toggles: " + toggler.getNumToggles());
+
+ }
+
+ // Inner classes ---------------------------------------------------------------
+
+ class Sender extends Thread
+ {
+ private volatile Exception e;
+
+ private Queue queue;
+
+ private long testTime;
+
+ private volatile int i;
+
+ public Exception getException()
+ {
+ return e;
+ }
+
+ private List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ public List<MessageReference> getReferences()
+ {
+ return refs;
+ }
+
+ Sender(Queue queue, long testTime)
+ {
+ this.testTime = testTime;
+
+ this.queue = queue;
+ }
+
+ public void run()
+ {
+ long start = System.currentTimeMillis();
+
+ while (System.currentTimeMillis() - start < testTime)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ queue.addLast(ref);
+
+ refs.add(ref);
+
+ i++;
+ }
+ }
+ }
+
+ class Toggler extends Thread
+ {
+ private volatile Exception e;
+
+ private Queue queue;
+
+ private FakeConsumer consumer;
+
+ private long testTime;
+
+ private boolean toggle;
+
+ private volatile int numToggles;
+
+ public int getNumToggles()
+ {
+ return numToggles;
+ }
+
+ public Exception getException()
+ {
+ return e;
+ }
+
+ Toggler(Queue queue, FakeConsumer consumer, long testTime)
+ {
+ this.testTime = testTime;
+
+ this.queue = queue;
+
+ this.consumer = consumer;
+ }
+
+ public void run()
+ {
+ long start = System.currentTimeMillis();
+
+ while (System.currentTimeMillis() - start < testTime)
+ {
+ if (toggle)
+ {
+ consumer.setStatusImmediate(HandleStatus.BUSY);
+ }
+ else
+ {
+ consumer.setStatusImmediate(HandleStatus.HANDLED);
+
+ queue.deliver();
+ }
+ toggle = !toggle;
+
+ numToggles++;
+ }
+ }
+ }
+
+}
+
+
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/integration/messaging/newcore/impl/bdbje/RealBDBJEEnvironmentTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/integration/messaging/newcore/impl/bdbje/RealBDBJEEnvironmentTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/integration/messaging/newcore/impl/bdbje/RealBDBJEEnvironmentTest.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,48 @@
+package org.jboss.test.integration.messaging.newcore.impl.bdbje;
+
+import java.io.File;
+
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
+import org.jboss.messaging.newcore.impl.bdbje.integration.RealBDBJEEnvironment;
+import org.jboss.test.unit.messaging.newcore.impl.bdbje.BDBJEEnvironmentTestBase;
+
+/**
+ *
+ * A RealBDBJEEnvironmentTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class RealBDBJEEnvironmentTest extends BDBJEEnvironmentTestBase
+{
+ protected void setUp() throws Exception
+ {
+ createDir(ENV_DIR);
+
+ env = createEnvironment();
+
+ env.setEnvironmentPath(ENV_DIR);
+
+ env.start();
+
+ database = env.getDatabase("test-db");
+ }
+
+ protected BDBJEEnvironment createEnvironment() throws Exception
+ {
+ BDBJEEnvironment env = new RealBDBJEEnvironment(true);
+
+ env.setTransacted(true);
+
+ return env;
+ }
+
+ protected void createDir(String path)
+ {
+ File file = new File(path);
+
+ deleteDirectory(file);
+
+ file.mkdir();
+ }
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/FakeConsumer.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/FakeConsumer.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/FakeConsumer.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,144 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.unit.fakes.messaging.newcore.impl;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.intf.Consumer;
+import org.jboss.messaging.newcore.intf.Filter;
+import org.jboss.messaging.newcore.intf.HandleStatus;
+import org.jboss.messaging.newcore.intf.MessageReference;
+
+/**
+ *
+ * A FakeConsumer
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class FakeConsumer implements Consumer
+{
+ private HandleStatus statusToReturn = HandleStatus.HANDLED;
+
+ private HandleStatus newStatus;
+
+ private int delayCountdown = 0;
+
+ private LinkedList<MessageReference> references = new LinkedList<MessageReference>();
+
+ private Filter filter;
+
+ public FakeConsumer()
+ {
+ }
+
+ public FakeConsumer(Filter filter)
+ {
+ this.filter = filter;
+ }
+
+ public synchronized MessageReference waitForNextReference(long timeout)
+ {
+ while (references.isEmpty() && timeout > 0)
+ {
+ long start = System.currentTimeMillis();
+ try
+ {
+ wait();
+ }
+ catch (InterruptedException e)
+ {
+ }
+ timeout -= (System.currentTimeMillis() - start);
+ }
+
+ if (timeout <= 0)
+ {
+ throw new IllegalStateException("Timed out waiting for reference");
+ }
+
+ return references.removeFirst();
+ }
+
+ public synchronized void setStatusImmediate(HandleStatus newStatus)
+ {
+ this.statusToReturn = newStatus;
+ }
+
+ public synchronized void setStatusDelayed(HandleStatus newStatus, int numReferences)
+ {
+ this.newStatus = newStatus;
+
+ this.delayCountdown = numReferences;
+ }
+
+ public synchronized List<MessageReference> getReferences()
+ {
+ return references;
+ }
+
+ public synchronized void clearReferences()
+ {
+ this.references.clear();
+ }
+
+ public synchronized HandleStatus handle(MessageReference reference)
+ {
+ if (filter != null)
+ {
+ if (filter.match(reference.getMessage()))
+ {
+ references.addLast(reference);
+ notify();
+
+ return HandleStatus.HANDLED;
+ }
+ else
+ {
+ return HandleStatus.NO_MATCH;
+ }
+ }
+
+ if (newStatus != null)
+ {
+ if (delayCountdown == 0)
+ {
+ statusToReturn = newStatus;
+
+ newStatus = null;
+ }
+ else
+ {
+ delayCountdown--;
+ }
+ }
+
+ if (statusToReturn == HandleStatus.HANDLED)
+ {
+ references.addLast(reference);
+ notify();
+ }
+
+ return statusToReturn;
+ }
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/FakeFilter.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/FakeFilter.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/FakeFilter.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,72 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.unit.fakes.messaging.newcore.impl;
+
+import org.jboss.messaging.newcore.intf.Filter;
+import org.jboss.messaging.newcore.intf.Message;
+
+/**
+ *
+ * A FakeFilter
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class FakeFilter implements Filter
+{
+ private String headerName;
+
+ private Object headerValue;
+
+ public FakeFilter(String headerName, Object headerValue)
+ {
+ this.headerName = headerName;
+
+ this.headerValue = headerValue;
+ }
+
+ public FakeFilter()
+ {
+ }
+
+ public boolean match(Message message)
+ {
+ if (headerName != null)
+ {
+ Object value = message.getHeader(headerName);
+
+ if (value != null && headerValue.equals(value))
+ {
+ return true;
+ }
+
+ return false;
+ }
+
+ return true;
+ }
+
+ public String getFilterString()
+ {
+ return null;
+ }
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/bdbje/FakeBDBJEEnvironment.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/bdbje/FakeBDBJEEnvironment.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/fakes/messaging/newcore/impl/bdbje/FakeBDBJEEnvironment.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,501 @@
+package org.jboss.test.unit.fakes.messaging.newcore.impl.bdbje;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import javax.transaction.xa.Xid;
+
+import org.jboss.messaging.newcore.impl.bdbje.BDBJECursor;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEDatabase;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJETransaction;
+import org.jboss.messaging.util.Pair;
+
+/**
+ *
+ * A FakeBDBJEEnvironment
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class FakeBDBJEEnvironment implements BDBJEEnvironment
+{
+ private String environmentPath;
+
+ private Map<String, FakeBDBJETransaction> transactions = new HashMap<String, FakeBDBJETransaction>();
+
+ private boolean transacted;
+
+ private boolean syncOS;
+
+ private boolean syncVM;
+
+ private long memoryCacheSize = -1;
+
+ private boolean started;
+
+ private Map<Thread, FakeBDBJETransaction> implicitTXs = new ConcurrentHashMap<Thread, FakeBDBJETransaction>();
+
+ private Map<String, BDBJEDatabase> databases = new HashMap<String, BDBJEDatabase>();
+
+ public boolean isSyncOS()
+ {
+ return syncOS;
+ }
+
+ public void setSyncOS(boolean sync)
+ {
+ if (started)
+ {
+ throw new IllegalStateException("Cannot set SyncOS when started");
+ }
+ syncOS = sync;
+ }
+
+ public boolean isSyncVM()
+ {
+ return syncVM;
+ }
+
+ public void setSyncVM(boolean sync)
+ {
+ if (started)
+ {
+ throw new IllegalStateException("Cannot set SyncVM when started");
+ }
+ syncVM = sync;
+ }
+
+ public boolean isTransacted()
+ {
+ return transacted;
+ }
+
+ public void setTransacted(boolean transacted)
+ {
+ if (started)
+ {
+ throw new IllegalStateException("Cannot set transacted when started");
+ }
+ this.transacted = transacted;
+ }
+
+ public long getMemoryCacheSize()
+ {
+ return memoryCacheSize;
+ }
+
+ public void setMemoryCacheSize(long size)
+ {
+ if (started)
+ {
+ throw new IllegalStateException("Cannot set memory cache size when started");
+ }
+ this.memoryCacheSize = size;
+ }
+
+ public void setEnvironmentPath(String environmentPath)
+ {
+ if (started)
+ {
+ throw new IllegalStateException("Cannot set environmentPath when started");
+ }
+ this.environmentPath = environmentPath;
+ }
+
+ public String getEnvironmentPath()
+ {
+ return environmentPath;
+ }
+
+ public void start() throws Exception
+ {
+ if (environmentPath == null)
+ {
+ throw new IllegalStateException("Must set environmentPath before starting");
+ }
+ if (started)
+ {
+ throw new IllegalStateException("Already started");
+ }
+ started = true;
+ }
+
+ public void stop() throws Exception
+ {
+ if (!started)
+ {
+ throw new IllegalStateException("Not started");
+ }
+ started = false;
+ }
+
+ public BDBJETransaction createTransaction() throws Exception
+ {
+ return createTransactionInternal(null);
+ }
+
+ public synchronized BDBJEDatabase getDatabase(String databaseName) throws Exception
+ {
+ BDBJEDatabase database = databases.get(databaseName);
+
+ if (database == null)
+ {
+ database = new FakeBDBJEDatabase();
+
+ databases.put(databaseName, database);
+ }
+
+ return database;
+ }
+
+ public List<Xid> getInDoubtXids() throws Exception
+ {
+ List<Xid> xids = new ArrayList<Xid>();
+
+ for (FakeBDBJETransaction tx: transactions.values())
+ {
+ if (tx.isPrepared())
+ {
+ xids.add(tx.getXid());
+ }
+ }
+
+ return xids;
+ }
+
+ public void startWork(Xid xid) throws Exception
+ {
+ if (implicitTXs.get(Thread.currentThread()) != null)
+ {
+ throw new IllegalStateException("Already implicit transaction");
+ }
+
+ implicitTXs.put(Thread.currentThread(), createTransactionInternal(xid));
+ }
+
+ public void endWork(Xid xid, boolean fail) throws Exception
+ {
+ FakeBDBJETransaction removed = implicitTXs.remove(Thread.currentThread());
+
+ if (removed == null)
+ {
+ throw new IllegalStateException("No implicit tx");
+ }
+ }
+
+ private FakeBDBJETransaction findTXForXid(Xid xid)
+ {
+ for (FakeBDBJETransaction tx: transactions.values())
+ {
+ if (xid.equals(tx.xid))
+ {
+ return tx;
+ }
+ }
+ return null;
+ }
+
+ public void prepare(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = findTXForXid(xid);
+
+ if (tx == null)
+ {
+ throw new IllegalStateException("Cannot find tx for xid " + xid);
+ }
+
+ if (implicitTXs.containsKey(Thread.currentThread()))
+ {
+ throw new IllegalStateException("Work not ended");
+ }
+
+ tx.prepare();
+ }
+
+ public void commit(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = findTXForXid(xid);
+
+ if (tx == null)
+ {
+ throw new IllegalStateException("Cannot find tx for xid " + xid);
+ }
+
+ if (implicitTXs.containsKey(Thread.currentThread()))
+ {
+ throw new IllegalStateException("Work not ended");
+ }
+
+ tx.commit();
+ }
+
+ public void rollback(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = findTXForXid(xid);
+
+ if (tx == null)
+ {
+ throw new IllegalStateException("Cannot find tx for xid " + xid);
+ }
+
+ if (implicitTXs.containsKey(Thread.currentThread()))
+ {
+ throw new IllegalStateException("Work not ended");
+ }
+
+ tx.rollback();
+ }
+
+ // Private -----------------------------------------------------------------------
+
+ private FakeBDBJETransaction createTransactionInternal(Xid xid) throws Exception
+ {
+ FakeBDBJETransaction tx = new FakeBDBJETransaction(xid);
+
+ transactions.put(tx.id, tx);
+
+ return tx;
+ }
+
+ // Inner classes ------------------------------------------------------------------
+
+ private class FakeBDBJETransaction implements BDBJETransaction
+ {
+ private String id = java.util.UUID.randomUUID().toString();
+
+ private Xid xid;
+
+ private List<Action> actions = new ArrayList<Action>();
+
+ private boolean prepared;
+
+ FakeBDBJETransaction(Xid xid)
+ {
+ this.xid = xid;
+ }
+
+ public void commit() throws Exception
+ {
+ for (Action action : actions)
+ {
+ if (action.put)
+ {
+ action.database.put(null, action.id, action.bytes, action.offset, action.length);
+ }
+ else
+ {
+ action.database.remove(null, action.id);
+ }
+ }
+
+ actions.clear();
+
+ transactions.remove(id);
+ }
+
+ public void prepare() throws Exception
+ {
+ prepared = true;
+ }
+
+ public void rollback() throws Exception
+ {
+ actions.clear();
+
+ transactions.remove(id);
+ }
+
+ public boolean isPrepared()
+ {
+ return prepared;
+ }
+
+ public Xid getXid()
+ {
+ return xid;
+ }
+
+ }
+
+ private class Action
+ {
+ public BDBJEDatabase database;
+
+ public boolean put;
+
+ public long id;
+
+ public byte[] bytes;
+
+ public int offset;
+
+ public int length;
+
+ public Action(BDBJEDatabase database, boolean put, long id, byte[] bytes, int offset, int length)
+ {
+ this.database = database;
+
+ this.put = put;
+
+ this.id = id;
+
+ this.bytes = bytes;
+
+ this.offset = offset;
+
+ this.length = length;
+ }
+ }
+
+ private class FakeBDBJEDatabase implements BDBJEDatabase
+ {
+ private Map<Long, byte[]> store = new LinkedHashMap<Long, byte[]>();
+
+ private AtomicInteger cursorCount = new AtomicInteger(0);
+
+ public void close() throws Exception
+ {
+ if (cursorCount.get() != 0)
+ {
+ throw new IllegalStateException("Cannot close. There are cursors open");
+ }
+ }
+
+ public BDBJECursor cursor() throws Exception
+ {
+ return new FakeBDBJECursor();
+ }
+
+ private BDBJETransaction getTx(BDBJETransaction tx)
+ {
+ if (tx == null)
+ {
+ tx = implicitTXs.get(Thread.currentThread());
+ }
+ return tx;
+ }
+
+ public void put(BDBJETransaction tx, long id, byte[] bytes, int offset,
+ int length) throws Exception
+ {
+ tx = getTx(tx);
+
+ if (tx == null)
+ {
+ if (offset == 0 && bytes.length == length)
+ {
+ store.put(id, bytes);
+ }
+ else
+ {
+ byte[] currentBytes = store.get(id);
+
+ if (offset == currentBytes.length)
+ {
+ byte[] newBytes = new byte[currentBytes.length + bytes.length];
+
+ ByteBuffer buffer = ByteBuffer.wrap(newBytes);
+
+ buffer.put(currentBytes);
+
+ buffer.put(bytes);
+
+ store.put(id, newBytes);
+ }
+ else if (offset < currentBytes.length)
+ {
+ if (offset + length > currentBytes.length)
+ {
+ throw new IllegalStateException("Invalid offset/length");
+ }
+
+ byte[] newBytes = new byte[currentBytes.length - (length - bytes.length)];
+
+ System.arraycopy(currentBytes, 0, newBytes, 0, offset);
+
+ System.arraycopy(bytes, 0, newBytes, offset, bytes.length);
+
+ System.arraycopy(currentBytes, offset + length, newBytes,
+ offset + bytes.length, currentBytes.length - (offset + length));
+
+ store.put(id, newBytes);
+ }
+ else
+ {
+ throw new IllegalStateException("Invalid offset " + offset);
+ }
+ }
+ }
+ else
+ {
+ FakeBDBJETransaction ftx = (FakeBDBJETransaction)tx;
+
+ ftx.actions.add(new Action(this, true, id, bytes, offset, length));
+ }
+
+ }
+
+ public void remove(BDBJETransaction tx, long id) throws Exception
+ {
+ tx = getTx(tx);
+
+ if (tx == null)
+ {
+ store.remove(id);
+ }
+ else
+ {
+ FakeBDBJETransaction ftx = (FakeBDBJETransaction)tx;
+
+ ftx.actions.add(new Action(this, false, id, null, -1, -1));
+ }
+ }
+
+ public byte[] get(long id) throws Exception
+ {
+ return store.get(id);
+ }
+
+ public long size() throws Exception
+ {
+ return store.size();
+ }
+
+ private class FakeBDBJECursor implements BDBJECursor
+ {
+ private Iterator<Map.Entry<Long,byte[]>> iterator = store.entrySet().iterator();
+
+ FakeBDBJECursor()
+ {
+ cursorCount.incrementAndGet();
+ }
+
+ public void close() throws Exception
+ {
+ cursorCount.decrementAndGet();
+ }
+
+ public Pair<Long, byte[]> getNext() throws Exception
+ {
+ if (iterator.hasNext())
+ {
+ Map.Entry<Long,byte[]> entry = iterator.next();
+
+ return new Pair<Long, byte[]>(entry.getKey(), entry.getValue());
+ }
+ else
+ {
+ return null;
+ }
+ }
+ }
+ }
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/UnitTestCase.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/UnitTestCase.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/UnitTestCase.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,211 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.unit.messaging;
+
+import java.io.File;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.transaction.xa.Xid;
+
+import junit.framework.TestCase;
+
+import org.jboss.jms.tx.MessagingXid;
+import org.jboss.messaging.newcore.impl.MessageImpl;
+import org.jboss.messaging.newcore.intf.Message;
+import org.jboss.messaging.newcore.intf.MessageReference;
+import org.jboss.messaging.newcore.intf.Queue;
+
+/**
+ *
+ * Helper base class for our unit tests
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class UnitTestCase extends TestCase
+{
+ protected void assertByteArraysEquivalent(byte[] bytes1, byte[] bytes2)
+ {
+ if (bytes1.length != bytes2.length)
+ {
+ fail("Byte arrays different sizes bytes1: " + dumpBytes(bytes1) + " bytes2: " + dumpBytes(bytes2));
+ }
+
+ for (int i = 0; i < bytes1.length; i++)
+ {
+ if (bytes1[i] != bytes2[i])
+ {
+ fail("Byte arrays not equivalent: " + dumpBytes(bytes1) + " bytes2: " + dumpBytes(bytes2));
+ }
+ }
+ }
+
+ protected String dumpBytes(byte[] bytes)
+ {
+ StringBuffer buff = new StringBuffer();
+
+ buff.append(System.identityHashCode(bytes) + ", size: " + bytes.length + " [");
+
+ for (int i = 0; i < bytes.length; i++)
+ {
+ buff.append(bytes[i]);
+
+ if (i != bytes.length - 1)
+ {
+ buff.append(", ");
+ }
+ }
+
+ buff.append("]");
+
+ return buff.toString();
+ }
+
+ protected boolean deleteDirectory(File directory)
+ {
+ if (directory.isDirectory())
+ {
+ String[] files = directory.list();
+
+ for (int j = 0; j < files.length; j++)
+ {
+ if (!deleteDirectory(new File(directory, files[j])))
+ {
+ return false;
+ }
+ }
+ }
+
+ return directory.delete();
+ }
+
+ protected void assertRefListsIdenticalRefs(List<MessageReference> l1, List<MessageReference> l2)
+ {
+ if (l1.size() != l2.size())
+ {
+ fail("Lists different sizes: " + l1.size() + ", " + l2.size());
+ }
+
+ Iterator<MessageReference> iter1 = l1.iterator();
+ Iterator<MessageReference> iter2 = l2.iterator();
+
+ while (iter1.hasNext())
+ {
+ MessageReference o1 = iter1.next();
+ MessageReference o2 = iter2.next();
+
+ assertTrue(o1 == o2);
+ }
+ }
+
+ protected void assertRefListsEquivalent(List<MessageReference> l1, List<MessageReference> l2)
+ {
+ if (l1.size() != l2.size())
+ {
+ fail("Lists different sizes: " + l1.size() + ", " + l2.size());
+ }
+
+ Iterator<MessageReference> iter1 = l1.iterator();
+ Iterator<MessageReference> iter2 = l2.iterator();
+
+ while (iter1.hasNext())
+ {
+ MessageReference o1 = iter1.next();
+ MessageReference o2 = iter2.next();
+
+ assertEquals(o1.getMessage().getMessageID(), o2.getMessage().getMessageID());
+
+ assertEquals(o1.getScheduledDeliveryTime(), o2.getScheduledDeliveryTime());
+
+ assertEquals(o1.getDeliveryCount(), o2.getDeliveryCount());
+ }
+ }
+
+ protected Message generateMessage(long id)
+ {
+ Message message = new MessageImpl(id, 0, true, 0, System.currentTimeMillis(), (byte)4);
+
+ return message;
+ }
+
+ protected void assertEquivalent(Message msg1, Message msg2)
+ {
+ assertEquals(msg1.getMessageID(), msg2.getMessageID());
+
+ assertEquals(msg1.isReliable(), msg2.isReliable());
+
+ assertEquals(msg1.getExpiration(), msg2.getExpiration());
+
+ assertEquals(msg1.getTimestamp(), msg2.getTimestamp());
+
+ assertEquals(msg1.getPriority(), msg2.getPriority());
+
+ assertEquals(msg1.getType(), msg2.getType());
+
+ if (msg1.getPayload() == null)
+ {
+ assertNull(msg2.getPayload());
+ }
+ else
+ {
+ assertByteArraysEquivalent(msg1.getPayload(), msg2.getPayload());
+ }
+
+ assertMapsEquivalent(msg1.getHeaders(), msg2.getHeaders());
+
+ assertEquals(msg1.getReferences().size(), msg2.getReferences().size());
+
+ for (int i = 0; i < msg1.getReferences().size(); i++)
+ {
+ MessageReference ref1 = msg1.getReferences().get(i);
+
+ MessageReference ref2 = msg2.getReferences().get(i);
+
+ assertEquals(ref1.getScheduledDeliveryTime(), ref2.getScheduledDeliveryTime());
+
+ assertEquals(ref1.getDeliveryCount(), ref2.getDeliveryCount());
+ }
+ }
+
+ protected void assertMapsEquivalent(Map<String, Object> headers1, Map<String, Object> headers2)
+ {
+ assertEquals(headers1.size(), headers2.size());
+
+ for (Map.Entry<String, Object> entry : headers1.entrySet())
+ {
+ assertEquals(entry.getValue(), headers2.get(entry.getKey()));
+ }
+ }
+
+ protected Xid generateXid()
+ {
+ String id = java.util.UUID.randomUUID().toString();
+
+ Xid xid = new MessagingXid("blah".getBytes(), 123, id.getBytes());
+
+ return xid;
+ }
+
+
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/BDBJEPersistenceManagerTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/BDBJEPersistenceManagerTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/BDBJEPersistenceManagerTest.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,681 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.unit.messaging.newcore.impl;
+
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.jboss.messaging.newcore.impl.MessageImpl;
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEDatabase;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEPersistenceManager;
+import org.jboss.messaging.newcore.intf.Message;
+import org.jboss.messaging.newcore.intf.MessageReference;
+import org.jboss.messaging.newcore.intf.Queue;
+import org.jboss.test.unit.fakes.messaging.newcore.impl.bdbje.FakeBDBJEEnvironment;
+import org.jboss.test.unit.messaging.UnitTestCase;
+
+/**
+ *
+ * A BDBJEPersistenceManagerTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class BDBJEPersistenceManagerTest extends UnitTestCase
+{
+ protected static final String ENV_DIR = "test-env";
+
+ protected BDBJEPersistenceManager pm;
+
+ protected BDBJEEnvironment bdb;
+
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ bdb = new FakeBDBJEEnvironment();
+
+ pm = new BDBJEPersistenceManager(bdb, ENV_DIR);
+
+ pm.start();
+ }
+
+ protected void tearDown() throws Exception
+ {
+ super.tearDown();
+
+ pm.stop();
+ }
+
+ // The tests ----------------------------------------------------------------
+
+ public void testCommitMessage() throws Exception
+ {
+ Message m = generateMessage(1);
+
+ Queue queue = new QueueImpl(67);
+
+ m.createReference(queue);
+
+ m.createReference(queue);
+
+ m.createReference(queue);
+
+ m.createReference(queue);
+
+ pm.commitMessage(m);
+
+ BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
+
+ BDBJEDatabase refDB = bdb.getDatabase(BDBJEPersistenceManager.REFERENCE_DB_NAME);
+
+ byte[] msgBytes = msgDB.get(m.getMessageID());
+
+ assertNotNull(msgBytes);
+
+ byte[] refBytes = refDB.get(m.getMessageID());
+
+ assertNotNull(refBytes);
+
+ Map<Long, Queue> queues = new HashMap<Long, Queue>();
+
+ queues.put(queue.getID(), queue);
+
+ Message m2 = extractMessage(queues, m.getMessageID(), msgBytes, refBytes);
+
+ assertEquivalent(m, m2);
+ }
+
+ public void testCommitMessages() throws Exception
+ {
+ List<Message> msgs = new ArrayList<Message>();
+
+ final int numMessages = 10;
+
+ Queue queue = new QueueImpl(67);
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ Message m = generateMessage(i);
+
+ m.createReference(queue);
+
+ m.createReference(queue);
+
+ m.createReference(queue);
+
+ m.createReference(queue);
+
+ msgs.add(m);
+ }
+
+ pm.commitMessages(msgs);
+
+ BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
+
+ BDBJEDatabase refDB = bdb.getDatabase(BDBJEPersistenceManager.REFERENCE_DB_NAME);
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ Message m = msgs.get(i);
+
+ byte[] msgBytes = msgDB.get(m.getMessageID());
+
+ assertNotNull(msgBytes);
+
+ byte[] refBytes = refDB.get(m.getMessageID());
+
+ assertNotNull(refBytes);
+
+ Map<Long, Queue> queues = new HashMap<Long, Queue>();
+
+ queues.put(queue.getID(), queue);
+
+ Message m2 = extractMessage(queues, m.getMessageID(), msgBytes, refBytes);
+
+ assertEquivalent(m, m2);
+ }
+ }
+
+ public void testDeleteReference() throws Exception
+ {
+ Message m = generateMessage(1);
+
+ Queue queue = new QueueImpl(67);
+
+ MessageReference ref1 = m.createReference(queue);
+
+ MessageReference ref2 = m.createReference(queue);
+
+ MessageReference ref3 = m.createReference(queue);
+
+ MessageReference ref4 = m.createReference(queue);
+
+ pm.commitMessage(m);
+
+ BDBJEDatabase msgDB = bdb.getDatabase(BDBJEPersistenceManager.MESSAGE_DB_NAME);
+
+ BDBJEDatabase refDB = bdb.getDatabase(BDBJEPersistenceManager.REFERENCE_DB_NAME);
+
+ byte[] msgBytes = msgDB.get(m.getMessageID());
+
+ assertNotNull(msgBytes);
+
+ byte[] refBytes = refDB.get(m.getMessageID());
+
+ assertNotNull(refBytes);
+
+ Map<Long, Queue> queues = new HashMap<Long, Queue>();
+
+ queues.put(queue.getID(), queue);
+
+ Message m2 = extractMessage(queues, m.getMessageID(), msgBytes, refBytes);
+
+ assertEquivalent(m, m2);
+
+ assertEquals(4, m2.getReferences().size());
+
+
+ pm.deleteReference(ref2);
+
+ msgBytes = msgDB.get(m.getMessageID());
+
+ assertNotNull(msgBytes);
+
+ refBytes = refDB.get(m.getMessageID());
+
+ assertNotNull(refBytes);
+
+ queues = new HashMap<Long, Queue>();
+
+ queues.put(queue.getID(), queue);
+
+ m2 = extractMessage(queues, m.getMessageID(), msgBytes, refBytes);
+
+ assertEquivalent(m, m2);
+
+ assertEquals(3, m2.getReferences().size());
+
+
+ pm.deleteReference(ref1);
+
+ msgBytes = msgDB.get(m.getMessageID());
+
+ assertNotNull(msgBytes);
+
+ refBytes = refDB.get(m.getMessageID());
+
+ assertNotNull(refBytes);
+
+ queues = new HashMap<Long, Queue>();
+
+ queues.put(queue.getID(), queue);
+
+ m2 = extractMessage(queues, m.getMessageID(), msgBytes, refBytes);
+
+ assertEquivalent(m, m2);
+
+ assertEquals(2, m2.getReferences().size());
+
+
+ pm.deleteReference(ref3);
+
+ msgBytes = msgDB.get(m.getMessageID());
+
+ assertNotNull(msgBytes);
+
+ refBytes = refDB.get(m.getMessageID());
+
+ assertNotNull(refBytes);
+
+ queues = new HashMap<Long, Queue>();
+
+ queues.put(queue.getID(), queue);
+
+ m2 = extractMessage(queues, m.getMessageID(), msgBytes, refBytes);
+
+ assertEquivalent(m, m2);
+
+ assertEquals(1, m2.getReferences().size());
+
+
+ pm.deleteReference(ref4);
+
+ msgBytes = msgDB.get(m.getMessageID());
+
+ assertNull(msgBytes);
+
+ refBytes = refDB.get(m.getMessageID());
+
+ assertNull(refBytes);
+ }
+
+ public void testDeleteReferences() throws Exception
+ {
+ //pm.deleteReferences(references)
+
+ // pm.
+ }
+
+
+
+
+//
+// public void testAddMultipleReferencesForSameMessageNoTx() throws Exception
+// {
+// FakeBDBJEIntf fakeBDB = new FakeBDBJEIntf();
+//
+// PersistenceManager pm = new BDBJEPersistenceManager(fakeBDB);
+//
+// MessageReference ref1 = generateReference(1);
+//
+// Queue queue = new QueueImpl(67);
+//
+// pm.addReference(null, queue, ref1);
+//
+// MessageReference ref2 = ref1.getMessage().createReference();
+//
+// pm.addReference(null, queue, ref2);
+//
+// MessageReference ref3 = ref1.getMessage().createReference();
+//
+// pm.addReference(null, queue, ref3);
+//
+// MessageReference ref4 = ref1.getMessage().createReference();
+//
+// pm.addReference(null, queue, ref4);
+//
+// byte[] bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNotNull(bytes);
+//
+// Message message = extractMessage(ref1.getMessage().getMessageID(), bytes);
+//
+// assertEquals(4, message.getReferences().size());
+//
+// assertTrue(message.isReliable());
+//
+// assertEquivalent(ref1.getMessage(), message);
+// }
+//
+// public void testRemoveReferenceNoTx() throws Exception
+// {
+// FakeBDBJEIntf fakeBDB = new FakeBDBJEIntf();
+//
+// PersistenceManager pm = new BDBJEPersistenceManager(fakeBDB);
+//
+// MessageReference ref1 = generateReference(1);
+//
+// Queue queue = new QueueImpl(67);
+//
+// pm.addReference(null, queue, ref1);
+//
+// MessageReference ref2 = ref1.getMessage().createReference();
+//
+// pm.addReference(null, queue, ref2);
+//
+// MessageReference ref3 = ref1.getMessage().createReference();
+//
+// pm.addReference(null, queue, ref3);
+//
+// MessageReference ref4 = ref1.getMessage().createReference();
+//
+// pm.addReference(null, queue, ref4);
+//
+// byte[] bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNotNull(bytes);
+//
+// Message message = extractMessage(ref1.getMessage().getMessageID(), bytes);
+//
+// assertEquals(4, message.getReferences().size());
+//
+// assertTrue(message.isReliable());
+//
+// assertEquivalent(ref1.getMessage(), message);
+//
+//
+// pm.removeReference(null, queue, ref3);
+//
+// bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNotNull(bytes);
+//
+// message = extractMessage(ref1.getMessage().getMessageID(), bytes);
+//
+// assertEquals(3, message.getReferences().size());
+//
+// assertTrue(message.isReliable());
+//
+// assertEquivalent(ref1.getMessage(), message);
+//
+//
+// pm.removeReference(null, queue, ref1);
+//
+// bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNotNull(bytes);
+//
+// message = extractMessage(ref1.getMessage().getMessageID(), bytes);
+//
+// assertEquals(2, message.getReferences().size());
+//
+// assertTrue(message.isReliable());
+//
+// assertEquivalent(ref1.getMessage(), message);
+//
+//
+// pm.removeReference(null, queue, ref2);
+//
+// bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNotNull(bytes);
+//
+// message = extractMessage(ref1.getMessage().getMessageID(), bytes);
+//
+// assertEquals(1, message.getReferences().size());
+//
+// assertTrue(message.isReliable());
+//
+// assertEquivalent(ref1.getMessage(), message);
+//
+//
+// pm.removeReference(null, queue, ref4);
+//
+// bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNull(bytes);
+//
+//
+// assertTrue(fakeBDB.getStore().isEmpty());
+// }
+//
+// public void testUpdateDeliveryCount() throws Exception
+// {
+// FakeBDBJEIntf fakeBDB = new FakeBDBJEIntf();
+//
+// PersistenceManager pm = new BDBJEPersistenceManager(fakeBDB);
+//
+// MessageReference ref1 = generateReference(1);
+//
+// Queue queue = new QueueImpl(67);
+//
+// pm.addReference(null, queue, ref1);
+//
+// MessageReference ref2 = ref1.getMessage().createReference();
+//
+// pm.addReference(null, queue, ref2);
+//
+// MessageReference ref3 = ref1.getMessage().createReference();
+//
+// pm.addReference(null, queue, ref3);
+//
+// MessageReference ref4 = ref1.getMessage().createReference();
+//
+// pm.addReference(null, queue, ref4);
+//
+// ref2.setDeliveryCount(765);
+//
+// pm.updateDeliveryCount(queue, ref2);
+//
+// byte[] bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNotNull(bytes);
+//
+// Message message = extractMessage(ref1.getMessage().getMessageID(), bytes);
+//
+// assertEquals(4, message.getReferences().size());
+//
+// assertTrue(message.isReliable());
+//
+// assertEquivalent(ref1.getMessage(), message);
+//
+// assertEquals(765, ref2.getDeliveryCount());
+//
+// assertEquals(ref2.getDeliveryCount(), message.getReferences().get(1).getDeliveryCount());
+//
+// ref4.setDeliveryCount(10101);
+//
+// pm.updateDeliveryCount(queue, ref4);
+//
+// bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNotNull(bytes);
+//
+// message = extractMessage(ref1.getMessage().getMessageID(), bytes);
+//
+// assertEquals(4, message.getReferences().size());
+//
+// assertTrue(message.isReliable());
+//
+// assertEquivalent(ref1.getMessage(), message);
+//
+// assertEquals(10101, ref4.getDeliveryCount());
+//
+// assertEquals(ref4.getDeliveryCount(), message.getReferences().get(3).getDeliveryCount());
+//
+// assertEquals(ref1.getDeliveryCount(), message.getReferences().get(0).getDeliveryCount());
+//
+// assertEquals(ref3.getDeliveryCount(), message.getReferences().get(2).getDeliveryCount());
+// }
+//
+// public void testAddReferenceTxCommit() throws Exception
+// {
+// FakeBDBJEIntf fakeBDB = new FakeBDBJEIntf();
+//
+// PersistenceManager pm = new BDBJEPersistenceManager(fakeBDB);
+//
+// MessageReference ref1 = generateReference(1);
+//
+// Queue queue = new QueueImpl(67);
+//
+// PersistenceTransaction tx = pm.createTransaction();
+//
+// pm.addReference(tx, queue, ref1);
+//
+// byte[] bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNull(bytes);
+//
+// tx.commit();
+//
+// bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNotNull(bytes);
+//
+// Message message = extractMessage(ref1.getMessage().getMessageID(), bytes);
+//
+// assertTrue(message.isReliable());
+//
+// assertEquivalent(ref1.getMessage(), message);
+// }
+//
+// public void testAddReferenceTxRollback() throws Exception
+// {
+// FakeBDBJEIntf fakeBDB = new FakeBDBJEIntf();
+//
+// PersistenceManager pm = new BDBJEPersistenceManager(fakeBDB);
+//
+// MessageReference ref1 = generateReference(1);
+//
+// Queue queue = new QueueImpl(67);
+//
+// PersistenceTransaction tx = pm.createTransaction();
+//
+// pm.addReference(tx, queue, ref1);
+//
+// byte[] bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNull(bytes);
+//
+// tx.rollback();
+//
+// bytes = fakeBDB.getStore().get(ref1.getMessage().getMessageID());
+//
+// assertNull(bytes);
+// }
+//
+// /*
+// test adding different refs for different queues in same tx
+//
+// plus removing refs for different queues in same tx
+//
+// test loading queues (multiple quuees)
+//
+// test loading queues with different queues holding refs for same message (like subs)
+// */
+//
+// public void testLoadQueues() throws Exception
+// {
+// Queue queue1 = new QueueImpl(1);
+//
+// Queue queue2 = new QueueImpl(2);
+//
+// Queue queue3 = new QueueImpl(3);
+//
+// FakeBDBJEIntf fakeBDB = new FakeBDBJEIntf();
+//
+// PersistenceManager pm = new BDBJEPersistenceManager(fakeBDB);
+//
+// MessageReference ref1 = generateReference(1);
+// MessageReference ref2 = generateReference(2);
+// MessageReference ref3 = generateReference(3);
+// MessageReference ref4 = generateReference(4);
+// MessageReference ref5 = generateReference(5);
+// MessageReference ref6 = generateReference(6);
+// MessageReference ref7 = generateReference(7);
+// MessageReference ref8 = generateReference(8);
+// MessageReference ref9 = generateReference(9);
+// MessageReference ref10 = generateReference(10);
+//
+// pm.addReference(null, queue1, ref1);
+// pm.addReference(null, queue1, ref2);
+// pm.addReference(null, queue1, ref3);
+//
+// pm.addReference(null, queue2, ref4);
+// pm.addReference(null, queue2, ref5);
+// pm.addReference(null, queue2, ref6);
+//
+// pm.addReference(null, queue3, ref7);
+// pm.addReference(null, queue3, ref8);
+// pm.addReference(null, queue3, ref9);
+// pm.addReference(null, queue3, ref10);
+//
+// assertEquals(10, fakeBDB.getStore().size());
+//
+// Map<Long, Queue> map = new HashMap<Long, Queue>();
+//
+// map.put(queue1.getID(), queue1);
+// map.put(queue2.getID(), queue2);
+// map.put(queue3.getID(), queue3);
+//
+// pm.loadQueues(map);
+//
+// assertEquals(3, queue1.getMessageCount());
+// assertEquals(3, queue2.getMessageCount());
+// assertEquals(4, queue3.getMessageCount());
+//
+// List<MessageReference> refs = new ArrayList<MessageReference>();
+//
+// refs.add(ref1);
+// refs.add(ref2);
+// refs.add(ref3);
+// assertRefListsEquivalent(queue1.list(null), refs);
+//
+// refs.clear();
+// refs.add(ref4);
+// refs.add(ref5);
+// refs.add(ref6);
+// assertRefListsEquivalent(queue2.list(null), refs);
+//
+// refs.clear();
+// refs.add(ref7);
+// refs.add(ref8);
+// refs.add(ref9);
+// refs.add(ref10);
+// assertRefListsEquivalent(queue3.list(null), refs);
+//
+// }
+//
+// // Private --------------------------------------------------------------------
+
+ private Message extractMessage(Map<Long, Queue> queues, long id, byte[] msgBytes, byte[] refBytes) throws Exception
+ {
+ ByteBuffer buffer = ByteBuffer.wrap(msgBytes);
+
+ int type = buffer.getInt();
+
+ long expiration = buffer.getLong();
+
+ long timestamp = buffer.getLong();
+
+ byte priority = buffer.get();
+
+ int headerSize = buffer.getInt();
+
+ byte[] headers = new byte[headerSize];
+
+ buffer.get(headers);
+
+ int payloadSize = buffer.getInt();
+
+ byte[] payload = null;
+
+ if (payloadSize != 0)
+ {
+ payload = new byte[payloadSize];
+
+ buffer.get(payload);
+ }
+
+ Message message = new MessageImpl(id, type, true, expiration, timestamp, priority,
+ headers, payload);
+
+ buffer = ByteBuffer.wrap(refBytes);
+
+ while (buffer.hasRemaining())
+ {
+ long queueID = buffer.getLong();
+
+ int deliveryCount = buffer.getInt();
+
+ long scheduledDeliveryTime = buffer.getLong();
+
+ MessageReference reference = message.createReference(queues.get(queueID));
+
+ reference.setDeliveryCount(deliveryCount);
+
+ reference.setScheduledDeliveryTime(scheduledDeliveryTime);
+ }
+
+ return message;
+ }
+
+
+ // Inner classes ---------------------------------------------------------------
+
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/MessageTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/MessageTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/MessageTest.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,270 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.unit.messaging.newcore.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.impl.MessageImpl;
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.intf.Message;
+import org.jboss.messaging.newcore.intf.MessageReference;
+import org.jboss.messaging.newcore.intf.Queue;
+import org.jboss.test.unit.messaging.UnitTestCase;
+
+/**
+ *
+ * Tests for Message and MessageReference
+ *
+ * TODO - Test header and payload, streaming and destreaming
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class MessageTest extends UnitTestCase
+{
+ public void testCreateMessage1()
+ {
+ long id = 56465;
+ int type = 655;
+ boolean reliable = true;
+ long expiration = 6712671;
+ long timestamp = 82798172;
+ byte priority = 32;
+
+ Message message = new MessageImpl(id, type, reliable, expiration, timestamp, priority);
+
+ assertEquals(id, message.getMessageID());
+ assertEquals(type, message.getType());
+ assertEquals(reliable, message.isReliable());
+ assertEquals(timestamp, message.getTimestamp());
+ assertEquals(priority, message.getPriority());
+
+ reliable = false;
+
+ message = new MessageImpl(id, type, reliable, expiration, timestamp, priority);
+
+ assertEquals(id, message.getMessageID());
+ assertEquals(type, message.getType());
+ assertEquals(reliable, message.isReliable());
+ assertEquals(timestamp, message.getTimestamp());
+ assertEquals(priority, message.getPriority());
+ }
+
+ public void testSetAndGetMessageID()
+ {
+ Message message = new MessageImpl();
+
+ long id = 765432;
+ message.setMessageID(id);
+ assertEquals(id, message.getMessageID());
+ }
+
+ public void testSetAndGetDestination()
+ {
+ Message message = new MessageImpl();
+
+ assertNull(message.getDestination());
+ String dest = "myDest";
+ message.setDestination(dest);
+ assertEquals(dest, message.getDestination());
+ }
+
+ public void testSetAndGetReliable()
+ {
+ Message message = new MessageImpl();
+
+ boolean reliable = true;
+ message.setReliable(reliable);
+ assertEquals(reliable, message.isReliable());
+
+ reliable = false;
+ message.setReliable(reliable);
+ assertEquals(reliable, message.isReliable());
+ }
+
+ public void testSetAndGetExpiration()
+ {
+ Message message = new MessageImpl();
+
+ long expiration = System.currentTimeMillis() + 10000;
+ message.setExpiration(expiration);
+ assertEquals(expiration, message.getExpiration());
+ assertFalse(message.isExpired());
+ message.setExpiration(System.currentTimeMillis() - 1);
+ assertTrue(message.isExpired());
+
+ expiration = 0; //O means never expire
+ message.setExpiration(expiration);
+ assertEquals(expiration, message.getExpiration());
+ assertFalse(message.isExpired());
+ }
+
+ public void testSetAndGetTimestamp()
+ {
+ Message message = new MessageImpl();
+
+ long timestamp = System.currentTimeMillis();
+ message.setTimestamp(timestamp);
+ assertEquals(timestamp, message.getTimestamp());
+ }
+
+ public void testSetAndGetPriority()
+ {
+ Message message = new MessageImpl();
+ byte priority = 7;
+ message.setPriority(priority);
+ assertEquals(priority, message.getPriority());
+ }
+
+ public void testSetAndGetConnectionID()
+ {
+ Message message = new MessageImpl();
+
+ assertNull(message.getConnectionID());
+ String connectionID = "conn123";
+ message.setConnectionID(connectionID);
+ assertEquals(connectionID, message.getConnectionID());
+ }
+
+ public void testHeaders()
+ {
+ Message message = new MessageImpl();
+
+ assertNotNull(message.getHeaders());
+ assertTrue(message.getHeaders().isEmpty());
+
+ String key1 = "key1";
+ String val1 = "wibble";
+ String key2 = "key2";
+ Object val2 = new Object();
+ String key3 = "key3";
+ Double val3 = new Double(123.456);
+ Long val4 = new Long(77777);
+ message.putHeader(key1, val1);
+ assertEquals(val1, message.getHeaders().get(key1));
+ assertEquals(1, message.getHeaders().size());
+ assertTrue(message.containsHeader(key1));
+ assertFalse(message.containsHeader("does not exist"));
+ message.putHeader(key2, val2);
+ assertEquals(val2, message.getHeaders().get(key2));
+ assertEquals(2, message.getHeaders().size());
+ assertTrue(message.containsHeader(key2));
+ message.putHeader(key3, val3);
+ assertEquals(val3, message.getHeaders().get(key3));
+ assertEquals(3, message.getHeaders().size());
+ assertTrue(message.containsHeader(key3));
+ message.putHeader(key3, val4);
+ assertEquals(val4, message.getHeaders().get(key3));
+ assertEquals(3, message.getHeaders().size());
+ assertEquals(val2, message.removeHeader(key2));
+ assertEquals(2, message.getHeaders().size());
+ assertFalse(message.containsHeader(key2));
+ assertNull(message.removeHeader("does not exist"));
+ assertEquals(val1, message.removeHeader(key1));
+ assertFalse(message.containsHeader(key2));
+ assertEquals(1, message.getHeaders().size());
+ assertEquals(val3, message.removeHeader(key3));
+ assertFalse(message.containsHeader(key3));
+ assertTrue(message.getHeaders().isEmpty());
+ }
+
+ public void testEquals()
+ {
+ Message message1 = new MessageImpl();
+ message1.setMessageID(1);
+
+ Message message2 = new MessageImpl();
+ message2.setMessageID(2);
+
+ Message message3 = new MessageImpl();
+ message3.setMessageID(1);
+
+ assertTrue(message1.equals(message1));
+ assertTrue(message2.equals(message2));
+ assertTrue(message3.equals(message3));
+
+ assertFalse(message1.equals(message2));
+ assertFalse(message2.equals(message1));
+
+ assertFalse(message2.equals(message3));
+ assertFalse(message3.equals(message2));
+
+ assertTrue(message1.equals(message3));
+ assertTrue(message3.equals(message1));
+
+ }
+
+ public void testHashcode()
+ {
+ long id1 = 6567575;
+ Message message1 = new MessageImpl();
+ message1.setMessageID(id1);
+
+ assertEquals((int) ((id1 >>> 32) ^ id1), message1.hashCode());
+ }
+
+ public void testMessageReference()
+ {
+ Message message = new MessageImpl();
+
+ assertTrue(message.getReferences().isEmpty());
+
+ Queue queue1 = new QueueImpl(1);
+ Queue queue2 = new QueueImpl(2);
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ MessageReference ref1 = message.createReference(queue1);
+ refs.add(ref1);
+ MessageReference ref2 = message.createReference(queue2);
+ refs.add(ref2);
+ MessageReference ref3 = message.createReference(queue1);
+ refs.add(ref3);
+ MessageReference ref4 = message.createReference(queue2);
+ refs.add(ref4);
+
+ assertRefListsIdenticalRefs(refs, message.getReferences());
+
+ assertEquals(queue1, ref1.getQueue());
+ assertEquals(queue2, ref2.getQueue());
+ assertEquals(queue1, ref3.getQueue());
+ assertEquals(queue2, ref4.getQueue());
+
+ int deliveryCount = 65235;
+ ref1.setDeliveryCount(deliveryCount);
+ assertEquals(deliveryCount, ref1.getDeliveryCount());
+
+ long scheduledDeliveryTime = 908298123;
+ ref1.setScheduledDeliveryTime(scheduledDeliveryTime);
+ assertEquals(scheduledDeliveryTime, ref1.getScheduledDeliveryTime());
+
+ Queue queue3 = new QueueImpl(3);
+ MessageReference ref5 = ref1.copy(queue3);
+
+ assertEquals(deliveryCount, ref5.getDeliveryCount());
+ assertEquals(scheduledDeliveryTime, ref5.getScheduledDeliveryTime());
+ assertEquals(queue3, ref5.getQueue());
+ }
+
+
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/QueueTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/QueueTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/QueueTest.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,1080 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.unit.messaging.newcore.impl;
+
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.impl.RoundRobinDistributionPolicy;
+import org.jboss.messaging.newcore.intf.Consumer;
+import org.jboss.messaging.newcore.intf.DistributionPolicy;
+import org.jboss.messaging.newcore.intf.Filter;
+import org.jboss.messaging.newcore.intf.HandleStatus;
+import org.jboss.messaging.newcore.intf.MessageReference;
+import org.jboss.messaging.newcore.intf.Queue;
+import org.jboss.test.unit.fakes.messaging.newcore.impl.FakeConsumer;
+import org.jboss.test.unit.fakes.messaging.newcore.impl.FakeFilter;
+import org.jboss.test.unit.messaging.UnitTestCase;
+
+/**
+ *
+ * A QueueTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class QueueTest extends UnitTestCase
+{
+ // The tests ----------------------------------------------------------------
+
+ public void testID()
+ {
+ final long id = 123;
+
+ Queue queue = new QueueImpl(id);
+
+ assertEquals(id, queue.getID());
+ }
+
+ public void testGetSetMaxSize()
+ {
+ final int maxSize = 123456;
+
+ final int id = 123;
+
+ Queue queue = new QueueImpl(id, null, maxSize);
+
+ assertEquals(id, queue.getID());
+
+ assertEquals(maxSize, queue.getMaxSize());
+
+ final int maxSize2 = 654321;
+
+ queue.setMaxSize(maxSize2);
+
+ assertEquals(maxSize2, queue.getMaxSize());
+ }
+
+ public void testAddRemoveConsumer()
+ {
+ Consumer cons1 = new FakeConsumer();
+
+ Consumer cons2 = new FakeConsumer();
+
+ Consumer cons3 = new FakeConsumer();
+
+ Queue queue = new QueueImpl(1);
+
+ assertEquals(0, queue.getConsumerCount());
+
+ queue.addConsumer(cons1);
+
+ assertEquals(1, queue.getConsumerCount());
+
+ assertTrue(queue.removeConsumer(cons1));
+
+ assertEquals(0, queue.getConsumerCount());
+
+ queue.addConsumer(cons1);
+
+ queue.addConsumer(cons2);
+
+ queue.addConsumer(cons3);
+
+ assertEquals(3, queue.getConsumerCount());
+
+ assertFalse(queue.removeConsumer(new FakeConsumer()));
+
+ assertEquals(3, queue.getConsumerCount());
+
+ assertTrue(queue.removeConsumer(cons1));
+
+ assertEquals(2, queue.getConsumerCount());
+
+ assertTrue(queue.removeConsumer(cons2));
+
+ assertEquals(1, queue.getConsumerCount());
+
+ assertTrue(queue.removeConsumer(cons3));
+
+ assertEquals(0, queue.getConsumerCount());
+
+ assertFalse(queue.removeConsumer(cons3));
+ }
+
+ public void testGetSetDistributionPolicy()
+ {
+ Queue queue = new QueueImpl(1);
+
+ assertNotNull(queue.getDistributionPolicy());
+
+ assertTrue(queue.getDistributionPolicy() instanceof RoundRobinDistributionPolicy);
+
+ DistributionPolicy policy = new DummyDistributionPolicy();
+
+ queue.setDistributionPolicy(policy);
+
+ assertEquals(policy, queue.getDistributionPolicy());
+ }
+
+ public void testGetSetFilter()
+ {
+ Queue queue = new QueueImpl(1);
+
+ assertNull(queue.getFilter());
+
+ Filter filter = new FakeFilter();
+
+ queue.setFilter(filter);
+
+ assertEquals(filter, queue.getFilter());
+
+ queue = new QueueImpl(1, filter);
+
+ assertEquals(filter, queue.getFilter());
+ }
+
+ public void testDefaultMaxSize()
+ {
+ Queue queue = new QueueImpl(1);
+
+ assertEquals(-1, queue.getMaxSize());
+
+ queue = new QueueImpl(1, new FakeFilter());
+
+ assertEquals(-1, queue.getMaxSize());
+ }
+
+ public void testSimpleAddLast()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(numMessages, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ }
+
+ public void testSimpleDirectDelivery()
+ {
+ Queue queue = new QueueImpl(1);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ }
+
+ public void testSimpleNonDirectDelivery()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ //Now add a consumer
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ assertTrue(consumer.getReferences().isEmpty());
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ queue.deliver();
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ }
+
+ public void testBusyConsumer()
+ {
+ Queue queue = new QueueImpl(1);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ consumer.setStatusImmediate(HandleStatus.BUSY);
+
+ queue.addConsumer(consumer);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ queue.deliver();
+
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ assertTrue(consumer.getReferences().isEmpty());
+
+ consumer.setStatusImmediate(HandleStatus.HANDLED);
+
+ queue.deliver();
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ }
+
+ public void testBusyConsumerThenAddMoreMessages()
+ {
+ Queue queue = new QueueImpl(1);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ consumer.setStatusImmediate(HandleStatus.BUSY);
+
+ queue.addConsumer(consumer);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ queue.deliver();
+
+ assertEquals(10, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ assertTrue(consumer.getReferences().isEmpty());
+
+ for (int i = numMessages; i < numMessages * 2; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(20, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ assertTrue(consumer.getReferences().isEmpty());
+
+ consumer.setStatusImmediate(HandleStatus.HANDLED);
+
+ for (int i = numMessages * 2; i < numMessages * 3; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ queue.deliver();
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+ }
+
+ public void testAddFirstAddLast()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs1 = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs1.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ LinkedList<MessageReference> refs2 = new LinkedList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i + numMessages);
+
+ refs2.addFirst(ref);
+
+ queue.addFirst(ref);
+ }
+
+ List<MessageReference> refs3 = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i + 2 * numMessages);
+
+ refs3.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ List<MessageReference> allRefs = new ArrayList<MessageReference>();
+
+ allRefs.addAll(refs2);
+ allRefs.addAll(refs1);
+ allRefs.addAll(refs3);
+
+ assertRefListsIdenticalRefs(allRefs, consumer.getReferences());
+ }
+
+
+ public void testChangeConsumersAndDeliver()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(numMessages, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ FakeConsumer cons1 = new FakeConsumer();
+
+ queue.addConsumer(cons1);
+
+ queue.deliver();
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertRefListsIdenticalRefs(refs, cons1.getReferences());
+
+ FakeConsumer cons2 = new FakeConsumer();
+
+ queue.addConsumer(cons2);
+
+ assertEquals(2, queue.getConsumerCount());
+
+ cons1.getReferences().clear();
+
+ refs.clear();
+
+ for (int i = 0; i < 2 * numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertEquals(numMessages, cons1.getReferences().size());
+
+ assertEquals(numMessages, cons2.getReferences().size());
+
+ cons1.getReferences().clear();
+ cons2.getReferences().clear();
+ refs.clear();
+
+ FakeConsumer cons3 = new FakeConsumer();
+
+ queue.addConsumer(cons3);
+
+ assertEquals(3, queue.getConsumerCount());
+
+ for (int i = 0; i < 3 * numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertEquals(numMessages, cons1.getReferences().size());
+
+ assertEquals(numMessages, cons2.getReferences().size());
+
+ assertEquals(numMessages, cons3.getReferences().size());
+
+ queue.removeConsumer(cons1);
+
+ cons3.getReferences().clear();
+ cons2.getReferences().clear();
+ refs.clear();
+
+ for (int i = 0; i < 2 * numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertEquals(numMessages, cons2.getReferences().size());
+
+ assertEquals(numMessages, cons3.getReferences().size());
+
+ queue.removeConsumer(cons3);
+
+ cons2.getReferences().clear();
+ refs.clear();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertEquals(numMessages, cons2.getReferences().size());
+
+ }
+
+ public void testRoundRobinWithQueueing()
+ {
+ Queue queue = new QueueImpl(1);
+
+ assertTrue(queue.getDistributionPolicy() instanceof RoundRobinDistributionPolicy);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ //Test first with queueing
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ FakeConsumer cons1 = new FakeConsumer();
+
+ FakeConsumer cons2 = new FakeConsumer();
+
+ queue.addConsumer(cons1);
+
+ queue.addConsumer(cons2);
+
+ queue.deliver();
+
+ assertEquals(numMessages / 2, cons1.getReferences().size());
+
+ assertEquals(numMessages / 2, cons2.getReferences().size());
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref;
+
+ ref = (i % 2 == 0) ? cons1.getReferences().get(i / 2) : cons2.getReferences().get(i / 2);
+
+ assertEquals(refs.get(i), ref);
+ }
+ }
+
+ public void testRoundRobinDirect()
+ {
+ Queue queue = new QueueImpl(1);
+
+ assertTrue(queue.getDistributionPolicy() instanceof RoundRobinDistributionPolicy);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ FakeConsumer cons1 = new FakeConsumer();
+
+ FakeConsumer cons2 = new FakeConsumer();
+
+ queue.addConsumer(cons1);
+
+ queue.addConsumer(cons2);
+
+ queue.deliver();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(numMessages / 2, cons1.getReferences().size());
+
+ assertEquals(numMessages / 2, cons2.getReferences().size());
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref;
+
+ ref = (i % 2 == 0) ? cons1.getReferences().get(i / 2) : cons2.getReferences().get(i / 2);
+
+ assertEquals(refs.get(i), ref);
+ }
+ }
+
+ public void testRemoveAllReferences()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ queue.addLast(ref);
+ }
+
+ assertEquals(numMessages, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ queue.removeAllReferences();
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ assertTrue(consumer.getReferences().isEmpty());
+ }
+
+ public void testMaxSize()
+ {
+ final int maxSize = 20;
+
+ Queue queue = new QueueImpl(1, null, maxSize);
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < maxSize; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ assertEquals(HandleStatus.BUSY, queue.addLast(ref));
+ }
+
+ assertEquals(maxSize, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ //Try to add more
+
+ for (int i = 0; i < 10; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ assertEquals(HandleStatus.BUSY, queue.addLast(ref));
+ }
+
+ assertEquals(maxSize, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ // Try to add at front too
+
+ for (int i = 0; i < 10; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ assertEquals(HandleStatus.BUSY, queue.addLast(ref));
+ }
+
+ //Increase the max size
+
+ queue.setMaxSize(2 * queue.getMaxSize());
+
+ for (int i = 0; i < maxSize; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ refs.add(ref);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref));
+ }
+
+ assertEquals(maxSize * 2, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ //Now try and decrease maxSize
+
+ try
+ {
+ queue.setMaxSize(maxSize);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalArgumentException e)
+ {
+ //Ok
+ }
+
+ assertEquals(2 * maxSize, queue.getMaxSize());
+ }
+
+ public void testQueueWithFilter()
+ {
+ Filter filter = new FakeFilter("fruit", "orange");
+
+ Queue queue = new QueueImpl(1, filter);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ MessageReference ref1 = generateReference(queue, 1);
+
+ ref1.getMessage().putHeader("fruit", "banana");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addLast(ref1));
+
+ MessageReference ref2 = generateReference(queue, 2);
+
+ ref2.getMessage().putHeader("cheese", "stilton");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addLast(ref2));
+
+ MessageReference ref3 = generateReference(queue, 3);
+
+ ref3.getMessage().putHeader("cake", "sponge");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addLast(ref3));
+
+ MessageReference ref4 = generateReference(queue, 4);
+
+ ref4.getMessage().putHeader("fruit", "orange");
+
+ refs.add(ref4);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref4));
+
+ MessageReference ref5 = generateReference(queue, 5);
+
+ ref5.getMessage().putHeader("fruit", "apple");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addLast(ref5));
+
+ MessageReference ref6 = generateReference(queue, 6);
+
+ ref6.getMessage().putHeader("fruit", "orange");
+
+ refs.add(ref6);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref6));
+
+ //Add a few first
+
+ MessageReference ref7 = generateReference(queue, 7);
+
+ ref7.getMessage().putHeader("fruit", "banana");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addFirst(ref7));
+
+ MessageReference ref8 = generateReference(queue, 8);
+
+ ref8.getMessage().putHeader("fruit", "nectarine");
+
+ assertEquals(HandleStatus.NO_MATCH, queue.addFirst(ref8));
+
+ MessageReference ref9 = generateReference(queue, 9);
+
+ ref9.getMessage().putHeader("fruit", "orange");
+
+ assertEquals(HandleStatus.HANDLED, queue.addFirst(ref9));
+
+ List<MessageReference> newRefs = new ArrayList<MessageReference>();
+
+ newRefs.add(ref9);
+ newRefs.addAll(refs);
+
+ queue.setFilter(null);
+
+ MessageReference ref10 = generateReference(queue, 10);
+
+ ref10.getMessage().putHeader("sport", "skiing");
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref10));
+
+ newRefs.add(ref10);
+
+ assertEquals(4, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ assertEquals(0, queue.getMessageCount());
+ assertEquals(0, queue.getScheduledCount());
+
+ assertRefListsIdenticalRefs(newRefs, consumer.getReferences());
+ }
+
+ public void testWithPriorities()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 10;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ ref.getMessage().setPriority((byte)i);
+
+ refs.add(ref);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref));
+ }
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ List<MessageReference> receivedRefs = consumer.getReferences();
+
+ //Should be in reverse order
+
+ assertEquals(refs.size(), receivedRefs.size());
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ assertEquals(refs.get(i), receivedRefs.get(9 - i));
+ }
+
+ //But if we send more - since we are now in direct mode - the order will be the send order
+ //since the refs don't get queued
+
+ consumer.clearReferences();
+
+ refs.clear();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ ref.getMessage().setPriority((byte)i);
+
+ refs.add(ref);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref));
+ }
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ }
+
+ public void testConsumerWithFiltersDirect()
+ {
+ testConsumerWithFilters(true);
+ }
+
+ public void testConsumerWithFiltersQueueing()
+ {
+ testConsumerWithFilters(false);
+ }
+
+ public void testList()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 20;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ queue.addLast(ref);
+
+ refs.add(ref);
+ }
+
+ assertEquals(numMessages, queue.getMessageCount());
+
+ List<MessageReference> list = queue.list(null);
+
+ assertRefListsIdenticalRefs(refs, list);
+ }
+
+ public void testListWithFilter()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 20;
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = generateReference(queue, i);
+
+ if (i % 2 == 0)
+ {
+ ref.getMessage().putHeader("god", "dog");
+ }
+
+ queue.addLast(ref);
+
+ refs.add(ref);
+ }
+
+ assertEquals(numMessages, queue.getMessageCount());
+
+ Filter filter = new FakeFilter("god", "dog");
+
+ List<MessageReference> list = queue.list(filter);
+
+ assertEquals(numMessages / 2, list.size());
+
+ for (int i = 0; i < numMessages; i += 2)
+ {
+ assertEquals(refs.get(i), list.get(i / 2));
+ }
+ }
+
+ /*
+ public void testQuickSpeedTest()
+ {
+ Queue queue = new QueueImpl(1);
+
+ final int numMessages = 1000000;
+
+ FakeConsumer cons = new FakeConsumer();
+
+ queue.addConsumer(cons);
+
+ long start = System.currentTimeMillis();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ MessageReference ref = this.generateReference(1);
+
+ queue.addLast(ref);
+ }
+
+ long end = System.currentTimeMillis();
+
+ double rate = 1000 * (double)numMessages / (end - start);
+
+ System.out.println("Rate: " + rate);
+
+ assertEquals(numMessages, cons.getReferences().size());
+ }
+ */
+
+ // Private ------------------------------------------------------------------------------
+
+ private void testConsumerWithFilters(boolean direct)
+ {
+ Queue queue = new QueueImpl(1);
+
+ Filter filter = new FakeFilter("fruit", "orange");
+
+ FakeConsumer consumer = new FakeConsumer(filter);
+
+ if (direct)
+ {
+ queue.addConsumer(consumer);
+ }
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ MessageReference ref1 = generateReference(queue, 1);
+
+ ref1.getMessage().putHeader("fruit", "banana");
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref1));
+
+ MessageReference ref2 = generateReference(queue, 2);
+
+ ref2.getMessage().putHeader("cheese", "stilton");
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref2));
+
+ MessageReference ref3 = generateReference(queue, 3);
+
+ ref3.getMessage().putHeader("cake", "sponge");
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref3));
+
+ MessageReference ref4 = generateReference(queue, 4);
+
+ ref4.getMessage().putHeader("fruit", "orange");
+
+ refs.add(ref4);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref4));
+
+ MessageReference ref5 = generateReference(queue, 5);
+
+ ref5.getMessage().putHeader("fruit", "apple");
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref5));
+
+ MessageReference ref6 = generateReference(queue, 6);
+
+ ref6.getMessage().putHeader("fruit", "orange");
+
+ refs.add(ref6);
+
+ assertEquals(HandleStatus.HANDLED, queue.addLast(ref6));
+
+ if (!direct)
+ {
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+ }
+
+ assertEquals(4, queue.getMessageCount());
+
+ assertEquals(2, consumer.getReferences().size());
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+
+ queue.removeConsumer(consumer);
+
+ consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ assertEquals(0, queue.getMessageCount());
+
+ assertEquals(4, consumer.getReferences().size());
+ }
+
+ // Inner classes ---------------------------------------------------------------
+
+ class DummyDistributionPolicy implements DistributionPolicy
+ {
+ public int select(List<Consumer> consumers, int lastPos)
+ {
+ return 0;
+ }
+ }
+
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/BDBJEEnvironmentTestBase.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/BDBJEEnvironmentTestBase.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/BDBJEEnvironmentTestBase.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,1726 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.unit.messaging.newcore.impl.bdbje;
+
+import java.util.List;
+
+import javax.transaction.xa.Xid;
+
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEDatabase;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJETransaction;
+import org.jboss.test.unit.messaging.UnitTestCase;
+
+/**
+ *
+ * Base for tests for BDBJEEnvironment and BDBJEDatabase
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public abstract class BDBJEEnvironmentTestBase extends UnitTestCase
+{
+ protected BDBJEEnvironment env;
+
+ protected BDBJEDatabase database;
+
+ protected static final String ENV_DIR = "test-bdb-environment";
+
+ protected static final String DB_NAME = "test-db";
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ env = createEnvironment();
+
+ env.setEnvironmentPath(ENV_DIR);
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+ }
+
+ protected abstract void createDir(String path);
+
+ protected void tearDown() throws Exception
+ {
+ database.close();
+
+ env.stop();
+ }
+
+ protected abstract BDBJEEnvironment createEnvironment() throws Exception;
+
+ // The tests ----------------------------------------------------------------
+
+ public void testGetInDoubtXidsCompleteWithCommit() throws Exception
+ {
+ List<Xid> xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+
+ Xid xid1 = generateXid();
+
+ env.startWork(xid1);
+
+ database.put(null, 1, new byte[10], 0, 10);
+
+ env.endWork(xid1, false);
+
+ env.prepare(xid1);
+
+ xids = env.getInDoubtXids();
+
+ assertEquals(xid1, xids.get(0));
+
+ env.commit(xid1);
+
+ xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+ }
+
+ public void testGetInDoubtXidsCompleteWithRollback() throws Exception
+ {
+ List<Xid> xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+
+ Xid xid1 = generateXid();
+
+ env.startWork(xid1);
+
+ database.put(null, 1, new byte[10], 0, 10);
+
+ env.endWork(xid1, false);
+
+ env.prepare(xid1);
+
+ xids = env.getInDoubtXids();
+
+ assertEquals(xid1, xids.get(0));
+
+ env.rollback(xid1);
+
+ xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+ }
+
+
+ public void testGetInDoubtXidsMultiple() throws Exception
+ {
+ List<Xid> xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+
+ Xid xid1 = generateXid();
+ env.startWork(xid1);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid1, false);
+
+ env.prepare(xid1);
+ xids = env.getInDoubtXids();
+ assertEquals(xid1, xids.get(0));
+
+
+ Xid xid2 = generateXid();
+ env.startWork(xid2);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid2, false);
+
+ env.prepare(xid2);
+ xids = env.getInDoubtXids();
+ assertTrue(xids.contains(xid1));
+ assertTrue(xids.contains(xid2));
+
+ Xid xid3 = generateXid();
+ env.startWork(xid3);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid3, false);
+
+ env.prepare(xid3);
+ xids = env.getInDoubtXids();
+ assertTrue(xids.contains(xid1));
+ assertTrue(xids.contains(xid2));
+ assertTrue(xids.contains(xid3));
+
+ }
+
+ public void testGetInDoubtXidsMultipleWithRestart() throws Exception
+ {
+ List<Xid> xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+
+ Xid xid1 = generateXid();
+ env.startWork(xid1);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid1, false);
+
+ env.prepare(xid1);
+ xids = env.getInDoubtXids();
+ assertEquals(xid1, xids.get(0));
+
+
+ Xid xid2 = generateXid();
+ env.startWork(xid2);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid2, false);
+
+ env.prepare(xid2);
+ xids = env.getInDoubtXids();
+ assertTrue(xids.contains(xid1));
+ assertTrue(xids.contains(xid2));
+
+ Xid xid3 = generateXid();
+ env.startWork(xid3);
+ database.put(null, 1, new byte[10], 0, 10);
+ env.endWork(xid3, false);
+
+ env.prepare(xid3);
+ xids = env.getInDoubtXids();
+ assertTrue(xids.contains(xid1));
+ assertTrue(xids.contains(xid2));
+ assertTrue(xids.contains(xid3));
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ xids = env.getInDoubtXids();
+ assertTrue(xids.contains(xid1));
+ assertTrue(xids.contains(xid2));
+ assertTrue(xids.contains(xid3));
+
+ env.commit(xid1);
+ env.commit(xid2);
+ env.commit(xid3);
+
+ xids = env.getInDoubtXids();
+
+ assertTrue(xids.isEmpty());
+ }
+
+ public void testPutAndRemoveNonTransactional() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutAndRemoveNonTransactionalWithRestart() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+ public void testPutAndRemoveMultipleNonTransactional() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ database.put(null, id3, bytes3, offset, bytes3.length);
+
+ assertContainsPair(id1, bytes1, 3);
+
+ assertContainsPair(id2, bytes2, 3);
+
+ assertContainsPair(id3, bytes3, 3);
+
+ database.remove(null, id2);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id3, bytes3, 2);
+
+ database.remove(null, id3);
+
+ assertContainsPair(id1, bytes1, 1);
+
+ database.remove(null, id1);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutTransactionalCommit() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes, offset, bytes.length);
+
+ tx.commit();
+
+ assertContainsPair(id, bytes, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutTransactionalWithRestart() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes, offset, bytes.length);
+
+ //Now restart before committing
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutXACommit() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.commit(xid);
+
+ assertContainsPair(id, bytes, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutXAWithRestart() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ env.endWork(xid, false);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertStoreEmpty();
+ }
+
+
+ public void testPutXAWithRestartAfterPrepare() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertStoreEmpty();
+ }
+
+ public void testRemoveTransactional() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.remove(tx, id);
+
+ tx.commit();
+
+ assertStoreEmpty();
+ }
+
+ public void testRemoveTransactionalWithRestart() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.remove(tx, id);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+ public void testRemoveXACommit() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.remove(null, id);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.commit(xid);
+
+ assertStoreEmpty();
+ }
+
+ public void testRemoveXAWithRestart() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.remove(null, id);
+
+ env.endWork(xid, false);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+ public void testRemoveXAWithRestartAfterPrepare() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.remove(null, id);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+ public void testPutTransactionalRollback() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes, offset, bytes.length);
+
+ tx.rollback();
+
+ assertStoreEmpty();
+ }
+
+ public void testPutXARollback() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.rollback(xid);
+
+ assertStoreEmpty();
+ }
+
+ public void testRemoveTransactionalRollback() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.remove(tx, id);
+
+ tx.rollback();
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+ public void testRemoveXARollback() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.remove(null, id);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.rollback(xid);
+
+ assertContainsPair(id, bytes, 1);
+ }
+
+
+ public void testPutAndRemoveMultipleTransactionalCommit() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ long id4 = 4;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id3, bytes3, offset, bytes3.length);
+
+ database.put(tx, id4, bytes4, offset, bytes4.length);
+
+ database.remove(tx, id1);
+
+ database.remove(tx, id2);
+
+ tx.commit();
+
+ assertContainsPair(id3, bytes3, 2);
+
+ assertContainsPair(id4, bytes4, 2);
+ }
+
+ public void testPutAndRemoveMultipleXACommit() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ long id4 = 4;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id3, bytes3, offset, bytes3.length);
+
+ database.put(null, id4, bytes4, offset, bytes4.length);
+
+ database.remove(null, id1);
+
+ database.remove(null, id2);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.commit(xid);
+
+ assertContainsPair(id3, bytes3, 2);
+
+ assertContainsPair(id4, bytes4, 2);
+ }
+
+ public void testPutAndRemoveMultipleXAWithRestart() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ long id4 = 4;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id3, bytes3, offset, bytes3.length);
+
+ database.put(null, id4, bytes4, offset, bytes4.length);
+
+ database.remove(null, id1);
+
+ database.remove(null, id2);
+
+ env.endWork(xid, false);
+
+ // Now restart
+
+ database.close();
+
+ env.stop();
+
+ env.start();
+
+ database = env.getDatabase(DB_NAME);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+ }
+
+ public void testPutAndRemoveMultipleTransactionalRollback() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ long id4 = 4;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id3, bytes3, offset, bytes3.length);
+
+ database.put(tx, id4, bytes4, offset, bytes4.length);
+
+ database.remove(tx, id1);
+
+ database.remove(tx, id2);
+
+ tx.rollback();
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+ }
+
+ public void testPutAndRemoveMultipleXARollback() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ byte[] bytes3 = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 };
+
+ byte[] bytes4 = new byte[] { 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 };
+
+ long id1 = 1;
+
+ long id2 = 2;
+
+ long id3 = 3;
+
+ long id4 = 4;
+
+ int offset = 0;
+
+ database.put(null, id1, bytes1, offset, bytes1.length);
+
+ database.put(null, id2, bytes2, offset, bytes2.length);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id3, bytes3, offset, bytes3.length);
+
+ database.put(null, id4, bytes4, offset, bytes4.length);
+
+ database.remove(null, id1);
+
+ database.remove(null, id2);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.rollback(xid);
+
+ assertContainsPair(id1, bytes1, 2);
+
+ assertContainsPair(id2, bytes2, 2);
+ }
+
+ public void testOverwiteNonTransactional() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes1, offset, bytes1.length);
+
+ assertContainsPair(id, bytes1, 1);
+
+ database.put(null, id, bytes2, offset, bytes1.length);
+
+ assertContainsPair(id, bytes2, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testOverwiteTransactionalCommit() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes1, offset, bytes1.length);
+
+ database.put(tx, id, bytes2, offset, bytes1.length);
+
+ tx.commit();
+
+ assertContainsPair(id, bytes2, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testOverwiteXACommit() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes1, offset, bytes1.length);
+
+ database.put(null, id, bytes2, offset, bytes1.length);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.commit(xid);
+
+ assertContainsPair(id, bytes2, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testOverwiteTransactionalRollback() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes1, offset, bytes1.length);
+
+ database.put(tx, id, bytes2, offset, bytes1.length);
+
+ tx.rollback();
+
+ assertStoreEmpty();
+ }
+
+ public void testOverwiteXARollback() throws Exception
+ {
+ byte[] bytes1 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ database.put(null, id, bytes1, offset, bytes1.length);
+
+ database.put(null, id, bytes2, offset, bytes1.length);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.rollback(xid);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutAndRemovePartialNonTransactional() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 20, 21, 22, 23 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ database.put(null, id, bytes, offset, bytes.length);
+
+ assertContainsPair(id, bytes, 1);
+
+ database.put(null, id, bytes2, 10, bytes2.length);
+
+ byte[] bytes3 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes3, 1);
+
+ database.put(null, id, bytes2, 3, bytes2.length);
+
+ byte[] bytes4 = new byte[] { 1, 2, 3, 20, 21, 22, 23, 8, 9, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes4, 1);
+
+ byte[] bytes5 = new byte[0];
+
+ //blank out 4 bytes
+ database.put(null, id, bytes5, 5, 4);
+
+ byte[] bytes6 = new byte[] { 1, 2, 3, 20, 21, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes6, 1);
+
+
+ database.put(null, id, new byte[0], 0, 4);
+
+ byte[] bytes7 = new byte[] { 21, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes7, 1);
+
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testPutAndRemovePartialTransactional() throws Exception
+ {
+ byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+
+ byte[] bytes2 = new byte[] { 20, 21, 22, 23 };
+
+ long id = 1;
+
+ int offset = 0;
+
+ BDBJETransaction tx = env.createTransaction();
+
+ database.put(tx, id, bytes, offset, bytes.length);
+
+ tx.commit();
+
+ assertContainsPair(id, bytes, 1);
+
+ tx = env.createTransaction();
+
+ database.put(tx, id, bytes2, 10, bytes2.length);
+
+ tx.commit();
+
+ byte[] bytes3 = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes3, 1);
+
+ tx = env.createTransaction();
+
+ database.put(tx, id, bytes2, 3, bytes2.length);
+
+ tx.commit();
+
+ byte[] bytes4 = new byte[] { 1, 2, 3, 20, 21, 22, 23, 8, 9, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes4, 1);
+
+ byte[] bytes5 = new byte[0];
+
+ tx = env.createTransaction();
+
+ //blank out 4 bytes
+ database.put(tx, id, bytes5, 5, 4);
+
+ tx.commit();
+
+ byte[] bytes6 = new byte[] { 1, 2, 3, 20, 21, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes6, 1);
+
+ database.put(null, id, new byte[0], 0, 4);
+
+ byte[] bytes7 = new byte[] { 21, 10, 20, 21, 22, 23 };
+
+ assertContainsPair(id, bytes7, 1);
+
+ database.remove(null, 1);
+
+ assertStoreEmpty();
+ }
+
+ public void testSetAndGetEnvironment() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ final String path = "/home/tim/test-path123";
+
+ createDir(path);
+
+ assertNull(bdb.getEnvironmentPath());
+
+ bdb.setEnvironmentPath(path);
+
+ assertEquals(path, bdb.getEnvironmentPath());
+
+ bdb.start();
+
+ try
+ {
+ bdb.setEnvironmentPath("blah");
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertEquals(path, bdb.getEnvironmentPath());
+
+ bdb.stop();
+
+ final String path2 = "test-path123651";
+
+ bdb.setEnvironmentPath(path2);
+
+ assertEquals(path2, bdb.getEnvironmentPath());
+ }
+
+
+ public void testSetAndGetTransacted() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ final String path = "/home/tim/test-path123";
+
+ createDir(path);
+
+ bdb.setEnvironmentPath(path);
+
+ bdb.setTransacted(false);
+
+ assertFalse(bdb.isTransacted());
+
+ bdb.setTransacted(true);
+
+ assertTrue(bdb.isTransacted());
+
+ bdb.start();
+
+ try
+ {
+ bdb.setTransacted(true);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertTrue(bdb.isTransacted());
+
+ bdb.stop();
+
+ bdb.setTransacted(false);
+
+ assertFalse(bdb.isTransacted());
+ }
+
+ public void testSetAndGetSyncOS() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ final String path = "/home/tim/test-path123";
+
+ createDir(path);
+
+ bdb.setEnvironmentPath(path);
+
+ assertFalse(bdb.isSyncOS());
+
+ bdb.setSyncOS(true);
+
+ assertTrue(bdb.isSyncOS());
+
+ bdb.start();
+
+ try
+ {
+ bdb.setSyncOS(true);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertTrue(bdb.isSyncOS());
+
+ bdb.stop();
+
+ bdb.setSyncOS(false);
+
+ assertFalse(bdb.isSyncOS());
+ }
+
+ public void testSetAndGetSyncVM() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ final String path = "/home/tim/test-path123";
+
+ bdb.setEnvironmentPath(path);
+
+ createDir(path);
+
+ assertFalse(bdb.isSyncVM());
+
+ bdb.setSyncVM(true);
+
+ assertTrue(bdb.isSyncVM());
+
+ bdb.start();
+
+ try
+ {
+ bdb.setSyncVM(true);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertTrue(bdb.isSyncVM());
+
+ bdb.stop();
+
+ bdb.setSyncVM(false);
+
+ assertFalse(bdb.isSyncVM());
+ }
+
+ public void testSetAndGetMemoryCacheSize() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ final String path = "/home/tim/test-path123";
+
+ createDir(path);
+
+ bdb.setEnvironmentPath(path);
+
+ assertEquals(-1, bdb.getMemoryCacheSize());
+
+ final long size = 16251762;
+
+ bdb.setMemoryCacheSize(size);
+
+ assertEquals(size, bdb.getMemoryCacheSize());
+
+ bdb.start();
+
+ try
+ {
+ bdb.setMemoryCacheSize(1897291289);
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ assertEquals(size, bdb.getMemoryCacheSize());
+
+ bdb.stop();
+
+ final long size2 = 1625534783;
+
+ bdb.setMemoryCacheSize(size2);
+
+ assertEquals(size2, bdb.getMemoryCacheSize());
+ }
+
+
+ public void testStartAndStop() throws Exception
+ {
+ BDBJEEnvironment bdb = createEnvironment();
+
+ try
+ {
+ bdb.start();
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ final String path = "/home/tim/test-path123";
+
+ createDir(path);
+
+ bdb.setEnvironmentPath(path);
+
+ bdb.start();
+
+ try
+ {
+ bdb.start();
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+
+ bdb.stop();
+
+ try
+ {
+ bdb.stop();
+
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //OK
+ }
+ }
+
+ public void testWrongOrderCommit() throws Exception
+ {
+ testXAWrongOrder(true);
+ }
+
+ public void testWrongOrderRollback() throws Exception
+ {
+ testXAWrongOrder(false);
+ }
+
+ public void testXAWrongXidCommit() throws Exception
+ {
+ testXAWrongXid(true);
+ }
+
+ public void testXAWrongXidRollback() throws Exception
+ {
+ testXAWrongXid(false);
+ }
+
+ private void testXAWrongXid(boolean commit) throws Exception
+ {
+ Xid xid = generateXid();
+
+ env.startWork(xid);
+
+ Xid xid2 = generateXid();
+
+ try
+ {
+ env.endWork(xid2, false);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ //do some work
+
+ database.put(null, 23, new byte[10], 0, 10);
+
+ env.endWork(xid, false);
+
+ try
+ {
+ env.prepare(xid2);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ env.prepare(xid);
+
+ if (commit)
+ {
+ try
+ {
+ env.commit(xid2);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+ env.commit(xid);
+ }
+ else
+ {
+ try
+ {
+ env.rollback(xid2);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+ env.rollback(xid);
+ }
+ }
+
+ private void testXAWrongOrder(boolean commit) throws Exception
+ {
+ Xid xid = generateXid();
+
+ try
+ {
+ env.endWork(xid, false);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.prepare(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.commit(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.rollback(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ env.startWork(xid);
+
+ //do some work
+
+ database.put(null, 23, new byte[10], 0, 10);
+
+ try
+ {
+ env.startWork(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.prepare(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.commit(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.rollback(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ env.endWork(xid, false);
+
+ try
+ {
+ env.startWork(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.endWork(xid, false);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.commit(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.rollback(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ env.prepare(xid);
+
+ try
+ {
+ env.startWork(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.endWork(xid, false);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ if (commit)
+ {
+ env.commit(xid);
+ }
+ else
+ {
+ env.rollback(xid);
+ }
+
+ try
+ {
+ env.endWork(xid, false);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.prepare(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.commit(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ try
+ {
+ env.rollback(xid);
+ fail("Should throw exception");
+ }
+ catch (IllegalStateException e)
+ {
+ //Ok
+ }
+
+ env.startWork(xid);
+
+ database.put(null, 23, new byte[10], 0, 10);
+
+ env.endWork(xid, false);
+
+ env.prepare(xid);
+
+ env.rollback(xid);
+
+ }
+
+ // Private -------------------------------------------------------------------------------------
+
+ private void assertContainsPair(long id, byte[] bytes, long size) throws Exception
+ {
+ byte[] b = database.get(id);
+
+ assertNotNull(b);
+
+ assertByteArraysEquivalent(bytes, b);
+
+ assertEquals(size, database.size());
+ }
+
+ private void assertStoreEmpty() throws Exception
+ {
+ assertEquals(0, database.size());
+ }
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/BDBSpeedTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/BDBSpeedTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/BDBSpeedTest.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,656 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.unit.messaging.newcore.impl.bdbje;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.transaction.xa.Xid;
+
+import org.jboss.jms.tx.MessagingXid;
+import org.jboss.messaging.newcore.impl.MessageImpl;
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEPersistenceManager;
+import org.jboss.messaging.newcore.intf.Message;
+import org.jboss.messaging.newcore.intf.MessageReference;
+import org.jboss.messaging.newcore.intf.Queue;
+import org.jboss.test.unit.messaging.UnitTestCase;
+
+/**
+ *
+ * A BDBSpeedTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class BDBSpeedTest extends UnitTestCase
+{
+
+// public void testCommitMessage() throws Exception
+// {
+// String envPath = "/home/tim/test-env";
+//
+// File file = new File(envPath);
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// final int numMessages = 5000;
+//
+// final int numRefs = 10;
+//
+// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
+//
+// bdb.start();
+//
+// Queue queue = new QueueImpl(1);
+//
+// //List<Message> messages = new ArrayList<Message>();
+//
+// long start = System.currentTimeMillis();
+//
+// byte[] payload = new byte[1 * 1024];
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
+//
+// for (int j = 0; j < numRefs; j++)
+// {
+// message.createReference(queue);
+//
+// message.setPayload(payload);
+// }
+//
+// //messages.add(message);
+//
+// bdb.commitMessage(message);
+// }
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Insert Rate: " + rate);
+//
+// }
+//
+// public void testCommitMessages() throws Exception
+// {
+// String envPath = "/home/tim/test-env";
+//
+// File file = new File(envPath);
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// final int numMessages = 5000;
+//
+// final int numRefs = 10;
+//
+// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
+//
+// bdb.start();
+//
+// Queue queue = new QueueImpl(1);
+//
+// List<Message> messages = new ArrayList<Message>();
+//
+// long start = System.currentTimeMillis();
+//
+// byte[] payload = new byte[1 * 1024];
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
+//
+// for (int j = 0; j < numRefs; j++)
+// {
+// message.createReference(queue);
+//
+// message.setPayload(payload);
+// }
+//
+// messages.add(message);
+//
+// if (i % 100 == 0)
+// {
+// bdb.commitMessages(messages);
+//
+// messages.clear();
+// }
+// }
+// bdb.commitMessages(messages);
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Insert Rate: " + rate);
+//
+// }
+//
+// public void testDeleteReferences() throws Exception
+// {
+// String envPath = "/home/tim/test-env";
+//
+// File file = new File(envPath);
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// final int numMessages = 5000;
+//
+// final int numRefs = 10;
+//
+// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
+//
+// bdb.start();
+//
+// Queue queue = new QueueImpl(1);
+//
+// List<Message> messages = new ArrayList<Message>();
+//
+// byte[] payload = new byte[1 * 1024];
+//
+// List<MessageReference> refs = new ArrayList<MessageReference>();
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
+//
+// for (int j = 0; j < numRefs; j++)
+// {
+// MessageReference ref = message.createReference(queue);
+//
+// message.setPayload(payload);
+//
+// refs.add(ref);
+// }
+//
+// messages.add(message);
+//
+// if (i % 100 == 0)
+// {
+// bdb.commitMessages(messages);
+//
+// messages.clear();
+// }
+// }
+// bdb.commitMessages(messages);
+//
+// long start = System.currentTimeMillis();
+//
+// //Now delete them
+//
+// bdb.deleteReferences(refs);
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Delete Rate: " + rate);
+//
+// }
+//
+ public void testDeleteReference() throws Exception
+ {
+ String envPath = "/home/tim/test-env";
+
+ File file = new File(envPath);
+
+ deleteDirectory(file);
+
+ file.mkdir();
+
+ final int numMessages = 5000;
+
+ final int numRefs = 1;
+
+ BDBJEPersistenceManager bdb = new BDBJEPersistenceManager(envPath);
+
+ bdb.start();
+
+ Queue queue = new QueueImpl(1);
+
+ List<Message> messages = new ArrayList<Message>();
+
+ byte[] payload = new byte[1 * 1024];
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ for (int i = 0; i < numMessages; i++)
+ {
+ Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
+
+ for (int j = 0; j < numRefs; j++)
+ {
+ MessageReference ref = message.createReference(queue);
+
+ message.setPayload(payload);
+
+ refs.add(ref);
+ }
+
+ messages.add(message);
+
+ if (i % 100 == 0)
+ {
+ bdb.commitMessages(messages);
+
+ messages.clear();
+ }
+ }
+ bdb.commitMessages(messages);
+
+ System.out.println("Added them");
+
+ long start = System.currentTimeMillis();
+
+ //Now delete them
+
+ for (MessageReference ref: refs)
+ {
+ bdb.deleteReference(ref);
+ }
+
+ long end = System.currentTimeMillis();
+
+ double rate = 1000 * (double)numMessages / (end - start);
+
+ System.out.println("Delete Rate: " + rate);
+
+ }
+//
+//
+// public void testPrepareMessages() throws Exception
+// {
+// String envPath = "/home/tim/test-env";
+//
+// File file = new File(envPath);
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// final int numMessages = 5000;
+//
+// final int numRefs = 10;
+//
+// BDBJEPersistenceManager2 bdb = new BDBJEPersistenceManager2(envPath);
+//
+// bdb.start();
+//
+// Queue queue = new QueueImpl(1);
+//
+// List<Message> messages = new ArrayList<Message>();
+//
+// long start = System.currentTimeMillis();
+//
+// byte[] payload = new byte[1 * 1024];
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Message message = new MessageImpl(i, 0, true, 0, System.currentTimeMillis(), (byte)4);
+//
+// for (int j = 0; j < numRefs; j++)
+// {
+// message.createReference(queue);
+//
+// message.setPayload(payload);
+// }
+//
+// messages.add(message);
+//
+// if (i % 100 == 0)
+// {
+// Xid xid = this.generateXid();
+//
+// bdb.prepareMessages(xid, messages);
+//
+// bdb.commitPreparedMessages(xid);
+//
+// messages.clear();
+// }
+// }
+//
+// Xid xid = this.generateXid();
+//
+// bdb.prepareMessages(xid, messages);
+//
+// bdb.commitPreparedMessages(xid);
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Insert Rate: " + rate);
+//
+// }
+
+// /*
+// * Test with message and ref in separate databases
+// */
+// public void testMsgandRefsDifferentDBSNoXA() throws Exception
+// {
+// File file = new File("/home/tim/test-env");
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// EnvironmentConfig envConfig = new EnvironmentConfig();
+//
+// envConfig.setAllowCreate(true);
+//
+// envConfig.setTransactional(true);
+//
+//// envConfig.setTxnNoSync(true);
+////
+//// envConfig.setTxnWriteNoSync(true);
+//
+// Environment env1 = new Environment(file, envConfig);
+//
+// DatabaseConfig dbConfig1 = new DatabaseConfig();
+//
+// dbConfig1.setTransactional(true);
+//
+// dbConfig1.setAllowCreate(true);
+//
+// Database msgDB = env1.openDatabase(null, "message", dbConfig1);
+//
+// DatabaseConfig dbConfig2 = new DatabaseConfig();
+//
+// dbConfig2.setTransactional(true);
+//
+// dbConfig2.setAllowCreate(true);
+//
+// Database refDB = env1.openDatabase(null, "ref", dbConfig2);
+//
+//
+// DatabaseConfig dbConfig3 = new DatabaseConfig();
+//
+// dbConfig3.setTransactional(true);
+//
+// dbConfig3.setAllowCreate(true);
+//
+// Database txDB = env1.openDatabase(null, "tx", dbConfig3);
+//
+//
+//
+// final int numMessages = 5000;
+//
+// byte[] msgBytes = new byte[256];
+//
+// final int numRefs = 10;
+//
+// byte[] refBytes = new byte[numRefs * 20];
+//
+// byte[] txBytes = new byte[numRefs * 50];
+//
+//
+// long start = System.currentTimeMillis();
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// byte[] keyBytes = new byte[8];
+//
+// ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
+//
+// buffer.putLong(i);
+//
+// DatabaseEntry key = new DatabaseEntry(keyBytes);
+//
+//
+// //Prepare refs
+//
+// DatabaseEntry refVal = new DatabaseEntry(refBytes);
+//
+// Transaction tx = env1.beginTransaction(null, null);
+//
+// refDB.put(tx, key, refVal);
+//
+// // Message
+//
+// DatabaseEntry msgVal = new DatabaseEntry(msgBytes);
+//
+// msgDB.put(tx, key, msgVal);
+//
+// //Write a tx record
+//
+// DatabaseEntry txVal = new DatabaseEntry(txBytes);
+//
+// txDB.put(tx, key, txVal);
+//
+// //Commit the prepare
+//
+// tx.commit();
+//
+// tx = env1.beginTransaction(null, null);
+//
+// //Now commit the refs
+//
+// refVal = new DatabaseEntry(refBytes);
+//
+// refDB.put(tx, key, refVal);
+//
+// //And delete the tx record
+//
+// txDB.delete(tx, key);
+//
+// tx.commit();
+// }
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Insert Rate: " + rate);
+//
+//
+// }
+//
+// public void testMsgandRefsDifferentDBSXA() throws Exception
+// {
+// File file = new File("/home/tim/test-env");
+//
+// deleteDirectory(file);
+//
+// file.mkdir();
+//
+// EnvironmentConfig envConfig = new EnvironmentConfig();
+//
+// envConfig.setAllowCreate(true);
+//
+// envConfig.setTransactional(true);
+//
+// XAEnvironment env1 = new XAEnvironment(file, envConfig);
+//
+// DatabaseConfig dbConfig1 = new DatabaseConfig();
+//
+// dbConfig1.setTransactional(true);
+//
+// dbConfig1.setAllowCreate(true);
+//
+// Database msgDB = env1.openDatabase(null, "message", dbConfig1);
+//
+// DatabaseConfig dbConfig2 = new DatabaseConfig();
+//
+// dbConfig2.setTransactional(true);
+//
+// dbConfig2.setAllowCreate(true);
+//
+// Database refDB = env1.openDatabase(null, "ref", dbConfig2);
+//
+//
+//
+// final int numMessages = 5000;
+//
+// byte[] msgBytes = new byte[256];
+//
+// final int numRefs = 10;
+//
+// byte[] refBytes = new byte[numRefs * 20];
+//
+// long start = System.currentTimeMillis();
+//
+// for (int i = 0; i < numMessages; i++)
+// {
+// Xid xid = generateXid();
+//
+// env1.start(xid, XAResource.TMNOFLAGS);
+//
+// byte[] keyBytes = new byte[8];
+//
+// ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
+//
+// buffer.putLong(i);
+//
+// DatabaseEntry key = new DatabaseEntry(keyBytes);
+//
+// DatabaseEntry msgVal = new DatabaseEntry(msgBytes);
+//
+// Transaction tx = env1.beginTransaction(null, null);
+//
+// msgDB.put(null, key, msgVal);
+//
+// //Now the refs
+//
+// DatabaseEntry refVal = new DatabaseEntry(refBytes);
+//
+// refDB.put(null, key, refVal);
+//
+// env1.end(xid, XAResource.TMSUCCESS);
+//
+// env1.prepare(xid);
+//
+// env1.commit(xid, false);
+//
+// // tx.commit();
+// }
+//
+// long end = System.currentTimeMillis();
+//
+// double rate = 1000 * (double)numMessages / (end - start);
+//
+// System.out.println("Insert Rate: " + rate);
+//
+// start = System.currentTimeMillis();
+// }
+//
+ private Xid generateXid()
+ {
+ String id = java.util.UUID.randomUUID().toString();
+
+ Xid xid = new MessagingXid("blah".getBytes(), 123, id.getBytes());
+
+ return xid;
+ }
+//
+//
+//// This is very slow
+//// public void testMsgandRefsSameDBSNoXA() throws Exception
+//// {
+//// File file = new File("/home/tim/test-env");
+////
+//// deleteDirectory(file);
+////
+//// file.mkdir();
+////
+//// EnvironmentConfig envConfig = new EnvironmentConfig();
+////
+//// envConfig.setAllowCreate(true);
+////
+//// envConfig.setTransactional(true);
+////
+//// Environment env1 = new Environment(file, envConfig);
+////
+//// DatabaseConfig dbConfig1 = new DatabaseConfig();
+////
+//// dbConfig1.setTransactional(true);
+////
+//// dbConfig1.setAllowCreate(true);
+////
+//// Database msgDB = env1.openDatabase(null, "message", dbConfig1);
+////
+//// final int numMessages = 5000;
+////
+//// byte[] msgBytes = new byte[10 * 1024];
+////
+//// byte[] refBytes = new byte[24];
+////
+//// long start = System.currentTimeMillis();
+////
+//// for (int i = 0; i < numMessages; i++)
+//// {
+//// byte[] keyBytes = new byte[8];
+////
+//// ByteBuffer buffer = ByteBuffer.wrap(keyBytes);
+////
+//// buffer.putLong(i);
+////
+//// DatabaseEntry key = new DatabaseEntry(keyBytes);
+////
+//// DatabaseEntry msgVal = new DatabaseEntry(msgBytes);
+////
+//// Transaction tx = env1.beginTransaction(null, null);
+////
+//// msgDB.put(tx, key, msgVal);
+////
+//// //Now the refs
+////
+//// final int numRefs = 50;
+////
+//// for (int j = 0; j < numRefs; j++)
+//// {
+//// keyBytes = new byte[8];
+////
+//// buffer = ByteBuffer.wrap(keyBytes);
+////
+//// buffer.putLong(j);
+////
+//// key = new DatabaseEntry(keyBytes);
+////
+//// DatabaseEntry value = new DatabaseEntry();
+////
+//// value.setPartial(0, refBytes.length, true);
+////
+//// value.setData(refBytes);
+////
+//// msgDB.put(tx, key, value);
+//// }
+////
+//// tx.commit();
+////
+//// }
+////
+//// long end = System.currentTimeMillis();
+////
+//// double rate = 1000 * (double)numMessages / (end - start);
+////
+//// System.out.println("Rate: " + rate);
+//// }
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/FakeBDBJEEnvironmentTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/FakeBDBJEEnvironmentTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/messaging/newcore/impl/bdbje/FakeBDBJEEnvironmentTest.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,25 @@
+package org.jboss.test.unit.messaging.newcore.impl.bdbje;
+
+import org.jboss.messaging.newcore.impl.bdbje.BDBJEEnvironment;
+import org.jboss.test.unit.fakes.messaging.newcore.impl.bdbje.FakeBDBJEEnvironment;
+
+/**
+ *
+ * A FakeBDBJEEnvironmentTest
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class FakeBDBJEEnvironmentTest extends BDBJEEnvironmentTestBase
+{
+ protected BDBJEEnvironment createEnvironment() throws Exception
+ {
+ BDBJEEnvironment env = new FakeBDBJEEnvironment();
+
+ return env;
+ }
+
+ protected void createDir(String path)
+ {
+ }
+}
Added: branches/Branch_New_Persistence/src/main/org/jboss/test/unit/timing/messaging/newcore/impl/QueueTest.java
===================================================================
--- branches/Branch_New_Persistence/src/main/org/jboss/test/unit/timing/messaging/newcore/impl/QueueTest.java (rev 0)
+++ branches/Branch_New_Persistence/src/main/org/jboss/test/unit/timing/messaging/newcore/impl/QueueTest.java 2007-12-05 19:44:34 UTC (rev 3414)
@@ -0,0 +1,225 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.unit.timing.messaging.newcore.impl;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.jboss.messaging.newcore.impl.QueueImpl;
+import org.jboss.messaging.newcore.intf.MessageReference;
+import org.jboss.messaging.newcore.intf.Queue;
+import org.jboss.test.unit.fakes.messaging.newcore.impl.FakeConsumer;
+import org.jboss.test.unit.messaging.UnitTestCase;
+
+/**
+ *
+ * A timing-sensitive QueueTest
+ *
+ * All the queue tests which are timing sensitive - go in here
+ *
+ * @author <a href="mailto:tim.fox at jboss.com">Tim Fox</a>
+ *
+ */
+public class QueueTest extends UnitTestCase
+{
+ private static final long TIMEOUT = 10000;
+
+ // The tests ----------------------------------------------------------------
+
+ public void testScheduledDirect()
+ {
+ testScheduled(true);
+ }
+
+ public void testScheduledQueueing()
+ {
+ testScheduled(false);
+ }
+
+ public void testScheduledNoConsumer() throws Exception
+ {
+ Queue queue = new QueueImpl(1);
+
+ //Send one scheduled
+
+ long now = System.currentTimeMillis();
+
+ MessageReference ref1 = generateReference(queue, 1);
+ ref1.setScheduledDeliveryTime(now + 7000);
+ queue.addLast(ref1);
+
+ //Send some non scheduled messages
+
+ MessageReference ref2 = generateReference(queue, 2);
+ queue.addLast(ref2);
+ MessageReference ref3 = generateReference(queue, 3);
+ queue.addLast(ref3);
+ MessageReference ref4 = generateReference(queue, 4);
+ queue.addLast(ref4);
+
+
+ //Now send some more scheduled messages
+
+ MessageReference ref5 = generateReference(queue, 5);
+ ref5.setScheduledDeliveryTime(now + 5000);
+ queue.addLast(ref5);
+
+ MessageReference ref6 = generateReference(queue, 6);
+ ref6.setScheduledDeliveryTime(now + 4000);
+ queue.addLast(ref6);
+
+ MessageReference ref7 = generateReference(queue, 7);
+ ref7.setScheduledDeliveryTime(now + 3000);
+ queue.addLast(ref7);
+
+ MessageReference ref8 = generateReference(queue, 8);
+ ref8.setScheduledDeliveryTime(now + 6000);
+ queue.addLast(ref8);
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ //Scheduled refs are added back to *FRONT* of queue - otherwise if there were many messages in the queue
+ //They may get stranded behind a big backlog
+
+ refs.add(ref1);
+ refs.add(ref8);
+ refs.add(ref5);
+ refs.add(ref6);
+ refs.add(ref7);
+
+ refs.add(ref2);
+ refs.add(ref3);
+ refs.add(ref4);
+
+ Thread.sleep(7500);
+
+ FakeConsumer consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+ }
+
+ private void testScheduled(boolean direct)
+ {
+ Queue queue = new QueueImpl(1);
+
+ FakeConsumer consumer = null;
+
+ if (direct)
+ {
+ consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+ }
+
+ //Send one scheduled
+
+ long now = System.currentTimeMillis();
+
+ MessageReference ref1 = generateReference(queue, 1);
+ ref1.setScheduledDeliveryTime(now + 7000);
+ queue.addLast(ref1);
+
+ //Send some non scheduled messages
+
+ MessageReference ref2 = generateReference(queue, 2);
+ queue.addLast(ref2);
+ MessageReference ref3 = generateReference(queue, 3);
+ queue.addLast(ref3);
+ MessageReference ref4 = generateReference(queue, 4);
+ queue.addLast(ref4);
+
+
+ //Now send some more scheduled messages
+
+ MessageReference ref5 = generateReference(queue, 5);
+ ref5.setScheduledDeliveryTime(now + 5000);
+ queue.addLast(ref5);
+
+ MessageReference ref6 = generateReference(queue, 6);
+ ref6.setScheduledDeliveryTime(now + 4000);
+ queue.addLast(ref6);
+
+ MessageReference ref7 = generateReference(queue, 7);
+ ref7.setScheduledDeliveryTime(now + 3000);
+ queue.addLast(ref7);
+
+ MessageReference ref8 = generateReference(queue, 8);
+ ref8.setScheduledDeliveryTime(now + 6000);
+ queue.addLast(ref8);
+
+ if (!direct)
+ {
+ consumer = new FakeConsumer();
+
+ queue.addConsumer(consumer);
+
+ queue.deliver();
+ }
+
+ List<MessageReference> refs = new ArrayList<MessageReference>();
+
+ refs.add(ref2);
+ refs.add(ref3);
+ refs.add(ref4);
+
+ assertRefListsIdenticalRefs(refs, consumer.getReferences());
+
+ refs.clear();
+ consumer.getReferences().clear();
+
+
+ MessageReference ref = consumer.waitForNextReference(TIMEOUT);
+ assertEquals(ref7, ref);
+ long now2 = System.currentTimeMillis();
+ assertTrue(now2 - now >= 3000);
+
+ ref = consumer.waitForNextReference(TIMEOUT);
+ assertEquals(ref6, ref);
+ now2 = System.currentTimeMillis();
+ assertTrue(now2 - now >= 4000);
+
+ ref = consumer.waitForNextReference(TIMEOUT);
+ assertEquals(ref5, ref);
+ now2 = System.currentTimeMillis();
+ assertTrue(now2 - now >= 5000);
+
+ ref = consumer.waitForNextReference(TIMEOUT);
+ assertEquals(ref8, ref);
+ now2 = System.currentTimeMillis();
+ assertTrue(now2 - now >= 6000);
+
+ ref = consumer.waitForNextReference(TIMEOUT);
+ assertEquals(ref1, ref);
+ now2 = System.currentTimeMillis();
+ assertTrue(now2 - now >= 7000);
+
+ assertTrue(consumer.getReferences().isEmpty());
+ }
+
+ // Inner classes ---------------------------------------------------------------
+
+}
+
More information about the jboss-cvs-commits
mailing list