Skip to content

Commit

Permalink
test: refactor tests (#369)
Browse files Browse the repository at this point in the history
  • Loading branch information
halildurmus committed Aug 12, 2023
1 parent f8a9cbf commit f1acaac
Show file tree
Hide file tree
Showing 39 changed files with 147 additions and 19 deletions.
2 changes: 2 additions & 0 deletions packages/windows_ai/test/collections_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,6 @@ void main() {
expect(newList.last, equals(1.5));
});
}, skip: getWindowsBuildNumber() < 18362);

tearDownAll(forceGC);
}
3 changes: 3 additions & 0 deletions packages/windows_data/test/collections_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import 'package:test/test.dart';
import 'package:win32/win32.dart';
import 'package:windows_data/windows_data.dart';
import 'package:windows_foundation/internal.dart';
import 'package:windows_foundation/windows_foundation.dart';

void main() {
Expand Down Expand Up @@ -188,4 +189,6 @@ void main() {
expect(map.lookup('key6')?.stringify(), equals('{"hello":"world"}'));
});
});

tearDownAll(forceGC);
}
3 changes: 3 additions & 0 deletions packages/windows_devices/test/collections_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import 'package:test/test.dart';
import 'package:win32/win32.dart';
import 'package:windows_devices/windows_devices.dart';
import 'package:windows_foundation/internal.dart';
import 'package:windows_foundation/windows_foundation.dart';

void main() {
Expand Down Expand Up @@ -308,4 +309,6 @@ void main() {
]));
});
});

tearDownAll(forceGC);
}
26 changes: 26 additions & 0 deletions packages/windows_foundation/lib/src/internal/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// All rights reserved. Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

import 'dart:async';
import 'dart:developer';
import 'dart:ffi';

import 'package:ffi/ffi.dart';
Expand All @@ -12,6 +14,29 @@ import '../winrt_enum.dart';
import '../winrt_struct.dart';
import 'extensions/extensions.dart';

/// Forces garbage collection through aggressive memory allocation.
///
/// Used to ensure `Finalizer`s run after all tests within a file.
///
/// Its purpose is to prevent any potential occurrences of "double free" or
/// "use after free" errors in the projection.
/// @nodoc
Future<void> forceGC({int fullGcCycles = 2}) async {
final barrier = reachabilityBarrier;

final storage = <List<int>>[];

void allocateMemory() {
storage.add(List.generate(30000, (n) => n));
if (storage.length > 100) storage.removeAt(0);
}

while (reachabilityBarrier < barrier + fullGcCycles) {
await Future<void>.delayed(Duration.zero);
allocateMemory();
}
}

/// Returns the restricted error description of the last error that occurred on
/// the current logical thread.
///
Expand Down Expand Up @@ -54,6 +79,7 @@ String? getRestrictedErrorDescription(int hresult) {
}

