mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Make VanillaChronicleHash.close() to wait until concurrent threads accessing the hash finish their work and don't permit new threads to come while close() is in progress and after that
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (C) 2015 higherfrequencytrading.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.openhft.chronicle.map;
|
||||
|
||||
import net.openhft.chronicle.hash.impl.stage.hash.ChainingInterface;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Semaphore;
|
||||
|
||||
public class MapCloseTest {
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void closeInContextTest() {
|
||||
ChronicleMap<Integer, Integer> map =
|
||||
ChronicleMap.of(Integer.class, Integer.class).entries(1).create();
|
||||
ExternalMapQueryContext<Integer, Integer, ?> cxt = map.queryContext(1);
|
||||
map.close();
|
||||
}
|
||||
|
||||
@Test(expected = RuntimeException.class)
|
||||
public void closeWithContextInAnotherThreadTest() throws InterruptedException {
|
||||
ChronicleMap<Integer, Integer> map =
|
||||
ChronicleMap.of(Integer.class, Integer.class).entries(1).create();
|
||||
Object lock = new Object();
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
synchronized (lock) {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
ExternalMapQueryContext<Integer, Integer, ?> cxt = map.queryContext(1);
|
||||
latch.countDown();
|
||||
synchronized (lock) {
|
||||
cxt.close();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
latch.await();
|
||||
map.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void vanillaChronicleHashAllContextsExpungeTest() throws InterruptedException {
|
||||
VanillaChronicleMap<Integer, Integer, Void> map =
|
||||
(VanillaChronicleMap<Integer, Integer, Void>)
|
||||
ChronicleMap.of(Integer.class, Integer.class).entries(1).create();
|
||||
Semaphore semaphore = new Semaphore(0);
|
||||
CountDownLatch latch = new CountDownLatch(2);
|
||||
class MapAccessThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
map.get(1);
|
||||
latch.countDown();
|
||||
try {
|
||||
semaphore.acquire();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
MapAccessThread t1 = new MapAccessThread();
|
||||
MapAccessThread t2 = new MapAccessThread();
|
||||
t1.start();
|
||||
t2.start();
|
||||
latch.await();
|
||||
Assert.assertEquals(2, map.allContexts().size());
|
||||
semaphore.release(2);
|
||||
t1.join();
|
||||
t2.join();
|
||||
|
||||
map.get(1);
|
||||
Assert.assertEquals(1, map.allContexts().size());
|
||||
ChainingInterface cxt = map.allContexts().get(0).get();
|
||||
Assert.assertTrue(cxt == map.queryContext(1));
|
||||
}
|
||||
}
|
||||
@@ -52,7 +52,8 @@ public class NestedContextsInIterationContextTest {
|
||||
.actualSegments(2)
|
||||
.create()) {
|
||||
map.put(42, 42);
|
||||
int keySegmentIndex = map.queryContext(42).segmentIndex();
|
||||
ExternalMapQueryContext<Integer, Integer, ?> queryCxt = map.queryContext(42);
|
||||
int keySegmentIndex = queryCxt.segmentIndex();
|
||||
try (MapSegmentContext<Integer, Integer, ?> cxt = map.segmentContext(keySegmentIndex)) {
|
||||
cxt.forEachSegmentEntry(e -> {
|
||||
int k = 0;
|
||||
@@ -80,6 +81,8 @@ public class NestedContextsInIterationContextTest {
|
||||
}
|
||||
throw new AssertionError("expected " + IllegalStateException.class);
|
||||
});
|
||||
} finally {
|
||||
queryCxt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,7 +95,8 @@ public class NestedContextsInIterationContextTest {
|
||||
.actualSegments(2)
|
||||
.create()) {
|
||||
map.put(42, 42);
|
||||
int keySegmentIndex = map.queryContext(42).segmentIndex();
|
||||
ExternalMapQueryContext<Integer, Integer, ?> queryCxt = map.queryContext(42);
|
||||
int keySegmentIndex = queryCxt.segmentIndex();
|
||||
try (MapSegmentContext<Integer, Integer, ?> cxt = map.segmentContext(keySegmentIndex)) {
|
||||
cxt.forEachSegmentEntry(e -> {
|
||||
int k = 0;
|
||||
@@ -120,6 +124,8 @@ public class NestedContextsInIterationContextTest {
|
||||
}
|
||||
throw new AssertionError("expected " + IllegalStateException.class);
|
||||
});
|
||||
} finally {
|
||||
queryCxt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -132,7 +138,8 @@ public class NestedContextsInIterationContextTest {
|
||||
.actualSegments(2)
|
||||
.create()) {
|
||||
map.put(42, 42);
|
||||
int keySegmentIndex = map.queryContext(42).segmentIndex();
|
||||
ExternalMapQueryContext<Integer, Integer, ?> queryCxt = map.queryContext(42);
|
||||
int keySegmentIndex = queryCxt.segmentIndex();
|
||||
try (MapSegmentContext<Integer, Integer, ?> cxt = map.segmentContext(keySegmentIndex)) {
|
||||
cxt.forEachSegmentEntry(e -> {
|
||||
int k = 0;
|
||||
@@ -154,6 +161,8 @@ public class NestedContextsInIterationContextTest {
|
||||
nestedC.readLock().lock();
|
||||
}
|
||||
});
|
||||
} finally {
|
||||
queryCxt.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user