From d75e6cc2cadc68c52ae090e3860c6dac2b1b50cd Mon Sep 17 00:00:00 2001 From: Alex Li Date: Thu, 27 Jun 2024 18:34:26 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Add=20`gc`=20test=20group?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/tests.yml | 2 + dio/dart_test.yaml | 13 ++++ dio/test/cancel_token_test.dart | 123 +++++++++++++++++--------------- melos.yaml | 4 +- 4 files changed, 81 insertions(+), 61 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 22624d2cc..1fcccb2d3 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/dio/dart_test.yaml b/dio/dart_test.yaml index 7e313fcd6..e1e58c9e6 100644 --- a/dio/dart_test.yaml +++ b/dio/dart_test.yaml @@ -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: diff --git a/dio/test/cancel_token_test.dart b/dio/test/cancel_token_test.dart index 76b6b506f..116c4476a 100644 --- a/dio/test/cancel_token_test.dart +++ b/dio/test/cancel_token_test.dart @@ -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 = {}; - final requestsReferences = >{}; - 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.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 = {}; + final requestsReferences = >{}; + 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.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'], + ); } diff --git a/melos.yaml b/melos.yaml index d06cc2cf1..1b67afb6e 100644 --- a/melos.yaml +++ b/melos.yaml @@ -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 @@ -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