OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:async'; | |
6 import 'dart:convert'; | |
7 import 'dart:io' as io; | |
8 import 'package:observatory/service_io.dart'; | |
9 import 'package:unittest/unittest.dart'; | |
10 import 'test_helper.dart'; | |
11 | |
12 Future setupTCP() async { | |
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. | |
15 var serverSocket = await io.ServerSocket.bind('127.0.0.1', 0); | |
16 serverSocket.listen((s) { | |
17 s.transform(UTF8.decoder).listen(print); | |
18 s.close(); | |
19 }); | |
20 var socket = await io.Socket.connect("127.0.0.1", serverSocket.port); | |
21 socket.write("foobar"); | |
22 socket.write("foobar"); | |
23 await socket.flush(); | |
24 | |
25 var socket2 = await io.Socket.connect("127.0.0.1", serverSocket.port); | |
26 socket2.write("foobarfoobar"); | |
27 await socket2.flush(); | |
28 } | |
29 | |
30 var tcpTests = [ | |
31 // Initial. | |
32 (Isolate isolate) async { | |
33 var result = await isolate.invokeRpcNoUpgrade('__getOpenSockets', {}); | |
34 expect(result['type'], equals('_opensockets')); | |
35 // We expect 3 sockets to be open (in this order): | |
36 // The server socket accepting connections, on port X | |
37 // The accepted connection on the client, on port Y | |
38 // The client connection, on port X | |
39 expect(result['data'].length, equals(5)); | |
40 // The first socket will have a name like listening:127.0.0.1:X | |
41 // The second will have a name like 127.0.0.1:Y | |
42 // The third will have a name like 127.0.0.1:X | |
43 expect(result['data'][0]['name'].startsWith('listening:127.0.0.1'), isTrue); | |
44 expect(result['data'][1]['name'].startsWith('127.0.0.1:'), isTrue); | |
45 expect(result['data'][2]['name'].startsWith('127.0.0.1:'), isTrue); | |
46 | |
47 var listening = await isolate.invokeRpcNoUpgrade( | |
48 '__getSocketByID', { 'id' : result['data'][0]['id'] }); | |
49 expect(listening['id'], equals(result['data'][0]['id'])); | |
50 expect(listening['listening'], isTrue); | |
51 expect(listening['socket_type'], equals('TCP')); | |
52 expect(listening['port'], greaterThanOrEqualTo(1024)); | |
53 // Stopwatch resolution on windows makes us sometimes report 0; | |
Søren Gjesse
2015/09/03 12:39:42
Maybe add a helper for this check - repeated below
ricow1
2015/09/03 17:10:51
Done.
| |
54 if (io.Platform.isWindows) { | |
55 expect(listening['last_read'], greaterThanOrEqualTo(0)); | |
56 } else { | |
57 expect(listening['last_read'], greaterThan(0)); | |
58 } | |
59 expect(listening['total_read'], equals(2)); | |
60 expect(listening['last_write'], equals(0)); | |
61 expect(listening['total_written'], equals(0)); | |
62 expect(listening['write_count'], equals(0)); | |
63 expect(listening['read_count'], equals(0)); | |
64 expect(listening['remote_host'], equals('NA')); | |
65 expect(listening['remote_port'], equals('NA')); | |
66 | |
67 var client = await isolate.invokeRpcNoUpgrade( | |
68 '__getSocketByID', { 'id' : result['data'][1]['id'] }); | |
69 expect(client['id'], equals(result['data'][1]['id'])); | |
70 | |
71 var server = await isolate.invokeRpcNoUpgrade( | |
72 '__getSocketByID', { 'id' : result['data'][2]['id'] }); | |
73 expect(server['id'], equals(result['data'][2]['id'])); | |
74 | |
75 // We expect the client to be connected on the port and | |
76 // host of the listening socket. | |
77 expect(client['remote_port'], equals(listening['port'])); | |
78 expect(client['remote_host'], equals(listening['host'])); | |
79 // We expect the third socket (accepted server) to be connected to the | |
80 // same port and host as the listening socket (the listening one). | |
81 expect(server['port'], equals(listening['port'])); | |
82 expect(server['host'], equals(listening['host'])); | |
83 | |
84 expect(client['listening'], isFalse); | |
85 expect(server['listening'], isFalse); | |
86 | |
87 expect(client['socket_type'], equals('TCP')); | |
88 expect(server['socket_type'], equals('TCP')); | |
89 | |
90 // We are using no reserved ports. | |
91 expect(client['port'], greaterThanOrEqualTo(1024)); | |
92 expect(server['port'], greaterThanOrEqualTo(1024)); | |
93 | |
94 // The client and server "mirror" each other in reads and writes, and the | |
95 // timestamps are in correct order. | |
96 expect(client['last_read'], equals(0)); | |
97 // Stopwatch resolution on windows makes us sometimes report 0; | |
98 if (io.Platform.isWindows) { | |
99 expect(server['last_read'], greaterThanOrEqualTo(0)); | |
100 } else { | |
101 expect(server['last_read'], greaterThan(0)); | |
102 } | |
103 expect(client['total_read'], equals(0)); | |
104 expect(server['total_read'], equals(12)); | |
105 expect(client['read_count'], equals(0)); | |
106 expect(server['read_count'], greaterThanOrEqualTo(1)); | |
107 | |
108 // Stopwatch resolution on windows makes us sometimes report 0; | |
109 if (io.Platform.isWindows) { | |
110 expect(client['last_write'], greaterThanOrEqualTo(0)); | |
111 } else { | |
112 expect(client['last_write'], greaterThan(0)); | |
113 } | |
114 expect(server['last_write'], equals(0)); | |
115 expect(client['total_written'], equals(12)); | |
116 expect(server['total_written'], equals(0)); | |
117 expect(client['write_count'], greaterThanOrEqualTo(2)); | |
118 expect(server['write_count'], equals(0)); | |
119 | |
120 // Order | |
121 // Stopwatch resolution on windows makes us sometimes report 0; | |
122 if (io.Platform.isWindows) { | |
123 expect(server['last_read'], greaterThanOrEqualTo(client['last_write'])); | |
124 } else { | |
125 expect(server['last_read'], greaterThan(client['last_write'])); | |
126 } | |
127 | |
128 var second_client = await isolate.invokeRpcNoUpgrade( | |
129 '__getSocketByID', { 'id' : result['data'][3]['id'] }); | |
130 expect(second_client['id'], equals(result['data'][3]['id'])); | |
131 var second_server = await isolate.invokeRpcNoUpgrade( | |
132 '__getSocketByID', { 'id' : result['data'][4]['id'] }); | |
133 expect(second_server['id'], equals(result['data'][4]['id'])); | |
134 | |
135 // We expect the client to be connected on the port and | |
136 // host of the listening socket. | |
137 expect(second_client['remote_port'], equals(listening['port'])); | |
138 expect(second_client['remote_host'], equals(listening['host'])); | |
139 // We expect the third socket (accepted server) to be connected to the | |
140 // same port and host as the listening socket (the listening one). | |
141 expect(second_server['port'], equals(listening['port'])); | |
142 expect(second_server['host'], equals(listening['host'])); | |
143 | |
144 expect(second_client['listening'], isFalse); | |
145 expect(second_server['listening'], isFalse); | |
146 | |
147 expect(second_client['socket_type'], equals('TCP')); | |
148 expect(second_server['socket_type'], equals('TCP')); | |
149 | |
150 // We are using no reserved ports. | |
151 expect(second_client['port'], greaterThanOrEqualTo(1024)); | |
152 expect(second_server['port'], greaterThanOrEqualTo(1024)); | |
153 | |
154 // The client and server "mirror" each other in reads and writes, and the | |
155 // timestamps are in correct order. | |
156 expect(second_client['last_read'], equals(0)); | |
157 // Stopwatch resolution on windows makes us sometimes report 0; | |
158 if (io.Platform.isWindows) { | |
159 expect(second_server['last_read'], greaterThanOrEqualTo(0)); | |
160 } else { | |
161 expect(second_server['last_read'], greaterThan(0)); | |
162 } | |
163 expect(second_client['total_read'], equals(0)); | |
164 expect(second_server['total_read'], equals(12)); | |
165 expect(second_client['read_count'], equals(0)); | |
166 expect(second_server['read_count'], greaterThanOrEqualTo(1)); | |
167 // Stopwatch resolution on windows makes us sometimes report 0; | |
168 if (io.Platform.isWindows) { | |
169 expect(second_client['last_write'], greaterThanOrEqualTo(0)); | |
170 } else { | |
171 expect(second_client['last_write'], greaterThan(0)); | |
172 } | |
173 expect(second_server['last_write'], equals(0)); | |
174 expect(second_client['total_written'], equals(12)); | |
175 expect(second_server['total_written'], equals(0)); | |
176 expect(second_client['write_count'], greaterThanOrEqualTo(1)); | |
177 expect(second_server['write_count'], equals(0)); | |
178 | |
179 // Order | |
180 // Stopwatch resolution on windows makes us sometimes report 0; | |
181 if (io.Platform.isWindows) { | |
182 expect(server['last_read'], greaterThanOrEqualTo(client['last_write'])); | |
183 } else { | |
184 expect(server['last_read'], greaterThan(client['last_write'])); | |
185 } | |
186 }, | |
187 ]; | |
188 | |
189 main(args) async => runIsolateTests(args, tcpTests, testeeBefore:setupTCP); | |
OLD | NEW |