|
21 | 21 | package org.apache.bookkeeper.bookie.storage.ldb; |
22 | 22 |
|
23 | 23 | import static org.junit.Assert.assertEquals; |
| 24 | +import static org.junit.Assert.assertFalse; |
24 | 25 | import static org.junit.Assert.assertTrue; |
25 | 26 |
|
26 | 27 | import java.io.File; |
27 | 28 | import java.io.IOException; |
| 29 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 30 | +import java.util.concurrent.atomic.AtomicLong; |
28 | 31 | import org.apache.bookkeeper.conf.ServerConfiguration; |
29 | 32 | import org.apache.bookkeeper.stats.NullStatsLogger; |
30 | 33 | import org.apache.bookkeeper.test.TestStatsProvider; |
| 34 | +import org.awaitility.Awaitility; |
31 | 35 | import org.junit.Test; |
| 36 | +import org.junit.jupiter.api.Timeout; |
32 | 37 |
|
33 | 38 | /** |
34 | 39 | * Unit test for {@link EntryLocationIndex}. |
@@ -231,4 +236,39 @@ public void testEntryIndexLookupLatencyStats() throws IOException { |
231 | 236 | assertEquals(1, lookupEntryLocationOpStats.getFailureCount()); |
232 | 237 | assertEquals(1, lookupEntryLocationOpStats.getSuccessCount()); |
233 | 238 | } |
| 239 | + |
| 240 | + @Test |
| 241 | + @Timeout(30) |
| 242 | + public void testClose() throws Exception { |
| 243 | + File tmpDir = File.createTempFile("bkTest", ".dir"); |
| 244 | + tmpDir.delete(); |
| 245 | + tmpDir.mkdir(); |
| 246 | + tmpDir.deleteOnExit(); |
| 247 | + |
| 248 | + EntryLocationIndex idx = new EntryLocationIndex(serverConfiguration, KeyValueStorageRocksDB.factory, |
| 249 | + tmpDir.getAbsolutePath(), NullStatsLogger.INSTANCE); |
| 250 | + |
| 251 | + // mock EntryLocationIndex is compacting |
| 252 | + idx.compacting.set(true); |
| 253 | + AtomicBoolean flag = new AtomicBoolean(false); |
| 254 | + AtomicLong costMills = new AtomicLong(0); |
| 255 | + new Thread(() -> { |
| 256 | + try { |
| 257 | + long start = System.currentTimeMillis(); |
| 258 | + idx.close(); |
| 259 | + costMills.set(System.currentTimeMillis() - start); |
| 260 | + flag.set(true); |
| 261 | + } catch (IOException e) { |
| 262 | + throw new RuntimeException(e); |
| 263 | + } |
| 264 | + }).start(); |
| 265 | + long sleepMills = 10_000; |
| 266 | + Thread.sleep(sleepMills); |
| 267 | + assertFalse(flag.get()); |
| 268 | + |
| 269 | + // mock EntryLocationIndex finish compacting |
| 270 | + idx.compacting.set(false); |
| 271 | + Awaitility.await().untilAsserted(() -> assertTrue(flag.get())); |
| 272 | + assertTrue(costMills.get() >= sleepMills); |
| 273 | + } |
234 | 274 | } |
0 commit comments