summaryrefslogtreecommitdiff
path: root/cpp/src/tests/BrokerMgmtAgent.cpp
blob: 02aa87f876e4ab805461f27a241811ecfe0a27d5 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 "MessagingFixture.h"
#include "qpid/management/Buffer.h"
#include "qpid/messaging/Message.h"
#include "qpid/amqp_0_10/Codecs.h"
#include "qpid/log/Logger.h"
#include "qpid/log/Options.h"

#include "qmf/org/apache/qpid/broker/mgmt/test/TestObject.h"

#include <iomanip>


using           qpid::management::Mutex;
using           qpid::management::Manageable;
using           qpid::management::Buffer;
using namespace qpid::messaging;
using namespace qpid::types;



namespace qpid {
    namespace tests {

        namespace _qmf = qmf::org::apache::qpid::broker::mgmt::test;
        namespace {

            typedef boost::shared_ptr<_qmf::TestObject> TestObjectPtr;
            typedef std::vector<TestObjectPtr> TestObjectVector;

            // Instantiates a broker and its internal management agent.  Provides
            // factories for constructing Receivers for object indication messages.
            //
            class AgentFixture
            {
                MessagingFixture *mFix;

            public:
                AgentFixture( unsigned int pubInterval=10,
                              bool qmfV2=false,
                              qpid::broker::Broker::Options opts = qpid::broker::Broker::Options())
                {
                    opts.enableMgmt=true;
                    opts.qmf2Support=qmfV2;
                    opts.mgmtPubInterval=pubInterval;
                    mFix = new MessagingFixture(opts, true);

                    _qmf::TestObject::registerSelf(getBrokerAgent());
                };
                ~AgentFixture()
                {
                    delete mFix;
                };
                ::qpid::management::ManagementAgent *getBrokerAgent() { return mFix->broker->getManagementAgent(); }
                Receiver createV1DataIndRcvr( const std::string package, const std::string klass )
                {
                    return mFix->session.createReceiver(std::string("kqueue; {create: always, delete: always, "
                                                                    "node: {type: queue, "
                                                                    "x-bindings: [{exchange: qpid.management, "
                                                                    "key: 'console.obj.1.0.")
                                                        + package + std::string(".") + klass
                                                        + std::string("'}]}}"));
                };
                Receiver createV2DataIndRcvr( const std::string package, const std::string klass )
                {
                    std::string p(package);
                    std::replace(p.begin(), p.end(), '.', '_');
                    std::string k(klass);
                    std::replace(k.begin(), k.end(), '.', '_');

                    return mFix->session.createReceiver(std::string("kqueue; {create: always, delete: always, "
                                                                    "node: {type: queue, "
                                                                    "x-bindings: [{exchange: qmf.default.topic, "
                                                                    "key: 'agent.ind.data.")
                                                        + p + std::string(".") + k
                                                        + std::string("'}]}}"));
                };
            };


            // A "management object" that supports the TestObject
            //
            class TestManageable : public qpid::management::Manageable
            {
                management::ManagementObject*  mgmtObj;
                const std::string key;
            public:
                TestManageable(management::ManagementAgent *agent, std::string _key)
                    : key(_key)
                {
                    _qmf::TestObject *tmp = new _qmf::TestObject(agent, this);

                    // seed it with some default values...
                    tmp->set_string1(key);
                    tmp->set_bool1(true);
                    qpid::types::Variant::Map vMap;
                    vMap["one"] = qpid::types::Variant(1);
                    vMap["two"] = qpid::types::Variant("two");
                    vMap["three"] = qpid::types::Variant("whatever");
                    tmp->set_map1(vMap);

                    mgmtObj = tmp;
                };
                ~TestManageable() { mgmtObj = 0; /* deleted by agent on shutdown */ };
                management::ManagementObject* GetManagementObject() const { return mgmtObj; };
                static void validateTestObjectProperties(_qmf::TestObject& to)
                {
                    // verify the default values are as expected.  We don't check 'string1',
                    // as it is the object key, and is unique for each object (no default value).
                    BOOST_CHECK(to.get_bool1() == true);
                    BOOST_CHECK(to.get_map1().size() == 3);
                    qpid::types::Variant::Map mappy = to.get_map1();
                    BOOST_CHECK(1 == (unsigned int)mappy["one"]);
                    BOOST_CHECK(mappy["two"].asString() == std::string("two"));
                    BOOST_CHECK(mappy["three"].asString() == std::string("whatever"));
                };
            };


