vijaykriishna commented on code in PR #16207: URL: https://github.com/apache/lucene/pull/16207#discussion_r3368628706
########## lucene/core.tests/src/test/org/apache/lucene/core/tests/util/hnsw/TestBlockingFloatHeap.java: ########## @@ -0,0 +1,476 @@ +/* + * 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.lucene.core.tests.util.hnsw; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.BrokenBarrierException; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.CyclicBarrier; +import java.util.concurrent.atomic.AtomicInteger; +import org.apache.lucene.tests.util.LuceneTestCase; +import org.apache.lucene.util.hnsw.BlockingFloatHeap; + +/** + * Unit tests for {@link BlockingFloatHeap} with focus on thread-safety and data race conditions. + */ +public class TestBlockingFloatHeap extends LuceneTestCase { + + /** Test basic offer and poll operations */ + public void testBasicOfferAndPoll() { + BlockingFloatHeap heap = new BlockingFloatHeap(10); + heap.offer(5.0f); + assertEquals(1, heap.size()); + assertEquals(5.0f, heap.peek(), 0.0f); + + heap.offer(3.0f); + assertEquals(2, heap.size()); + assertEquals(3.0f, heap.peek(), 0.0f); + + heap.offer(7.0f); + assertEquals(3, heap.size()); + assertEquals(3.0f, heap.peek(), 0.0f); + + float result = heap.poll(); + assertEquals(3.0f, result, 0.0f); + assertEquals(2, heap.size()); + assertEquals(5.0f, heap.peek(), 0.0f); + + result = heap.poll(); + assertEquals(5.0f, result, 0.0f); + assertEquals(1, heap.size()); + assertEquals(7.0f, heap.peek(), 0.0f); + + result = heap.poll(); + assertEquals(7.0f, result, 0.0f); + assertEquals(0, heap.size()); + } + + /** Test poll on empty heap throws exception */ + public void testPollEmptyHeap() { + BlockingFloatHeap heap = new BlockingFloatHeap(10); + expectThrows(IllegalStateException.class, heap::poll); + } + + /** Test offer with array */ + public void testOfferArray() { + BlockingFloatHeap heap = new BlockingFloatHeap(10); + float[] values = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f}; + heap.offer(values, 5); + assertEquals(5, heap.size()); + assertEquals(1.0f, heap.peek(), 0.0f); + + float result = heap.poll(); + assertEquals(1.0f, result, 0.0f); + assertEquals(4, heap.size()); + assertEquals(2.0f, heap.peek(), 0.0f); + } + + /** Test heap maintains min-heap property */ + public void testHeapProperty() { + BlockingFloatHeap heap = new BlockingFloatHeap(100); + int[] testValues = {42, 17, 93, 8, 55, 34, 71, 2, 99, 11}; + + for (int val : testValues) { + heap.offer(val); + } + + assertEquals(10, heap.size()); + + int previous = (int) heap.poll(); + while (heap.size() > 0) { + int current = (int) heap.poll(); + assertTrue( + "Heap property violated: " + current + " should be >= " + previous, current >= previous); + previous = current; + } + } + + /** + * Test concurrent offer and poll operations. This specifically tests the data race fix where + * poll() was checking size > 0 outside the lock. + */ + @SuppressWarnings("unused") + public void testConcurrentOfferAndPoll() throws InterruptedException { + BlockingFloatHeap heap = new BlockingFloatHeap(1000); + int numThreads = 8; + int operationsPerThread = 100; + AtomicInteger errors = new AtomicInteger(0); + CyclicBarrier barrier = new CyclicBarrier(numThreads); + CountDownLatch producersReady = new CountDownLatch(numThreads / 2); + + Thread[] threads = new Thread[numThreads]; + + // Create producer/consumer threads + for (int i = 0; i < numThreads / 2; i++) { + threads[i] = + new Thread( + () -> { + try { + barrier.await(); // Synchronize thread start + for (int j = 0; j < operationsPerThread; j++) { + heap.offer((float) (random().nextDouble() * 1000)); + } + producersReady.countDown(); // Signal that this producer has completed + } catch (InterruptedException | BrokenBarrierException ignored) { + errors.incrementAndGet(); + } + }); + } + + for (int i = numThreads / 2; i < numThreads; i++) { + threads[i] = + new Thread( + () -> { + try { + barrier.await(); // Synchronize thread start + // Wait for producers to add items to the heap + producersReady.await(); + for (int j = 0; j < operationsPerThread / 2; j++) { + if (heap.size() > 0) { + heap.poll(); + } + } Review Comment: Ok. I'll dig a little deeper. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
