blob: 161424dd47462cbe178572af148830efdc333adb [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
18import java.time.Duration;
19import java.util.Arrays;
20import java.util.UUID;
21import java.util.concurrent.CountDownLatch;
22import java.util.concurrent.Executor;
23import java.util.concurrent.Executors;
24import java.util.concurrent.TimeUnit;
25
26import io.atomix.Atomix;
27import io.atomix.AtomixClient;
28import io.atomix.resource.ResourceType;
29
30import org.junit.AfterClass;
31import org.junit.BeforeClass;
32import org.junit.Test;
33import org.onlab.util.Tools;
34import org.onosproject.store.service.Task;
35import org.onosproject.store.service.WorkQueueStats;
36
37import com.google.common.util.concurrent.Uninterruptibles;
38
39import static org.junit.Assert.assertEquals;
40import static org.junit.Assert.assertNull;
41import static org.junit.Assert.assertTrue;
42
43/**
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
154 @Test
155 public void testAutomaticTaskProcessing() throws Throwable {
156 String queueName = UUID.randomUUID().toString();
157 Atomix atomix1 = createAtomixClient();
158 AtomixWorkQueue queue1 = atomix1.getResource(queueName, AtomixWorkQueue.class).join();
159 Executor executor = Executors.newSingleThreadExecutor();
160
161 CountDownLatch latch1 = new CountDownLatch(1);
162 queue1.registerTaskProcessor(s -> latch1.countDown(), 2, executor);
163
164 AtomixClient atomix2 = createAtomixClient();
165 AtomixWorkQueue queue2 = atomix2.getResource(queueName, AtomixWorkQueue.class).join();
166 byte[] item1 = DEFAULT_PAYLOAD;
167 queue2.addOne(item1).join();
168
169 Uninterruptibles.awaitUninterruptibly(latch1, 500, TimeUnit.MILLISECONDS);
170 queue1.stopProcessing();
171
172 byte[] item2 = DEFAULT_PAYLOAD;
173 byte[] item3 = DEFAULT_PAYLOAD;
174
175 Tools.delay((int) DEFAULT_PROCESSING_TIME.toMillis());
176
177 queue2.addMultiple(Arrays.asList(item2, item3)).join();
178
179 WorkQueueStats stats = queue1.stats().join();
180 assertEquals(2, stats.totalPending());
181 assertEquals(0, stats.totalInProgress());
182 assertEquals(1, stats.totalCompleted());
183
184 CountDownLatch latch2 = new CountDownLatch(2);
185 queue1.registerTaskProcessor(s -> latch2.countDown(), 2, executor);
186
187 Uninterruptibles.awaitUninterruptibly(latch2, 500, TimeUnit.MILLISECONDS);
188 }
189}