summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/AsyncCompletion.cpp
blob: e33b2dc35d3e74fef712af34e823ee43e677a6eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 *
 * Copyright (c) 2006 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */


#include "unit_test.h"
#include "test_tools.h"
#include "BrokerFixture.h"
#include "qpid/broker/NullMessageStore.h"
#include "qpid/sys/BlockingQueue.h"
#include "qpid/client/AsyncSession.h"
#include "qpid/sys/Time.h"

using namespace std;
using namespace qpid;
using namespace client;
using namespace framing;

namespace qpid { namespace broker {
class TransactionContext;
class PersistableQueue;
}}

using broker::PersistableMessage;
using broker::NullMessageStore;
using broker::TransactionContext;
using broker::PersistableQueue;
using sys::TIME_SEC;
using boost::intrusive_ptr;

/** @file Unit tests for async completion.
 * Using a dummy store, verify that the broker indicates async completion of
 * message enqueues at the correct time.
 */

class AsyncCompletionMessageStore : public NullMessageStore {
  public:
    sys::BlockingQueue<boost::intrusive_ptr<PersistableMessage> > enqueued;
    
    AsyncCompletionMessageStore() : NullMessageStore() {}
    ~AsyncCompletionMessageStore(){}

    void enqueue(TransactionContext*,
                 const boost::intrusive_ptr<PersistableMessage>& msg,
                 const PersistableQueue& )
    {
        enqueued.push(msg);
    }
};

QPID_AUTO_TEST_SUITE(AsyncCompletionTestSuite)

QPID_AUTO_TEST_CASE(testWaitTillComplete) {
    AsyncCompletionMessageStore* store = new AsyncCompletionMessageStore;
    SessionFixture fix;
    fix.broker->setStore(store); // Broker will delete store.
    AsyncSession s = fix.session;

    static const int count = 3;

    s.queueDeclare("q", arg::durable=true);
    Completion transfers[count];
    for (int i = 0; i < count; ++i) {
        Message msg(boost::lexical_cast<string>(i), "q");
        msg.getDeliveryProperties().setDeliveryMode(PERSISTENT);
        transfers[i] = s.messageTransfer(arg::content=msg);
    }

    // Get hold of the broker-side messages. 
    typedef vector<intrusive_ptr<PersistableMessage> > BrokerMessages;
    BrokerMessages enqueued;
    for (int j = 0; j < count; ++j) 
        enqueued.push_back(store->enqueued.pop(TIME_SEC));

    // Send a sync, make sure it does not complete till all messages are complete.
    // In reverse order for fun.
    Completion sync = s.executionSync(arg::sync=true);
    for (int k = count-1; k >= 0; --k) {
        BOOST_CHECK(!transfers[k].isComplete()); // Should not be complete yet.
        BOOST_CHECK(!sync.isComplete()); // Should not be complete yet.
        enqueued[k]->enqueueComplete();
    }
    sync.wait();                // Should complete now, all messages are completed.
}

QPID_AUTO_TEST_SUITE_END()