Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/src/client/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ class ResponseFuture<R> extends DelegatingFuture<R>
: super(_call.response
.fold<R?>(null, _ensureOnlyOneResponse)
.then(_ensureOneResponse));

ResponseFuture._next(this._call, Future<R> future) : super(future);

ResponseFuture<R> responseThen(FutureOr<R> Function(R p1) onValue,
{Function? onError}) {
return ResponseFuture._next(_call, then(onValue, onError: onError));
}

ResponseFuture<R> responseCatchError(Function onError,
{bool Function(Object error)? test}) {
return ResponseFuture._next(_call, catchError(onError, test: test));
}

ResponseFuture<R> responseWhenComplete(FutureOr Function() action) {
return ResponseFuture._next(_call, whenComplete(action));
}

ResponseFuture<R> responseTimeout(Duration timeLimit,
{FutureOr<R> Function()? onTimeout}) {
return ResponseFuture._next(
_call, timeout(timeLimit, onTimeout: onTimeout));
}
}

/// A gRPC response producing a stream of values.
Expand All @@ -75,6 +97,15 @@ class ResponseStream<R> extends DelegatingStream<R>

@override
ResponseFuture<R> get single => ResponseFuture(_call);

ResponseStream._next(this._call, Stream<R> future) : super(future);

ResponseStream<S> responseTransform<S>(StreamTransformer<R, S> transformer) {
return ResponseStream<S>._next(
_call as dynamic,
transform(transformer),
);
}
}

abstract class _ResponseMixin<Q, R> implements Response {
Expand Down
86 changes: 86 additions & 0 deletions test/client_tests/response_future_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'package:grpc/grpc.dart';
import 'package:grpc/src/client/common.dart';
import 'package:mockito/mockito.dart';
import 'package:test/test.dart';

import '../src/client_utils.dart';

class FakeClientCall<T> extends Fake implements ClientCall<dynamic, T> {
final T? _response;
final GrpcError? _error;

FakeClientCall(this._response, this._error);

factory FakeClientCall.value(T response) => FakeClientCall(response, null);

factory FakeClientCall.error(GrpcError error) => FakeClientCall(null, error);

@override
Stream<T> get response =>
_error != null ? Stream.error(_error!) : Stream.value(_response!);
}

void main() {
test(
'Returns a new [ResponseFuture] which is completed with the result of the call to `onValue`',
() async {
final response = 1;
final responseFuture = ResponseFuture(FakeClientCall.value(response));

expect(await responseFuture, response);
expect(await responseFuture.responseThen((p1) => p1), response);
expect(responseFuture.responseThen((p1) => p1), isA<ResponseFuture>());

expect(await responseFuture.responseThen((p1) => p1 * 2), response * 2);
expect(
await responseFuture
.responseThen((p1) => p1 * 2)
.responseThen((p2) => p2 * 2),
response * 2 * 2);

expect(responseFuture.responseThen((p1) => p1), isA<ResponseFuture>());
expect(responseFuture.then((p1) => '2'), isA<Future>());

expect(await responseFuture.responseThen((p1) => p1), response);
expect(await responseFuture.then((p1) => '2'), '2');
});

test('Support Future after ResponseFuture', () async {
final response = 1;
final responseFuture = ResponseFuture<int>(FakeClientCall.value(response));

final future = Future<String>.value('1');
expect(responseFuture.then((value) => future), isA<Future<String>>());
expect(await responseFuture.then((value) => future), '1');
});

test('Support ResponseFuture after ResponseFuture', () async {
final response1 = 1;
final response2 = 2;

final responseFuture1 =
ResponseFuture<int>(FakeClientCall.value(response1));
final responseFuture2 =
ResponseFuture<int>(FakeClientCall.value(response2));

expect(responseFuture1.responseThen((value) => responseFuture2),
isA<ResponseFuture<int>>());
expect(await responseFuture1, response1);
expect(await responseFuture1.responseThen((value) => responseFuture2),
response2);
});

test(
'Returns a new [ResponseFuture] that will be completed with the result of calling the `onError` callback.',
() async {
final response = 1;
final responseFuture =
ResponseFuture<int>(FakeClientCall.error(GrpcError.unknown()));

final responseFutureCatchError =
responseFuture.responseCatchError((error) => response * 2);

expect(responseFutureCatchError, isA<ResponseFuture<int>>());
expect(await responseFutureCatchError, response * 2);
});
}