diff options
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/src/qpid/broker/NullMessageStore.cpp | 4 | ||||
-rw-r--r-- | cpp/src/qpid/broker/NullMessageStore.h | 2 | ||||
-rw-r--r-- | cpp/src/qpid/broker/TransactionalStore.h | 6 | ||||
-rw-r--r-- | cpp/src/qpid/broker/TxBuffer.cpp | 20 | ||||
-rw-r--r-- | cpp/test/unit/qpid/broker/TxAckTest.cpp | 2 | ||||
-rw-r--r-- | cpp/test/unit/qpid/broker/TxBufferTest.cpp | 84 | ||||
-rw-r--r-- | cpp/test/unit/qpid/broker/TxPublishTest.cpp | 2 |
7 files changed, 66 insertions, 54 deletions
diff --git a/cpp/src/qpid/broker/NullMessageStore.cpp b/cpp/src/qpid/broker/NullMessageStore.cpp index 3a07961670..87df407ad8 100644 --- a/cpp/src/qpid/broker/NullMessageStore.cpp +++ b/cpp/src/qpid/broker/NullMessageStore.cpp @@ -46,8 +46,8 @@ void NullMessageStore::committed(const string * const){ void NullMessageStore::aborted(const string * const){ std::cout << "WARNING: Persistence not enabled." << std::endl; } -TransactionContext* NullMessageStore::begin(){ - return 0; +std::auto_ptr<TransactionContext> NullMessageStore::begin(){ + return std::auto_ptr<TransactionContext>(); } void NullMessageStore::commit(TransactionContext*){ } diff --git a/cpp/src/qpid/broker/NullMessageStore.h b/cpp/src/qpid/broker/NullMessageStore.h index 9b89920416..3b34a7f6a0 100644 --- a/cpp/src/qpid/broker/NullMessageStore.h +++ b/cpp/src/qpid/broker/NullMessageStore.h @@ -39,7 +39,7 @@ namespace qpid { void dequeue(TransactionContext* ctxt, Message::shared_ptr& msg, const Queue& queue, const string * const xid); void committed(const string * const xid); void aborted(const string * const xid); - TransactionContext* begin(); + std::auto_ptr<TransactionContext> begin(); void commit(TransactionContext* ctxt); void abort(TransactionContext* ctxt); ~NullMessageStore(){} diff --git a/cpp/src/qpid/broker/TransactionalStore.h b/cpp/src/qpid/broker/TransactionalStore.h index 57dc411cd8..f11c5de31b 100644 --- a/cpp/src/qpid/broker/TransactionalStore.h +++ b/cpp/src/qpid/broker/TransactionalStore.h @@ -18,8 +18,12 @@ #ifndef _TransactionalStore_ #define _TransactionalStore_ +#include <memory> + namespace qpid { namespace broker { + struct InvalidTransactionContextException : public std::exception {}; + class TransactionContext{ public: virtual ~TransactionContext(){} @@ -27,7 +31,7 @@ namespace qpid { class TransactionalStore{ public: - virtual TransactionContext* begin() = 0; + virtual std::auto_ptr<TransactionContext> begin() = 0; virtual void commit(TransactionContext*) = 0; virtual void abort(TransactionContext*) = 0; diff --git a/cpp/src/qpid/broker/TxBuffer.cpp b/cpp/src/qpid/broker/TxBuffer.cpp index fe2ea8fbb1..920b9862b0 100644 --- a/cpp/src/qpid/broker/TxBuffer.cpp +++ b/cpp/src/qpid/broker/TxBuffer.cpp @@ -20,27 +20,31 @@ using std::mem_fun; using namespace qpid::broker; -bool TxBuffer::prepare(TransactionalStore* const store){ - TransactionContext* ctxt(0); +bool TxBuffer::prepare(TransactionalStore* const store) +{ + std::auto_ptr<TransactionContext> ctxt; if(store) ctxt = store->begin(); for(op_iterator i = ops.begin(); i < ops.end(); i++){ - if(!(*i)->prepare(ctxt)){ - if(store) store->abort(ctxt); + if(!(*i)->prepare(ctxt.get())){ + if(store) store->abort(ctxt.get()); return false; } } - if(store) store->commit(ctxt); + if(store) store->commit(ctxt.get()); return true; } -void TxBuffer::commit(){ +void TxBuffer::commit() +{ for_each(ops.begin(), ops.end(), mem_fun(&TxOp::commit)); } -void TxBuffer::rollback(){ +void TxBuffer::rollback() +{ for_each(ops.begin(), ops.end(), mem_fun(&TxOp::rollback)); } -void TxBuffer::enlist(TxOp* const op){ +void TxBuffer::enlist(TxOp* const op) +{ ops.push_back(op); } diff --git a/cpp/test/unit/qpid/broker/TxAckTest.cpp b/cpp/test/unit/qpid/broker/TxAckTest.cpp index a619809b97..b6f8fbc1a1 100644 --- a/cpp/test/unit/qpid/broker/TxAckTest.cpp +++ b/cpp/test/unit/qpid/broker/TxAckTest.cpp @@ -48,7 +48,7 @@ class TxAckTest : public CppUnit::TestCase void enqueue(TransactionContext*, Message::shared_ptr&, const Queue&, const string * const){} void committed(const string * const){} void aborted(const string * const){} - TransactionContext* begin(){ return 0; } + std::auto_ptr<TransactionContext> begin(){ return std::auto_ptr<TransactionContext>(); } void commit(TransactionContext*){} void abort(TransactionContext*){} ~TestMessageStore(){} diff --git a/cpp/test/unit/qpid/broker/TxBufferTest.cpp b/cpp/test/unit/qpid/broker/TxBufferTest.cpp index 35b92671aa..e9c531fad1 100644 --- a/cpp/test/unit/qpid/broker/TxBufferTest.cpp +++ b/cpp/test/unit/qpid/broker/TxBufferTest.cpp @@ -34,35 +34,6 @@ template <class T> void assertEqualVector(std::vector<T>& expected, std::vector< class TxBufferTest : public CppUnit::TestCase { - class TestTransactionContext : public TransactionContext{ - enum states {OPEN = 1, COMMITTED = 2, ABORTED = 3}; - int state; - public: - TestTransactionContext() : state(OPEN) {} - void commit(){ - if(state != OPEN) throw "txn already completed"; - state = COMMITTED; - } - - void abort(){ - if(state != OPEN) throw "txn already completed"; - state = ABORTED; - } - - bool isCommitted(){ - return state == COMMITTED; - } - - bool isAborted(){ - return state == ABORTED; - } - - bool isOpen(){ - return state == OPEN; - } - ~TestTransactionContext(){} - }; - class MockTxOp : public TxOp{ enum op_codes {PREPARE=2, COMMIT=4, ROLLBACK=8}; std::vector<int> expected; @@ -105,24 +76,45 @@ class TxBufferTest : public CppUnit::TestCase std::vector<int> expected; std::vector<int> actual; + enum states {OPEN = 1, COMMITTED = 2, ABORTED = 3}; + int state; + + class TestTransactionContext : public TransactionContext{ + MockTransactionalStore* store; + public: + TestTransactionContext(MockTransactionalStore* _store) : store(_store) {} + void commit(){ + if(store->state != OPEN) throw "txn already completed"; + store->state = COMMITTED; + } + + void abort(){ + if(store->state != OPEN) throw "txn already completed"; + store->state = ABORTED; + } + ~TestTransactionContext(){} + }; + + public: - TestTransactionContext txn; + MockTransactionalStore() : state(OPEN){} - TransactionContext* begin(){ + std::auto_ptr<TransactionContext> begin(){ actual.push_back(BEGIN); - return &txn; + std::auto_ptr<TransactionContext> txn(new TestTransactionContext(this)); + return txn; } void commit(TransactionContext* ctxt){ actual.push_back(COMMIT); - TestTransactionContext* _txn(dynamic_cast<TestTransactionContext*>(ctxt)); - CPPUNIT_ASSERT_EQUAL(_txn, &txn); - _txn->commit(); + TestTransactionContext* txn(dynamic_cast<TestTransactionContext*>(ctxt)); + CPPUNIT_ASSERT(txn); + txn->commit(); } void abort(TransactionContext* ctxt){ actual.push_back(ABORT); - TestTransactionContext* _txn(dynamic_cast<TestTransactionContext*>(ctxt)); - CPPUNIT_ASSERT_EQUAL(_txn, &txn); - _txn->abort(); + TestTransactionContext* txn(dynamic_cast<TestTransactionContext*>(ctxt)); + CPPUNIT_ASSERT(txn); + txn->abort(); } MockTransactionalStore& expectBegin(){ expected.push_back(BEGIN); @@ -139,6 +131,18 @@ class TxBufferTest : public CppUnit::TestCase void check(){ assertEqualVector(expected, actual); } + + bool isCommitted(){ + return state == COMMITTED; + } + + bool isAborted(){ + return state == ABORTED; + } + + bool isOpen(){ + return state == OPEN; + } ~MockTransactionalStore(){} }; @@ -170,7 +174,7 @@ class TxBufferTest : public CppUnit::TestCase CPPUNIT_ASSERT(buffer.prepare(&store)); buffer.commit(); store.check(); - CPPUNIT_ASSERT(store.txn.isCommitted()); + CPPUNIT_ASSERT(store.isCommitted()); opA.check(); opB.check(); opC.check(); @@ -193,7 +197,7 @@ class TxBufferTest : public CppUnit::TestCase CPPUNIT_ASSERT(!buffer.prepare(&store)); store.check(); - CPPUNIT_ASSERT(store.txn.isAborted()); + CPPUNIT_ASSERT(store.isAborted()); opA.check(); opB.check(); opC.check(); diff --git a/cpp/test/unit/qpid/broker/TxPublishTest.cpp b/cpp/test/unit/qpid/broker/TxPublishTest.cpp index b8387b0752..a3a72b9475 100644 --- a/cpp/test/unit/qpid/broker/TxPublishTest.cpp +++ b/cpp/test/unit/qpid/broker/TxPublishTest.cpp @@ -49,7 +49,7 @@ class TxPublishTest : public CppUnit::TestCase void dequeue(TransactionContext*, Message::shared_ptr&, const Queue&, const string * const){} void committed(const string * const){} void aborted(const string * const){} - TransactionContext* begin(){ return 0; } + std::auto_ptr<TransactionContext> begin(){ return std::auto_ptr<TransactionContext>(); } void commit(TransactionContext*){} void abort(TransactionContext*){} ~TestMessageStore(){} |