Search in sources :

Example 1 with FilePathMem

use of org.h2.store.fs.FilePathMem in project h2database by h2database.

the class TestOutOfMemory method testMVStoreUsingInMemoryFileSystem.

private void testMVStoreUsingInMemoryFileSystem() {
    FilePath.register(new FilePathMem());
    String fileName = "memFS:" + getTestName();
    final AtomicReference<Throwable> exRef = new AtomicReference<>();
    MVStore store = new MVStore.Builder().fileName(fileName).backgroundExceptionHandler(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            exRef.compareAndSet(null, e);
        }
    }).open();
    try {
        Map<Integer, byte[]> map = store.openMap("test");
        Random r = new Random(1);
        try {
            for (int i = 0; i < 100; i++) {
                byte[] data = new byte[10 * 1024 * 1024];
                r.nextBytes(data);
                map.put(i, data);
            }
            Throwable throwable = exRef.get();
            if (throwable instanceof OutOfMemoryError)
                throw (OutOfMemoryError) throwable;
            if (throwable instanceof IllegalStateException)
                throw (IllegalStateException) throwable;
            fail();
        } catch (OutOfMemoryError | IllegalStateException e) {
        // expected
        }
        try {
            store.close();
        } catch (IllegalStateException e) {
        // expected
        }
        store.closeImmediately();
        store = MVStore.open(fileName);
        store.openMap("test");
        store.close();
    } finally {
        // just in case, otherwise if this test suffers a spurious failure,
        // succeeding tests will too, because they will OOM
        store.closeImmediately();
        FileUtils.delete(fileName);
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) MVStore(org.h2.mvstore.MVStore) Random(java.util.Random) FilePathMem(org.h2.store.fs.FilePathMem)

Aggregations

Random (java.util.Random)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 MVStore (org.h2.mvstore.MVStore)1 FilePathMem (org.h2.store.fs.FilePathMem)1