OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:convert'; | 6 import 'dart:convert'; |
7 import 'dart:io' as io; | 7 import 'dart:io' as io; |
8 import 'package:observatory/service_io.dart'; | 8 import 'package:observatory/service_io.dart'; |
9 import 'package:unittest/unittest.dart'; | 9 import 'package:unittest/unittest.dart'; |
10 import 'test_helper.dart'; | 10 import 'test_helper.dart'; |
11 | 11 |
12 Future setupTCP() async { | 12 Future setupTCP() async { |
13 // Note that we don't close after us, by design we leave the sockets opens | 13 // Note that we don't close after us, by design we leave the sockets opens |
14 // to allow us to query them from the other isolate. | 14 // to allow us to query them from the other isolate. |
15 var serverSocket = await io.ServerSocket.bind('127.0.0.1', 0); | 15 var serverSocket = await io.ServerSocket.bind('127.0.0.1', 0); |
16 serverSocket.listen((s) { | 16 serverSocket.listen((s) { |
17 s.transform(UTF8.decoder).listen(print); | 17 s.transform(UTF8.decoder).listen(print); |
18 s.close(); | 18 s.close(); |
19 }); | 19 }); |
20 var socket = await io.Socket.connect("127.0.0.1", serverSocket.port); | 20 var socket = await io.Socket.connect("127.0.0.1", serverSocket.port); |
21 socket.write("foobar"); | 21 socket.write("foobar"); |
22 socket.write("foobar"); | 22 socket.write("foobar"); |
23 await socket.flush(); | 23 await socket.flush(); |
24 | 24 |
25 var socket2 = await io.Socket.connect("127.0.0.1", serverSocket.port); | 25 var socket2 = await io.Socket.connect("127.0.0.1", serverSocket.port); |
26 socket2.write("foobarfoobar"); | 26 socket2.write("foobarfoobar"); |
27 await socket2.flush(); | 27 await socket2.flush(); |
28 } | 28 } |
29 | 29 |
30 void expectTimeBiggerThanZero(time) { | |
31 // Stopwatch resolution on windows makes us sometimes report 0; | |
32 if (io.Platform.isWindows) { | |
33 expect(time, greaterThanOrEqualTo(0)); | |
34 } else { | |
35 expect(time, greaterThan(0)); | |
36 } | |
37 } | |
38 | |
39 var tcpTests = [ | 30 var tcpTests = [ |
40 // Initial. | 31 // Initial. |
41 (Isolate isolate) async { | 32 (Isolate isolate) async { |
42 var result = await isolate.invokeRpcNoUpgrade('__getOpenSockets', {}); | 33 var result = await isolate.invokeRpcNoUpgrade('__getOpenSockets', {}); |
43 expect(result['type'], equals('_opensockets')); | 34 expect(result['type'], equals('_opensockets')); |
44 // We expect 3 sockets to be open (in this order): | 35 // We expect 3 sockets to be open (in this order): |
45 // The server socket accepting connections, on port X | 36 // The server socket accepting connections, on port X |
46 // The accepted connection on the client, on port Y | 37 // The accepted connection on the client, on port Y |
47 // The client connection, on port X | 38 // The client connection, on port X |
48 expect(result['data'].length, equals(5)); | 39 expect(result['data'].length, equals(5)); |
49 // The first socket will have a name like listening:127.0.0.1:X | 40 // The first socket will have a name like listening:127.0.0.1:X |
50 // The second will have a name like 127.0.0.1:Y | 41 // The second will have a name like 127.0.0.1:Y |
51 // The third will have a name like 127.0.0.1:X | 42 // The third will have a name like 127.0.0.1:X |
52 expect(result['data'][0]['name'].startsWith('listening:127.0.0.1'), isTrue); | 43 expect(result['data'][0]['name'].startsWith('listening:127.0.0.1'), isTrue); |
53 expect(result['data'][1]['name'].startsWith('127.0.0.1:'), isTrue); | 44 expect(result['data'][1]['name'].startsWith('127.0.0.1:'), isTrue); |
54 expect(result['data'][2]['name'].startsWith('127.0.0.1:'), isTrue); | 45 expect(result['data'][2]['name'].startsWith('127.0.0.1:'), isTrue); |
55 | 46 |
56 var listening = await isolate.invokeRpcNoUpgrade( | 47 var listening = await isolate.invokeRpcNoUpgrade( |
57 '__getSocketByID', { 'id' : result['data'][0]['id'] }); | 48 '__getSocketByID', { 'id' : result['data'][0]['id'] }); |
58 expect(listening['id'], equals(result['data'][0]['id'])); | 49 expect(listening['id'], equals(result['data'][0]['id'])); |
59 expect(listening['listening'], isTrue); | 50 expect(listening['listening'], isTrue); |
60 expect(listening['socket_type'], equals('TCP')); | 51 expect(listening['socket_type'], equals('TCP')); |
61 expect(listening['port'], greaterThanOrEqualTo(1024)); | 52 expect(listening['port'], greaterThanOrEqualTo(1024)); |
62 expectTimeBiggerThanZero(listening['last_read']); | 53 expect(listening['last_read'], greaterThan(0)); |
63 | 54 |
64 expect(listening['total_read'], equals(2)); | 55 expect(listening['total_read'], equals(2)); |
65 expect(listening['last_write'], equals(0)); | 56 expect(listening['last_write'], equals(0)); |
66 expect(listening['total_written'], equals(0)); | 57 expect(listening['total_written'], equals(0)); |
67 expect(listening['write_count'], equals(0)); | 58 expect(listening['write_count'], equals(0)); |
68 expect(listening['read_count'], equals(2)); | 59 expect(listening['read_count'], equals(2)); |
69 expect(listening['remote_host'], equals('NA')); | 60 expect(listening['remote_host'], equals('NA')); |
70 expect(listening['remote_port'], equals('NA')); | 61 expect(listening['remote_port'], equals('NA')); |
71 | 62 |
72 var client = await isolate.invokeRpcNoUpgrade( | 63 var client = await isolate.invokeRpcNoUpgrade( |
(...skipping 19 matching lines...) Expand all Loading... |
92 expect(client['socket_type'], equals('TCP')); | 83 expect(client['socket_type'], equals('TCP')); |
93 expect(server['socket_type'], equals('TCP')); | 84 expect(server['socket_type'], equals('TCP')); |
94 | 85 |
95 // We are using no reserved ports. | 86 // We are using no reserved ports. |
96 expect(client['port'], greaterThanOrEqualTo(1024)); | 87 expect(client['port'], greaterThanOrEqualTo(1024)); |
97 expect(server['port'], greaterThanOrEqualTo(1024)); | 88 expect(server['port'], greaterThanOrEqualTo(1024)); |
98 | 89 |
99 // The client and server "mirror" each other in reads and writes, and the | 90 // The client and server "mirror" each other in reads and writes, and the |
100 // timestamps are in correct order. | 91 // timestamps are in correct order. |
101 expect(client['last_read'], equals(0)); | 92 expect(client['last_read'], equals(0)); |
102 expectTimeBiggerThanZero(server['last_read']); | 93 expect(server['last_read'], greaterThan(0)); |
103 expect(client['total_read'], equals(0)); | 94 expect(client['total_read'], equals(0)); |
104 expect(server['total_read'], equals(12)); | 95 expect(server['total_read'], equals(12)); |
105 expect(client['read_count'], equals(0)); | 96 expect(client['read_count'], equals(0)); |
106 expect(server['read_count'], greaterThanOrEqualTo(1)); | 97 expect(server['read_count'], greaterThanOrEqualTo(1)); |
107 | 98 |
108 expectTimeBiggerThanZero(client['last_write']); | 99 expect(client['last_write'], greaterThan(0)); |
109 expect(server['last_write'], equals(0)); | 100 expect(server['last_write'], equals(0)); |
110 expect(client['total_written'], equals(12)); | 101 expect(client['total_written'], equals(12)); |
111 expect(server['total_written'], equals(0)); | 102 expect(server['total_written'], equals(0)); |
112 expect(client['write_count'], greaterThanOrEqualTo(2)); | 103 expect(client['write_count'], greaterThanOrEqualTo(2)); |
113 expect(server['write_count'], equals(0)); | 104 expect(server['write_count'], equals(0)); |
114 | 105 |
115 // Order | 106 // Order |
116 // Stopwatch resolution on windows can make us have the same timestamp. | 107 // Stopwatch resolution on windows can make us have the same timestamp. |
117 if (io.Platform.isWindows) { | 108 if (io.Platform.isWindows) { |
118 expect(server['last_read'], greaterThanOrEqualTo(client['last_write'])); | 109 expect(server['last_read'], greaterThanOrEqualTo(client['last_write'])); |
(...skipping 23 matching lines...) Expand all Loading... |
142 expect(second_client['socket_type'], equals('TCP')); | 133 expect(second_client['socket_type'], equals('TCP')); |
143 expect(second_server['socket_type'], equals('TCP')); | 134 expect(second_server['socket_type'], equals('TCP')); |
144 | 135 |
145 // We are using no reserved ports. | 136 // We are using no reserved ports. |
146 expect(second_client['port'], greaterThanOrEqualTo(1024)); | 137 expect(second_client['port'], greaterThanOrEqualTo(1024)); |
147 expect(second_server['port'], greaterThanOrEqualTo(1024)); | 138 expect(second_server['port'], greaterThanOrEqualTo(1024)); |
148 | 139 |
149 // The client and server "mirror" each other in reads and writes, and the | 140 // The client and server "mirror" each other in reads and writes, and the |
150 // timestamps are in correct order. | 141 // timestamps are in correct order. |
151 expect(second_client['last_read'], equals(0)); | 142 expect(second_client['last_read'], equals(0)); |
152 expectTimeBiggerThanZero(second_server['last_read']); | 143 expect(second_server['last_read'], greaterThan(0)); |
153 expect(second_client['total_read'], equals(0)); | 144 expect(second_client['total_read'], equals(0)); |
154 expect(second_server['total_read'], equals(12)); | 145 expect(second_server['total_read'], equals(12)); |
155 expect(second_client['read_count'], equals(0)); | 146 expect(second_client['read_count'], equals(0)); |
156 expect(second_server['read_count'], greaterThanOrEqualTo(1)); | 147 expect(second_server['read_count'], greaterThanOrEqualTo(1)); |
157 | 148 |
158 expectTimeBiggerThanZero(second_client['last_write']); | 149 expect(second_client['last_write'], greaterThan(0)); |
159 expect(second_server['last_write'], equals(0)); | 150 expect(second_server['last_write'], equals(0)); |
160 expect(second_client['total_written'], equals(12)); | 151 expect(second_client['total_written'], equals(12)); |
161 expect(second_server['total_written'], equals(0)); | 152 expect(second_server['total_written'], equals(0)); |
162 expect(second_client['write_count'], greaterThanOrEqualTo(1)); | 153 expect(second_client['write_count'], greaterThanOrEqualTo(1)); |
163 expect(second_server['write_count'], equals(0)); | 154 expect(second_server['write_count'], equals(0)); |
164 | 155 |
165 // Order | 156 // Order |
166 // Stopwatch resolution on windows make us sometimes report the same value. | 157 // Stopwatch resolution on windows make us sometimes report the same value. |
167 if (io.Platform.isWindows) { | 158 if (io.Platform.isWindows) { |
168 expect(server['last_read'], greaterThanOrEqualTo(client['last_write'])); | 159 expect(server['last_read'], greaterThanOrEqualTo(client['last_write'])); |
169 } else { | 160 } else { |
170 expect(server['last_read'], greaterThan(client['last_write'])); | 161 expect(server['last_read'], greaterThan(client['last_write'])); |
171 } | 162 } |
172 }, | 163 }, |
173 ]; | 164 ]; |
174 | 165 |
175 main(args) async => runIsolateTests(args, tcpTests, testeeBefore:setupTCP); | 166 main(args) async => runIsolateTests(args, tcpTests, testeeBefore:setupTCP); |
OLD | NEW |