/// Returns the Windows build number of the current system.
/// @nodoc
int getWindowsBuildNumber() {
final key = Registry.openPath(RegistryHive.localMachine,
path: r'Software\Microsoft\Windows NT\CurrentVersion');
Expand Down
3 changes: 3 additions & 0 deletions packages/windows_foundation/test/collections/imap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import 'package:test/test.dart';
import 'package:win32/win32.dart';
import 'package:windows_foundation/internal.dart';
import 'package:windows_foundation/windows_foundation.dart';

void main() {
Expand Down Expand Up @@ -46,4 +47,6 @@ void main() {
expect(map.lookup('key3'), equals('value3'));
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,6 @@ void main() {
expect(map['key3'], equals('value3'));
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,6 @@ void main() {
]));
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import 'package:test/test.dart';
import 'package:win32/win32.dart';
import 'package:windows_foundation/internal.dart';
import 'package:windows_foundation/windows_foundation.dart';

void main() {
Expand Down Expand Up @@ -108,9 +109,10 @@ void main() {
final dateTimeListVal = map.lookup('key14');
expect(dateTimeListVal, isA<List<DateTime>>());
final dateTimeList = dateTimeListVal as List<DateTime>;
expect(dateTimeList.first.millisecondsSinceEpoch,
final [dateTime1, dateTime2] = dateTimeList;
expect(dateTime1.millisecondsSinceEpoch,
equals(DateTime(2020, 7, 11, 17, 30).millisecondsSinceEpoch));
expect(dateTimeList.last.millisecondsSinceEpoch,
expect(dateTime2.millisecondsSinceEpoch,
equals(DateTime(2022, 7, 11, 17, 30).millisecondsSinceEpoch));

expect(map.lookup('key15'), equals([2.5, 0.99]));
Expand Down Expand Up @@ -267,4 +269,6 @@ void main() {
expect(map.lookup('key24'), equals(24));
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import 'package:test/test.dart';
import 'package:win32/win32.dart';
import 'package:windows_foundation/internal.dart';
import 'package:windows_foundation/windows_foundation.dart';

void main() {
Expand Down Expand Up @@ -151,4 +152,6 @@ void main() {
expect(map.lookup('key4'), equals('value4'));
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ void main() {
final dateTimeListVal = map.lookup('key14');
expect(dateTimeListVal, isA<List<DateTime>>());
final dateTimeList = dateTimeListVal as List<DateTime>;
expect(dateTimeList.first.millisecondsSinceEpoch,
final [dateTime1, dateTime2] = dateTimeList;
expect(dateTime1.millisecondsSinceEpoch,
equals(DateTime(2020, 7, 11, 17, 30).millisecondsSinceEpoch));
expect(dateTimeList.last.millisecondsSinceEpoch,
expect(dateTime2.millisecondsSinceEpoch,
equals(DateTime(2022, 7, 11, 17, 30).millisecondsSinceEpoch));

expect(map.lookup('key15'), equals([2.5, 0.99]));
Expand Down Expand Up @@ -268,4 +269,6 @@ void main() {
expect(map.lookup('key23'), equals(23));
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import 'package:test/test.dart';
import 'package:win32/win32.dart';
import 'package:windows_foundation/internal.dart';
import 'package:windows_foundation/uri.dart' as winrt_uri;
import 'package:windows_foundation/windows_foundation.dart';

Expand Down Expand Up @@ -40,4 +41,6 @@ void main() {
expect(stringMap.tryCast(IPropertySet.fromPtr, IID_IPropertySet), isNull);
});
});

tearDownAll(forceGC);
}
7 changes: 7 additions & 0 deletions packages/windows_foundation/test/helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:ffi';

import 'package:test/test.dart';
import 'package:win32/win32.dart' hide IUnknown;
import 'package:windows_foundation/internal.dart';
import 'package:windows_foundation/windows_foundation.dart';

void main() {
Expand Down Expand Up @@ -46,6 +47,10 @@ void main() {
expect(refCount(calendar), equals(1));
});

test('forceGC', () {
expect(() async => forceGC(), returnsNormally);
});

test('getWindowHandle', () {
expect(getWindowHandle(), isZero);
});
Expand All @@ -58,4 +63,6 @@ void main() {
propertySet.release();
expect(refCount(propertySet), 1);
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ void main() {
expect(list, orderedEquals([true, false]));
free(array);
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ void main() {
free(array);
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ void main() {
expect(list, orderedEquals([1.0, 2.5]));
free(array);
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,6 @@ void main() {
free(array);
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ void main() {
list, orderedEquals([FileAttributes.normal, FileAttributes.directory]));
free(array);
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,6 @@ void main() {
expect(list, orderedEquals([guid1, guid2]));
allocator.releaseAll();
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,6 @@ void main() {
expect(list, orderedEquals([0, 9223372036854775807]));
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ void main() {
final value = pv.value;
expect(value, isA<List<Object?>>());
final list = value as List<Object?>;
expect(list.first, isA<IInspectable>());
final firstObject = list.first as IInspectable;
final [element1, element2] = list;
expect(element1, isA<IInspectable>());
final firstObject = element1 as IInspectable;
expect(getClassName(firstObject),
equals('Windows.Foundation.Collections.PropertySet'));
expect(list.last, isA<IInspectable>());
final secondObject = list.last as IInspectable;
expect(element2, isA<IInspectable>());
final secondObject = element2 as IInspectable;
expect(getClassName(secondObject),
equals('Windows.Foundation.Collections.StringMap'));
});
Expand All @@ -62,4 +63,6 @@ void main() {
expect(value, orderedEquals(['Hello', 'World']));
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ void main() {
expect(pv.type, equals(PropertyType.inspectableArray));
final list = pv.getInspectableArray();
expect(list.length, equals(2));
expect(list.first, isA<Uri>());
expect(list.first.toString(), equals('https://dartwinrt.dev/'));
expect(list.last, isNull);
final [element1, element2] = list;
expect(element1, isA<Uri>());
expect(element1.toString(), equals('https://dartwinrt.dev/'));
expect(element2, isNull);
});

test('String', () {
Expand Down Expand Up @@ -201,4 +202,6 @@ void main() {
expect(list[8], isA<Uri>());
});
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ void main() {
expect(list, orderedEquals(['d', '\x00', ' ', r'$']));
free(array);
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,6 @@ void main() {
expect(list.length, equals(2));
expect(list, orderedEquals([size1, size2]));
});

tearDownAll(forceGC);
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@ void main() {
expect(winrtUri.host, equals('dartwinrt.dev'));
});
});

tearDownAll(forceGC);
}
2 changes: 2 additions & 0 deletions packages/windows_foundation/test/internal/hstring_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,6 @@ void main() {
}
});
});

tearDownAll(forceGC);
}
2 changes: 2 additions & 0 deletions packages/windows_foundation/test/ireference_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,6 @@ void main() {
expect(reference.value, equals('Hello World'));
});
});

tearDownAll(forceGC);
}
4 changes: 4 additions & 0 deletions packages/windows_foundation/test/memorybuffer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import 'package:test/test.dart';
import 'package:win32/win32.dart';
import 'package:windows_foundation/internal.dart';
import 'package:windows_foundation/windows_foundation.dart';

void main() {
Expand All @@ -18,6 +19,7 @@ void main() {
test('create', () {
final buffer = MemoryBuffer.create(10);
expect(buffer.addRef(), equals(2));
expect(buffer.release(), equals(1));
buffer.close();
});

Expand All @@ -30,4 +32,6 @@ void main() {
buffer.close();
});
});

tearDownAll(forceGC);
}
2 changes: 2 additions & 0 deletions packages/windows_foundation/test/propertyvalue_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -398,4 +398,6 @@ void main() {
expect(pv.value, equals([10000, 20000, 30000, 40000, 50000]));
});
});

tearDownAll(forceGC);
}

0 comments on commit f1acaac

Please sign in to comment.