|
| 1 | +package org.lmdbjava; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.RepeatedTest; |
| 4 | + |
| 5 | +import java.nio.ByteBuffer; |
| 6 | +import java.nio.file.Path; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | +import java.util.function.Consumer; |
| 10 | + |
| 11 | +import static org.assertj.core.api.Assertions.assertThat; |
| 12 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 13 | +import static org.lmdbjava.EnvFlags.MDB_NOSUBDIR; |
| 14 | +import static org.lmdbjava.EnvFlags.MDB_NOTLS; |
| 15 | + |
| 16 | +public class TestSegfault { |
| 17 | + // @Test |
| 18 | + @RepeatedTest(1000) |
| 19 | + void testSegFault() { |
| 20 | + try (final ExecutorService executor1 = Executors.newSingleThreadExecutor()) { |
| 21 | + try (final ExecutorService executor2 = Executors.newSingleThreadExecutor()) { |
| 22 | + executor1.execute(makeRunnable(this::txParentDeniedIfEnvClosed)); |
| 23 | + executor2.execute(makeRunnable(this::txParentDeniedIfEnvClosed)); |
| 24 | + executor1.execute(makeRunnable(this::txParentDeniedIfEnvClosed)); |
| 25 | +// executor2.execute(makeRunnable(this::txParentROChildRWIncompatible)); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + private Runnable makeRunnable(final Consumer<Env<ByteBuffer>> consumer) { |
| 31 | + return () -> { |
| 32 | + final Path file = FileUtil.createTempFile(); |
| 33 | + try { |
| 34 | + try (Env<ByteBuffer> env = Env.create().setMapSize(2_085_760_999).setMaxDbs(1).open(file.toFile(), MDB_NOSUBDIR)) { |
| 35 | + consumer.accept(env); |
| 36 | + } |
| 37 | + } catch (final Exception e) { |
| 38 | + e.printStackTrace(); |
| 39 | + } finally { |
| 40 | + FileUtil.delete(file); |
| 41 | + } |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + private void txParentDeniedIfEnvClosed(final Env<ByteBuffer> env) { |
| 46 | + assertThatThrownBy( |
| 47 | + () -> { |
| 48 | + try (Txn<ByteBuffer> txRoot = env.txnWrite()) { |
| 49 | + try (final Txn<ByteBuffer> txChild = env.txn(txRoot)) { |
| 50 | + env.close(); |
| 51 | + assertThat(txChild.getParent()).isEqualTo(txRoot); |
| 52 | + } |
| 53 | + } |
| 54 | + }) |
| 55 | + .isInstanceOf(Env.AlreadyClosedException.class); |
| 56 | + } |
| 57 | +} |
| 58 | + |
0 commit comments