File: TestWatchers.cc

package info (click to toggle)
zookeeper 3.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,804 kB
  • sloc: java: 121,943; cpp: 13,986; ansic: 12,419; javascript: 11,754; xml: 4,965; python: 2,829; sh: 2,444; makefile: 241; perl: 114
file content (773 lines) | stat: -rw-r--r-- 31,298 bytes parent folder | download | duplicates (9)
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
/**
 * 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 <cppunit/extensions/HelperMacros.h>
#include "CppAssertHelper.h"

#include "ZKMocks.h"
#include "CollectionUtil.h"
#include "Util.h"

class Zookeeper_watchers : public CPPUNIT_NS::TestFixture
{
    CPPUNIT_TEST_SUITE(Zookeeper_watchers);
    CPPUNIT_TEST(testDefaultSessionWatcher1);
    CPPUNIT_TEST(testDefaultSessionWatcher2);
    CPPUNIT_TEST(testObjectSessionWatcher1);
    CPPUNIT_TEST(testObjectSessionWatcher2);
    CPPUNIT_TEST(testNodeWatcher1);
    CPPUNIT_TEST(testChildWatcher1);
    CPPUNIT_TEST(testChildWatcher2);
    CPPUNIT_TEST_SUITE_END();

    static void watcher(zhandle_t *, int, int, const char *,void*){}
    zhandle_t *zh;
    FILE *logfile;
    
public:

    Zookeeper_watchers() {
      logfile = openlogfile("Zookeeper_watchers");
    }

    ~Zookeeper_watchers() {
      if (logfile) {
        fflush(logfile);
        fclose(logfile);
        logfile = 0;
      }
    }

    void setUp()
    {
        zoo_set_log_stream(logfile);

        zoo_deterministic_conn_order(0);
        zh=0;
    }
    
    void tearDown()
    {
        zookeeper_close(zh);
    }
    
    class ConnectionWatcher: public WatcherAction{
    public:
        ConnectionWatcher():connected_(false),counter_(0){}
        virtual void onConnectionEstablished(zhandle_t*){
            synchronized(mx_);
            counter_++;
            connected_=true;
        }
        SyncedBoolCondition isConnectionEstablished() const{
            return SyncedBoolCondition(connected_,mx_);
        }
        bool connected_;
        int counter_;
    };

    class DisconnectWatcher: public WatcherAction{
    public:
        DisconnectWatcher():disconnected_(false),counter_(0){}
        virtual void onConnectionLost(zhandle_t*){
            synchronized(mx_);
            counter_++;
            disconnected_=true;
        }
        SyncedBoolCondition isDisconnected() const{
            return SyncedBoolCondition(disconnected_,mx_);
        }
        bool disconnected_;
        int counter_;
    };

    class CountingDataWatcher: public WatcherAction{
    public:
        CountingDataWatcher():disconnected_(false),counter_(0){}
        virtual void onNodeValueChanged(zhandle_t*,const char* path){
            synchronized(mx_);
            counter_++;
        }
        virtual void onConnectionLost(zhandle_t*){
            synchronized(mx_);
            counter_++;
            disconnected_=true;
        }
        bool disconnected_;
        int counter_;
    };

    class DeletionCountingDataWatcher: public WatcherAction{
    public:
        DeletionCountingDataWatcher():counter_(0){}
        virtual void onNodeDeleted(zhandle_t*,const char* path){
            synchronized(mx_);
            counter_++;
        }
        int counter_;
    };

    class ChildEventCountingWatcher: public WatcherAction{
    public:
        ChildEventCountingWatcher():counter_(0){}
        virtual void onChildChanged(zhandle_t*,const char* path){
            synchronized(mx_);
            counter_++;
        }
        int counter_;
    };

#ifndef THREADED
    
    // verify: the default watcher is called once for a session event
    void testDefaultSessionWatcher1(){
        Mock_gettimeofday timeMock;
        ZookeeperServer zkServer;
        // must call zookeeper_close() while all the mocks are in scope
        CloseFinally guard(&zh);

        ConnectionWatcher watcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &watcher,0);
        CPPUNIT_ASSERT(zh!=0);
        
        int fd=0;
        int interest=0;
        timeval tv;
        // open the socket
        int rc=zookeeper_interest(zh,&fd,&interest,&tv);        
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);        
        CPPUNIT_ASSERT_EQUAL(ZOO_CONNECTING_STATE,zoo_state(zh));
        // send the handshake packet to the server
        rc=zookeeper_process(zh,interest);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);        
        CPPUNIT_ASSERT_EQUAL(ZOO_ASSOCIATING_STATE,zoo_state(zh));
        // receive the server handshake response
        rc=zookeeper_process(zh,interest);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        // verify connected
        CPPUNIT_ASSERT_EQUAL(ZOO_CONNECTED_STATE,zoo_state(zh));
        CPPUNIT_ASSERT(watcher.connected_);
        CPPUNIT_ASSERT_EQUAL(1,watcher.counter_);
    }
    
    // test case: connect to server, set a default watcher, disconnect from the server
    // verify: the default watcher is called once
    void testDefaultSessionWatcher2(){
        Mock_gettimeofday timeMock;
        ZookeeperServer zkServer;
        // must call zookeeper_close() while all the mocks are in scope
        CloseFinally guard(&zh);

        DisconnectWatcher watcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &watcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // simulate connected state
        forceConnected(zh);
        
        // first operation
        AsyncCompletion ignored;
        zkServer.addOperationResponse(new ZooGetResponse("1",1));
        int rc=zoo_aget(zh,"/x/y/1",0,asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        // this will process the response and activate the watcher
        rc=zookeeper_process(zh,ZOOKEEPER_READ);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);

        // now, disconnect
        zkServer.setConnectionLost();
        rc=zookeeper_process(zh,ZOOKEEPER_READ);
        CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS,rc);
        // verify disconnected
        CPPUNIT_ASSERT(watcher.disconnected_);
        CPPUNIT_ASSERT_EQUAL(1,watcher.counter_);
    }
    
    // testcase: connect to the server, set a watcher object on a node, 
    //           disconnect from the server
    // verify: the watcher object as well as the default watcher are called
    void testObjectSessionWatcher1(){
        Mock_gettimeofday timeMock;
        ZookeeperServer zkServer;
        // must call zookeeper_close() while all the mocks are in scope
        CloseFinally guard(&zh);

        DisconnectWatcher defWatcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &defWatcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // simulate connected state
        forceConnected(zh);
        
        AsyncCompletion ignored;
        CountingDataWatcher wobject;
        zkServer.addOperationResponse(new ZooStatResponse);
        int rc=zoo_awexists(zh,"/x/y/1",activeWatcher,&wobject,
                asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        // this will process the response and activate the watcher
        rc=zookeeper_process(zh,ZOOKEEPER_READ);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);

        // now, disconnect
        zkServer.setConnectionLost();
        rc=zookeeper_process(zh,ZOOKEEPER_READ);
        CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS,rc);

        // verify the default watcher has been triggered
        CPPUNIT_ASSERT(defWatcher.disconnected_);
        // and triggered only once
        CPPUNIT_ASSERT_EQUAL(1,defWatcher.counter_);
        
        // the path-specific watcher has been triggered as well
        CPPUNIT_ASSERT(wobject.disconnected_);
        // only once!
        CPPUNIT_ASSERT_EQUAL(1,wobject.counter_);
    }

    // testcase: connect to the server, set a watcher object on a node, 
    //           set a def watcher on another node,disconnect from the server
    // verify: the watcher object as well as the default watcher are called
    void testObjectSessionWatcher2(){
        Mock_gettimeofday timeMock;
        ZookeeperServer zkServer;
        // must call zookeeper_close() while all the mocks are in scope
        CloseFinally guard(&zh);

        DisconnectWatcher defWatcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &defWatcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // simulate connected state
        forceConnected(zh);
        
        // set the default watcher
        AsyncCompletion ignored;
        zkServer.addOperationResponse(new ZooStatResponse);
        int rc=zoo_aexists(zh,"/a/b/c",1,asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);

        CountingDataWatcher wobject;
        zkServer.addOperationResponse(new ZooStatResponse);
        rc=zoo_awexists(zh,"/x/y/z",activeWatcher,&wobject,
                asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        
        // this will process the response and activate the watcher
        while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) {
          millisleep(100);
        }
        CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc);

        // disconnect now
        zkServer.setConnectionLost();
        rc=zookeeper_process(zh,ZOOKEEPER_READ);
        CPPUNIT_ASSERT_EQUAL((int)ZCONNECTIONLOSS,rc);

        // verify the default watcher has been triggered
        CPPUNIT_ASSERT(defWatcher.disconnected_);
        // and triggered only once
        CPPUNIT_ASSERT_EQUAL(1,defWatcher.counter_);
        
        // the path-specific watcher has been triggered as well
        CPPUNIT_ASSERT(wobject.disconnected_);
        // only once!
        CPPUNIT_ASSERT_EQUAL(1,wobject.counter_);
    }

    // testcase: register 2 node watches for different paths, trigger the watches
    // verify: the data watchers are processed, the default watcher is not called
    void testNodeWatcher1(){
        Mock_gettimeofday timeMock;
        ZookeeperServer zkServer;
        // must call zookeeper_close() while all the mocks are in scope
        CloseFinally guard(&zh);

        DisconnectWatcher defWatcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &defWatcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // simulate connected state
        forceConnected(zh);
        
        AsyncCompletion ignored;
        CountingDataWatcher wobject1;
        zkServer.addOperationResponse(new ZooStatResponse);
        int rc=zoo_awexists(zh,"/a/b/c",activeWatcher,&wobject1,
                asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);

        CountingDataWatcher wobject2;
        zkServer.addOperationResponse(new ZooStatResponse);
        rc=zoo_awexists(zh,"/x/y/z",activeWatcher,&wobject2,
                asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        
        // this will process the response and activate the watcher
        while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) {
          millisleep(100);
        }
        CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc);

        // we are all set now; let's trigger the watches
        zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHANGED_EVENT,"/a/b/c"));
        zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHANGED_EVENT,"/x/y/z"));
        // make sure all watchers have been processed
        while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) {
          millisleep(100);
        }
        CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc);
        
        CPPUNIT_ASSERT_EQUAL(1,wobject1.counter_);
        CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_);        
        CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_);
    }

    // testcase: set up both a children and a data watchers on the node /a, then
    //           delete the node by sending a DELETE_EVENT event
    // verify: both watchers are triggered
    void testChildWatcher1(){
        Mock_gettimeofday timeMock;
        ZookeeperServer zkServer;
        // must call zookeeper_close() while all the mocks are in scope
        CloseFinally guard(&zh);

        DeletionCountingDataWatcher defWatcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &defWatcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // simulate connected state
        forceConnected(zh);
        
        AsyncCompletion ignored;
        DeletionCountingDataWatcher wobject1;
        zkServer.addOperationResponse(new ZooStatResponse);
        int rc=zoo_awexists(zh,"/a",activeWatcher,&wobject1,
                asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);

        typedef ZooGetChildrenResponse::StringVector ZooVector;
        zkServer.addOperationResponse(new ZooGetChildrenResponse(
                Util::CollectionBuilder<ZooVector>()("/a/1")("/a/2")
                ));
        DeletionCountingDataWatcher wobject2;
        rc=zoo_awget_children(zh,"/a",activeWatcher,
                &wobject2,asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        
        // this will process the response and activate the watcher
        while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) {
          millisleep(100);
        }
        CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc);

        // we are all set now; let's trigger the watches
        zkServer.addRecvResponse(new ZNodeEvent(ZOO_DELETED_EVENT,"/a"));
        // make sure the watchers have been processed
        while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) {
          millisleep(100);
        }
        CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc);

        CPPUNIT_ASSERT_EQUAL(1,wobject1.counter_);
        CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_);        
        CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_);
    }

    // testcase: create both a child and data watch on the node /a, send a ZOO_CHILD_EVENT
    // verify: only the child watch triggered
    void testChildWatcher2(){
        Mock_gettimeofday timeMock;
        ZookeeperServer zkServer;
        // must call zookeeper_close() while all the mocks are in scope
        CloseFinally guard(&zh);

        ChildEventCountingWatcher defWatcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &defWatcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // simulate connected state
        forceConnected(zh);
        
        AsyncCompletion ignored;
        ChildEventCountingWatcher wobject1;
        zkServer.addOperationResponse(new ZooStatResponse);
        int rc=zoo_awexists(zh,"/a",activeWatcher,&wobject1,
                asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);

        typedef ZooGetChildrenResponse::StringVector ZooVector;
        zkServer.addOperationResponse(new ZooGetChildrenResponse(
                Util::CollectionBuilder<ZooVector>()("/a/1")("/a/2")
                ));
        ChildEventCountingWatcher wobject2;
        rc=zoo_awget_children(zh,"/a",activeWatcher,
                &wobject2,asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        
        // this will process the response and activate the watcher
        while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) {
          millisleep(100);
        }
        CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc);

        // we are all set now; let's trigger the watches
        zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHILD_EVENT,"/a"));
        // make sure the watchers have been processed
        while((rc=zookeeper_process(zh,ZOOKEEPER_READ))==ZOK) {
          millisleep(100);
        }
        CPPUNIT_ASSERT_EQUAL((int)ZNOTHING,rc);

        CPPUNIT_ASSERT_EQUAL(0,wobject1.counter_);
        CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_);        
        CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_);
    }

#else
    // verify: the default watcher is called once for a session event
    void testDefaultSessionWatcher1(){
        Mock_gettimeofday timeMock;
        // zookeeper simulator
        ZookeeperServer zkServer;
        // detects when all watchers have been delivered
        WatcherDeliveryTracker deliveryTracker(ZOO_SESSION_EVENT,ZOO_CONNECTED_STATE);
        Mock_poll pollMock(&zkServer,ZookeeperServer::FD);
        // must call zookeeper_close() while all the mocks are in the scope!
        CloseFinally guard(&zh);
        
        ConnectionWatcher watcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &watcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // wait till watcher proccessing has completed (the connection 
        // established event)
        CPPUNIT_ASSERT(ensureCondition(
                deliveryTracker.isWatcherProcessingCompleted(),1000)<1000);
        
        // verify the watcher has been triggered
        CPPUNIT_ASSERT(ensureCondition(watcher.isConnectionEstablished(),1000)<1000);
        // triggered only once
        CPPUNIT_ASSERT_EQUAL(1,watcher.counter_);
    }

    // test case: connect to server, set a default watcher, disconnect from the server
    // verify: the default watcher is called once
    void testDefaultSessionWatcher2(){
        Mock_gettimeofday timeMock;
        // zookeeper simulator
        ZookeeperServer zkServer;
        Mock_poll pollMock(&zkServer,ZookeeperServer::FD);
        // must call zookeeper_close() while all the mocks are in the scope!
        CloseFinally guard(&zh);
        
        // detects when all watchers have been delivered
        WatcherDeliveryTracker deliveryTracker(ZOO_SESSION_EVENT,ZOO_CONNECTING_STATE);
        DisconnectWatcher watcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &watcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // make sure the client has connected
        CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000);
        // set a default watch
        AsyncCompletion ignored;
        // a successful server response will activate the watcher 
        zkServer.addOperationResponse(new ZooStatResponse);
        int rc=zoo_aexists(zh,"/x/y/z",1,asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);

        // now, initiate a disconnect
        zkServer.setConnectionLost();
        CPPUNIT_ASSERT(ensureCondition(
                deliveryTracker.isWatcherProcessingCompleted(),1000)<1000);
        
        // verify the watcher has been triggered
        CPPUNIT_ASSERT(watcher.disconnected_);
        // triggered only once
        CPPUNIT_ASSERT_EQUAL(1,watcher.counter_);
    }

    // testcase: connect to the server, set a watcher object on a node, 
    //           disconnect from the server
    // verify: the watcher object as well as the default watcher are called
    void testObjectSessionWatcher1(){
        Mock_gettimeofday timeMock;
        // zookeeper simulator
        ZookeeperServer zkServer;
        Mock_poll pollMock(&zkServer,ZookeeperServer::FD);
        // must call zookeeper_close() while all the mocks are in the scope!
        CloseFinally guard(&zh);
        
        // detects when all watchers have been delivered
        WatcherDeliveryTracker deliveryTracker(ZOO_SESSION_EVENT,ZOO_CONNECTING_STATE);
        DisconnectWatcher defWatcher;
        // use the tracker to find out when the watcher has been activated
        WatcherActivationTracker activationTracker;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &defWatcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // make sure the client has connected
        CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000);

        AsyncCompletion ignored;
        // this successful server response will activate the watcher 
        zkServer.addOperationResponse(new ZooStatResponse);
        CountingDataWatcher wobject;
        activationTracker.track(&wobject);
        // set a path-specific watcher
        int rc=zoo_awexists(zh,"/x/y/z",activeWatcher,&wobject,
                asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        // make sure the watcher gets activated before we continue
        CPPUNIT_ASSERT(ensureCondition(activationTracker.isWatcherActivated(),1000)<1000);
        
        // now, initiate a disconnect
        zkServer.setConnectionLost();
        // make sure all watchers have been processed
        CPPUNIT_ASSERT(ensureCondition(
                deliveryTracker.isWatcherProcessingCompleted(),1000)<1000);
        
        // verify the default watcher has been triggered
        CPPUNIT_ASSERT(defWatcher.disconnected_);
        // and triggered only once
        CPPUNIT_ASSERT_EQUAL(1,defWatcher.counter_);
        
        // the path-specific watcher has been triggered as well
        CPPUNIT_ASSERT(wobject.disconnected_);
        // only once!
        CPPUNIT_ASSERT_EQUAL(1,wobject.counter_);
    }

    // testcase: connect to the server, set a watcher object on a node, 
    //           set a def watcher on another node,disconnect from the server
    // verify: the watcher object as well as the default watcher are called
    void testObjectSessionWatcher2(){
        Mock_gettimeofday timeMock;
        // zookeeper simulator
        ZookeeperServer zkServer;
        Mock_poll pollMock(&zkServer,ZookeeperServer::FD);
        // must call zookeeper_close() while all the mocks are in the scope!
        CloseFinally guard(&zh);
        
        // detects when all watchers have been delivered
        WatcherDeliveryTracker deliveryTracker(ZOO_SESSION_EVENT,ZOO_CONNECTING_STATE);
        DisconnectWatcher defWatcher;
        // use the tracker to find out when the watcher has been activated
        WatcherActivationTracker activationTracker;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &defWatcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // make sure the client has connected
        CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000);

        // set a default watch
        AsyncCompletion ignored;
        // a successful server response will activate the watcher 
        zkServer.addOperationResponse(new ZooStatResponse);
        activationTracker.track(&defWatcher);
        int rc=zoo_aexists(zh,"/a/b/c",1,asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        // make sure the watcher gets activated before we continue
        CPPUNIT_ASSERT(ensureCondition(activationTracker.isWatcherActivated(),1000)<1000);

        // this successful server response will activate the watcher 
        zkServer.addOperationResponse(new ZooStatResponse);
        CountingDataWatcher wobject;
        activationTracker.track(&wobject);
        // set a path-specific watcher
        rc=zoo_awexists(zh,"/x/y/z",activeWatcher,&wobject,
                asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        // make sure the watcher gets activated before we continue
        CPPUNIT_ASSERT(ensureCondition(activationTracker.isWatcherActivated(),1000)<1000);
        
        // now, initiate a disconnect
        zkServer.setConnectionLost();
        // make sure all watchers have been processed
        CPPUNIT_ASSERT(ensureCondition(
                deliveryTracker.isWatcherProcessingCompleted(),1000)<1000);
        
        // verify the default watcher has been triggered
        CPPUNIT_ASSERT(defWatcher.disconnected_);
        // and triggered only once
        CPPUNIT_ASSERT_EQUAL(1,defWatcher.counter_);
        
        // the path-specific watcher has been triggered as well
        CPPUNIT_ASSERT(wobject.disconnected_);
        // only once!
        CPPUNIT_ASSERT_EQUAL(1,wobject.counter_);
    }

    // testcase: register 2 node watches for different paths, trigger the watches
    // verify: the data watchers are processed, the default watcher is not called
    void testNodeWatcher1(){
        Mock_gettimeofday timeMock;
        // zookeeper simulator
        ZookeeperServer zkServer;
        Mock_poll pollMock(&zkServer,ZookeeperServer::FD);
        // must call zookeeper_close() while all the mocks are in the scope!
        CloseFinally guard(&zh);
        
        // detects when all watchers have been delivered
        WatcherDeliveryTracker deliveryTracker(ZOO_CHANGED_EVENT,0,false);
        CountingDataWatcher defWatcher;
        // use the tracker to find out when the watcher has been activated
        WatcherActivationTracker activationTracker;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &defWatcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // make sure the client has connected
        CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000);

        // don't care about completions
        AsyncCompletion ignored;
        // set a one-shot watch
        // a successful server response will activate the watcher 
        zkServer.addOperationResponse(new ZooStatResponse);
        CountingDataWatcher wobject1;
        activationTracker.track(&wobject1);
        int rc=zoo_awexists(zh,"/a/b/c",activeWatcher,&wobject1,
                asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        // make sure the watcher gets activated before we continue
        CPPUNIT_ASSERT(ensureCondition(activationTracker.isWatcherActivated(),1000)<1000);

        // this successful server response will activate the watcher 
        zkServer.addOperationResponse(new ZooStatResponse);
        CountingDataWatcher wobject2;
        activationTracker.track(&wobject2);
        // set a path-specific watcher
        rc=zoo_awexists(zh,"/x/y/z",activeWatcher,&wobject2,
                asyncCompletion,&ignored);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        // make sure the watcher gets activated before we continue
        CPPUNIT_ASSERT(ensureCondition(activationTracker.isWatcherActivated(),1000)<1000);

        // we are all set now; let's trigger the watches
        zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHANGED_EVENT,"/a/b/c"));
        zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHANGED_EVENT,"/x/y/z"));
        // make sure all watchers have been processed
        CPPUNIT_ASSERT(ensureCondition(
                deliveryTracker.deliveryCounterEquals(2),1000)<1000);
        
        CPPUNIT_ASSERT_EQUAL(1,wobject1.counter_);
        CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_);        
        CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_);
    }

    // testcase: set up both a children and a data watchers on the node /a, then
    //           delete the node (that is, send a DELETE_EVENT)
    // verify: both watchers are triggered
    void testChildWatcher1(){
        Mock_gettimeofday timeMock;
        // zookeeper simulator
        ZookeeperServer zkServer;
        Mock_poll pollMock(&zkServer,ZookeeperServer::FD);
        // must call zookeeper_close() while all the mocks are in the scope!
        CloseFinally guard(&zh);
        
        // detects when all watchers have been delivered
        WatcherDeliveryTracker deliveryTracker(ZOO_DELETED_EVENT,0);
        DeletionCountingDataWatcher defWatcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &defWatcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // make sure the client has connected
        CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000);
        
        // a successful server response will activate the watcher 
        zkServer.addOperationResponse(new ZooStatResponse);
        DeletionCountingDataWatcher wobject1;
        Stat stat;
        // add a node watch
        int rc=zoo_wexists(zh,"/a",activeWatcher,&wobject1,&stat);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        
        typedef ZooGetChildrenResponse::StringVector ZooVector;
        zkServer.addOperationResponse(new ZooGetChildrenResponse(
                Util::CollectionBuilder<ZooVector>()("/a/1")("/a/2")
                ));
        DeletionCountingDataWatcher wobject2;
        String_vector children;
        rc=zoo_wget_children(zh,"/a",activeWatcher,&wobject2,&children);
        deallocate_String_vector(&children);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        
        // we are all set now; let's trigger the watches
        zkServer.addRecvResponse(new ZNodeEvent(ZOO_DELETED_EVENT,"/a"));
        // make sure the watchers have been processed
        CPPUNIT_ASSERT(ensureCondition(
                deliveryTracker.isWatcherProcessingCompleted(),1000)<1000);

        CPPUNIT_ASSERT_EQUAL(1,wobject1.counter_);
        CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_);        
        CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_);
    }
    
    // testcase: create both a child and data watch on the node /a, send a ZOO_CHILD_EVENT
    // verify: only the child watch triggered
    void testChildWatcher2(){
        Mock_gettimeofday timeMock;
        // zookeeper simulator
        ZookeeperServer zkServer;
        Mock_poll pollMock(&zkServer,ZookeeperServer::FD);
        // must call zookeeper_close() while all the mocks are in the scope!
        CloseFinally guard(&zh);
        
        // detects when all watchers have been delivered
        WatcherDeliveryTracker deliveryTracker(ZOO_CHILD_EVENT,0);
        ChildEventCountingWatcher defWatcher;
        zh=zookeeper_init("localhost:2121",activeWatcher,10000,TEST_CLIENT_ID,
                &defWatcher,0);
        CPPUNIT_ASSERT(zh!=0);
        // make sure the client has connected
        CPPUNIT_ASSERT(ensureCondition(ClientConnected(zh),1000)<1000);
        // a successful server response will activate the watcher 
        zkServer.addOperationResponse(new ZooStatResponse);
        ChildEventCountingWatcher wobject1;
        Stat stat;
        // add a node watch
        int rc=zoo_wexists(zh,"/a",activeWatcher,&wobject1,&stat);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);
        
        typedef ZooGetChildrenResponse::StringVector ZooVector;
        zkServer.addOperationResponse(new ZooGetChildrenResponse(
                Util::CollectionBuilder<ZooVector>()("/a/1")("/a/2")
                ));
        ChildEventCountingWatcher wobject2;
        String_vector children;
        rc=zoo_wget_children(zh,"/a",activeWatcher,&wobject2,&children);
        deallocate_String_vector(&children);
        CPPUNIT_ASSERT_EQUAL((int)ZOK,rc);

        // we are all set now; let's trigger the watches
        zkServer.addRecvResponse(new ZNodeEvent(ZOO_CHILD_EVENT,"/a"));
        // make sure the watchers have been processed
        CPPUNIT_ASSERT(ensureCondition(
                deliveryTracker.isWatcherProcessingCompleted(),1000)<1000);

        CPPUNIT_ASSERT_EQUAL(0,wobject1.counter_);
        CPPUNIT_ASSERT_EQUAL(1,wobject2.counter_);        
        CPPUNIT_ASSERT_EQUAL(0,defWatcher.counter_);
    }

#endif //THREADED
};

CPPUNIT_TEST_SUITE_REGISTRATION(Zookeeper_watchers);