|
| 1 | +// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | +// |
| 5 | +// Tests that SIGPIPE won't terminate websocket client dart app. |
| 6 | + |
| 7 | +import 'dart:async'; |
| 8 | +import 'dart:convert'; |
| 9 | +import 'dart:ffi'; |
| 10 | +import 'dart:io'; |
| 11 | + |
| 12 | +import "package:async_helper/async_helper.dart"; |
| 13 | +import "package:expect/expect.dart"; |
| 14 | +import 'package:ffi/ffi.dart'; |
| 15 | +import 'package:path/path.dart' as p; |
| 16 | + |
| 17 | +import '../../../tests/ffi/dylib_utils.dart'; |
| 18 | + |
| 19 | +class Isolate extends Opaque {} |
| 20 | + |
| 21 | +abstract class FfiBindings { |
| 22 | + static final ffiTestFunctions = dlopenPlatformSpecific("ffi_test_functions"); |
| 23 | + |
| 24 | + static final RestoreSIGPIPEHandler = |
| 25 | + ffiTestFunctions.lookupFunction<Void Function(), void Function()>( |
| 26 | + "RestoreSIGPIPEHandler"); |
| 27 | + static final SleepOnAnyOS = ffiTestFunctions.lookupFunction< |
| 28 | + Void Function(IntPtr), void Function(int)>("SleepOnAnyOS"); |
| 29 | +} |
| 30 | + |
| 31 | +Future<void> main() async { |
| 32 | + asyncStart(); |
| 33 | + |
| 34 | + final server = await Process.start(Platform.executable, <String>[ |
| 35 | + p.join(p.dirname(Platform.script.toFilePath()), |
| 36 | + "socket_sigpipe_test_server.dart") |
| 37 | + ]); |
| 38 | + final serverPort = Completer<int>(); |
| 39 | + server.stdout |
| 40 | + .transform(utf8.decoder) |
| 41 | + .transform(LineSplitter()) |
| 42 | + .listen((line) { |
| 43 | + print('server stdout: $line'); |
| 44 | + if (!serverPort.isCompleted) { |
| 45 | + serverPort.complete(int.parse(line)); |
| 46 | + } |
| 47 | + }); |
| 48 | + server.stderr |
| 49 | + .transform(utf8.decoder) |
| 50 | + .transform(LineSplitter()) |
| 51 | + .listen((data) { |
| 52 | + print('server stderr: $data'); |
| 53 | + }); |
| 54 | + |
| 55 | + FfiBindings.RestoreSIGPIPEHandler(); |
| 56 | + final ws = |
| 57 | + await WebSocket.connect('ws://localhost:${await serverPort.future}'); |
| 58 | + ws.listen((var data) { |
| 59 | + print('Got $data'); |
| 60 | + // Sleep to prevent closed socket events coming through and being handled. |
| 61 | + // This way websocket stays open and writing into it should trigger SIGPIPE. |
| 62 | + // Unless of course we requested SIGPIPE not to be generated on broken socket |
| 63 | + // pipe. This is what this test is testing - that the SIGPIPE is not generated |
| 64 | + // on broken socket pipe. |
| 65 | + ws.add('foo'); |
| 66 | + FfiBindings.SleepOnAnyOS(10 /*seconds*/); // give server time to exit |
| 67 | + ws.add('baz'); |
| 68 | + ws.close(); |
| 69 | + }, onDone: () { |
| 70 | + asyncEnd(); |
| 71 | + }, onError: (e, st) { |
| 72 | + Expect.fail('Client websocket failed $e $st'); |
| 73 | + }); |
| 74 | +} |
0 commit comments