Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.store.primitives.resources.impl; |
| 17 | |
| 18 | import java.time.Duration; |
| 19 | import java.util.Arrays; |
| 20 | import java.util.UUID; |
| 21 | import java.util.concurrent.CountDownLatch; |
| 22 | import java.util.concurrent.Executor; |
| 23 | import java.util.concurrent.Executors; |
| 24 | import java.util.concurrent.TimeUnit; |
| 25 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 26 | import com.google.common.util.concurrent.Uninterruptibles; |
| 27 | import io.atomix.protocols.raft.proxy.RaftProxy; |
| 28 | import io.atomix.protocols.raft.service.RaftService; |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 29 | import org.junit.Test; |
| 30 | import org.onlab.util.Tools; |
| 31 | import org.onosproject.store.service.Task; |
| 32 | import org.onosproject.store.service.WorkQueueStats; |
| 33 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 34 | import static org.junit.Assert.assertEquals; |
| 35 | import static org.junit.Assert.assertNull; |
| 36 | import static org.junit.Assert.assertTrue; |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 37 | |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Unit tests for {@link AtomixWorkQueue}. |
| 40 | */ |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 41 | public class AtomixWorkQueueTest extends AtomixTestBase<AtomixWorkQueue> { |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 42 | private static final Duration DEFAULT_PROCESSING_TIME = Duration.ofMillis(100); |
| 43 | private static final byte[] DEFAULT_PAYLOAD = "hello world".getBytes(); |
| 44 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 45 | @Override |
| 46 | protected RaftService createService() { |
| 47 | return new AtomixWorkQueueService(); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | @Override |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 51 | protected AtomixWorkQueue createPrimitive(RaftProxy proxy) { |
| 52 | return new AtomixWorkQueue(proxy); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Test |
| 56 | public void testAdd() throws Throwable { |
| 57 | String queueName = UUID.randomUUID().toString(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 58 | AtomixWorkQueue queue1 = newPrimitive(queueName); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 59 | byte[] item = DEFAULT_PAYLOAD; |
| 60 | queue1.addOne(item).join(); |
| 61 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 62 | AtomixWorkQueue queue2 = newPrimitive(queueName); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 63 | byte[] task2 = DEFAULT_PAYLOAD; |
| 64 | queue2.addOne(task2).join(); |
| 65 | |
| 66 | WorkQueueStats stats = queue1.stats().join(); |
| 67 | assertEquals(stats.totalPending(), 2); |
| 68 | assertEquals(stats.totalInProgress(), 0); |
| 69 | assertEquals(stats.totalCompleted(), 0); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void testAddMultiple() throws Throwable { |
| 74 | String queueName = UUID.randomUUID().toString(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 75 | AtomixWorkQueue queue1 = newPrimitive(queueName); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 76 | byte[] item1 = DEFAULT_PAYLOAD; |
| 77 | byte[] item2 = DEFAULT_PAYLOAD; |
| 78 | queue1.addMultiple(Arrays.asList(item1, item2)).join(); |
| 79 | |
| 80 | WorkQueueStats stats = queue1.stats().join(); |
| 81 | assertEquals(stats.totalPending(), 2); |
| 82 | assertEquals(stats.totalInProgress(), 0); |
| 83 | assertEquals(stats.totalCompleted(), 0); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void testTakeAndComplete() throws Throwable { |
| 88 | String queueName = UUID.randomUUID().toString(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 89 | AtomixWorkQueue queue1 = newPrimitive(queueName); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 90 | byte[] item1 = DEFAULT_PAYLOAD; |
| 91 | queue1.addOne(item1).join(); |
| 92 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 93 | AtomixWorkQueue queue2 = newPrimitive(queueName); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 94 | Task<byte[]> removedTask = queue2.take().join(); |
| 95 | |
| 96 | WorkQueueStats stats = queue2.stats().join(); |
| 97 | assertEquals(stats.totalPending(), 0); |
| 98 | assertEquals(stats.totalInProgress(), 1); |
| 99 | assertEquals(stats.totalCompleted(), 0); |
| 100 | |
| 101 | assertTrue(Arrays.equals(removedTask.payload(), item1)); |
| 102 | queue2.complete(Arrays.asList(removedTask.taskId())).join(); |
| 103 | |
| 104 | stats = queue1.stats().join(); |
| 105 | assertEquals(stats.totalPending(), 0); |
| 106 | assertEquals(stats.totalInProgress(), 0); |
| 107 | assertEquals(stats.totalCompleted(), 1); |
| 108 | |
| 109 | // Another take should return null |
| 110 | assertNull(queue2.take().join()); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testUnexpectedClientClose() throws Throwable { |
| 115 | String queueName = UUID.randomUUID().toString(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 116 | AtomixWorkQueue queue1 = newPrimitive(queueName); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 117 | byte[] item1 = DEFAULT_PAYLOAD; |
| 118 | queue1.addOne(item1).join(); |
| 119 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 120 | AtomixWorkQueue queue2 = newPrimitive(queueName); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 121 | queue2.take().join(); |
| 122 | |
| 123 | WorkQueueStats stats = queue1.stats().join(); |
| 124 | assertEquals(0, stats.totalPending()); |
| 125 | assertEquals(1, stats.totalInProgress()); |
| 126 | assertEquals(0, stats.totalCompleted()); |
| 127 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 128 | queue2.proxy.close().join(); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 129 | |
| 130 | stats = queue1.stats().join(); |
| 131 | assertEquals(1, stats.totalPending()); |
| 132 | assertEquals(0, stats.totalInProgress()); |
| 133 | assertEquals(0, stats.totalCompleted()); |
| 134 | } |
| 135 | |
| 136 | @Test |
| 137 | public void testAutomaticTaskProcessing() throws Throwable { |
| 138 | String queueName = UUID.randomUUID().toString(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 139 | AtomixWorkQueue queue1 = newPrimitive(queueName); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 140 | Executor executor = Executors.newSingleThreadExecutor(); |
| 141 | |
| 142 | CountDownLatch latch1 = new CountDownLatch(1); |
| 143 | queue1.registerTaskProcessor(s -> latch1.countDown(), 2, executor); |
| 144 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 145 | AtomixWorkQueue queue2 = newPrimitive(queueName); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 146 | byte[] item1 = DEFAULT_PAYLOAD; |
| 147 | queue2.addOne(item1).join(); |
| 148 | |
Aaron Kruglikov | 4fc7c26 | 2017-01-26 11:55:21 -0800 | [diff] [blame] | 149 | assertTrue(Uninterruptibles.awaitUninterruptibly(latch1, 5000, TimeUnit.MILLISECONDS)); |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 150 | queue1.stopProcessing(); |
| 151 | |
| 152 | byte[] item2 = DEFAULT_PAYLOAD; |
| 153 | byte[] item3 = DEFAULT_PAYLOAD; |
| 154 | |
| 155 | Tools.delay((int) DEFAULT_PROCESSING_TIME.toMillis()); |
| 156 | |
| 157 | queue2.addMultiple(Arrays.asList(item2, item3)).join(); |
| 158 | |
| 159 | WorkQueueStats stats = queue1.stats().join(); |
| 160 | assertEquals(2, stats.totalPending()); |
| 161 | assertEquals(0, stats.totalInProgress()); |
| 162 | assertEquals(1, stats.totalCompleted()); |
| 163 | |
| 164 | CountDownLatch latch2 = new CountDownLatch(2); |
| 165 | queue1.registerTaskProcessor(s -> latch2.countDown(), 2, executor); |
| 166 | |
| 167 | Uninterruptibles.awaitUninterruptibly(latch2, 500, TimeUnit.MILLISECONDS); |
| 168 | } |
Madan Jampani | 819d61d | 2016-07-25 20:29:43 -0700 | [diff] [blame] | 169 | |
| 170 | @Test |
| 171 | public void testDestroy() { |
| 172 | String queueName = UUID.randomUUID().toString(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 173 | AtomixWorkQueue queue1 = newPrimitive(queueName); |
Madan Jampani | 819d61d | 2016-07-25 20:29:43 -0700 | [diff] [blame] | 174 | byte[] item = DEFAULT_PAYLOAD; |
| 175 | queue1.addOne(item).join(); |
| 176 | |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 177 | AtomixWorkQueue queue2 = newPrimitive(queueName); |
Madan Jampani | 819d61d | 2016-07-25 20:29:43 -0700 | [diff] [blame] | 178 | byte[] task2 = DEFAULT_PAYLOAD; |
| 179 | queue2.addOne(task2).join(); |
| 180 | |
| 181 | WorkQueueStats stats = queue1.stats().join(); |
| 182 | assertEquals(stats.totalPending(), 2); |
| 183 | assertEquals(stats.totalInProgress(), 0); |
| 184 | assertEquals(stats.totalCompleted(), 0); |
| 185 | |
| 186 | queue2.destroy().join(); |
| 187 | |
| 188 | stats = queue1.stats().join(); |
| 189 | assertEquals(stats.totalPending(), 0); |
| 190 | assertEquals(stats.totalInProgress(), 0); |
| 191 | assertEquals(stats.totalCompleted(), 0); |
| 192 | } |
| 193 | |
| 194 | @Test |
| 195 | public void testCompleteAttemptWithIncorrectSession() { |
| 196 | String queueName = UUID.randomUUID().toString(); |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 197 | AtomixWorkQueue queue1 = newPrimitive(queueName); |
Madan Jampani | 819d61d | 2016-07-25 20:29:43 -0700 | [diff] [blame] | 198 | byte[] item = DEFAULT_PAYLOAD; |
| 199 | queue1.addOne(item).join(); |
| 200 | |
| 201 | Task<byte[]> task = queue1.take().join(); |
| 202 | String taskId = task.taskId(); |
| 203 | |
| 204 | // Create another client and get a handle to the same queue. |
Jordan Halterman | 2bf177c | 2017-06-29 01:49:08 -0700 | [diff] [blame] | 205 | AtomixWorkQueue queue2 = newPrimitive(queueName); |
Madan Jampani | 819d61d | 2016-07-25 20:29:43 -0700 | [diff] [blame] | 206 | |
| 207 | // Attempt completing the task with new client and verify task is not completed |
| 208 | queue2.complete(taskId).join(); |
| 209 | |
| 210 | WorkQueueStats stats = queue1.stats().join(); |
| 211 | assertEquals(stats.totalPending(), 0); |
| 212 | assertEquals(stats.totalInProgress(), 1); |
| 213 | assertEquals(stats.totalCompleted(), 0); |
| 214 | } |
Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame] | 215 | } |