|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * LmdbJava |
| 4 | + * %% |
| 5 | + * Copyright (C) 2016 - 2023 The LmdbJava Open Source Project |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | + |
| 21 | +package org.lmdbjava; |
| 22 | + |
| 23 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.rules.TemporaryFolder; |
| 27 | +import org.mockito.MockedStatic; |
| 28 | +import org.mockito.Mockito; |
| 29 | + |
| 30 | +import java.io.File; |
| 31 | +import java.io.IOException; |
| 32 | +import java.nio.ByteBuffer; |
| 33 | + |
| 34 | +import static java.nio.ByteBuffer.allocateDirect; |
| 35 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 36 | +import static org.junit.Assert.fail; |
| 37 | +import static org.lmdbjava.DbiFlags.MDB_CREATE; |
| 38 | +import static org.lmdbjava.Env.create; |
| 39 | + |
| 40 | +@SuppressFBWarnings({"DM_GC", "RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT"}) |
| 41 | +@SuppressWarnings("PMD.DoNotCallGarbageCollectionExplicitly") |
| 42 | +public class GarbageCollectionTest { |
| 43 | + |
| 44 | + private static final String DB_NAME = "my DB"; |
| 45 | + private static final String KEY_PREFIX = "Uncorruptedkey"; |
| 46 | + private static final String VAL_PREFIX = "Uncorruptedval"; |
| 47 | + |
| 48 | + @Rule |
| 49 | + public final TemporaryFolder tmp = new TemporaryFolder(); |
| 50 | + private void putBuffer(Dbi<ByteBuffer> db, Txn<ByteBuffer> txn, int i) { |
| 51 | + ByteBuffer key = allocateDirect(24); |
| 52 | + ByteBuffer val = allocateDirect(24); |
| 53 | + key.put((KEY_PREFIX+i).getBytes(UTF_8)).flip(); |
| 54 | + val.put((VAL_PREFIX+i).getBytes(UTF_8)).flip(); |
| 55 | + db.put(txn, key, val); |
| 56 | + } |
| 57 | + @Test |
| 58 | + public void buffersNotGarbageCollectedTest() throws IOException { |
| 59 | + final File path = tmp.newFolder(); |
| 60 | + |
| 61 | + final Env<ByteBuffer> env = create() |
| 62 | + .setMapSize(2_085_760_999) |
| 63 | + .setMaxDbs(1) |
| 64 | + .open(path); |
| 65 | + final Dbi<ByteBuffer> db = env.openDbi(DB_NAME, MDB_CREATE); |
| 66 | + |
| 67 | + // Trigger compilation and whatnot |
| 68 | + try (Txn<ByteBuffer> txn = env.txnWrite()) { |
| 69 | + for (int i = 0; i < 5000; i++) { |
| 70 | + putBuffer(db, txn, i); |
| 71 | + } |
| 72 | + txn.commit(); |
| 73 | + } |
| 74 | + // Call gc before writing to lmdb and after last reference to buffer by changing the behavior of mask |
| 75 | + try (MockedStatic<MaskedFlag> mockedStatic = Mockito.mockStatic(MaskedFlag.class)) { |
| 76 | + mockedStatic.when(MaskedFlag::mask).thenAnswer(invocationOnMock -> { |
| 77 | + System.gc(); |
| 78 | + return 0; |
| 79 | + }); |
| 80 | + try (Txn<ByteBuffer> txn = env.txnWrite()) { |
| 81 | + for (int i = 0; i < 1000; i++) { |
| 82 | + putBuffer(db, txn, i); |
| 83 | + } |
| 84 | + txn.commit(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Find corrupt keys |
| 89 | + try (Txn<ByteBuffer> txn = env.txnRead()) { |
| 90 | + try (Cursor<ByteBuffer> c = db.openCursor(txn)) { |
| 91 | + if (c.first()) { |
| 92 | + do { |
| 93 | + byte[] rkey = new byte[c.key().remaining()]; |
| 94 | + c.key().get(rkey); |
| 95 | + byte[] rval = new byte[c.val().remaining()]; |
| 96 | + c.val().get(rval); |
| 97 | + String skey = new String(rkey, UTF_8); |
| 98 | + String sval = new String(rval, UTF_8); |
| 99 | + if (!skey.startsWith("Uncorruptedkey")) { |
| 100 | + fail("Found corrupt key " + skey); |
| 101 | + } |
| 102 | + if (!sval.startsWith("Uncorruptedval")) { |
| 103 | + fail("Found corrupt val " + sval); |
| 104 | + } |
| 105 | + } while (c.next()); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + env.close(); |
| 110 | + } |
| 111 | +} |
0 commit comments