Skip to content

Commit

Permalink
✅ Add gc test group
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Jun 27, 2024
1 parent 8f94c33 commit d75e6cc
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 61 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ jobs:
cache: true
flutter-version: ${{ matrix.sdk == 'min' && '3.3.0' || '' }}
channel: ${{ matrix.sdk == 'min' && '' || matrix.channel }}
- run: |
echo TARGET_DART_SDK=${{ matrix.sdk }} >> $GITHUB_ENV
- name: Prepare dependencies for the project management
run: dart pub get
- uses: bluefireteam/melos-action@v3
Expand Down
13 changes: 13 additions & 0 deletions dio/dart_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ tags:
skip: false
default:
skip: true
gc:
skip: "Skipping Garbage Collection with old Dart SDKs."
presets:
all:
skip: true
default:
skip: false
min:
skip: true
stable:
skip: false
beta:
skip: false

override_platforms:
chrome:
Expand Down
123 changes: 64 additions & 59 deletions dio/test/cancel_token_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,65 +115,70 @@ void main() {
});
});

test('deallocates HttpClientRequest', () async {
final client = MockHttpClient();
final dio = Dio();
dio.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () => client,
);
final token = CancelToken();
final requests = <MockHttpClientRequest>{};
final requestsReferences = <WeakReference<MockHttpClientRequest>>{};
when(client.openUrl(any, any)).thenAnswer((_) async {
final request = MockHttpClientRequest();
requests.add(request);
requestsReferences.add(WeakReference(request));
when(request.close()).thenAnswer((_) async {
final response = MockHttpClientResponse();
when(response.headers).thenReturn(MockHttpHeaders());
when(response.statusCode).thenReturn(200);
when(response.reasonPhrase).thenReturn('OK');
when(response.isRedirect).thenReturn(false);
when(response.redirects).thenReturn([]);
when(response.cast())
.thenAnswer((_) => const Stream<Uint8List>.empty());
await Future.delayed(const Duration(milliseconds: 200));
return response;
});
when(request.abort()).thenAnswer((realInvocation) {
requests.remove(request);
test(
'deallocates HttpClientRequest',
() async {
final client = MockHttpClient();
final dio = Dio();
dio.httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () => client,
);
final token = CancelToken();
final requests = <MockHttpClientRequest>{};
final requestsReferences = <WeakReference<MockHttpClientRequest>>{};
when(client.openUrl(any, any)).thenAnswer((_) async {
final request = MockHttpClientRequest();
requests.add(request);
requestsReferences.add(WeakReference(request));
when(request.close()).thenAnswer((_) async {
final response = MockHttpClientResponse();
when(response.headers).thenReturn(MockHttpHeaders());
when(response.statusCode).thenReturn(200);
when(response.reasonPhrase).thenReturn('OK');
when(response.isRedirect).thenReturn(false);
when(response.redirects).thenReturn([]);
when(response.cast())
.thenAnswer((_) => const Stream<Uint8List>.empty());
await Future.delayed(const Duration(milliseconds: 200));
return response;
});
when(request.abort()).thenAnswer((realInvocation) {
requests.remove(request);
});
return request;
});
return request;
});

final futures = [
dio.get('https://does.not.exists', cancelToken: token),
dio.get('https://does.not.exists', cancelToken: token),
];
for (final future in futures) {
expectLater(
future,
throwsDioException(DioExceptionType.cancel),
);
}

// Opening requests.
await Future.delayed(const Duration(milliseconds: 100));
token.cancel();
// Aborting requests.
await Future.delayed(const Duration(seconds: 1));
expect(requests, isEmpty);

try {
await Future.wait(futures);
} catch (_) {
// Waiting here until all futures are completed.
}
expect(requests, isEmpty);
expect(requestsReferences, hasLength(2));

// GC.
List.generate(4 * 1024 * 1024, (index) => Object());
expect(requestsReferences.every((e) => e.target == null), isTrue);
});
final futures = [
dio.get('https://does.not.exists', cancelToken: token),
dio.get('https://does.not.exists', cancelToken: token),
];
for (final future in futures) {
expectLater(
future,
throwsDioException(DioExceptionType.cancel),
);
}

// Opening requests.
await Future.delayed(const Duration(milliseconds: 100));
token.cancel();
// Aborting requests.
await Future.delayed(const Duration(seconds: 1));
expect(requests, isEmpty);

try {
await Future.wait(futures);
} catch (_) {
// Waiting here until all futures are completed.
}
expect(requests, isEmpty);
expect(requestsReferences, hasLength(2));

// GC.
List.generate(4 * 1024 * 1024, (index) => Object());
await Future.delayed(const Duration(seconds: 1));
expect(requestsReferences.every((e) => e.target == null), isTrue);
},
tags: ['gc'],
);
}
4 changes: 2 additions & 2 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ scripts:
melos run test:flutter
test:vm:
name: Dart VM tests
exec: dart test --preset ${TEST_PRESET:-default} --coverage coverage/vm --chain-stack-traces
exec: dart test --preset=${TEST_PRESET:-default},${TARGET_DART_SDK:-stable} --coverage=coverage/vm --chain-stack-traces
packageFilters:
flutter: false
dirExists: test
Expand All @@ -81,7 +81,7 @@ scripts:
TEST_PLATFORM: firefox
test:web:single:
name: Dart Web tests in a browser
exec: dart test --platform ${TEST_PLATFORM} --coverage coverage/${TEST_PLATFORM} --preset ${TEST_PRESET:-default} --chain-stack-traces
exec: dart test --platform ${TEST_PLATFORM} --coverage=coverage/${TEST_PLATFORM} --preset=${TEST_PRESET:-default},${TARGET_DART_SDK:-stable} --chain-stack-traces
packageFilters:
flutter: false
dirExists: test
Expand Down

0 comments on commit d75e6cc

Please sign in to comment.