Skip to content

Commit

Permalink
feat(http2):add sendTimeout and receiveTimeout in Http2Adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
sunhapper committed Aug 19, 2023
1 parent 3ad3947 commit bbb3394
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
2 changes: 1 addition & 1 deletion plugins/http2_adapter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

*None.*
- Add send timeout and receive timeout

## 2.3.1+1

Expand Down
30 changes: 27 additions & 3 deletions plugins/http2_adapter/lib/src/http2_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,21 @@ class Http2Adapter implements HttpClientAdapter {
}

if (hasRequestData) {
await requestStream!.listen((data) {
final requestStreamFuture = requestStream!.listen((data) {
stream.outgoingMessages.add(DataStreamMessage(data));
}).asFuture();
final sendTimeout = options.sendTimeout;
if (sendTimeout != null) {
await requestStreamFuture.timeout(sendTimeout, onTimeout: () {
throw DioException.sendTimeout(
timeout: sendTimeout,
requestOptions: options,
);
});
} else {
await requestStreamFuture;
}
}

await stream.outgoingMessages.close();

final sc = StreamController<Uint8List>();
Expand Down Expand Up @@ -145,7 +155,21 @@ class Http2Adapter implements HttpClientAdapter {
cancelOnError: true,
);

await completer.future;
final receiveTimeout = options.receiveTimeout;
if (receiveTimeout != null) {
await completer.future.timeout(
receiveTimeout,
onTimeout: () {
subscription.cancel().whenComplete(() => sc.close());
throw DioException.receiveTimeout(
timeout: receiveTimeout,
requestOptions: options,
);
},
);
} else {
await completer.future;
}

// Handle redirection
if (needRedirect) {
Expand Down
39 changes: 39 additions & 0 deletions plugins/http2_adapter/test/http2_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:dio_http2_adapter/dio_http2_adapter.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -76,4 +79,40 @@ void main() {
final res = await dio.post('post', data: 'TEST');
expect(res.data.toString(), contains('TEST'));
});


test('catch DioException when receiveTimeout', () {
final dio = Dio()
..options.baseUrl = 'https://httpbun.com/'
..httpClientAdapter = Http2Adapter(
ConnectionManager(
idleTimeout: Duration(milliseconds: 10),
),
);
dio.options.receiveTimeout = Duration(seconds: 5);

expectLater(
dio.get('/drip?delay=10&numbytes=1'),
allOf([
throwsA(isA<DioException>()),
throwsA(predicate(
(DioException e) => e.type == DioExceptionType.receiveTimeout)),
throwsA(predicate(
(DioException e) => e.message!.contains('0:00:05.000000'))),
]),
);
}, testOn: 'vm');

test('no DioException when receiveTimeout > request duration', () async {
final dio = Dio()
..options.baseUrl = 'https://httpbun.com/'
..httpClientAdapter = Http2Adapter(
ConnectionManager(
idleTimeout: Duration(milliseconds: 10),
),
);
dio.options.receiveTimeout = Duration(seconds: 5);

await dio.get('/drip?delay=1&numbytes=1');
});
}
Binary file added plugins/http2_adapter/test/mock/flutter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bbb3394

Please sign in to comment.