aboutsummaryrefslogtreecommitdiffstats
path: root/feature-pooling-messages/src/test/java/org/onap/policy/drools/pooling/FeatureTest.java
blob: c507b68a9a22960638318bae7b9937c5e85185d0 (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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2020, 2024 Nordix 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.pooling;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.onap.policy.drools.pooling.PoolingProperties.PREFIX;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import java.util.Deque;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import java.util.TreeMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import lombok.Getter;
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.onap.policy.common.endpoints.event.comm.Topic;
import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
import org.onap.policy.common.endpoints.event.comm.TopicListener;
import org.onap.policy.common.endpoints.event.comm.TopicSink;
import org.onap.policy.common.endpoints.event.comm.TopicSource;
import org.onap.policy.drools.controller.DroolsController;
import org.onap.policy.drools.system.PolicyController;
import org.onap.policy.drools.system.PolicyEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * End-to-end tests of the pooling feature. Launches one or more "hosts", each one having
 * its own feature object. Uses real feature objects. However, the following are not:
 * <dl>
 * <dt>DMaaP sources and sinks</dt>
 * <dd>simulated using queues. There is one queue for the external topic, and one queue
 * for each host's internal topic. Messages published to the "admin" channel are simply
 * sent to all of the hosts' internal topic queues</dd>
 * <dt>PolicyEngine, PolicyController, DroolsController</dt>
 * <dd>mocked</dd>
 * </dl>
 *
 * <p>Invoke {@link #runSlow()}, before the test, to slow things down.
 */

class FeatureTest {
    private static final Logger logger = LoggerFactory.getLogger(FeatureTest.class);
    /**
     * Name of the topic used for inter-host communication.
     */
    private static final String INTERNAL_TOPIC = "my.internal.topic";
    /**
     * Name of the topic from which "external" events "arrive".
     */
    private static final String EXTERNAL_TOPIC = "my.external.topic";
    /**
     * Name of the controller.
     */
    private static final String CONTROLLER1 = "controller.one";
    private static long stdReactivateWaitMs = 200;
    private static long stdIdentificationMs = 60;
    private static long stdStartHeartbeatMs = 60;
    private static long stdActiveHeartbeatMs = 50;
    private static long stdInterHeartbeatMs = 5;
    private static long stdOfflinePubWaitMs = 2;
    private static long stdPollMs = 2;
    private static long stdInterPollMs = 2;
    private static long stdEventWaitSec = 10;
    /**
     * Used to decode events from the external topic.
     */
    private static final Gson mapper = new Gson();
    /**
     * Used to identify the current context.
     */
    private static final ThreadLocal<Context> currentContext = new ThreadLocal<Context>();
    /**
     * Context for the current test case.
     */
    private Context ctx;

    /**
     * Setup.
     */

    @BeforeEach
    public void setUp() {
        ctx = null;
    }

    /**
     * Tear down.
     */

    @AfterEach
    public void tearDown() {
        if (ctx != null) {
            ctx.destroy();
        }
    }

    @Test
    void test_SingleHost() throws Exception {
        run(70, 1);
    }

    @Test
    void test_TwoHosts() throws Exception {
        run(200, 2);
    }

    @Test
    void test_ThreeHosts() throws Exception {
        run(200, 3);
    }

    private void run(int nmessages, int nhosts) throws Exception {
        ctx = new Context(nmessages);
        for (int x = 0; x < nhosts; ++x) {
            ctx.addHost();
        }
        ctx.startHosts();
        for (int x = 0; x < nmessages; ++x) {
            ctx.offerExternal(makeMessage(x));
        }
        ctx.awaitEvents(stdEventWaitSec, TimeUnit.SECONDS);
        assertEquals(0, ctx.getDecodeErrors());
        assertEquals(0, ctx.getRemainingEvents());
        ctx.checkAllSawAMsg();
    }

    private String makeMessage(int reqnum) {
        return "{\"reqid\":\"req" + reqnum + "\", \"data\":\"hello " + reqnum + "\"}";
    }

    /**
     * Invoke this to slow the timers down.
     */

    protected static void runSlow() {
        stdReactivateWaitMs = 10000;
        stdIdentificationMs = 10000;
        stdStartHeartbeatMs = 15000;
        stdActiveHeartbeatMs = 12000;
        stdInterHeartbeatMs = 5000;
        stdOfflinePubWaitMs = 2;
        stdPollMs = 2;
        stdInterPollMs = 2000;
        stdEventWaitSec = 1000;
    }

    /**
     * Decodes an event.
     *
     * @param event event
     * @return the decoded event, or {@code null} if it cannot be decoded
     */

    private static Object decodeEvent(String event) {
        try {
            return mapper.fromJson(event, TreeMap.class);
        } catch (JsonParseException e) {
            logger.warn("cannot decode external event", e);
            return null;
        }
    }

    /**
     * Context used for a single test case.
     */

    private static class Context {
        /**
         * Hosts that have been added to this context.
         */
        private final Deque<Host> hosts = new LinkedList<>();
        /**
         * Maps a drools controller to its policy controller.
         */
        private final IdentityHashMap<DroolsController, PolicyController> drools2policy = new IdentityHashMap<>();
        /**
         * Maps a channel to its queue. Does <i>not</i> include the "admin" channel.
         */
        private final ConcurrentMap<String, BlockingQueue<String>> channel2queue = new ConcurrentHashMap<>(7);
        /**
         * Counts the number of decode errors.
         */
        private final AtomicInteger numDecodeErrors = new AtomicInteger(0);
        /**
         * Number of events we're still waiting to receive.
         */
        private final CountDownLatch eventCounter;

        /**
         * The current host. Set by {@link #withHost(Host, VoidFunction)} and used by
         * {@link #getCurrentHost()}.
         */
        @Getter
        private Host currentHost = null;

        /**
         * Constructor.
         *
         * @param events number of events to be processed
         */

        public Context(int events) {
            eventCounter = new CountDownLatch(events);
        }

        /**
         * Destroys the context, stopping any hosts that remain.
         */

        public void destroy() {
            stopHosts();
            hosts.clear();
        }

        /**
         * Creates and adds a new host to the context.
         *
         * @return the new Host
         */

        public Host addHost() {
            Host host = new Host(this);
            hosts.add(host);
            return host;
        }

        /**
         * Starts the hosts.
         */

        public void startHosts() {
            hosts.forEach(host -> host.start());
        }

        /**
         * Stops the hosts.
         */

        public void stopHosts() {
            hosts.forEach(host -> host.stop());
        }

        /**
         * Verifies that all hosts processed at least one message.
         */

        public void checkAllSawAMsg() {
            int msgs = 0;
            for (Host host : hosts) {
                assertTrue(host.messageSeen(), "msgs=" + msgs);
                ++msgs;
            }
        }

        /**
         * Sets {@link #currentHost} to the specified host, and then invokes the given
         * function. Resets {@link #currentHost} to {@code null} before returning.
         *
         * @param host host
         * @param func function to invoke
         */

        public void withHost(Host host, VoidFunction func) {
            currentHost = host;
            func.apply();
            currentHost = null;
        }

        /**
         * Offers an event to the external topic. As each host needs a copy, it is posted
         * to each Host's queue.
         *
         * @param event event
         */

        public void offerExternal(String event) {
            for (Host host : hosts) {
                host.getExternalTopic().offer(event);
            }
        }

        /**
         * Adds an internal channel to the set of channels.
         *
         * @param channel channel
         * @param queue   the channel's queue
         */

        public void addInternal(String channel, BlockingQueue<String> queue) {
            channel2queue.put(channel, queue);
        }

        /**
         * Offers a message to all internal channels.
         *
         * @param message message
         */

        public void offerInternal(String message) {
            channel2queue.values().forEach(queue -> queue.offer(message));
        }

        /**
         * Associates a controller with its drools controller.
         *
         * @param controller       controller
         * @param droolsController drools controller
         */

        public void addController(PolicyController controller, DroolsController droolsController) {
            drools2policy.put(droolsController, controller);
        }

        /**
         * Get controller.
         *
         * @param droolsController drools controller
         * @return the controller associated with a drools controller, or {@code null} if
         *         it has no associated controller
         */

        public PolicyController getController(DroolsController droolsController) {
            return drools2policy.get(droolsController);
        }

        /**
         * Get decode errors.
         *
         * @return the number of decode errors so far
         */

        public int getDecodeErrors() {
            return numDecodeErrors.get();
        }

        /**
         * Increments the count of decode errors.
         */

        public void bumpDecodeErrors() {
            numDecodeErrors.incrementAndGet();
        }

        /**
         * Get remaining events.
         *
         * @return the number of events that haven't been processed
         */

        public long getRemainingEvents() {
            return eventCounter.getCount();
        }

        /**
         * Adds an event to the counter.
         */

        public void addEvent() {
            eventCounter.countDown();
        }

        /**
         * Waits, for a period of time, for all events to be processed.
         *
         * @param time  time
         * @param units units
         * @return {@code true} if all events have been processed, {@code false} otherwise
         * @throws InterruptedException throws interrupted
         */

        public boolean awaitEvents(long time, TimeUnit units) throws InterruptedException {
            return eventCounter.await(time, units);
        }

    }

    /**
     * Simulates a single "host".
     */

    private static class Host {
        private final Context context;
        private final PoolingFeature feature;

        /**
         * {@code True} if this host has processed a message, {@code false} otherwise.
         */

        private final AtomicBoolean sawMsg = new AtomicBoolean(false);

        /**
         * This host's internal "DMaaP" topic.
         */

        private final BlockingQueue<String> msgQueue = new LinkedBlockingQueue<>();

        /**
         * Queue for the external "DMaaP" topic.
         */
        @Getter
        private final BlockingQueue<String> externalTopic = new LinkedBlockingQueue<String>();

        /**
         * Source that reads from the external topic and posts to the listener.
         */

        private TopicSource externalSource;

        // mock objects
        private final PolicyEngine engine = mock(PolicyEngine.class);
        private final ListenerController controller = mock(ListenerController.class);
        private final DroolsController drools = mock(DroolsController.class);

        /**
         * Constructor.
         *
         * @param context context
         */

        public Host(Context context) {
            this.context = context;
            when(controller.getName()).thenReturn(CONTROLLER1);
            when(controller.getDrools()).thenReturn(drools);
            // stop consuming events if the controller stops
            when(controller.stop()).thenAnswer(args -> {
                externalSource.unregister(controller);
                return true;
            });
            doAnswer(new MyExternalTopicListener(context, this)).when(controller).onTopicEvent(any(), any(), any());
            context.addController(controller, drools);
            // arrange to read from the external topic
            externalSource = new TopicSourceImpl(EXTERNAL_TOPIC, externalTopic);
            feature = new PoolingFeatureImpl(context);
        }

        /**
         * Get name.
         *
         * @return the host name
         */

        public String getName() {
            return feature.getHost();
        }

        /**
         * Starts threads for the host so that it begins consuming from both the external
         * "DMaaP" topic and its own internal "DMaaP" topic.
         */

        public void start() {
            context.withHost(this, () -> {
                feature.beforeStart(engine);
                feature.afterCreate(controller);
                // assign the queue for this host's internal topic
                context.addInternal(getName(), msgQueue);
                feature.beforeStart(controller);
                // start consuming events from the external topic
                externalSource.register(controller);
                feature.afterStart(controller);
            });
        }

        /**
         * Stops the host's threads.
         */

        public void stop() {
            feature.beforeStop(controller);
            externalSource.unregister(controller);
            feature.afterStop(controller);
        }

        /**
         * Offers an event to the feature, before the policy controller handles it.
         *
         * @param protocol protocol
         * @param topic2   topic
         * @param event    event
         * @return {@code true} if the event was handled, {@code false} otherwise
         */

        public boolean beforeOffer(CommInfrastructure protocol, String topic2, String event) {
            return feature.beforeOffer(controller, protocol, topic2, event);
        }

        /**
         * Offers an event to the feature, after the policy controller handles it.
         *
         * @param protocol protocol
         * @param topic    topic
         * @param event    event
         * @param success  success
         * @return {@code true} if the event was handled, {@code false} otherwise
         */

        public boolean afterOffer(CommInfrastructure protocol, String topic, String event, boolean success) {
            return feature.afterOffer(controller, protocol, topic, event, success);
        }

        /**
         * Offers an event to the feature, before the drools controller handles it.
         *
         * @param fact fact
         * @return {@code true} if the event was handled, {@code false} otherwise
         */

        public boolean beforeInsert(Object fact) {
            return feature.beforeInsert(drools, fact);
        }

        /**
         * Offers an event to the feature, after the drools controller handles it.
         *
         * @param fact          fact
         * @param successInsert {@code true} if it was successfully inserted by the drools
         *                      controller, {@code false} otherwise
         * @return {@code true} if the event was handled, {@code false} otherwise
         */

        public boolean afterInsert(Object fact, boolean successInsert) {
            return feature.afterInsert(drools, fact, successInsert);
        }

        /**
         * Indicates that a message was seen for this host.
         */

        public void sawMessage() {
            sawMsg.set(true);
        }

        /**
         * Message seen.
         *
         * @return {@code true} if a message was seen for this host, {@code false} otherwise
         */

        public boolean messageSeen() {
            return sawMsg.get();
        }

        /**
         * Get internal queue.
         *
         * @return the queue associated with this host's internal topic
         */

        public BlockingQueue<String> getInternalQueue() {
            return msgQueue;
        }
    }

    /**
     * Listener for the external topic. Simulates the actions taken by
     * <i>AggregatedPolicyController.onTopicEvent</i>.
     */

    private static class MyExternalTopicListener implements Answer<Void> {
        private final Context context;
        private final Host host;

        public MyExternalTopicListener(Context context, Host host) {
            this.context = context;
            this.host = host;
        }

        @Override
        public Void answer(InvocationOnMock args) throws Throwable {
            int index = 0;
            CommInfrastructure commType = args.getArgument(index++);
            String topic = args.getArgument(index++);
            String event = args.getArgument(index++);
            if (host.beforeOffer(commType, topic, event)) {
                return null;
            }
            boolean result;
            Object fact = decodeEvent(event);
            if (fact == null) {
                result = false;
                context.bumpDecodeErrors();
            } else {
                result = true;
                if (!host.beforeInsert(fact)) {
                    // feature did not handle it so we handle it here
                    host.afterInsert(fact, result);
                    host.sawMessage();
                    context.addEvent();
                }
            }
            host.afterOffer(commType, topic, event, result);
            return null;
        }
    }

    /**
     * Sink implementation that puts a message on the queue specified by the
     * <i>channel</i> embedded within the message. If it's the "admin" channel, then the
     * message is placed on all queues.
     */

    private static class TopicSinkImpl extends TopicImpl implements TopicSink {
        private final Context context;

        /**
         * Constructor.
         *
         * @param context context
         */

        public TopicSinkImpl(Context context) {
            this.context = context;
        }

        @Override
        public synchronized boolean send(String message) {
            if (!isAlive()) {
                return false;
            }
            try {
                context.offerInternal(message);
                return true;
            } catch (JsonParseException e) {
                logger.warn("could not decode message: {}", message);
                context.bumpDecodeErrors();
                return false;
            }
        }
    }

    /**
     * Source implementation that reads from a queue associated with a topic.
     */

    private static class TopicSourceImpl extends TopicImpl implements TopicSource {

        private final String topic;
        /**
         * Queue from which to retrieve messages.
         */
        private final BlockingQueue<String> queue;
        /**
         * Manages the current consumer thread. The "first" item is used as a trigger to
         * tell the thread to stop processing, while the "second" item is triggered <i>by
         * the thread</i> when it completes.
         */
        private AtomicReference<Pair<CountDownLatch, CountDownLatch>> pair = new AtomicReference<>(null);

        /**
         * Constructor.
         *
         * @param type  topic type
         * @param queue topic from which to read
         */

        public TopicSourceImpl(String type, BlockingQueue<String> queue) {
            this.topic = type;
            this.queue = queue;
        }

        @Override
        public String getTopic() {
            return topic;
        }

        @Override
        public boolean offer(String event) {
            throw new UnsupportedOperationException("offer topic source");
        }

        /**
         * Starts a thread that takes messages from the queue and gives them to the
         * listener. Stops the thread of any previously registered listener.
         */

        @Override
        public void register(TopicListener listener) {
            Pair<CountDownLatch, CountDownLatch> newPair = Pair.of(new CountDownLatch(1), new CountDownLatch(1));
            reregister(newPair);
            Thread thread = new Thread(() -> {
                try {
                    do {
                        processMessages(newPair.getLeft(), listener);
                    } while (!newPair.getLeft().await(stdInterPollMs, TimeUnit.MILLISECONDS));
                    logger.info("topic source thread completed");
                } catch (InterruptedException e) {
                    logger.warn("topic source thread aborted", e);
                    Thread.currentThread().interrupt();
                } catch (RuntimeException e) {
                    logger.warn("topic source thread aborted", e);
                }
                newPair.getRight().countDown();
            });
            thread.setDaemon(true);
            thread.start();
        }

        /**
         * Stops the thread of <i>any</i> currently registered listener.
         */

        @Override
        public void unregister(TopicListener listener) {
            reregister(null);
        }

        /**
         * Registers a new "pair" with this source, stopping the consumer associated with
         * any previous registration.
         *
         * @param newPair the new "pair", or {@code null} to unregister
         */

        private void reregister(Pair<CountDownLatch, CountDownLatch> newPair) {
            try {
                Pair<CountDownLatch, CountDownLatch> oldPair = pair.getAndSet(newPair);
                if (oldPair == null) {
                    if (newPair == null) {
                        // unregister was invoked twice in a row
                        logger.warn("re-unregister for topic source");
                    }
                    // no previous thread to stop
                    return;
                }
                // need to stop the previous thread
                // tell it to stop
                oldPair.getLeft().countDown();
                // wait for it to stop
                if (!oldPair.getRight().await(2, TimeUnit.SECONDS)) {
                    logger.warn("old topic registration is still running");
                }
            } catch (InterruptedException e) {
                logger.warn("old topic registration may still be running", e);
                Thread.currentThread().interrupt();
            }
            if (newPair != null) {
                // register was invoked twice in a row
                logger.warn("re-register for topic source");
            }
        }

        /**
         * Polls for messages from the topic and offers them to the listener.
         *
         * @param stopped  triggered if processing should stop
         * @param listener listener
         * @throws InterruptedException throws interrupted exception
         */

        private void processMessages(CountDownLatch stopped, TopicListener listener) throws InterruptedException {
            for (int x = 0; x < 5 && stopped.getCount() > 0; ++x) {
                String msg = queue.poll(stdPollMs, TimeUnit.MILLISECONDS);
                if (msg == null) {
                    return;
                }
                listener.onTopicEvent(CommInfrastructure.KAFKA, topic, msg);
            }
        }
    }

    /**
     * Topic implementation. Most methods just throw
     * {@link UnsupportedOperationException}.
     */

    private static class TopicImpl implements Topic {

        /**
         * Constructor.
         */

        public TopicImpl() {
            super();
        }

        @Override
        public String getTopic() {
            return INTERNAL_TOPIC;
        }

        @Override
        public String getEffectiveTopic() {
            return INTERNAL_TOPIC;
        }

        @Override
        public CommInfrastructure getTopicCommInfrastructure() {
            throw new UnsupportedOperationException("topic protocol");
        }

        @Override
        public List<String> getServers() {
            throw new UnsupportedOperationException("topic servers");
        }

        @Override
        public String[] getRecentEvents() {
            throw new UnsupportedOperationException("topic events");
        }

        @Override
        public void register(TopicListener topicListener) {
            throw new UnsupportedOperationException("register topic");
        }

        @Override
        public void unregister(TopicListener topicListener) {
            throw new UnsupportedOperationException("unregister topic");
        }

        @Override
        public synchronized boolean start() {
            return true;
        }

        @Override
        public synchronized boolean stop() {
            return true;
        }

        @Override
        public synchronized void shutdown() {
            // do nothing
        }

        @Override
        public synchronized boolean isAlive() {
            return true;
        }

        @Override
        public boolean lock() {
            throw new UnsupportedOperationException("lock topicink");
        }

        @Override
        public boolean unlock() {
            throw new UnsupportedOperationException("unlock topic");
        }

        @Override
        public boolean isLocked() {
            throw new UnsupportedOperationException("topic isLocked");
        }
    }

    /**
     * Feature with overrides.
     */

    private static class PoolingFeatureImpl extends PoolingFeature {
        private final Context context;

        /**
         * Constructor.
         *
         * @param context context
         */

        public PoolingFeatureImpl(Context context) {
            this.context = context;
            /*
             * Note: do NOT extract anything from "context" at this point, because it
             * hasn't been fully initialized yet
             */
        }

        @Override
        public Properties getProperties(String featName) {
            Properties props = new Properties();
            props.setProperty(PoolingProperties.PROP_EXTRACTOR_PREFIX + ".java.util.Map", "${reqid}");
            props.setProperty(specialize(PoolingProperties.FEATURE_ENABLED, CONTROLLER1), "true");
            props.setProperty(specialize(PoolingProperties.POOLING_TOPIC, CONTROLLER1), INTERNAL_TOPIC);
            props.setProperty(specialize(PoolingProperties.OFFLINE_LIMIT, CONTROLLER1), "10000");
            props.setProperty(specialize(PoolingProperties.OFFLINE_AGE_MS, CONTROLLER1), "1000000");
            props.setProperty(specialize(PoolingProperties.OFFLINE_PUB_WAIT_MS, CONTROLLER1), "" + stdOfflinePubWaitMs);
            props.setProperty(specialize(PoolingProperties.START_HEARTBEAT_MS, CONTROLLER1), "" + stdStartHeartbeatMs);
            props.setProperty(specialize(PoolingProperties.REACTIVATE_MS, CONTROLLER1), "" + stdReactivateWaitMs);
            props.setProperty(specialize(PoolingProperties.IDENTIFICATION_MS, CONTROLLER1), "" + stdIdentificationMs);
            props.setProperty(specialize(PoolingProperties.ACTIVE_HEARTBEAT_MS, CONTROLLER1),
                "" + stdActiveHeartbeatMs);
            props.setProperty(specialize(PoolingProperties.INTER_HEARTBEAT_MS, CONTROLLER1), "" + stdInterHeartbeatMs);
            return props;
        }

        @Override
        public PolicyController getController(DroolsController droolsController) {
            return context.getController(droolsController);
        }

        /**
         * Embeds a specializer within a property name, after the prefix.
         *
         * @param propnm property name into which it should be embedded
         * @param spec   specializer to be embedded
         * @return the property name, with the specializer embedded within it
         */

        private String specialize(String propnm, String spec) {
            String suffix = propnm.substring(PREFIX.length());
            return PREFIX + spec + "." + suffix;
        }

        @Override
        protected PoolingManagerImpl makeManager(String host, PolicyController controller, PoolingProperties props,
                                                 CountDownLatch activeLatch) {
            currentContext.set(context);
            return new PoolingManagerTest(host, controller, props, activeLatch);
        }
    }

    /**
     * Pooling Manager with overrides.
     */

    private static class PoolingManagerTest extends PoolingManagerImpl {

        /**
         * Constructor.
         *
         * @param host        the host
         * @param controller  the controller
         * @param props       the properties
         * @param activeLatch the latch
         */

        public PoolingManagerTest(String host, PolicyController controller, PoolingProperties props,
                                  CountDownLatch activeLatch) {
            super(host, controller, props, activeLatch);
        }

        @Override
        protected TopicMessageManager makeTopicMessagesManager(String topic) throws PoolingFeatureException {
            return new TopicMessageManagerImpl(topic);
        }

        @Override
        protected boolean canDecodeEvent(DroolsController drools, String topic) {
            return true;
        }

        @Override
        protected Object decodeEventWrapper(DroolsController drools, String topic, String event) {
            return decodeEvent(event);
        }
    }

    /**
     * DMaaP Manager with overrides.
     */

    private static class TopicMessageManagerImpl extends TopicMessageManager {

        /**
         * Constructor.
         *
         * @param topic the topic
         * @throws PoolingFeatureException if an error occurs
         */

        public TopicMessageManagerImpl(String topic) throws PoolingFeatureException {
            super(topic);
        }

        @Override
        protected List<TopicSource> getTopicSources() {
            return List.of(
                new TopicSourceImpl(INTERNAL_TOPIC, currentContext.get().getCurrentHost().getInternalQueue()));
        }

        @Override
        protected List<TopicSink> getTopicSinks() {
            return List.of(new TopicSinkImpl(currentContext.get()));
        }
    }

    /**
     * Controller that also implements the {@link TopicListener} interface.
     */

    private static interface ListenerController extends PolicyController, TopicListener {
    }

    /**
     * Simple function that takes no arguments and returns nothing.
     */

    @FunctionalInterface
    private static interface VoidFunction {
        void apply();
    }
}