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 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 import "dart:async"; | 11 import "dart:async"; |
12 import "dart:io"; | 12 import "dart:io"; |
13 | 13 |
14 createReverseStringTransformer() { | 14 createReverseStringTransformer() { |
15 return new StreamTransformer.fromHandlers( | 15 return new StreamTransformer.fromHandlers(handleData: (String data, sink) { |
16 handleData: (String data, sink) { | 16 var sb = new StringBuffer(); |
17 var sb = new StringBuffer(); | 17 for (int i = data.length - 1; i >= 0; i--) sb.write(data[i]); |
18 for (int i = data.length - 1; i >= 0; i--) sb.write(data[i]); | 18 sink.add(sb.toString()); |
19 sink.add(sb.toString()); | 19 }); |
20 }); | |
21 } | 20 } |
22 | 21 |
23 testPipe({int messages, bool transform}) { | 22 testPipe({int messages, bool transform}) { |
24 HttpServer.bind("127.0.0.1", 0).then((server) { | 23 HttpServer.bind("127.0.0.1", 0).then((server) { |
25 server.listen((request) { | 24 server.listen((request) { |
26 WebSocketTransformer.upgrade(request).then((websocket) { | 25 WebSocketTransformer.upgrade(request).then((websocket) { |
27 (transform ? websocket.transform(createReverseStringTransformer()) | 26 (transform |
28 : websocket) | 27 ? websocket.transform(createReverseStringTransformer()) |
| 28 : websocket) |
29 .pipe(websocket) | 29 .pipe(websocket) |
30 .then((_) => server.close()); | 30 .then((_) => server.close()); |
31 }); | 31 }); |
32 }); | 32 }); |
33 WebSocket.connect("ws://127.0.0.1:${server.port}/").then((client) { | 33 WebSocket.connect("ws://127.0.0.1:${server.port}/").then((client) { |
34 var count = 0; | 34 var count = 0; |
35 next() { | 35 next() { |
36 if (count < messages) { | 36 if (count < messages) { |
37 client.add("Hello"); | 37 client.add("Hello"); |
38 } else { | 38 } else { |
39 client.close(); | 39 client.close(); |
40 } | 40 } |
41 } | 41 } |
42 | 42 |
43 client.listen( | 43 client.listen((data) { |
44 (data) { | 44 count++; |
45 count++; | 45 if (transform) { |
46 if (transform) { | 46 Expect.equals("olleH", data); |
47 Expect.equals("olleH", data); | 47 } else { |
48 } else { | 48 Expect.equals("Hello", data); |
49 Expect.equals("Hello", data); | 49 } |
50 } | 50 next(); |
51 next(); | 51 }, onDone: () => print("Client received close")); |
52 }, | |
53 onDone: () => print("Client received close")); | |
54 | 52 |
55 next(); | 53 next(); |
56 }); | 54 }); |
57 }); | 55 }); |
58 } | 56 } |
59 | 57 |
60 void main() { | 58 void main() { |
61 testPipe(messages: 0, transform: false); | 59 testPipe(messages: 0, transform: false); |
62 testPipe(messages: 0, transform: true); | 60 testPipe(messages: 0, transform: true); |
63 testPipe(messages: 1, transform: false); | 61 testPipe(messages: 1, transform: false); |
64 testPipe(messages: 1, transform: true); | 62 testPipe(messages: 1, transform: true); |
65 testPipe(messages: 10, transform: false); | 63 testPipe(messages: 10, transform: false); |
66 testPipe(messages: 10, transform: true); | 64 testPipe(messages: 10, transform: true); |
67 } | 65 } |
OLD | NEW |