Implemented compactOnLaunch test
ADD
packages/isar_test/test/compact_on_launch_test.dart
+import 'dart:io';
+
+import 'package:isar/isar.dart';
+import 'package:test/test.dart';
+
+import 'util/common.dart';
+import 'util/sync_async_helper.dart';
+
+part 'compact_on_launch_test.g.dart';
+
+@Collection()
+class Model {
+ Id id = Isar.autoIncrement;
+
+ List<int> buffer = List.filled(16000, 42);
+
+ @override
+ // ignore: hash_and_equals
+ bool operator ==(Object other) =>
+ identical(this, other) ||
+ other is Model &&
+ runtimeType == other.runtimeType &&
+ id == other.id &&
+ listEquals(buffer, other.buffer);
+
+ @override
+ String toString() {
+ return 'Model{id: $id}';
+ }
+}
+
+void main() {
+ group('Compact on launch', () {
+ late Isar isar;
+ late File file;
+
+ setUp(() async {
+ isar = await openTempIsar([ModelSchema]);
+ file = File(isar.path!);
+
+ await isar.tWriteTxn(
+ () => isar.models.tPutAll(List.filled(100, Model())),
+ );
+ });
+
+ tearDown(() => isar.close(deleteFromDisk: true));
+
+ isarTestVm('No compact on launch', () async {
+ await isar.close();
+ final size1 = file.lengthSync();
+
+ isar = await openTempIsar([ModelSchema], name: isar.name);
+ await isar.tWriteTxn(() => isar.models.where().limit(50).tDeleteAll());
+ await isar.close();
+
+ final size2 = file.lengthSync();
+
+ isar = await openTempIsar([ModelSchema], name: isar.name);
+
+ expect(size1, size2);
+ });
+
+ isarTestVm('minFileSize', () async {
+ await isar.close();
+ final size1 = file.lengthSync();
+
+ isar = await openTempIsar([ModelSchema], name: isar.name);
+ await isar.tWriteTxn(() => isar.models.where().limit(50).tDeleteAll());
+ await isar.close();
+
+ isar = await openTempIsar(
+ [ModelSchema],
+ name: isar.name,
+ compactOnLaunch: CompactCondition(minFileSize: size1 * 2),
+ );
+ await isar.close();
+ final size2 = file.lengthSync();
+ expect(size1, size2);
+
+ isar = await openTempIsar(
+ [ModelSchema],
+ name: isar.name,
+ compactOnLaunch: CompactCondition(minFileSize: size1 ~/ 2),
+ );
+ final size3 = file.lengthSync();
+ expect(size3, lessThan(size2));
+ });
+
+ isarTestVm('minBytes', () async {
+ await isar.close();
+ final size1 = file.lengthSync();
+
+ isar = await openTempIsar([ModelSchema], name: isar.name);
+ await isar.tWriteTxn(() => isar.models.where().limit(10).tDeleteAll());
+ await isar.close();
+
+ isar = await openTempIsar(
+ [ModelSchema],
+ name: isar.name,
+ compactOnLaunch: CompactCondition(minBytes: size1 ~/ 2),
+ );
+ await isar.close();
+ final size2 = file.lengthSync();
+ expect(size1, size2);
+
+ isar = await openTempIsar(
+ [ModelSchema],
+ name: isar.name,
+ compactOnLaunch: CompactCondition(minBytes: size1 ~/ 2),
+ );
+ await isar.tWriteTxn(() => isar.models.where().limit(80).tDeleteAll());
+ await isar.close();
+ final size3 = file.lengthSync();
+ expect(size3, size2);
+
+ isar = await openTempIsar(
+ [ModelSchema],
+ name: isar.name,
+ compactOnLaunch: CompactCondition(minBytes: size1 ~/ 2),
+ );
+ final size4 = file.lengthSync();
+ expect(size4, lessThan(size3));
+ });
+
+ isarTestVm('minRatio', () async {
+ await isar.close();
+ final size1 = file.lengthSync();
+
+ isar = await openTempIsar([ModelSchema], name: isar.name);
+ await isar.tWriteTxn(() => isar.models.where().limit(10).tDeleteAll());
+ await isar.close();
+
+ isar = await openTempIsar(
+ [ModelSchema],
+ name: isar.name,
+ compactOnLaunch: const CompactCondition(minRatio: 2),
+ );
+ await isar.close();
+ final size2 = file.lengthSync();
+ expect(size1, size2);
+
+ isar = await openTempIsar(
+ [ModelSchema],
+ name: isar.name,
+ compactOnLaunch: const CompactCondition(minRatio: 2),
+ );
+ await isar.tWriteTxn(() => isar.models.where().limit(80).tDeleteAll());
+ await isar.close();
+ final size3 = file.lengthSync();
+ expect(size3, size2);
+
+ isar = await openTempIsar(
+ [ModelSchema],
+ name: isar.name,
+ compactOnLaunch: const CompactCondition(minRatio: 2),
+ );
+ final size4 = file.lengthSync();
+ expect(size4, lessThan(size3));
+ });
+ });
+}

MODIFY
packages/isar_test/test/util/common.dart
packages/isar_test/test/util/common.dart
@@ -114,6 +114,7 @@ String? testTempPath;
Future<Isar> openTempIsar(
List<CollectionSchema<dynamic>> schemas, {
String? name,
+ CompactCondition? compactOnLaunch,
}) async {
await _prepareTest();
if (!kIsWeb && testTempPath == null) {
@@ -126,6 +127,7 @@ Future<Isar> openTempIsar(
schemas: schemas,
name: name ?? getRandomName(),
directory: kIsWeb ? '' : testTempPath!,
+ compactOnLaunch: compactOnLaunch,
);
await isar.verify();

MODIFY
packages/isar_test/test/util/sync_async_helper.dart
packages/isar_test/test/util/sync_async_helper.dart
@@ -12,6 +12,7 @@ Future<Isar> tOpen({
String? directory,
String name = Isar.defaultName,
bool relaxedDurability = true,
+ CompactCondition? compactOnLaunch,
}) {
if (syncTest) {
final isar = Isar.openSync(
@@ -19,6 +20,7 @@ Future<Isar> tOpen({
directory: directory,
name: name,
relaxedDurability: relaxedDurability,
+ compactOnLaunch: compactOnLaunch,
inspector: false,
);
return SynchronousFuture(isar);
@@ -28,6 +30,7 @@ Future<Isar> tOpen({
directory: directory,
name: name,
relaxedDurability: relaxedDurability,
+ compactOnLaunch: compactOnLaunch,
inspector: false,
);
}

Implemented compactOnLaunch test