OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // OtherResources=signal_test_script.dart | 5 // OtherResources=signal_test_script.dart |
6 // OtherResources=signals_test_script.dart | 6 // OtherResources=signals_test_script.dart |
7 | 7 |
8 import "dart:io"; | 8 import "dart:io"; |
9 import "dart:convert"; | 9 import "dart:convert"; |
10 | 10 |
11 import "package:expect/expect.dart"; | 11 import "package:expect/expect.dart"; |
12 import "package:async_helper/async_helper.dart"; | 12 import "package:async_helper/async_helper.dart"; |
13 | 13 |
14 void testSignals(int usr1Expect, | 14 void testSignals(int usr1Expect, int usr2Expect, |
15 int usr2Expect, | 15 [int usr1Send, int usr2Send, bool shouldFail = false]) { |
16 [int usr1Send, | |
17 int usr2Send, | |
18 bool shouldFail = false]) { | |
19 if (usr1Send == null) usr1Send = usr1Expect; | 16 if (usr1Send == null) usr1Send = usr1Expect; |
20 if (usr2Send == null) usr2Send = usr2Expect; | 17 if (usr2Send == null) usr2Send = usr2Expect; |
21 asyncStart(); | 18 asyncStart(); |
22 Process.start(Platform.executable, | 19 Process.start(Platform.executable, [ |
23 [Platform.script.resolve('signals_test_script.dart').toFilePath(), | 20 Platform.script.resolve('signals_test_script.dart').toFilePath(), |
24 usr1Expect.toString(), | 21 usr1Expect.toString(), |
25 usr2Expect.toString()]) | 22 usr2Expect.toString() |
26 .then((process) { | 23 ]).then((process) { |
27 process.stdin.close(); | 24 process.stdin.close(); |
28 process.stderr.drain(); | 25 process.stderr.drain(); |
29 int v = 0; | 26 int v = 0; |
30 process.stdout.listen((out) { | 27 process.stdout.listen((out) { |
31 // Send as many signals as 'ready\n' received on stdout | 28 // Send as many signals as 'ready\n' received on stdout |
32 int count = out.where((c) => c == '\n'.codeUnitAt(0)).length; | 29 int count = out.where((c) => c == '\n'.codeUnitAt(0)).length; |
33 for (int i = 0; i < count; i++) { | 30 for (int i = 0; i < count; i++) { |
34 if (v < usr1Send) { | 31 if (v < usr1Send) { |
35 process.kill(ProcessSignal.SIGUSR1); | 32 process.kill(ProcessSignal.SIGUSR1); |
36 } else if (v < usr1Send + usr2Send) { | 33 } else if (v < usr1Send + usr2Send) { |
37 process.kill(ProcessSignal.SIGUSR2); | 34 process.kill(ProcessSignal.SIGUSR2); |
38 } | |
39 v++; | |
40 } | 35 } |
41 }); | 36 v++; |
42 process.exitCode.then((exitCode) { | 37 } |
43 Expect.equals(shouldFail, exitCode != 0); | |
44 asyncEnd(); | |
45 }); | |
46 }); | 38 }); |
| 39 process.exitCode.then((exitCode) { |
| 40 Expect.equals(shouldFail, exitCode != 0); |
| 41 asyncEnd(); |
| 42 }); |
| 43 }); |
47 } | 44 } |
48 | 45 |
49 void testSignal(ProcessSignal signal) { | 46 void testSignal(ProcessSignal signal) { |
50 asyncStart(); | 47 asyncStart(); |
51 Process.start(Platform.executable, | 48 Process.start(Platform.executable, [ |
52 [Platform.script.resolve('signal_test_script.dart').toFilePath(), | 49 Platform.script.resolve('signal_test_script.dart').toFilePath(), |
53 signal.toString()]) | 50 signal.toString() |
54 .then((process) { | 51 ]).then((process) { |
| 52 process.stdin.close(); |
| 53 process.stderr.drain(); |
| 54 |
| 55 var output = ""; |
| 56 process.stdout.transform(UTF8.decoder).listen((str) { |
| 57 output += str; |
| 58 if (output == 'ready\n') { |
| 59 process.kill(signal); |
| 60 } |
| 61 }, onDone: () { |
| 62 Expect.equals('ready\n$signal\n', output); |
| 63 }); |
| 64 process.exitCode.then((exitCode) { |
| 65 Expect.equals(0, exitCode); |
| 66 asyncEnd(); |
| 67 }); |
| 68 }); |
| 69 } |
| 70 |
| 71 void testMultipleSignals(List<ProcessSignal> signals) { |
| 72 for (var signal in signals) { |
| 73 asyncStart(); |
| 74 Process |
| 75 .start( |
| 76 Platform.executable, |
| 77 [Platform.script.resolve('signal_test_script.dart').toFilePath()] |
| 78 ..addAll(signals.map((s) => s.toString()))) |
| 79 .then((process) { |
55 process.stdin.close(); | 80 process.stdin.close(); |
56 process.stderr.drain(); | 81 process.stderr.drain(); |
57 | 82 |
58 var output = ""; | 83 var output = ""; |
59 process.stdout.transform(UTF8.decoder) | 84 process.stdout.transform(UTF8.decoder).listen((str) { |
60 .listen((str) { | 85 output += str; |
61 output += str; | 86 if (output == 'ready\n') { |
62 if (output == 'ready\n') { | 87 process.kill(signal); |
63 process.kill(signal); | 88 } |
64 } | 89 }, onDone: () { |
65 }, onDone: () { | 90 Expect.equals('ready\n$signal\n', output); |
66 Expect.equals('ready\n$signal\n', output); | 91 }); |
67 }); | |
68 process.exitCode.then((exitCode) { | 92 process.exitCode.then((exitCode) { |
69 Expect.equals(0, exitCode); | 93 Expect.equals(0, exitCode); |
70 asyncEnd(); | 94 asyncEnd(); |
71 }); | 95 }); |
72 }); | 96 }); |
73 } | |
74 | |
75 void testMultipleSignals(List<ProcessSignal> signals) { | |
76 for (var signal in signals) { | |
77 asyncStart(); | |
78 Process.start(Platform.executable, | |
79 [Platform.script.resolve('signal_test_script.dart').toFilePath()] | |
80 ..addAll(signals.map((s) => s.toString()))) | |
81 .then((process) { | |
82 process.stdin.close(); | |
83 process.stderr.drain(); | |
84 | |
85 var output = ""; | |
86 process.stdout.transform(UTF8.decoder) | |
87 .listen((str) { | |
88 output += str; | |
89 if (output == 'ready\n') { | |
90 process.kill(signal); | |
91 } | |
92 }, onDone: () { | |
93 Expect.equals('ready\n$signal\n', output); | |
94 }); | |
95 process.exitCode.then((exitCode) { | |
96 Expect.equals(0, exitCode); | |
97 asyncEnd(); | |
98 }); | |
99 }); | |
100 } | 97 } |
101 } | 98 } |
102 | 99 |
103 | |
104 void testListenCancel() { | 100 void testListenCancel() { |
105 for (int i = 0; i < 10; i++) { | 101 for (int i = 0; i < 10; i++) { |
106 ProcessSignal.SIGINT.watch().listen(null).cancel(); | 102 ProcessSignal.SIGINT.watch().listen(null).cancel(); |
107 } | 103 } |
108 } | 104 } |
109 | 105 |
110 void main() { | 106 void main() { |
111 testListenCancel(); | 107 testListenCancel(); |
112 if (Platform.isWindows) return; | 108 if (Platform.isWindows) return; |
113 testSignals(0, 0); | 109 testSignals(0, 0); |
114 testSignals(1, 0); | 110 testSignals(1, 0); |
115 testSignals(0, 1); | 111 testSignals(0, 1); |
116 testSignals(1, 1); | 112 testSignals(1, 1); |
117 testSignals(10, 10); | 113 testSignals(10, 10); |
118 testSignals(10, 1); | 114 testSignals(10, 1); |
119 testSignals(1, 10); | 115 testSignals(1, 10); |
120 testSignals(1, 0, 0, 1, true); | 116 testSignals(1, 0, 0, 1, true); |
121 testSignals(0, 1, 1, 0, true); | 117 testSignals(0, 1, 1, 0, true); |
122 | 118 |
123 testSignal(ProcessSignal.SIGHUP); | 119 testSignal(ProcessSignal.SIGHUP); |
124 testSignal(ProcessSignal.SIGINT); | 120 testSignal(ProcessSignal.SIGINT); |
125 testSignal(ProcessSignal.SIGTERM); | 121 testSignal(ProcessSignal.SIGTERM); |
126 testSignal(ProcessSignal.SIGUSR1); | 122 testSignal(ProcessSignal.SIGUSR1); |
127 testSignal(ProcessSignal.SIGUSR2); | 123 testSignal(ProcessSignal.SIGUSR2); |
128 testSignal(ProcessSignal.SIGWINCH); | 124 testSignal(ProcessSignal.SIGWINCH); |
129 | 125 |
130 testMultipleSignals([ | 126 testMultipleSignals([ |
131 ProcessSignal.SIGHUP, | 127 ProcessSignal.SIGHUP, |
132 ProcessSignal.SIGINT, | 128 ProcessSignal.SIGINT, |
133 ProcessSignal.SIGTERM, | 129 ProcessSignal.SIGTERM, |
134 ProcessSignal.SIGUSR1, | 130 ProcessSignal.SIGUSR1, |
135 ProcessSignal.SIGUSR2, | 131 ProcessSignal.SIGUSR2, |
136 ProcessSignal.SIGWINCH]); | 132 ProcessSignal.SIGWINCH |
| 133 ]); |
137 } | 134 } |
OLD | NEW |