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:
leventov
2016-07-17 23:25:49 +07:00
parent b3c1f7b1e2
commit 7ada09a8dc
12 changed files with 322 additions and 32 deletions
@@ -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));
}
}