From 797145a00e7d3cfee628ea2fe8306686016af56f Mon Sep 17 00:00:00 2001 From: Alex Li Date: Tue, 19 Mar 2024 01:21:52 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Improve=20multiple=20timeo?= =?UTF-8?q?ut=20tests=20(#2142)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Further try #2140, using `drip` to control the overall response duration. - Make cancellation calls only after the response stream has been obtained to keep the call sequence. - Using the non-routable URL (10.0.0.0) to mock connection timeouts. ### New Pull Request Checklist - [x] I have read the [Documentation](https://pub.dev/documentation/dio/latest/) - [x] I have searched for a similar pull request in the [project](https://github.com/cfug/dio/pulls) and found none - [x] I have updated this branch with the latest `main` branch to avoid conflicts (via merge from master or rebase) - [x] I have added the required tests to prove the fix/feature I'm adding - [ ] I have updated the documentation (if necessary) - [x] I have run the tests without failures - [ ] I have updated the `CHANGELOG.md` in the corresponding package --- dio/test/stacktrace_test.dart | 5 ++-- dio/test/timeout_test.dart | 3 ++- .../lib/src/test/download_stream_tests.dart | 24 +++++++++++++------ dio_test/lib/src/test/timeout_tests.dart | 8 ++++--- dio_test/lib/src/utils.dart | 2 ++ 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/dio/test/stacktrace_test.dart b/dio/test/stacktrace_test.dart index 559c7fabc..4a5f209d4 100644 --- a/dio/test/stacktrace_test.dart +++ b/dio/test/stacktrace_test.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:dio/io.dart'; +import 'package:dio_test/util.dart'; import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; @@ -60,8 +61,8 @@ void main() async { await HttpOverrides.runWithHttpOverrides(() async { final timeout = Duration(milliseconds: 10); final dio = Dio() - ..options.connectTimeout = timeout - ..options.baseUrl = 'https://does.not.exist'; + ..options.baseUrl = nonRoutableUrl + ..options.connectTimeout = timeout; when(httpClientMock.openUrl('GET', any)).thenAnswer( (_) async { diff --git a/dio/test/timeout_test.dart b/dio/test/timeout_test.dart index bb9e7e8e5..ed4747179 100644 --- a/dio/test/timeout_test.dart +++ b/dio/test/timeout_test.dart @@ -2,6 +2,7 @@ import 'dart:io'; import 'package:dio/dio.dart'; import 'package:dio/io.dart'; +import 'package:dio_test/util.dart'; import 'package:test/test.dart'; void main() { @@ -17,7 +18,7 @@ void main() { test('update between calls', () async { final client = HttpClient(); final dio = Dio() - ..options.baseUrl = 'https://httpbun.com' + ..options.baseUrl = nonRoutableUrl ..httpClientAdapter = IOHttpClientAdapter( createHttpClient: () => client, ); diff --git a/dio_test/lib/src/test/download_stream_tests.dart b/dio_test/lib/src/test/download_stream_tests.dart index e6cb1dcba..92a03eada 100644 --- a/dio_test/lib/src/test/download_stream_tests.dart +++ b/dio_test/lib/src/test/download_stream_tests.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:io'; import 'package:dio/dio.dart'; @@ -49,16 +50,24 @@ void downloadStreamTests( test('cancels request', () async { final cancelToken = CancelToken(); - Future.delayed(const Duration(milliseconds: 10), () { + final res = await dio.get( + '/drip', + queryParameters: {'duration': '5', 'delay': '0'}, + options: Options(responseType: ResponseType.stream), + cancelToken: cancelToken, + ); + + Future.delayed(const Duration(seconds: 2), () { cancelToken.cancel(); }); + final completer = Completer(); + res.data!.stream.listen((event) {}, onError: (e, s) { + completer.completeError(e, s); + }); + await expectLater( - dio.get( - '/bytes/10000', - options: Options(responseType: ResponseType.stream), - cancelToken: cancelToken, - ), + completer.future, throwsDioException( DioExceptionType.cancel, stackTraceContains: 'test/download_stream_tests.dart', @@ -76,8 +85,9 @@ void downloadStreamTests( await expectLater( dio.download( - '/bytes/5000', + '/drip', path, + queryParameters: {'duration': '5', 'delay': '0'}, cancelToken: cancelToken, ), throwsDioException( diff --git a/dio_test/lib/src/test/timeout_tests.dart b/dio_test/lib/src/test/timeout_tests.dart index 95e531eda..7bdd86166 100644 --- a/dio_test/lib/src/test/timeout_tests.dart +++ b/dio_test/lib/src/test/timeout_tests.dart @@ -4,6 +4,8 @@ import 'package:dio/dio.dart'; import 'package:dio_test/src/matcher.dart'; import 'package:test/test.dart'; +import '../utils.dart'; + void timeoutTests( Dio Function() create, ) { @@ -15,10 +17,10 @@ void timeoutTests( group('Timeout exception of', () { group('connectTimeout', () { - test('with response', () async { + test('throws', () async { dio.options.connectTimeout = Duration(milliseconds: 3); await expectLater( - dio.get('/'), + dio.get(nonRoutableUrl), throwsDioException( DioExceptionType.connectionTimeout, messageContains: dio.options.connectTimeout.toString(), @@ -98,7 +100,7 @@ void timeoutTests( ); dio.options.connectTimeout = Duration(milliseconds: 10); await expectLater( - dio.get('/drip-lines?delay=1'), + dio.get(nonRoutableUrl), throwsDioException( DioExceptionType.connectionTimeout, messageContains: '0:00:00.010000', diff --git a/dio_test/lib/src/utils.dart b/dio_test/lib/src/utils.dart index 93e7bdc8c..d47de1547 100644 --- a/dio_test/lib/src/utils.dart +++ b/dio_test/lib/src/utils.dart @@ -1,3 +1,5 @@ const kIsWeb = bool.hasEnvironment('dart.library.js_util') ? bool.fromEnvironment('dart.library.js_util') : identical(0, 0.0); + +const nonRoutableUrl = 'http://10.0.0.0';