            // decode a V1 Content Indication message
            //
            void decodeV1ObjectUpdates(const Message& inMsg, TestObjectVector& objs, const size_t objLen)
            {
                const size_t MAX_BUFFER_SIZE=65536;
                char tmp[MAX_BUFFER_SIZE];

                objs.clear();

                BOOST_CHECK(inMsg.getContent().size() <= MAX_BUFFER_SIZE);

                ::memcpy(tmp, inMsg.getContent().data(), inMsg.getContent().size());
                Buffer buf(tmp, inMsg.getContent().size());

                while (buf.available() > 8) {     // 8 == qmf v1 header size
                    BOOST_CHECK_EQUAL(buf.getOctet(), 'A');
                    BOOST_CHECK_EQUAL(buf.getOctet(), 'M');
                    BOOST_CHECK_EQUAL(buf.getOctet(), '2');
                    BOOST_CHECK_EQUAL(buf.getOctet(), 'c'); // opcode == content indication
                    // @@todo: kag: how do we skip 'i' entries???
                    buf.getLong();  // ignore sequence

                    std::string str1;                   // decode content body as string
                    buf.getRawData(str1, objLen);

                    TestObjectPtr fake(new _qmf::TestObject(0,0));
                    fake->readProperties( str1 );
                    objs.push_back(fake);
                }
            }


            // decode a V2 Content Indication message
            //
            void decodeV2ObjectUpdates(const qpid::messaging::Message& inMsg, TestObjectVector& objs)
            {
                objs.clear();

                BOOST_CHECK_EQUAL(inMsg.getContentType(), std::string("amqp/list"));

                const ::qpid::types::Variant::Map& m = inMsg.getProperties();
                Variant::Map::const_iterator iter = m.find(std::string("qmf.opcode"));
                BOOST_CHECK(iter != m.end());
                BOOST_CHECK_EQUAL(iter->second.asString(), std::string("_data_indication"));

                Variant::List vList;
                ::qpid::amqp_0_10::ListCodec::decode(inMsg.getContent(), vList);

                for (Variant::List::iterator lIter = vList.begin(); lIter != vList.end(); lIter++) {
                    TestObjectPtr fake(new _qmf::TestObject(0,0));
                    fake->readTimestamps(lIter->asMap());
                    fake->mapDecodeValues((lIter->asMap())["_values"].asMap());
                    objs.push_back(fake);
                }
            }
        }

        QPID_AUTO_TEST_SUITE(BrokerMgmtAgent)

        // verify that an object that is added to the broker's management database is
        // published correctly.  Furthermore, verify that it is published once after
        // it has been deleted.
        //
        QPID_AUTO_TEST_CASE(v1ObjPublish)
        {
            AgentFixture* fix = new AgentFixture(3);
            management::ManagementAgent* agent;
            agent = fix->getBrokerAgent();

            // create a manageable test object
            TestManageable *tm = new TestManageable(agent, std::string("obj1"));
            uint32_t objLen = tm->GetManagementObject()->writePropertiesSize();

            Receiver r1 = fix->createV1DataIndRcvr("org.apache.qpid.broker.mgmt.test", "#");

            agent->addObject(tm->GetManagementObject(), 1);

            // wait for the object to be published
            Message m1;
            BOOST_CHECK(r1.fetch(m1, Duration::SECOND * 6));

            TestObjectVector objs;
            decodeV1ObjectUpdates(m1, objs, objLen);
            BOOST_CHECK(objs.size() > 0);

            for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

                TestManageable::validateTestObjectProperties(**oIter);

                qpid::types::Variant::Map mappy;
                (*oIter)->writeTimestamps(mappy);
                BOOST_CHECK(0 == mappy["_delete_ts"].asUint64());   // not deleted
            }

            // destroy the object

            tm->GetManagementObject()->resourceDestroy();

            // wait for the deleted object to be published

            bool isDeleted = false;
            while (!isDeleted && r1.fetch(m1, Duration::SECOND * 6)) {

                decodeV1ObjectUpdates(m1, objs, objLen);
                BOOST_CHECK(objs.size() > 0);

                for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

                    TestManageable::validateTestObjectProperties(**oIter);

                    qpid::types::Variant::Map mappy;
                    (*oIter)->writeTimestamps(mappy);
                    if (mappy["_delete_ts"].asUint64() != 0)
                        isDeleted = true;
                }
            }

