zhuzhurk commented on code in PR #21779: URL: https://github.com/apache/flink/pull/21779#discussion_r1090394702
########## flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/watermarkstatus/HeapPriorityQueueTest.java: ########## @@ -0,0 +1,316 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.streaming.runtime.watermarkstatus; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ThreadLocalRandom; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Test for {@link HeapPriorityQueue}. */ +class HeapPriorityQueueTest { + private static final HeapPriorityQueue.PriorityComparator<TestElement> + TEST_ELEMENT_PRIORITY_COMPARATOR = + (left, right) -> Long.compare(left.getPriority(), right.getPriority()); + + @Test + void testPeekPollOrder() { + final int initialCapacity = 4; + final int testSize = 1000; + final Comparator<Long> comparator = getTestElementPriorityComparator(); + HeapPriorityQueue<TestElement> priorityQueue = newPriorityQueue(initialCapacity); + HashSet<TestElement> checkSet = new HashSet<>(testSize); + + insertRandomElements(priorityQueue, checkSet, testSize); + + long lastPriorityValue = getHighestPriorityValueForComparator(); + int lastSize = priorityQueue.size(); + assertThat(testSize).isEqualTo(lastSize); + TestElement testElement; + while ((testElement = priorityQueue.peek()) != null) { + assertThat(priorityQueue.isEmpty()).isFalse(); + assertThat(lastSize).isEqualTo(priorityQueue.size()); + assertThat(testElement).isEqualTo(priorityQueue.poll()); + assertThat(checkSet.remove(testElement)).isTrue(); + assertThat(comparator.compare(testElement.getPriority(), lastPriorityValue) >= 0) + .isTrue(); + lastPriorityValue = testElement.getPriority(); + --lastSize; + } + + assertThat(priorityQueue.isEmpty()).isTrue(); + assertThat(priorityQueue.size()).isZero(); + assertThat(checkSet.size()).isZero(); + } + + @Test + void testRemoveInsertMixKeepsOrder() { + + HeapPriorityQueue<TestElement> priorityQueue = newPriorityQueue(3); + final Comparator<Long> comparator = getTestElementPriorityComparator(); + final ThreadLocalRandom random = ThreadLocalRandom.current(); + final int testSize = 300; + final int addCounterMax = testSize / 4; + int iterationsTillNextAdds = random.nextInt(addCounterMax); + HashSet<TestElement> checkSet = new HashSet<>(testSize); + + insertRandomElements(priorityQueue, checkSet, testSize); + + // check that the whole set is still in order + while (!checkSet.isEmpty()) { + + final long highestPrioValue = getHighestPriorityValueForComparator(); + + Iterator<TestElement> iterator = checkSet.iterator(); + TestElement element = iterator.next(); + iterator.remove(); + + final boolean removesHead = element.equals(priorityQueue.peek()); + + if (removesHead) { + assertThat(priorityQueue.remove(element)).isTrue(); + } else { + priorityQueue.remove(element); + } + + long currentPriorityWatermark; + + // test some bulk polling from time to time + if (removesHead) { + currentPriorityWatermark = element.getPriority(); + } else { + currentPriorityWatermark = highestPrioValue; + } + + while ((element = priorityQueue.poll()) != null) { + assertThat(comparator.compare(element.getPriority(), currentPriorityWatermark) >= 0) + .isTrue(); + currentPriorityWatermark = element.getPriority(); + if (--iterationsTillNextAdds == 0) { + // some random adds + iterationsTillNextAdds = random.nextInt(addCounterMax); + insertRandomElements( + priorityQueue, new HashSet<>(checkSet), 1 + random.nextInt(3)); + currentPriorityWatermark = priorityQueue.peek().getPriority(); + } + } + + assertThat(priorityQueue.isEmpty()).isTrue(); + + checkSet.forEach(priorityQueue::add); + } + } + + @Test + void testPoll() { + HeapPriorityQueue<TestElement> priorityQueue = newPriorityQueue(3); + final Comparator<Long> comparator = getTestElementPriorityComparator(); + + assertThat(priorityQueue.poll()).isNull(); + + final int testSize = 345; + HashSet<TestElement> checkSet = new HashSet<>(testSize); + insertRandomElements(priorityQueue, checkSet, testSize); + + long lastPriorityValue = getHighestPriorityValueForComparator(); + while (!priorityQueue.isEmpty()) { + TestElement removed = priorityQueue.poll(); + assertThat(removed).isNotNull(); + assertThat(checkSet.remove(removed)).isTrue(); + assertThat(comparator.compare(removed.getPriority(), lastPriorityValue) >= 0).isTrue(); + lastPriorityValue = removed.getPriority(); + } + assertThat(checkSet.isEmpty()).isTrue(); Review Comment: ```suggestion assertThat(checkSet).isEmpty(); ``` ########## flink-streaming-java/src/test/java/org/apache/flink/streaming/runtime/watermarkstatus/HeapPriorityQueueTest.java: ########## @@ -0,0 +1,316 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.streaming.runtime.watermarkstatus; + +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ThreadLocalRandom; + +import static org.assertj.core.api.Assertions.assertThat; + +/** Test for {@link HeapPriorityQueue}. */ +class HeapPriorityQueueTest { + private static final HeapPriorityQueue.PriorityComparator<TestElement> + TEST_ELEMENT_PRIORITY_COMPARATOR = + (left, right) -> Long.compare(left.getPriority(), right.getPriority()); + + @Test + void testPeekPollOrder() { + final int initialCapacity = 4; + final int testSize = 1000; + final Comparator<Long> comparator = getTestElementPriorityComparator(); + HeapPriorityQueue<TestElement> priorityQueue = newPriorityQueue(initialCapacity); + HashSet<TestElement> checkSet = new HashSet<>(testSize); + + insertRandomElements(priorityQueue, checkSet, testSize); + + long lastPriorityValue = getHighestPriorityValueForComparator(); + int lastSize = priorityQueue.size(); + assertThat(testSize).isEqualTo(lastSize); + TestElement testElement; + while ((testElement = priorityQueue.peek()) != null) { + assertThat(priorityQueue.isEmpty()).isFalse(); + assertThat(lastSize).isEqualTo(priorityQueue.size()); + assertThat(testElement).isEqualTo(priorityQueue.poll()); + assertThat(checkSet.remove(testElement)).isTrue(); + assertThat(comparator.compare(testElement.getPriority(), lastPriorityValue) >= 0) + .isTrue(); + lastPriorityValue = testElement.getPriority(); + --lastSize; + } + + assertThat(priorityQueue.isEmpty()).isTrue(); + assertThat(priorityQueue.size()).isZero(); + assertThat(checkSet.size()).isZero(); Review Comment: ```suggestion assertThat(checkSet).isEmpty(); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
