Skip to content

Commit e7045ed

Browse files
committed
test fixes for transaction not cleaned up
1 parent 18c88fc commit e7045ed

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

tests/objectbox-java-test/src/main/java/io/objectbox/CursorBytesTest.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
import java.util.Arrays;
2222
import java.util.Random;
2323

24-
25-
import static org.junit.Assert.assertEquals;
26-
import static org.junit.Assert.assertNotNull;
27-
import static org.junit.Assert.assertTrue;
24+
import static org.junit.Assert.*;
2825

2926
// NOTE: Sizes must be multiple of 4 (currently not enforced)
3027
public class CursorBytesTest extends AbstractObjectBoxTest {
@@ -55,8 +52,8 @@ public void testFirstLastNextPrev() {
5552
assertTrue(Arrays.equals(new byte[]{4, 5, 6, 7}, cursor.getNext()));
5653
assertTrue(Arrays.equals(new byte[]{2, 3, 4, 5}, cursor.getPrev()));
5754
// getLast is currently unsupported
58-
// assertTrue(Arrays.equals(new byte[]{8, 9, 10, 11, 12, 13}, cursor.getLast()));
59-
// assertTrue(Arrays.equals(new byte[]{4, 5, 6, 7, 8}, cursor.getPrev()));
55+
// assertTrue(Arrays.equals(new byte[]{8, 9, 10, 11, 12, 13}, cursor.getLast()));
56+
// assertTrue(Arrays.equals(new byte[]{4, 5, 6, 7, 8}, cursor.getPrev()));
6057

6158
cursor.close();
6259
transaction.abort();
@@ -65,19 +62,23 @@ public void testFirstLastNextPrev() {
6562
@Test
6663
public void testRemove() {
6764
Transaction transaction = store.beginTx();
68-
KeyValueCursor cursor = transaction.createKeyValueCursor();
69-
70-
cursor.put(1, new byte[]{1, 1, 0, 0});
71-
cursor.put(2, new byte[]{2, 1, 0, 0});
72-
cursor.put(4, new byte[]{4, 1, 0, 0});
73-
74-
assertTrue(cursor.removeAt(2));
75-
76-
// now 4 should be next to 1
77-
assertTrue(cursor.seek(1));
78-
byte[] next = cursor.getNext();
79-
assertNotNull(next);
80-
assertTrue(Arrays.equals(new byte[]{4, 1, 0, 0}, next));
65+
try {
66+
KeyValueCursor cursor = transaction.createKeyValueCursor();
67+
68+
cursor.put(1, new byte[]{1, 1, 0, 0});
69+
cursor.put(2, new byte[]{2, 1, 0, 0});
70+
cursor.put(4, new byte[]{4, 1, 0, 0});
71+
72+
assertTrue(cursor.removeAt(2));
73+
74+
// now 4 should be next to 1
75+
assertTrue(cursor.seek(1));
76+
byte[] next = cursor.getNext();
77+
assertNotNull(next);
78+
assertTrue(Arrays.equals(new byte[]{4, 1, 0, 0}, next));
79+
} finally {
80+
transaction.close();
81+
}
8182
}
8283

8384
@Test

tests/objectbox-java-test/src/main/java/io/objectbox/CursorTest.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,7 @@
2323
import java.util.concurrent.CountDownLatch;
2424
import java.util.concurrent.TimeUnit;
2525

26-
27-
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.assertFalse;
29-
import static org.junit.Assert.assertNotNull;
30-
import static org.junit.Assert.assertNull;
31-
import static org.junit.Assert.assertSame;
32-
import static org.junit.Assert.assertTrue;
26+
import static org.junit.Assert.*;
3327

3428
public class CursorTest extends AbstractObjectBoxTest {
3529

@@ -64,7 +58,7 @@ public void testPutEntityWithInvalidId() {
6458
cursor.put(entity);
6559
} finally {
6660
cursor.close();
67-
transaction.abort();
61+
transaction.close();
6862
}
6963
}
7064

@@ -149,15 +143,19 @@ public void testPutSameIndexValue() {
149143
String value = "lulu321";
150144
entity.setSimpleString(value);
151145
Transaction transaction = store.beginTx();
152-
153-
Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class);
154-
long key = cursor.put(entity);
155-
// And again
156-
entity.setSimpleInt(1977);
157-
cursor.put(entity);
158-
assertEquals(key, cursor.lookupKeyUsingIndex(9, value));
159-
TestEntity read = cursor.get(key);
160-
cursor.close();
146+
TestEntity read;
147+
try {
148+
Cursor<TestEntity> cursor = transaction.createCursor(TestEntity.class);
149+
long key = cursor.put(entity);
150+
// And again
151+
entity.setSimpleInt(1977);
152+
cursor.put(entity);
153+
assertEquals(key, cursor.lookupKeyUsingIndex(9, value));
154+
read = cursor.get(key);
155+
cursor.close();
156+
} finally {
157+
transaction.close();
158+
}
161159
assertEquals(1977, read.getSimpleInt());
162160
assertEquals(value, read.getSimpleString());
163161
}
@@ -264,13 +262,17 @@ public void testLookupKeyUsingIndex_samePrefix() {
264262
@Test
265263
public void testClose() {
266264
Transaction tx = store.beginReadTx();
267-
Cursor<TestEntity> cursor = tx.createCursor(TestEntity.class);
268-
assertFalse(cursor.isClosed());
269-
cursor.close();
270-
assertTrue(cursor.isClosed());
265+
try {
266+
Cursor<TestEntity> cursor = tx.createCursor(TestEntity.class);
267+
assertFalse(cursor.isClosed());
268+
cursor.close();
269+
assertTrue(cursor.isClosed());
271270

272-
// Double close should be fine
273-
cursor.close();
271+
// Double close should be fine
272+
cursor.close();
273+
} finally {
274+
tx.close();
275+
}
274276
}
275277

276278
@Test

0 commit comments

Comments
 (0)