Catch errors in ProxiedDevice to make sure we don't crash on errors. (#101332)
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user