aboutsummaryrefslogtreecommitdiffstats
path: root/models-sim/models-sim-dmaap/src/test/java/org/onap/policy/models/sim/dmaap/provider/ConsumerGroupDataTest.java
blob: 4513ffb825d8409686183bce3ef14822867b7d0d (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
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy Models
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.models.sim.dmaap.provider;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ConsumerGroupDataTest {
    private static final int WAIT_MS = 5000;
    private static final int MIN_WAIT_MS = WAIT_MS / 2;
    private static final String MY_TOPIC = "my-topic";
    private static final String MY_CONSUMER = "my-consumer";
    private static final String MSG1 = "hello";
    private static final String MSG2 = "there";
    private static final String MSG3 = "world";
    private static final int MAX_THREADS = 30;

    private MyData data;
    private MyReader thread;
    private List<MyReader> threads;

    /**
     * Sets up.
     */
    @Before
    public void setUp() {
        data = new MyData();
        thread = null;
        threads = new ArrayList<>(MAX_THREADS);
    }

    /**
     * Stops any running thread.
     */
    @After
    public void tearDown() {
        for (MyReader thr : threads) {
            thr.interrupt();
        }

        for (MyReader thr : threads) {
            thr.await();
        }
    }

    @Test
    public void testShouldRemove() throws InterruptedException {
        assertFalse(data.shouldRemove());
        assertTrue(data.shouldRemove());

        data = new MyData();

        // start a reader thread and wait for it to poll its queue
        startReader(0, 10);
        assertTrue(data.await());

        assertFalse(data.shouldRemove());
    }

    @Test
    public void testRead() {
        data.enqueue(MSG1, MSG2, MSG3, MSG1, MSG2, MSG3);

        // this reader only wants one
        startReader(1, 1);
        assertTrue(thread.await());
        assertEquals("[hello]", thread.result.toString());

        // this reader wants three
        startReader(3, 1);
        assertTrue(thread.await());
        assertEquals("[there, world, hello]", thread.result.toString());

        // this reader wants three, but will only get two
        startReader(3, 1);
        assertTrue(thread.await());
        assertEquals("[there, world]", thread.result.toString());
    }

    @Test
    public void testRead_Idle() throws InterruptedException {
        // force it to idle
        data.shouldRemove();
        data.shouldRemove();

        long tbeg = System.currentTimeMillis();
        assertSame(ConsumerGroupData.UNREADABLE_LIST, data.read(1, WAIT_MS));

        // should not have waited
        assertTrue(System.currentTimeMillis() < tbeg + MIN_WAIT_MS);
    }

    @Test
    public void testRead_NegativeCount() throws InterruptedException {
        data.enqueue(MSG1, MSG2);
        startReader(-1, 3);
        assertTrue(data.await());

        // wait time should be unaffected
        assertEquals(3L, data.waitMs2);

        assertTrue(thread.await());

        // should only return one message
        assertEquals("[hello]", thread.result.toString());
    }

    @Test
    public void testRead_NegativeWait() throws InterruptedException {
        data.enqueue(MSG1, MSG2, MSG3);
        startReader(2, -3);
        assertTrue(data.await());

        assertEquals(0L, data.waitMs2);

        assertTrue(thread.await());

        // should return two messages, as requested
        assertEquals("[hello, there]", thread.result.toString());
    }

    @Test
    public void testRead_NoMessages() throws InterruptedException {
        startReader(0, 0);
        assertTrue(data.await());

        assertTrue(thread.await());
        assertTrue(thread.result.isEmpty());
    }

    @Test
    public void testRead_MultiThreaded() {
        // queue up a bunch of messages
        final int expected = MAX_THREADS * 3;
        for (int x = 0; x < expected; ++x) {
            data.enqueue(MSG1);
        }

        for (int x = 0; x < MAX_THREADS; ++x) {
            startReader(4, 1);
        }

        int actual = 0;
        for (MyReader thr : threads) {
            thr.await();
            actual += thr.result.size();
        }

        assertEquals(expected, actual);
    }


    /**
     * Starts a reader thread.
     *
     * @param limit number of messages to read at one time
     * @param waitMs wait time, in milliseconds
     */
    private void startReader(int limit, long waitMs) {
        thread = new MyReader(limit, waitMs);

        thread.setDaemon(true);
        thread.start();

        threads.add(thread);
    }


    private class MyData extends ConsumerGroupData {

        /**
         * Decremented when {@link #getNextMessage(long)} is invoked.
         */
        private final CountDownLatch latch = new CountDownLatch(1);

        /**
         * Messages to be added to the queue when {@link #getNextMessage(long)} is
         * invoked.
         */
        private final List<String> messages = new ArrayList<>();

        /**
         * Value passed to {@link #getNextMessage(long)}.
         */
        private volatile long waitMs2 = -1;

        /**
         * Constructs the object.
         */
        public MyData() {
            super(MY_TOPIC, MY_CONSUMER);
        }

        /**
         * Arranges for messages to be injected into the queue the next time
         * {@link #getNextMessage(long)} is invoked.
         *
         * @param messages the messages to be injected
         */
        public void enqueue(String... messages) {
            this.messages.addAll(Arrays.asList(messages));
        }

        @Override
        protected String getNextMessage(long waitMs) throws InterruptedException {
            waitMs2 = waitMs;

            latch.countDown();

            synchronized (messages) {
                write(messages);
                messages.clear();
            }

            return super.getNextMessage(waitMs);
        }

        /**
         * Waits for {@link #getNextMessage(long)} to be invoked.
         *
         * @return {@code true} if {@link #getNextMessage(long)} was invoked,
         *         {@code false} if the timer expired first
         * @throws InterruptedException if the current thread is interrupted while waiting
         */
        public boolean await() throws InterruptedException {
            return latch.await(WAIT_MS, TimeUnit.MILLISECONDS);
        }
    }

    /**
     * Thread that will invoke the consumer group's read() method one time.
     */
    private class MyReader extends Thread {
        private final ConsumerGroupData group = data;
        private final int limit;
        private final long waitMs;

        /**
         * Result returned by the read() method.
         */
        private List<String> result = Collections.emptyList();

        public MyReader(int limit, long waitMs) {
            this.limit = limit;
            this.waitMs = waitMs;
        }

        @Override
        public void run() {
            try {
                result = group.read(limit, waitMs);

            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }

        /**
         * Waits for the thread to complete.
         *
         * @return {@code true} if the thread completed, {@code false} if the thread is
         *         still running
         */
        public boolean await() {
            try {
                this.join(WAIT_MS);

            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }

            return !this.isAlive();
        }
    }
}