blob: 5335152c3f23eff9c324be80f603cbf8be8305ec [file] [log] [blame]
Madan Jampani35708a92016-07-06 10:48:19 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.store.primitives.resources.impl;
17
Madan Jampani819d61d2016-07-25 20:29:43 -070018import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertNull;
20import static org.junit.Assert.assertTrue;
21import io.atomix.Atomix;
22import io.atomix.AtomixClient;
23import io.atomix.resource.ResourceType;
24
Madan Jampani35708a92016-07-06 10:48:19 -070025import java.time.Duration;
26import java.util.Arrays;
27import java.util.UUID;
28import java.util.concurrent.CountDownLatch;
29import java.util.concurrent.Executor;
30import java.util.concurrent.Executors;
31import java.util.concurrent.TimeUnit;
32
Madan Jampani35708a92016-07-06 10:48:19 -070033import org.junit.AfterClass;
34import org.junit.BeforeClass;
Yuta HIGUCHIed9adcc2017-01-26 10:09:24 -080035import org.junit.Ignore;
Madan Jampani35708a92016-07-06 10:48:19 -070036import org.junit.Test;
37import org.onlab.util.Tools;
38import org.onosproject.store.service.Task;
39import org.onosproject.store.service.WorkQueueStats;
40
41import com.google.common.util.concurrent.Uninterruptibles;
42
Madan Jampani35708a92016-07-06 10:48:19 -070043/**
44 * Unit tests for {@link AtomixWorkQueue}.
45 */
46public class AtomixWorkQueueTest extends AtomixTestBase {
47
48 private static final Duration DEFAULT_PROCESSING_TIME = Duration.ofMillis(100);
49 private static final byte[] DEFAULT_PAYLOAD = "hello world".getBytes();
50
51 @BeforeClass
52 public static void preTestSetup() throws Throwable {
53 createCopycatServers(1);
54 }
55
56 @AfterClass
57 public static void postTestCleanup() throws Exception {
58 clearTests();
59 }
60
61 @Override
62 protected ResourceType resourceType() {
63 return new ResourceType(AtomixWorkQueue.class);
64 }
65
66 @Test
67 public void testAdd() throws Throwable {
68 String queueName = UUID.randomUUID().toString();
69 Atomix atomix1 = createAtomixClient();
70 AtomixWorkQueue queue1 = atomix1.getResource(queueName, AtomixWorkQueue.class).join();
71 byte[] item = DEFAULT_PAYLOAD;
72 queue1.addOne(item).join();
73
74 Atomix atomix2 = createAtomixClient();
75 AtomixWorkQueue queue2 = atomix2.getResource(queueName, AtomixWorkQueue.class).join();
76 byte[] task2 = DEFAULT_PAYLOAD;
77 queue2.addOne(task2).join();
78
79 WorkQueueStats stats = queue1.stats().join();
80 assertEquals(stats.totalPending(), 2);
81 assertEquals(stats.totalInProgress(), 0);
82 assertEquals(stats.totalCompleted(), 0);
83 }
84
85 @Test
86 public void testAddMultiple() throws Throwable {
87 String queueName = UUID.randomUUID().toString();
88 Atomix atomix1 = createAtomixClient();
89 AtomixWorkQueue queue1 = atomix1.getResource(queueName, AtomixWorkQueue.class).join();
90 byte[] item1 = DEFAULT_PAYLOAD;
91 byte[] item2 = DEFAULT_PAYLOAD;
92 queue1.addMultiple(Arrays.asList(item1, item2)).join();
93
94 WorkQueueStats stats = queue1.stats().join();
95 assertEquals(stats.totalPending(), 2);
96 assertEquals(stats.totalInProgress(), 0);
97 assertEquals(stats.totalCompleted(), 0);
98 }
99
100 @Test
101 public void testTakeAndComplete() throws Throwable {
102 String queueName = UUID.randomUUID().toString();
103 Atomix atomix1 = createAtomixClient();
104 AtomixWorkQueue queue1 = atomix1.getResource(queueName, AtomixWorkQueue.class).join();
105 byte[] item1 = DEFAULT_PAYLOAD;
106 queue1.addOne(item1).join();
107
108 Atomix atomix2 = createAtomixClient();
109 AtomixWorkQueue queue2 = atomix2.getResource(queueName, AtomixWorkQueue.class).join();
110 Task<byte[]> removedTask = queue2.take().join();
111
112 WorkQueueStats stats = queue2.stats().join();
113 assertEquals(stats.totalPending(), 0);
114 assertEquals(stats.totalInProgress(), 1);
115 assertEquals(stats.totalCompleted(), 0);
116
117 assertTrue(Arrays.equals(removedTask.payload(), item1));
118 queue2.complete(Arrays.asList(removedTask.taskId())).join();
119
120 stats = queue1.stats().join();
121 assertEquals(stats.totalPending(), 0);
122 assertEquals(stats.totalInProgress(), 0);
123 assertEquals(stats.totalCompleted(), 1);
124
125 // Another take should return null
126 assertNull(queue2.take().join());
127 }
128
129 @Test
130 public void testUnexpectedClientClose() throws Throwable {
131 String queueName = UUID.randomUUID().toString();
132 Atomix atomix1 = createAtomixClient();
133 AtomixWorkQueue queue1 = atomix1.getResource(queueName, AtomixWorkQueue.class).join();
134 byte[] item1 = DEFAULT_PAYLOAD;
135 queue1.addOne(item1).join();
136
137 AtomixClient atomix2 = createAtomixClient();
138 AtomixWorkQueue queue2 = atomix2.getResource(queueName, AtomixWorkQueue.class).join();
139 queue2.take().join();
140
141 WorkQueueStats stats = queue1.stats().join();
142 assertEquals(0, stats.totalPending());
143 assertEquals(1, stats.totalInProgress());
144 assertEquals(0, stats.totalCompleted());
145
146 atomix2.close().join();
147
148 stats = queue1.stats().join();
149 assertEquals(1, stats.totalPending());
150 assertEquals(0, stats.totalInProgress());
151 assertEquals(0, stats.totalCompleted());
152 }
153
Yuta HIGUCHIed9adcc2017-01-26 10:09:24 -0800154 @Ignore("Disable due to ONOS-5906")
Madan Jampani35708a92016-07-06 10:48:19 -0700155 @Test
156 public void testAutomaticTaskProcessing() throws Throwable {
157 String queueName = UUID.randomUUID().toString();
158 Atomix atomix1 = createAtomixClient();
159 AtomixWorkQueue queue1 = atomix1.getResource(queueName, AtomixWorkQueue.class).join();
160 Executor executor = Executors.newSingleThreadExecutor();
161
162 CountDownLatch latch1 = new CountDownLatch(1);
163 queue1.registerTaskProcessor(s -> latch1.countDown(), 2, executor);
164
165 AtomixClient atomix2 = createAtomixClient();
166 AtomixWorkQueue queue2 = atomix2.getResource(queueName, AtomixWorkQueue.class).join();
167 byte[] item1 = DEFAULT_PAYLOAD;
168 queue2.addOne(item1).join();
169
170 Uninterruptibles.awaitUninterruptibly(latch1, 500, TimeUnit.MILLISECONDS);
171 queue1.stopProcessing();
172
173 byte[] item2 = DEFAULT_PAYLOAD;
174 byte[] item3 = DEFAULT_PAYLOAD;
175
176 Tools.delay((int) DEFAULT_PROCESSING_TIME.toMillis());
177
178 queue2.addMultiple(Arrays.asList(item2, item3)).join();
179
180 WorkQueueStats stats = queue1.stats().join();
181 assertEquals(2, stats.totalPending());
182 assertEquals(0, stats.totalInProgress());
183 assertEquals(1, stats.totalCompleted());
184
185 CountDownLatch latch2 = new CountDownLatch(2);
186 queue1.registerTaskProcessor(s -> latch2.countDown(), 2, executor);
187
188 Uninterruptibles.awaitUninterruptibly(latch2, 500, TimeUnit.MILLISECONDS);
189 }
Madan Jampani819d61d2016-07-25 20:29:43 -0700190
191 @Test
192 public void testDestroy() {
193 String queueName = UUID.randomUUID().toString();
194 Atomix atomix1 = createAtomixClient();
195 AtomixWorkQueue queue1 = atomix1.getResource(queueName, AtomixWorkQueue.class).join();
196 byte[] item = DEFAULT_PAYLOAD;
197 queue1.addOne(item).join();
198
199 Atomix atomix2 = createAtomixClient();
200 AtomixWorkQueue queue2 = atomix2.getResource(queueName, AtomixWorkQueue.class).join();
201 byte[] task2 = DEFAULT_PAYLOAD;
202 queue2.addOne(task2).join();
203
204 WorkQueueStats stats = queue1.stats().join();
205 assertEquals(stats.totalPending(), 2);
206 assertEquals(stats.totalInProgress(), 0);
207 assertEquals(stats.totalCompleted(), 0);
208
209 queue2.destroy().join();
210
211 stats = queue1.stats().join();
212 assertEquals(stats.totalPending(), 0);
213 assertEquals(stats.totalInProgress(), 0);
214 assertEquals(stats.totalCompleted(), 0);
215 }
216
217 @Test
218 public void testCompleteAttemptWithIncorrectSession() {
219 String queueName = UUID.randomUUID().toString();
220 Atomix atomix1 = createAtomixClient();
221 AtomixWorkQueue queue1 = atomix1.getResource(queueName, AtomixWorkQueue.class).join();
222 byte[] item = DEFAULT_PAYLOAD;
223 queue1.addOne(item).join();
224
225 Task<byte[]> task = queue1.take().join();
226 String taskId = task.taskId();
227
228 // Create another client and get a handle to the same queue.
229 Atomix atomix2 = createAtomixClient();
230 AtomixWorkQueue queue2 = atomix2.getResource(queueName, AtomixWorkQueue.class).join();
231
232 // Attempt completing the task with new client and verify task is not completed
233 queue2.complete(taskId).join();
234
235 WorkQueueStats stats = queue1.stats().join();
236 assertEquals(stats.totalPending(), 0);
237 assertEquals(stats.totalInProgress(), 1);
238 assertEquals(stats.totalCompleted(), 0);
239 }
Madan Jampani35708a92016-07-06 10:48:19 -0700240}