Skip to content

Commit

Permalink
⚡️ Improve multiple timeout tests (#2142)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
AlexV525 authored Mar 18, 2024
1 parent b261a7b commit 797145a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
5 changes: 3 additions & 2 deletions dio/test/stacktrace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion dio/test/timeout_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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,
);
Expand Down
24 changes: 17 additions & 7 deletions dio_test/lib/src/test/download_stream_tests.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';

import 'package:dio/dio.dart';
Expand Down Expand Up @@ -49,16 +50,24 @@ void downloadStreamTests(
test('cancels request', () async {
final cancelToken = CancelToken();

Future.delayed(const Duration(milliseconds: 10), () {
final res = await dio.get<ResponseBody>(
'/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',
Expand All @@ -76,8 +85,9 @@ void downloadStreamTests(

await expectLater(
dio.download(
'/bytes/5000',
'/drip',
path,
queryParameters: {'duration': '5', 'delay': '0'},
cancelToken: cancelToken,
),
throwsDioException(
Expand Down
8 changes: 5 additions & 3 deletions dio_test/lib/src/test/timeout_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand All @@ -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(),
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions dio_test/lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit 797145a

Please sign in to comment.