Skip to content

Commit da727c7

Browse files
Tests: make max size and size on disk test easier to maintain
Instead of asserting some very specific sizes and messages that are not really of importance, ensure the APIs fulfill reasonable expectations.
1 parent 4c70d99 commit da727c7

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreBuilderTest.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -261,31 +261,31 @@ public void maxSize_invalidValues_throw() {
261261
public void maxFileSize() {
262262
assumeFalse(IN_MEMORY); // no max size support for in-memory
263263

264+
// To avoid frequently changing the limit choose one high enough to insert at least one object successfully,
265+
// then keep inserting until the limit is hit.
264266
builder = createBoxStoreBuilder(null);
265-
// The empty data.mdb file is around 12 KB, but creating will fail also if slightly above that
266-
builder.maxSizeInKByte(15);
267-
DbFullException couldNotPut = assertThrows(
268-
DbFullException.class,
269-
() -> builder.build()
270-
);
271-
assertEquals("Could not put", couldNotPut.getMessage());
272-
273-
builder.maxSizeInKByte(30); // Empty file is around 12 KB, object below adds about 8 KB each.
267+
builder.maxSizeInKByte(150);
274268
store = builder.build();
275-
putTestEntity(LONG_STRING, 1);
276-
TestEntity testEntity2 = createTestEntity(LONG_STRING, 2);
277-
DbFullException dbFullException = assertThrows(
278-
DbFullException.class,
279-
() -> getTestEntityBox().put(testEntity2)
280-
);
281-
assertEquals("Could not commit tx", dbFullException.getMessage());
282269

283-
// Re-open with larger size.
270+
putTestEntity(LONG_STRING, 1); // Should work
271+
272+
boolean dbFullExceptionThrown = false;
273+
for (int i = 2; i < 1000; i++) {
274+
TestEntity testEntity = createTestEntity(LONG_STRING, i);
275+
try {
276+
getTestEntityBox().put(testEntity);
277+
} catch (DbFullException e) {
278+
dbFullExceptionThrown = true;
279+
break;
280+
}
281+
}
282+
assertTrue("DbFullException was not thrown", dbFullExceptionThrown);
283+
284+
// Check re-opening with larger size allows to insert again
284285
store.close();
285-
builder.maxSizeInKByte(40);
286+
builder.maxSizeInKByte(200);
286287
store = builder.build();
287-
testEntity2.setId(0); // Clear ID of object that failed to put.
288-
getTestEntityBox().put(testEntity2);
288+
getTestEntityBox().put(createTestEntity(LONG_STRING, 1000));
289289
}
290290

291291
@Test

tests/objectbox-java-test/src/test/java/io/objectbox/BoxStoreTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,20 @@ private Callable<String> createTestCallable(final int[] countHolder) {
309309
@Test
310310
public void testSizeOnDisk() {
311311
// Note: initial database does have a non-zero (file) size.
312+
@SuppressWarnings("deprecation")
312313
long legacySizeOnDisk = store.sizeOnDisk();
313314
assertTrue(legacySizeOnDisk > 0);
314315

315316
assertTrue(store.getDbSize() > 0);
316317

317318
long sizeOnDisk = store.getDbSizeOnDisk();
318-
assertEquals(IN_MEMORY ? 0 : 12288, sizeOnDisk);
319+
// Check the file size is at least a reasonable value
320+
assertTrue("Size is not reasonable", IN_MEMORY ? sizeOnDisk == 0 : sizeOnDisk > 10000 /* 10 KB */);
321+
322+
// Check the file size increases after inserting
323+
putTestEntities(10);
324+
long sizeOnDiskAfterPut = store.getDbSizeOnDisk();
325+
assertTrue("Size did not increase", IN_MEMORY ? sizeOnDiskAfterPut == 0 : sizeOnDiskAfterPut > sizeOnDisk);
319326
}
320327

321328
@Test

0 commit comments

Comments
 (0)