            BOOST_CHECK(isDeleted);

            r1.close();
            delete fix;
            delete tm;
        }

        // Repeat the previous test, but with V2-based object support
        //
        QPID_AUTO_TEST_CASE(v2ObjPublish)
        {
            AgentFixture* fix = new AgentFixture(3, true);
            management::ManagementAgent* agent;
            agent = fix->getBrokerAgent();

            TestManageable *tm = new TestManageable(agent, std::string("obj2"));

            Receiver r1 = fix->createV2DataIndRcvr(tm->GetManagementObject()->getPackageName(), "#");

            agent->addObject(tm->GetManagementObject(), "testobj-1");

            // wait for the object to be published
            Message m1;
            BOOST_CHECK(r1.fetch(m1, Duration::SECOND * 6));

            TestObjectVector objs;
            decodeV2ObjectUpdates(m1, objs);
            BOOST_CHECK(objs.size() > 0);

            for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

                TestManageable::validateTestObjectProperties(**oIter);

                qpid::types::Variant::Map mappy;
                (*oIter)->writeTimestamps(mappy);
                BOOST_CHECK(0 == mappy["_delete_ts"].asUint64());
            }

            // destroy the object

            tm->GetManagementObject()->resourceDestroy();

            // wait for the deleted object to be published

            bool isDeleted = false;
            while (!isDeleted && r1.fetch(m1, Duration::SECOND * 6)) {

                decodeV2ObjectUpdates(m1, objs);
                BOOST_CHECK(objs.size() > 0);

                for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

                    TestManageable::validateTestObjectProperties(**oIter);

                    qpid::types::Variant::Map mappy;
                    (*oIter)->writeTimestamps(mappy);
                    if (mappy["_delete_ts"].asUint64() != 0)
                        isDeleted = true;
                }
            }

            BOOST_CHECK(isDeleted);

            r1.close();
            delete fix;
            delete tm;
        }


        // verify that a deleted object is exported correctly using the
        // exportDeletedObjects() method.  V1 testcase.
        //
        QPID_AUTO_TEST_CASE(v1ExportDelObj)
        {
            AgentFixture* fix = new AgentFixture(3);
            management::ManagementAgent* agent;
            agent = fix->getBrokerAgent();

            // create a manageable test object
            TestManageable *tm = new TestManageable(agent, std::string("myObj"));
            uint32_t objLen = tm->GetManagementObject()->writePropertiesSize();

            Receiver r1 = fix->createV1DataIndRcvr("org.apache.qpid.broker.mgmt.test", "#");

            agent->addObject(tm->GetManagementObject(), 1);

            // wait for the object to be published
            Message m1;
            BOOST_CHECK(r1.fetch(m1, Duration::SECOND * 6));

            TestObjectVector objs;
            decodeV1ObjectUpdates(m1, objs, objLen);
            BOOST_CHECK(objs.size() > 0);

            // destroy the object, then immediately export (before the next poll cycle)

            ::qpid::management::ManagementAgent::DeletedObjectList delObjs;
            tm->GetManagementObject()->resourceDestroy();
            agent->exportDeletedObjects( delObjs );
            BOOST_CHECK(delObjs.size() == 1);

            // wait for the deleted object to be published

            bool isDeleted = false;
            while (!isDeleted && r1.fetch(m1, Duration::SECOND * 6)) {

                decodeV1ObjectUpdates(m1, objs, objLen);
                BOOST_CHECK(objs.size() > 0);

                for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

                    TestManageable::validateTestObjectProperties(**oIter);

                    qpid::types::Variant::Map mappy;
                    (*oIter)->writeTimestamps(mappy);
                    if (mappy["_delete_ts"].asUint64() != 0)
                        isDeleted = true;
                }
            }

            BOOST_CHECK(isDeleted);

            // verify there are no deleted objects to export now.

            agent->exportDeletedObjects( delObjs );
            BOOST_CHECK(delObjs.size() == 0);

            r1.close();
            delete fix;
            delete tm;
        }


        // verify that a deleted object is imported correctly using the
        // importDeletedObjects() method.  V1 testcase.
        //
        QPID_AUTO_TEST_CASE(v1ImportDelObj)
        {
            AgentFixture* fix = new AgentFixture(3);
            management::ManagementAgent* agent;
            agent = fix->getBrokerAgent();

            // create a manageable test object
            TestManageable *tm = new TestManageable(agent, std::string("anObj"));
            uint32_t objLen = tm->GetManagementObject()->writePropertiesSize();

            Receiver r1 = fix->createV1DataIndRcvr("org.apache.qpid.broker.mgmt.test", "#");

            agent->addObject(tm->GetManagementObject(), 1);

            // wait for the object to be published
            Message m1;
            BOOST_CHECK(r1.fetch(m1, Duration::SECOND * 6));

            TestObjectVector objs;
            decodeV1ObjectUpdates(m1, objs, objLen);
            BOOST_CHECK(objs.size() > 0);

            // destroy the object, then immediately export (before the next poll cycle)

            ::qpid::management::ManagementAgent::DeletedObjectList delObjs;
            tm->GetManagementObject()->resourceDestroy();
            agent->exportDeletedObjects( delObjs );
            BOOST_CHECK(delObjs.size() == 1);

            // destroy the broker, and reinistantiate a new one without populating it
            // with a TestObject.

            r1.close();
            delete fix;
            delete tm;    // should no longer be necessary

            fix = new AgentFixture(3);
            r1 = fix->createV1DataIndRcvr("org.apache.qpid.broker.mgmt.test", "#");
            agent = fix->getBrokerAgent();
            agent->importDeletedObjects( delObjs );

            // wait for the deleted object to be published

            bool isDeleted = false;
            while (!isDeleted && r1.fetch(m1, Duration::SECOND * 6)) {

                decodeV1ObjectUpdates(m1, objs, objLen);
                BOOST_CHECK(objs.size() > 0);

                for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

                    TestManageable::validateTestObjectProperties(**oIter);

                    qpid::types::Variant::Map mappy;
                    (*oIter)->writeTimestamps(mappy);
                    if (mappy["_delete_ts"].asUint64() != 0)
                        isDeleted = true;
                }
            }

            BOOST_CHECK(isDeleted);

            // verify there are no deleted objects to export now.

            agent->exportDeletedObjects( delObjs );
            BOOST_CHECK(delObjs.size() == 0);

            r1.close();
            delete fix;
        }


        // verify that an object that is added and deleted prior to the
        // first poll cycle is accounted for by the export
        //
        QPID_AUTO_TEST_CASE(v1ExportFastDelObj)
        {
            AgentFixture* fix = new AgentFixture(3);
            management::ManagementAgent* agent;
            agent = fix->getBrokerAgent();

            // create a manageable test object
            TestManageable *tm = new TestManageable(agent, std::string("objectifyMe"));

            // add, then immediately delete and export the object...

            ::qpid::management::ManagementAgent::DeletedObjectList delObjs;
            agent->addObject(tm->GetManagementObject(), 999);
            tm->GetManagementObject()->resourceDestroy();
            agent->exportDeletedObjects( delObjs );
            BOOST_CHECK(delObjs.size() == 1);

            delete fix;
            delete tm;
        }


        // Verify that we can export and import multiple deleted objects correctly.
        //
        QPID_AUTO_TEST_CASE(v1ImportMultiDelObj)
        {
            AgentFixture* fix = new AgentFixture(3);
            management::ManagementAgent* agent;
            agent = fix->getBrokerAgent();

            Receiver r1 = fix->createV1DataIndRcvr("org.apache.qpid.broker.mgmt.test", "#");

            // populate the agent with multiple test objects
            const size_t objCount = 50;
            std::vector<TestManageable *> tmv;
            uint32_t objLen;

            for (size_t i = 0; i < objCount; i++) {
                std::stringstream key;
                key << "testobj-" << std::setfill('x') << std::setw(4) << i;
                // (no, seriously, I didn't just do that.)
                // Note well: we have to keep the key string length EXACTLY THE SAME
                // FOR ALL OBJECTS, so objLen will be the same.  Otherwise the
                // decodeV1ObjectUpdates() will fail (v1 lacks explict encoded length).
                TestManageable *tm = new TestManageable(agent, key.str());
                objLen = tm->GetManagementObject()->writePropertiesSize();
                agent->addObject(tm->GetManagementObject(), i + 1);
                tmv.push_back(tm);
            }

            // wait for the objects to be published
            Message m1;
            uint32_t    msgCount = 0;
            while(r1.fetch(m1, Duration::SECOND * 6)) {
                TestObjectVector objs;
                decodeV1ObjectUpdates(m1, objs, objLen);
                msgCount += objs.size();
            }

            BOOST_CHECK_EQUAL(msgCount, objCount);

            // destroy some of the objects, then immediately export (before the next poll cycle)

            uint32_t delCount = 0;
            for (size_t i = 0; i < objCount; i += 2) {
                tmv[i]->GetManagementObject()->resourceDestroy();
                delCount++;
            }

            ::qpid::management::ManagementAgent::DeletedObjectList delObjs;
            agent->exportDeletedObjects( delObjs );
            BOOST_CHECK_EQUAL(delObjs.size(), delCount);

            // destroy the broker, and reinistantiate a new one without populating it
            // with TestObjects.

            r1.close();
            delete fix;
            while (tmv.size()) {
                delete tmv.back();
                tmv.pop_back();
            }

            fix = new AgentFixture(3);
            r1 = fix->createV1DataIndRcvr("org.apache.qpid.broker.mgmt.test", "#");
            agent = fix->getBrokerAgent();
            agent->importDeletedObjects( delObjs );

            // wait for the deleted object to be published, verify the count

            uint32_t countDels = 0;
            while (r1.fetch(m1, Duration::SECOND * 6)) {
                TestObjectVector objs;
                decodeV1ObjectUpdates(m1, objs, objLen);
                BOOST_CHECK(objs.size() > 0);

                
                for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

                    TestManageable::validateTestObjectProperties(**oIter);

                    qpid::types::Variant::Map mappy;
                    (*oIter)->writeTimestamps(mappy);
                    if (mappy["_delete_ts"].asUint64() != 0)
                        countDels++;
                }
            }

            // make sure we get the correct # of deleted objects
            BOOST_CHECK_EQUAL(countDels, delCount);

            // verify there are no deleted objects to export now.

            agent->exportDeletedObjects( delObjs );
            BOOST_CHECK(delObjs.size() == 0);

            r1.close();
            delete fix;
        }

        // Verify that we can export and import multiple deleted objects correctly.
        // QMF V2 variant
        QPID_AUTO_TEST_CASE(v2ImportMultiDelObj)
        {
            AgentFixture* fix = new AgentFixture(3, true);
            management::ManagementAgent* agent;
            agent = fix->getBrokerAgent();

            Receiver r1 = fix->createV2DataIndRcvr("org.apache.qpid.broker.mgmt.test", "#");

            // populate the agent with multiple test objects
            const size_t objCount = 50;
            std::vector<TestManageable *> tmv;

            for (size_t i = 0; i < objCount; i++) {
                std::stringstream key;
                key << "testobj-" << i;
                TestManageable *tm = new TestManageable(agent, key.str());
                if (tm->GetManagementObject()->writePropertiesSize()) {}
                agent->addObject(tm->GetManagementObject(), key.str());
                tmv.push_back(tm);
            }

            // wait for the objects to be published
            Message m1;
            uint32_t    msgCount = 0;
            while(r1.fetch(m1, Duration::SECOND * 6)) {
                TestObjectVector objs;
                decodeV2ObjectUpdates(m1, objs);
                msgCount += objs.size();
            }

            BOOST_CHECK_EQUAL(msgCount, objCount);

            // destroy some of the objects, then immediately export (before the next poll cycle)

            uint32_t delCount = 0;
            for (size_t i = 0; i < objCount; i += 2) {
                tmv[i]->GetManagementObject()->resourceDestroy();
                delCount++;
            }

            ::qpid::management::ManagementAgent::DeletedObjectList delObjs;
            agent->exportDeletedObjects( delObjs );
            BOOST_CHECK_EQUAL(delObjs.size(), delCount);

            // destroy the broker, and reinistantiate a new one without populating it
            // with TestObjects.

            r1.close();
            delete fix;
            while (tmv.size()) {
                delete tmv.back();
                tmv.pop_back();
            }

            fix = new AgentFixture(3, true);
            r1 = fix->createV2DataIndRcvr("org.apache.qpid.broker.mgmt.test", "#");
            agent = fix->getBrokerAgent();
            agent->importDeletedObjects( delObjs );

            // wait for the deleted object to be published, verify the count

            uint32_t countDels = 0;
            while (r1.fetch(m1, Duration::SECOND * 6)) {
                TestObjectVector objs;
                decodeV2ObjectUpdates(m1, objs);
                BOOST_CHECK(objs.size() > 0);

                for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {

                    TestManageable::validateTestObjectProperties(**oIter);

                    qpid::types::Variant::Map mappy;
                    (*oIter)->writeTimestamps(mappy);
                    if (mappy["_delete_ts"].asUint64() != 0)
                        countDels++;
                }
            }

            // make sure we get the correct # of deleted objects
            BOOST_CHECK_EQUAL(countDels, delCount);

            // verify there are no deleted objects to export now.

            agent->exportDeletedObjects( delObjs );
            BOOST_CHECK(delObjs.size() == 0);

            r1.close();
            delete fix;
        }

        // See QPID-2997
        QPID_AUTO_TEST_CASE(v2RapidRestoreObj)
        {
            AgentFixture* fix = new AgentFixture(3, true);
            management::ManagementAgent* agent;
            agent = fix->getBrokerAgent();

            // two objects, same ObjID
            TestManageable *tm1 = new TestManageable(agent, std::string("obj2"));
            TestManageable *tm2 = new TestManageable(agent, std::string("obj2"));

            Receiver r1 = fix->createV2DataIndRcvr(tm1->GetManagementObject()->getPackageName(), "#");

            // add, then immediately delete and re-add a copy of the object
            agent->addObject(tm1->GetManagementObject(), "testobj-1");
            tm1->GetManagementObject()->resourceDestroy();
            agent->addObject(tm2->GetManagementObject(), "testobj-1");

            // expect: a delete notification, then an update notification
            TestObjectVector objs;
            bool isDeleted = false;
            bool isAdvertised = false;
            size_t count = 0;
            Message m1;
            while (r1.fetch(m1, Duration::SECOND * 6)) {

                decodeV2ObjectUpdates(m1, objs);
                BOOST_CHECK(objs.size() > 0);

                for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {
                    count++;
                    TestManageable::validateTestObjectProperties(**oIter);

                    qpid::types::Variant::Map mappy;
                    (*oIter)->writeTimestamps(mappy);
                    if (mappy["_delete_ts"].asUint64() != 0) {
                        isDeleted = true;
                        BOOST_CHECK(isAdvertised == false);  // delete must be first
                    } else {
                        isAdvertised = true;
                        BOOST_CHECK(isDeleted == true);     // delete must be first
                    }
                }
            }

            BOOST_CHECK(isDeleted);
            BOOST_CHECK(isAdvertised);
            BOOST_CHECK(count == 2);

            r1.close();
            delete fix;
            delete tm1;
            delete tm2;
        }

        // See QPID-2997
        QPID_AUTO_TEST_CASE(v2DuplicateErrorObj)
        {
            AgentFixture* fix = new AgentFixture(3, true);
            management::ManagementAgent* agent;
            agent = fix->getBrokerAgent();

            // turn off the expected error log message
            qpid::log::Options logOpts;
            logOpts.selectors.clear();
            logOpts.selectors.push_back("critical+");
            qpid::log::Logger::instance().configure(logOpts);

            // two objects, same ObjID
            TestManageable *tm1 = new TestManageable(agent, std::string("obj2"));
            TestManageable *tm2 = new TestManageable(agent, std::string("obj2"));
            // Keep a pointer to the ManagementObject.  This test simulates a user-caused error
            // case (duplicate objects) where the broker has no choice but to leak a management
            // object (safest assumption).  To prevent valgrind from flagging this leak, we
            // manually clean up the object at the end of the test.
            management::ManagementObject *save = tm2->GetManagementObject();

            Receiver r1 = fix->createV2DataIndRcvr(tm1->GetManagementObject()->getPackageName(), "#");

            // add, then immediately delete and re-add a copy of the object
            agent->addObject(tm1->GetManagementObject(), "testobj-1");
            agent->addObject(tm2->GetManagementObject(), "testobj-1");

            TestObjectVector objs;
            size_t count = 0;
            Message m1;
            while (r1.fetch(m1, Duration::SECOND * 6)) {

                decodeV2ObjectUpdates(m1, objs);
                BOOST_CHECK(objs.size() > 0);

                for (TestObjectVector::iterator oIter = objs.begin(); oIter != objs.end(); oIter++) {
                    count++;
                    TestManageable::validateTestObjectProperties(**oIter);
                }
            }

            BOOST_CHECK(count == 1);    // only one should be accepted.

            r1.close();
            delete fix;
            delete tm1;
            delete tm2;
            delete save;
        }

        QPID_AUTO_TEST_SUITE_END()
    }
}