Skip to content

Commit 4f6fe9b

Browse files
fix: fix headers not completing when call is terminated (grpc#728)
Fixes grpc#727
1 parent c18e185 commit 4f6fe9b

File tree

5 files changed

+81
-2
lines changed

5 files changed

+81
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.0.1
2+
3+
* Fix header and trailing not completing if the call is terminated. Fixes [#727](https://github.com/grpc/grpc-dart/issues/727)
4+
15
## 4.0.0
26

37
* Set compressed flag correctly for grpc-encoding = identity. Fixes [#669](https://github.com/grpc/grpc-dart/issues/669) (https://github.com/grpc/grpc-dart/pull/693)

lib/src/client/call.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,12 @@ class ClientCall<Q, R> implements Response {
483483
if (_responseSubscription != null) {
484484
futures.add(_responseSubscription!.cancel());
485485
}
486+
if (!_headers.isCompleted) {
487+
_headers.complete({});
488+
}
489+
if (!_trailers.isCompleted) {
490+
_trailers.complete({});
491+
}
486492
await Future.wait(futures);
487493
}
488494

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: grpc
22
description: Dart implementation of gRPC, a high performance, open-source universal RPC framework.
3-
version: 4.0.0
3+
version: 4.0.1
44

55
repository: https://github.com/grpc/grpc-dart
66

test/client_tests/call_test.dart

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,26 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
import 'package:grpc/grpc.dart';
1617
import 'package:grpc/src/client/call.dart';
1718
import 'package:test/test.dart';
1819

20+
import '../src/client_utils.dart';
21+
1922
void main() {
23+
const dummyValue = 0;
24+
const cancelDurationMillis = 300;
25+
26+
late ClientHarness harness;
27+
28+
setUp(() {
29+
harness = ClientHarness()..setUp();
30+
});
31+
32+
tearDown(() {
33+
harness.tearDown();
34+
});
35+
2036
test('WebCallOptions mergeWith CallOptions returns WebCallOptions', () {
2137
final options =
2238
WebCallOptions(bypassCorsPreflight: true, withCredentials: true);
@@ -28,4 +44,57 @@ void main() {
2844
expect(mergedOptions.bypassCorsPreflight, true);
2945
expect(mergedOptions.withCredentials, true);
3046
});
47+
48+
test(
49+
'Cancelling a call correctly complete headers future',
50+
() async {
51+
final clientCall = harness.client.unary(dummyValue);
52+
53+
Future.delayed(
54+
Duration(milliseconds: cancelDurationMillis),
55+
).then((_) => clientCall.cancel());
56+
57+
expect(await clientCall.headers, isEmpty);
58+
59+
await expectLater(
60+
clientCall,
61+
throwsA(
62+
isA<GrpcError>().having(
63+
(e) => e.codeName,
64+
'Test codename',
65+
contains('CANCELLED'),
66+
),
67+
),
68+
);
69+
},
70+
);
71+
72+
test(
73+
'Cancelling a call correctly complete trailers futures',
74+
() async {
75+
final clientCall = harness.client.unary(dummyValue);
76+
77+
Future.delayed(
78+
Duration(milliseconds: cancelDurationMillis),
79+
).then((_) {
80+
clientCall.cancel();
81+
});
82+
83+
expect(
84+
await clientCall.trailers,
85+
isEmpty,
86+
);
87+
88+
await expectLater(
89+
clientCall,
90+
throwsA(
91+
isA<GrpcError>().having(
92+
(e) => e.codeName,
93+
'Test codename',
94+
contains('CANCELLED'),
95+
),
96+
),
97+
);
98+
},
99+
);
31100
}

test/keepalive_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ void main() {
4949
services: [FakeEchoService()],
5050
keepAliveOptions: serverOptions,
5151
);
52-
await server.serve(address: 'localhost', port: 8081);
52+
await server.serve(address: 'localhost', port: 0);
5353
fakeChannel = FakeClientChannel(
5454
'localhost',
5555
port: server.port!,

0 commit comments

Comments
 (0)