Catch errors in ProxiedDevice to make sure we don't crash on errors. (#101332)

This commit is contained in:
Lau Ching Jun
2022-04-06 09:51:10 -07:00
committed by GitHub
parent 815cac47e9
commit 7f64e2ae02
2 changed files with 85 additions and 10 deletions

View File

@@ -137,6 +137,71 @@ void main() {
'hostPort': 350,
});
});
group('socket done', () {
late Stream<DaemonMessage> broadcastOutput;
late FakeSocket fakeSocket;
const String id = 'random_id';
setUp(() async {
final FakeServerSocket fakeServerSocket = FakeServerSocket(400);
final ProxiedPortForwarder portForwarder = ProxiedPortForwarder(
clientDaemonConnection,
deviceId: 'device_id',
logger: bufferLogger,
createSocketServer: (Logger logger, int? hostPort) async =>
fakeServerSocket,
);
broadcastOutput = serverDaemonConnection.incomingCommands.asBroadcastStream();
unawaited(portForwarder.forward(300));
// Consumes the message.
DaemonMessage message = await broadcastOutput.first;
serverDaemonConnection.sendResponse(message.data['id']!, <String, Object?>{'hostPort': 350});
fakeSocket = FakeSocket();
fakeServerSocket.controller.add(fakeSocket);
// Consumes the message.
message = await broadcastOutput.first;
serverDaemonConnection.sendResponse(message.data['id']!, id);
// Pump the event queue so that the socket future error handler has a
// chance to be listened to.
await pumpEventQueue();
});
testWithoutContext('without error, should calls proxy.disconnect', () async {
// It will try to disconnect the remote port when socket is done.
fakeSocket.doneCompleter.complete(true);
final DaemonMessage message = await broadcastOutput.first;
expect(message.data['id'], isNotNull);
expect(message.data['method'], 'proxy.disconnect');
expect(message.data['params'], <String, Object?>{
'id': 'random_id',
});
});
testWithoutContext('with error, should also calls proxy.disconnect', () async {
fakeSocket.doneCompleter.complete(true);
final DaemonMessage message = await broadcastOutput.first;
expect(message.data['id'], isNotNull);
expect(message.data['method'], 'proxy.disconnect');
expect(message.data['params'], <String, Object?>{
'id': 'random_id',
});
// Send an error response and make sure that it won't crash the client.
serverDaemonConnection.sendErrorResponse(message.data['id']!, 'some error', StackTrace.current);
// Wait the event queue and make sure that it doesn't crash.
await pumpEventQueue();
});
});
});
}