Update Deps & Refactor

This commit is contained in:
Tsavo Knott 2024-04-12 14:42:22 -04:00
parent a2981c4f1d
commit 25ea43a435
3 changed files with 94 additions and 46 deletions

View File

@ -68,14 +68,19 @@ Future<int> main(List<String> args) async {
help: 'The server host to claim to be connecting to, for use in TLS and ' help: 'The server host to claim to be connecting to, for use in TLS and '
'HTTP/2 :authority header. If unspecified, the value of ' 'HTTP/2 :authority header. If unspecified, the value of '
'--server_host will be used.'); '--server_host will be used.');
argumentParser.addOption(_serverPortArgument, help: 'The server port to connect to. For example, "8080".'); argumentParser.addOption(_serverPortArgument,
help: 'The server port to connect to. For example, "8080".');
argumentParser.addOption(_testCaseArgument, argumentParser.addOption(_testCaseArgument,
help: 'The name of the test case to execute. For example, "empty_unary".'); help:
'The name of the test case to execute. For example, "empty_unary".');
argumentParser.addOption(_useTLSArgument, argumentParser.addOption(_useTLSArgument,
defaultsTo: 'false', help: 'Whether to use a plaintext or encrypted connection.'); defaultsTo: 'false',
help: 'Whether to use a plaintext or encrypted connection.');
argumentParser.addOption(_useTestCAArgument, argumentParser.addOption(_useTestCAArgument,
defaultsTo: 'false', help: 'Whether to replace platform root CAs with ca.pem as the CA root.'); defaultsTo: 'false',
argumentParser.addOption(_defaultServiceAccountArgument, help: 'Email of the GCE default service account.'); help: 'Whether to replace platform root CAs with ca.pem as the CA root.');
argumentParser.addOption(_defaultServiceAccountArgument,
help: 'Email of the GCE default service account.');
argumentParser.addOption(_oauthScopeArgument, argumentParser.addOption(_oauthScopeArgument,
help: 'OAuth scope. For example, ' help: 'OAuth scope. For example, '
'"https://www.googleapis.com/auth/xapi.zoo".'); '"https://www.googleapis.com/auth/xapi.zoo".');
@ -87,11 +92,14 @@ Future<int> main(List<String> args) async {
late Tester testClient; late Tester testClient;
try { try {
testClient = Tester( testClient = Tester(
serverHost: arguments[_serverHostArgument] ?? (throw 'Must specify --$_serverHostArgument'), serverHost: arguments[_serverHostArgument] ??
(throw 'Must specify --$_serverHostArgument'),
serverHostOverride: arguments[_serverHostOverrideArgument], serverHostOverride: arguments[_serverHostOverrideArgument],
serverPort: int.tryParse(arguments[_serverPortArgument] ?? (throw 'Must specify --$_serverPortArgument')) ?? serverPort: int.tryParse(arguments[_serverPortArgument] ??
(throw 'Must specify --$_serverPortArgument')) ??
(throw 'Invalid port "${arguments[_serverPortArgument]}"'), (throw 'Invalid port "${arguments[_serverPortArgument]}"'),
testCase: arguments[_testCaseArgument] ?? (throw 'Must specify --$_testCaseArgument'), testCase: arguments[_testCaseArgument] ??
(throw 'Must specify --$_testCaseArgument'),
useTls: arguments[_useTLSArgument] == 'true', useTls: arguments[_useTLSArgument] == 'true',
useTestCA: arguments[_useTestCAArgument] == 'true', useTestCA: arguments[_useTestCAArgument] == 'true',
defaultServiceAccount: arguments[_defaultServiceAccountArgument], defaultServiceAccount: arguments[_defaultServiceAccountArgument],

View File

@ -110,14 +110,16 @@ void main() {
test('Server returns error on unimplemented path', () async { test('Server returns error on unimplemented path', () async {
harness harness
..expectErrorResponse(StatusCode.unimplemented, 'Path /Test/NotFound not found') ..expectErrorResponse(
StatusCode.unimplemented, 'Path /Test/NotFound not found')
..sendRequestHeader('/Test/NotFound'); ..sendRequestHeader('/Test/NotFound');
await harness.fromServer.done; await harness.fromServer.done;
}); });
/// Returns a service method handler that verifies that awaiting the request /// Returns a service method handler that verifies that awaiting the request
/// throws a specific error. /// throws a specific error.
Future<int> Function(ServiceCall call, Future<int> request) expectError(expectedError) { Future<int> Function(ServiceCall call, Future<int> request) expectError(
expectedError) {
return expectAsync2((ServiceCall call, Future<int> request) async { return expectAsync2((ServiceCall call, Future<int> request) async {
try { try {
final result = await request; final result = await request;
@ -136,7 +138,8 @@ void main() {
/// Returns a service method handler that verifies that awaiting the request /// Returns a service method handler that verifies that awaiting the request
/// stream throws a specific error. /// stream throws a specific error.
Stream<int> Function(ServiceCall call, Stream<int> request) expectErrorStreaming(expectedError) { Stream<int> Function(ServiceCall call, Stream<int> request)
expectErrorStreaming(expectedError) {
return (ServiceCall call, Stream<int> request) async* { return (ServiceCall call, Stream<int> request) async* {
try { try {
await for (var entry in request) { await for (var entry in request) {
@ -156,16 +159,19 @@ void main() {
test('Server returns error on missing request for unary call', () async { test('Server returns error on missing request for unary call', () async {
harness harness
..service.unaryHandler = expectError(GrpcError.unimplemented('No request received')) ..service.unaryHandler =
expectError(GrpcError.unimplemented('No request received'))
..expectErrorResponse(StatusCode.unimplemented, 'No request received') ..expectErrorResponse(StatusCode.unimplemented, 'No request received')
..sendRequestHeader('/Test/Unary') ..sendRequestHeader('/Test/Unary')
..toServer.close(); ..toServer.close();
await harness.fromServer.done; await harness.fromServer.done;
}); });
test('Server returns error if multiple headers are received for unary call', () async { test('Server returns error if multiple headers are received for unary call',
() async {
harness harness
..service.unaryHandler = expectError(GrpcError.unimplemented('Expected request')) ..service.unaryHandler =
expectError(GrpcError.unimplemented('Expected request'))
..expectErrorResponse(StatusCode.unimplemented, 'Expected request') ..expectErrorResponse(StatusCode.unimplemented, 'Expected request')
..sendRequestHeader('/Test/Unary') ..sendRequestHeader('/Test/Unary')
..toServer.add(HeadersStreamMessage([])) ..toServer.add(HeadersStreamMessage([]))
@ -175,7 +181,8 @@ void main() {
test('Server returns error on too many requests for unary call', () async { test('Server returns error on too many requests for unary call', () async {
harness harness
..service.unaryHandler = expectError(GrpcError.unimplemented('Too many requests')) ..service.unaryHandler =
expectError(GrpcError.unimplemented('Too many requests'))
..expectErrorResponse(StatusCode.unimplemented, 'Too many requests') ..expectErrorResponse(StatusCode.unimplemented, 'Too many requests')
..sendRequestHeader('/Test/Unary') ..sendRequestHeader('/Test/Unary')
..sendData(dummyValue) ..sendData(dummyValue)
@ -186,8 +193,10 @@ void main() {
test('Server returns request deserialization errors', () async { test('Server returns request deserialization errors', () async {
harness harness
..service.bidirectionalHandler = expectErrorStreaming(GrpcError.internal('Error deserializing request: Failed')) ..service.bidirectionalHandler = expectErrorStreaming(
..expectErrorResponse(StatusCode.internal, 'Error deserializing request: Failed') GrpcError.internal('Error deserializing request: Failed'))
..expectErrorResponse(
StatusCode.internal, 'Error deserializing request: Failed')
..sendRequestHeader('/Test/RequestError') ..sendRequestHeader('/Test/RequestError')
..sendData(dummyValue) ..sendData(dummyValue)
..toServer.close(); ..toServer.close();
@ -196,8 +205,10 @@ void main() {
test('Server returns response serialization errors', () async { test('Server returns response serialization errors', () async {
harness harness
..service.bidirectionalHandler = expectErrorStreaming(GrpcError.internal('Error sending response: Failed')) ..service.bidirectionalHandler = expectErrorStreaming(
..expectErrorResponse(StatusCode.internal, 'Error sending response: Failed') GrpcError.internal('Error sending response: Failed'))
..expectErrorResponse(
StatusCode.internal, 'Error sending response: Failed')
..sendRequestHeader('/Test/ResponseError') ..sendRequestHeader('/Test/ResponseError')
..sendData(dummyValue) ..sendData(dummyValue)
..sendData(dummyValue) ..sendData(dummyValue)
@ -258,9 +269,12 @@ void main() {
await harness.fromServer.done; await harness.fromServer.done;
}); });
test('Server returns error if request stream is closed before sending anything', () async { test(
'Server returns error if request stream is closed before sending anything',
() async {
harness harness
..expectErrorResponse(StatusCode.unavailable, 'Request stream closed unexpectedly') ..expectErrorResponse(
StatusCode.unavailable, 'Request stream closed unexpectedly')
..toServer.close(); ..toServer.close();
await harness.fromServer.done; await harness.fromServer.done;
}); });
@ -291,7 +305,8 @@ void main() {
} }
test('with sync interceptor', () => doTest(interceptor)); test('with sync interceptor', () => doTest(interceptor));
test('with async interceptor', () => doTest((call, method) async => interceptor(call, method))); test('with async interceptor',
() => doTest((call, method) async => interceptor(call, method)));
}); });
group('returns error if interceptor blocks request', () { group('returns error if interceptor blocks request', () {
@ -305,14 +320,16 @@ void main() {
Future<void> doTest(Interceptor handler) async { Future<void> doTest(Interceptor handler) async {
harness harness
..interceptor.handler = handler ..interceptor.handler = handler
..expectErrorResponse(StatusCode.unauthenticated, 'Request is unauthenticated') ..expectErrorResponse(
StatusCode.unauthenticated, 'Request is unauthenticated')
..sendRequestHeader('/Test/Unary'); ..sendRequestHeader('/Test/Unary');
await harness.fromServer.done; await harness.fromServer.done;
} }
test('with sync interceptor', () => doTest(interceptor)); test('with sync interceptor', () => doTest(interceptor));
test('with async interceptor', () => doTest((call, method) async => interceptor(call, method))); test('with async interceptor',
() => doTest((call, method) async => interceptor(call, method)));
}); });
group('returns internal error if interceptor throws exception', () { group('returns internal error if interceptor throws exception', () {
@ -323,14 +340,16 @@ void main() {
Future<void> doTest(Interceptor handler) async { Future<void> doTest(Interceptor handler) async {
harness harness
..interceptor.handler = handler ..interceptor.handler = handler
..expectErrorResponse(StatusCode.internal, 'Exception: Reason is unknown') ..expectErrorResponse(
StatusCode.internal, 'Exception: Reason is unknown')
..sendRequestHeader('/Test/Unary'); ..sendRequestHeader('/Test/Unary');
await harness.fromServer.done; await harness.fromServer.done;
} }
test('with sync interceptor', () => doTest(interceptor)); test('with sync interceptor', () => doTest(interceptor));
test('with async interceptor', () => doTest((call, method) async => interceptor(call, method))); test('with async interceptor',
() => doTest((call, method) async => interceptor(call, method)));
}); });
test("don't fail if interceptor await 2 times", () async { test("don't fail if interceptor await 2 times", () async {
@ -342,7 +361,8 @@ void main() {
harness harness
..interceptor.handler = interceptor ..interceptor.handler = interceptor
..expectErrorResponse(StatusCode.internal, 'Exception: Reason is unknown') ..expectErrorResponse(
StatusCode.internal, 'Exception: Reason is unknown')
..sendRequestHeader('/Test/Unary') ..sendRequestHeader('/Test/Unary')
..sendData(1); ..sendData(1);

View File

@ -110,14 +110,16 @@ void main() {
test('Server returns error on unimplemented path', () async { test('Server returns error on unimplemented path', () async {
harness harness
..expectErrorResponse(StatusCode.unimplemented, 'Path /Test/NotFound not found') ..expectErrorResponse(
StatusCode.unimplemented, 'Path /Test/NotFound not found')
..sendRequestHeader('/Test/NotFound'); ..sendRequestHeader('/Test/NotFound');
await harness.fromServer.done; await harness.fromServer.done;
}); });
/// Returns a service method handler that verifies that awaiting the request /// Returns a service method handler that verifies that awaiting the request
/// throws a specific error. /// throws a specific error.
Future<int> Function(ServiceCall call, Future<int> request) expectError(expectedError) { Future<int> Function(ServiceCall call, Future<int> request) expectError(
expectedError) {
return expectAsync2((ServiceCall call, Future<int> request) async { return expectAsync2((ServiceCall call, Future<int> request) async {
try { try {
final result = await request; final result = await request;
@ -136,7 +138,8 @@ void main() {
/// Returns a service method handler that verifies that awaiting the request /// Returns a service method handler that verifies that awaiting the request
/// stream throws a specific error. /// stream throws a specific error.
Stream<int> Function(ServiceCall call, Stream<int> request) expectErrorStreaming(expectedError) { Stream<int> Function(ServiceCall call, Stream<int> request)
expectErrorStreaming(expectedError) {
return (ServiceCall call, Stream<int> request) async* { return (ServiceCall call, Stream<int> request) async* {
try { try {
await for (var entry in request) { await for (var entry in request) {
@ -156,7 +159,8 @@ void main() {
test('Server returns error on missing request for unary call', () async { test('Server returns error on missing request for unary call', () async {
harness harness
..service.unaryHandler = expectError(GrpcError.unimplemented('No request received')) ..service.unaryHandler =
expectError(GrpcError.unimplemented('No request received'))
..expectErrorResponse(StatusCode.unimplemented, 'No request received') ..expectErrorResponse(StatusCode.unimplemented, 'No request received')
..sendRequestHeader('/Test/Unary') ..sendRequestHeader('/Test/Unary')
..toServer.close(); ..toServer.close();
@ -177,9 +181,11 @@ void main() {
await harness.fromServer.done; await harness.fromServer.done;
}); });
test('Server returns error if multiple headers are received for unary call', () async { test('Server returns error if multiple headers are received for unary call',
() async {
harness harness
..service.unaryHandler = expectError(GrpcError.unimplemented('Expected request')) ..service.unaryHandler =
expectError(GrpcError.unimplemented('Expected request'))
..expectErrorResponse(StatusCode.unimplemented, 'Expected request') ..expectErrorResponse(StatusCode.unimplemented, 'Expected request')
..sendRequestHeader('/Test/Unary') ..sendRequestHeader('/Test/Unary')
..toServer.add(HeadersStreamMessage([])) ..toServer.add(HeadersStreamMessage([]))
@ -189,7 +195,8 @@ void main() {
test('Server returns error on too many requests for unary call', () async { test('Server returns error on too many requests for unary call', () async {
harness harness
..service.unaryHandler = expectError(GrpcError.unimplemented('Too many requests')) ..service.unaryHandler =
expectError(GrpcError.unimplemented('Too many requests'))
..expectErrorResponse(StatusCode.unimplemented, 'Too many requests') ..expectErrorResponse(StatusCode.unimplemented, 'Too many requests')
..sendRequestHeader('/Test/Unary') ..sendRequestHeader('/Test/Unary')
..sendData(dummyValue) ..sendData(dummyValue)
@ -200,8 +207,10 @@ void main() {
test('Server returns request deserialization errors', () async { test('Server returns request deserialization errors', () async {
harness harness
..service.bidirectionalHandler = expectErrorStreaming(GrpcError.internal('Error deserializing request: Failed')) ..service.bidirectionalHandler = expectErrorStreaming(
..expectErrorResponse(StatusCode.internal, 'Error deserializing request: Failed') GrpcError.internal('Error deserializing request: Failed'))
..expectErrorResponse(
StatusCode.internal, 'Error deserializing request: Failed')
..sendRequestHeader('/Test/RequestError') ..sendRequestHeader('/Test/RequestError')
..sendData(dummyValue) ..sendData(dummyValue)
..toServer.close(); ..toServer.close();
@ -210,8 +219,10 @@ void main() {
test('Server returns response serialization errors', () async { test('Server returns response serialization errors', () async {
harness harness
..service.bidirectionalHandler = expectErrorStreaming(GrpcError.internal('Error sending response: Failed')) ..service.bidirectionalHandler = expectErrorStreaming(
..expectErrorResponse(StatusCode.internal, 'Error sending response: Failed') GrpcError.internal('Error sending response: Failed'))
..expectErrorResponse(
StatusCode.internal, 'Error sending response: Failed')
..sendRequestHeader('/Test/ResponseError') ..sendRequestHeader('/Test/ResponseError')
..sendData(dummyValue) ..sendData(dummyValue)
..sendData(dummyValue) ..sendData(dummyValue)
@ -272,9 +283,12 @@ void main() {
await harness.fromServer.done; await harness.fromServer.done;
}); });
test('Server returns error if request stream is closed before sending anything', () async { test(
'Server returns error if request stream is closed before sending anything',
() async {
harness harness
..expectErrorResponse(StatusCode.unavailable, 'Request stream closed unexpectedly') ..expectErrorResponse(
StatusCode.unavailable, 'Request stream closed unexpectedly')
..toServer.close(); ..toServer.close();
await harness.fromServer.done; await harness.fromServer.done;
}); });
@ -305,7 +319,8 @@ void main() {
} }
test('with sync interceptor', () => doTest(interceptor)); test('with sync interceptor', () => doTest(interceptor));
test('with async interceptor', () => doTest((call, method) async => interceptor(call, method))); test('with async interceptor',
() => doTest((call, method) async => interceptor(call, method)));
}); });
group('returns error if interceptor blocks request', () { group('returns error if interceptor blocks request', () {
@ -319,14 +334,16 @@ void main() {
Future<void> doTest(Interceptor handler) async { Future<void> doTest(Interceptor handler) async {
harness harness
..interceptor.handler = handler ..interceptor.handler = handler
..expectErrorResponse(StatusCode.unauthenticated, 'Request is unauthenticated') ..expectErrorResponse(
StatusCode.unauthenticated, 'Request is unauthenticated')
..sendRequestHeader('/Test/Unary'); ..sendRequestHeader('/Test/Unary');
await harness.fromServer.done; await harness.fromServer.done;
} }
test('with sync interceptor', () => doTest(interceptor)); test('with sync interceptor', () => doTest(interceptor));
test('with async interceptor', () => doTest((call, method) async => interceptor(call, method))); test('with async interceptor',
() => doTest((call, method) async => interceptor(call, method)));
}); });
group('returns internal error if interceptor throws exception', () { group('returns internal error if interceptor throws exception', () {
@ -337,14 +354,16 @@ void main() {
Future<void> doTest(Interceptor handler) async { Future<void> doTest(Interceptor handler) async {
harness harness
..interceptor.handler = handler ..interceptor.handler = handler
..expectErrorResponse(StatusCode.internal, 'Exception: Reason is unknown') ..expectErrorResponse(
StatusCode.internal, 'Exception: Reason is unknown')
..sendRequestHeader('/Test/Unary'); ..sendRequestHeader('/Test/Unary');
await harness.fromServer.done; await harness.fromServer.done;
} }
test('with sync interceptor', () => doTest(interceptor)); test('with sync interceptor', () => doTest(interceptor));
test('with async interceptor', () => doTest((call, method) async => interceptor(call, method))); test('with async interceptor',
() => doTest((call, method) async => interceptor(call, method)));
}); });
test("don't fail if interceptor await 2 times", () async { test("don't fail if interceptor await 2 times", () async {
@ -356,7 +375,8 @@ void main() {
harness harness
..interceptor.handler = interceptor ..interceptor.handler = interceptor
..expectErrorResponse(StatusCode.internal, 'Exception: Reason is unknown') ..expectErrorResponse(
StatusCode.internal, 'Exception: Reason is unknown')
..sendRequestHeader('/Test/Unary') ..sendRequestHeader('/Test/Unary')
..sendData(1); ..sendData(1);