63 lines
1.6 KiB
Dart
63 lines
1.6 KiB
Dart
import 'dart:async' as async;
|
|
|
|
import 'package:flame_lua_runtime/runtime/resources/resource_load_limiter.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void main() {
|
|
group('ResourceLoadLimiter', () {
|
|
test('limits concurrent tasks', () async {
|
|
final limiter = ResourceLoadLimiter(2);
|
|
var active = 0;
|
|
var maxActive = 0;
|
|
|
|
Future<int> task(int value) async {
|
|
active++;
|
|
if (active > maxActive) {
|
|
maxActive = active;
|
|
}
|
|
await Future<void>.delayed(const Duration(milliseconds: 5));
|
|
active--;
|
|
return value;
|
|
}
|
|
|
|
final results = await Future.wait([
|
|
limiter.run(() => task(1)),
|
|
limiter.run(() => task(2)),
|
|
limiter.run(() => task(3)),
|
|
limiter.run(() => task(4)),
|
|
]);
|
|
|
|
expect(results, [1, 2, 3, 4]);
|
|
expect(maxActive, 2);
|
|
expect(limiter.activeCount, 0);
|
|
expect(limiter.pendingCount, 0);
|
|
});
|
|
|
|
test('clearPending cancels queued tasks', () async {
|
|
final limiter = ResourceLoadLimiter(1);
|
|
final hold = async.Completer<void>();
|
|
|
|
final first = limiter.run(() => hold.future);
|
|
final second = limiter.run(() async => 2);
|
|
|
|
expect(limiter.activeCount, 1);
|
|
expect(limiter.pendingCount, 1);
|
|
|
|
final secondExpectation = expectLater(
|
|
second,
|
|
throwsA(isA<ResourceLoadCancelledException>()),
|
|
);
|
|
|
|
limiter.clearPending();
|
|
hold.complete();
|
|
|
|
await first;
|
|
await secondExpectation;
|
|
});
|
|
|
|
test('rejects invalid concurrency', () {
|
|
expect(() => ResourceLoadLimiter(0), throwsArgumentError);
|
|
});
|
|
});
|
|
}
|