|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.calc.execution.schedule.queue; |
| 21 | + |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +import java.util.ArrayDeque; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.Queue; |
| 29 | + |
| 30 | +public class IndexedBlockingQueueTest { |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testRejectNonPositiveCapacity() { |
| 34 | + Assert.assertThrows(IllegalArgumentException.class, () -> new SimpleQueue(0)); |
| 35 | + Assert.assertThrows(IllegalArgumentException.class, () -> new ReserveQueue(-1)); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testRejectDuplicateIdPush() throws InterruptedException { |
| 40 | + SimpleQueue queue = new SimpleQueue(2); |
| 41 | + Element first = new Element(1); |
| 42 | + Element duplicate = new Element(1); |
| 43 | + |
| 44 | + queue.push(first); |
| 45 | + Assert.assertThrows(IllegalStateException.class, () -> queue.push(duplicate)); |
| 46 | + Assert.assertEquals(1, queue.size()); |
| 47 | + Assert.assertSame(first, queue.poll()); |
| 48 | + Assert.assertTrue(queue.isEmpty()); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testRejectRepushWithoutReservedSpace() { |
| 53 | + ReserveQueue queue = new ReserveQueue(1); |
| 54 | + |
| 55 | + Assert.assertThrows(IllegalStateException.class, () -> queue.repush(new Element(1))); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testRejectDecreaseWithoutReservedSpace() { |
| 60 | + ReserveQueue queue = new ReserveQueue(1); |
| 61 | + |
| 62 | + Assert.assertThrows(IllegalStateException.class, queue::decreaseReservedSize); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testReservedSpaceCanOnlyBeReleasedOnce() throws InterruptedException { |
| 67 | + ReserveQueue queue = new ReserveQueue(1); |
| 68 | + Element element = new Element(1); |
| 69 | + queue.push(element); |
| 70 | + |
| 71 | + Assert.assertSame(element, queue.poll()); |
| 72 | + queue.decreaseReservedSize(); |
| 73 | + |
| 74 | + Assert.assertThrows(IllegalStateException.class, queue::decreaseReservedSize); |
| 75 | + } |
| 76 | + |
| 77 | + private static class SimpleQueue extends IndexedBlockingQueue<Element> { |
| 78 | + private final Queue<Element> elements = new ArrayDeque<>(); |
| 79 | + private final Map<ID, Element> keyedElements = new HashMap<>(); |
| 80 | + |
| 81 | + private SimpleQueue(int capacity) { |
| 82 | + super(capacity, new Element(0)); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected Element remove(Element element) { |
| 87 | + Element removed = keyedElements.remove(element.getDriverTaskId()); |
| 88 | + if (removed != null) { |
| 89 | + elements.remove(removed); |
| 90 | + } |
| 91 | + return removed; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected Element get(Element element) { |
| 96 | + return keyedElements.get(element.getDriverTaskId()); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean isEmpty() { |
| 101 | + return elements.isEmpty(); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected Element pollFirst() { |
| 106 | + Element first = elements.remove(); |
| 107 | + keyedElements.remove(first.getDriverTaskId()); |
| 108 | + return first; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + protected void pushToQueue(Element element) { |
| 113 | + elements.add(element); |
| 114 | + keyedElements.put(element.getDriverTaskId(), element); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + protected boolean contains(Element element) { |
| 119 | + return keyedElements.containsKey(element.getDriverTaskId()); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + protected void clearAllElements() { |
| 124 | + elements.clear(); |
| 125 | + keyedElements.clear(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static class ReserveQueue extends IndexedBlockingReserveQueue<Element> { |
| 130 | + private final Queue<Element> elements = new ArrayDeque<>(); |
| 131 | + private final Map<ID, Element> keyedElements = new HashMap<>(); |
| 132 | + |
| 133 | + private ReserveQueue(int capacity) { |
| 134 | + super(capacity, new Element(0)); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected Element remove(Element element) { |
| 139 | + Element removed = keyedElements.remove(element.getDriverTaskId()); |
| 140 | + if (removed != null) { |
| 141 | + elements.remove(removed); |
| 142 | + } |
| 143 | + return removed; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + protected Element get(Element element) { |
| 148 | + return keyedElements.get(element.getDriverTaskId()); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public boolean isEmpty() { |
| 153 | + return elements.isEmpty(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + protected Element pollFirst() { |
| 158 | + Element first = elements.remove(); |
| 159 | + keyedElements.remove(first.getDriverTaskId()); |
| 160 | + return first; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + protected void pushToQueue(Element element) { |
| 165 | + elements.add(element); |
| 166 | + keyedElements.put(element.getDriverTaskId(), element); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + protected boolean contains(Element element) { |
| 171 | + return keyedElements.containsKey(element.getDriverTaskId()); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + protected void clearAllElements() { |
| 176 | + elements.clear(); |
| 177 | + keyedElements.clear(); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private static class Element implements IDIndexedAccessible { |
| 182 | + private ElementId id; |
| 183 | + |
| 184 | + private Element(int id) { |
| 185 | + this.id = new ElementId(id); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public ID getDriverTaskId() { |
| 190 | + return id; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public void setId(ID id) { |
| 195 | + this.id = (ElementId) id; |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public boolean equals(Object obj) { |
| 200 | + return obj instanceof Element && ((Element) obj).id.equals(id); |
| 201 | + } |
| 202 | + |
| 203 | + @Override |
| 204 | + public int hashCode() { |
| 205 | + return id.hashCode(); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + private static class ElementId implements ID { |
| 210 | + private final int id; |
| 211 | + |
| 212 | + private ElementId(int id) { |
| 213 | + this.id = id; |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public boolean equals(Object obj) { |
| 218 | + return obj instanceof ElementId && ((ElementId) obj).id == id; |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + public int hashCode() { |
| 223 | + return Integer.hashCode(id); |
| 224 | + } |
| 225 | + } |
| 226 | +} |
0 commit comments