Madan Jampani | 35708a9 | 2016-07-06 10:48:19 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 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 | |
| 26 | import io.atomix.Atomix; |
| 27 | import io.atomix.AtomixClient; |
| 28 | import io.atomix.resource.ResourceType; |
| 29 | |
| 30 | import org.junit.AfterClass; |
| 31 | import org.junit.BeforeClass; |
| 32 | import org.junit.Test; |
| 33 | import org.onlab.util.Tools; |
| 34 | import org.onosproject.store.service.Task; |
| 35 | import org.onosproject.store.service.WorkQueueStats; |
| 36 | |
| 37 | import com.google.common.util.concurrent.Uninterruptibles; |
| 38 | |
| 39 | import static org.junit.Assert.assertEquals; |
| 40 | import static org.junit.Assert.assertNull; |
| 41 | import static org.junit.Assert.assertTrue; |
| 42 | |
| 43 | /** |
| 44 | * Unit tests for {@link AtomixWorkQueue}. |
| 45 | */ |
| 46 | public 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 | } |