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 expect(listening['last_read'], greaterThan(0)); | |
54 expect(listening['total_read'], equals(2)); | |
55 expect(listening['last_write'], equals(0)); | |
56 expect(listening['total_written'], equals(0)); | |
57 expect(listening['write_count'], equals(0)); | |
58 expect(listening['read_count'], equals(0)); | |
59 expect(listening['remote_host'], equals('NA')); | |
60 expect(listening['remote_port'], equals('NA')); | |
61 | |
62 var client = await isolate.invokeRpcNoUpgrade( | |
63 '__getSocketByID', { 'id' : result['data'][1]['id'] }); | |
64 expect(client['id'], equals(result['data'][1]['id'])); | |
65 | |
66 var server = await isolate.invokeRpcNoUpgrade( | |
67 '__getSocketByID', { 'id' : result['data'][2]['id'] }); | |
68 expect(server['id'], equals(result['data'][2]['id'])); | |
69 | |
70 // We expect the client to be connected on the port and | |
71 // host of the listening socket. | |
72 expect(client['remote_port'], equals(listening['port'])); | |
73 expect(client['remote_host'], equals(listening['host'])); | |
74 // We expect the third socket (accepted server) to be connected to the | |
75 // same port and host as the listening socket (the listening one). | |
76 expect(server['port'], equals(listening['port'])); | |
77 expect(server['host'], equals(listening['host'])); | |
78 | |
79 expect(client['listening'], isFalse); | |
80 expect(server['listening'], isFalse); | |
81 | |
82 expect(client['socket_type'], equals('TCP')); | |
83 expect(server['socket_type'], equals('TCP')); | |
84 | |
85 // We are using no reserved ports. | |
86 expect(client['port'], greaterThanOrEqualTo(1024)); | |
87 expect(server['port'], greaterThanOrEqualTo(1024)); | |
88 | |
89 // The client and server "mirror" each other in reads and writes, and the | |
90 // timestamps are in correct order. | |
91 expect(client['last_read'], equals(0)); | |
92 expect(server['last_read'], greaterThan(0)); | |
93 expect(client['total_read'], equals(0)); | |
94 expect(server['total_read'], equals(12)); | |
95 expect(client['read_count'], equals(0)); | |
96 // We did not flush after first write, so only one read. | |
97 // This may be fragile -> should we just make sure it is bigger than 1? | |
98 expect(server['read_count'], equals(1)); | |
99 | |
100 expect(client['last_write'], greaterThan(0)); | |
101 expect(server['last_write'], equals(0)); | |
102 expect(client['total_written'], equals(12)); | |
103 expect(server['total_written'], equals(0)); | |
104 expect(client['write_count'], equals(2)); | |
105 expect(server['write_count'], equals(0)); | |
106 | |
107 // Order | |
108 expect(server['last_read'], greaterThan(client['last_write'])); | |
109 | |
110 var second_client = await isolate.invokeRpcNoUpgrade( | |
111 '__getSocketByID', { 'id' : result['data'][3]['id'] }); | |
112 expect(second_client['id'], equals(result['data'][3]['id'])); | |
113 var second_server = await isolate.invokeRpcNoUpgrade( | |
114 '__getSocketByID', { 'id' : result['data'][4]['id'] }); | |
115 expect(second_server['id'], equals(result['data'][4]['id'])); | |
116 | |
117 // We expect the client to be connected on the port and | |
118 // host of the listening socket. | |
119 expect(second_client['remote_port'], equals(listening['port'])); | |
120 expect(second_client['remote_host'], equals(listening['host'])); | |
121 // We expect the third socket (accepted server) to be connected to the | |
122 // same port and host as the listening socket (the listening one). | |
123 expect(second_server['port'], equals(listening['port'])); | |
124 expect(second_server['host'], equals(listening['host'])); | |
125 | |
126 expect(second_client['listening'], isFalse); | |
127 expect(second_server['listening'], isFalse); | |
128 | |
129 expect(second_client['socket_type'], equals('TCP')); | |
130 expect(second_server['socket_type'], equals('TCP')); | |
131 | |
132 // We are using no reserved ports. | |
133 expect(second_client['port'], greaterThanOrEqualTo(1024)); | |
134 expect(second_server['port'], greaterThanOrEqualTo(1024)); | |
135 | |
136 // The client and server "mirror" each other in reads and writes, and the | |
137 // timestamps are in correct order. | |
138 expect(second_client['last_read'], equals(0)); | |
139 expect(second_server['last_read'], greaterThan(0)); | |
140 expect(second_client['total_read'], equals(0)); | |
141 expect(second_server['total_read'], equals(12)); | |
142 expect(second_client['read_count'], equals(0)); | |
143 expect(second_server['read_count'], equals(1)); | |
144 | |
145 expect(second_client['last_write'], greaterThan(0)); | |
146 expect(second_server['last_write'], equals(0)); | |
147 expect(second_client['total_written'], equals(12)); | |
148 expect(second_server['total_written'], equals(0)); | |
149 expect(second_client['write_count'], equals(1)); | |
150 expect(second_server['write_count'], equals(0)); | |
151 | |
152 // Order | |
153 expect(server['last_read'], greaterThan(client['last_write'])); | |
154 }, | |
155 ]; | |
156 | |
157 main(args) async => runIsolateTests(args, tcpTests, testeeBefore:setupTCP); | |
OLD | NEW |