|
| 1 | +// Copyright 2021-present StarRocks, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.starrocks.connector.iceberg; |
| 16 | + |
| 17 | +import org.apache.iceberg.io.CloseableIterator; |
| 18 | +import org.junit.jupiter.api.Assertions; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.util.NoSuchElementException; |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.atomic.AtomicReference; |
| 24 | + |
| 25 | +public class SynchronizedCloseableIteratorTest { |
| 26 | + |
| 27 | + // Mimics Iceberg's ParallelIterable: once closed, hasNext()/next() throw |
| 28 | + // IllegalStateException("Already closed"). |
| 29 | + private static class AlreadyClosedIterator implements CloseableIterator<Integer> { |
| 30 | + private int index = 0; |
| 31 | + private final int size; |
| 32 | + private volatile boolean closed = false; |
| 33 | + |
| 34 | + AlreadyClosedIterator(int size) { |
| 35 | + this.size = size; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public boolean hasNext() { |
| 40 | + if (closed) { |
| 41 | + throw new IllegalStateException("Already closed"); |
| 42 | + } |
| 43 | + return index < size; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public Integer next() { |
| 48 | + if (closed) { |
| 49 | + throw new IllegalStateException("Already closed"); |
| 50 | + } |
| 51 | + return index++; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void close() { |
| 56 | + closed = true; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testDrainThenClosedBehavior() { |
| 62 | + SynchronizedCloseableIterator<Integer> it = |
| 63 | + new SynchronizedCloseableIterator<>(new AlreadyClosedIterator(3)); |
| 64 | + int count = 0; |
| 65 | + while (it.hasNext()) { |
| 66 | + it.next(); |
| 67 | + count++; |
| 68 | + } |
| 69 | + Assertions.assertEquals(3, count); |
| 70 | + |
| 71 | + Assertions.assertDoesNotThrow(it::close); |
| 72 | + Assertions.assertDoesNotThrow(it::close); // close is idempotent |
| 73 | + Assertions.assertFalse(it.hasNext()); // no reopen / no throw after close |
| 74 | + Assertions.assertThrows(NoSuchElementException.class, it::next); |
| 75 | + } |
| 76 | + |
| 77 | + // After close, hasNext() reports exhausted even if an element was prefetched, so a closed |
| 78 | + // source never advertises another batch; an already-prefetched element is still drainable. |
| 79 | + @Test |
| 80 | + public void testClosedReportsExhaustedAfterPrefetch() { |
| 81 | + SynchronizedCloseableIterator<Integer> it = |
| 82 | + new SynchronizedCloseableIterator<>(new AlreadyClosedIterator(5)); |
| 83 | + Assertions.assertTrue(it.hasNext()); // prefetches and buffers one element |
| 84 | + Assertions.assertDoesNotThrow(it::close); |
| 85 | + Assertions.assertFalse(it.hasNext()); // closed -> exhausted, does not advertise the buffer |
| 86 | + Assertions.assertDoesNotThrow(it::next); // an in-flight next() still returns the buffered element |
| 87 | + } |
| 88 | + |
| 89 | + // Consuming on one thread while another closes must never surface |
| 90 | + // IllegalStateException("Already closed") from the delegate. |
| 91 | + @Test |
| 92 | + public void testConcurrentConsumeAndClose() throws InterruptedException { |
| 93 | + for (int round = 0; round < 2000; round++) { |
| 94 | + SynchronizedCloseableIterator<Integer> it = |
| 95 | + new SynchronizedCloseableIterator<>(new AlreadyClosedIterator(1000)); |
| 96 | + AtomicReference<Throwable> failure = new AtomicReference<>(); |
| 97 | + CountDownLatch start = new CountDownLatch(1); |
| 98 | + |
| 99 | + Thread consumer = new Thread(() -> { |
| 100 | + try { |
| 101 | + start.await(); |
| 102 | + while (it.hasNext()) { |
| 103 | + it.next(); |
| 104 | + } |
| 105 | + } catch (Throwable t) { |
| 106 | + failure.set(t); |
| 107 | + } |
| 108 | + }); |
| 109 | + Thread closer = new Thread(() -> { |
| 110 | + try { |
| 111 | + start.await(); |
| 112 | + it.close(); |
| 113 | + } catch (Throwable t) { |
| 114 | + failure.set(t); |
| 115 | + } |
| 116 | + }); |
| 117 | + |
| 118 | + consumer.start(); |
| 119 | + closer.start(); |
| 120 | + start.countDown(); |
| 121 | + consumer.join(); |
| 122 | + closer.join(); |
| 123 | + |
| 124 | + if (failure.get() != null) { |
| 125 | + Assertions.fail("concurrent consume/close threw: " + failure.get()); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments