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 setupUDP() async { | 12 Future setupUDP() async { |
13 var server = await io.RawDatagramSocket.bind('127.0.0.1', 0); | 13 var server = await io.RawDatagramSocket.bind('127.0.0.1', 0); |
14 server.listen((io.RawSocketEvent event) { | 14 server.listen((io.RawSocketEvent event) { |
15 if (event == io.RawSocketEvent.READ) { | 15 if(event == io.RawSocketEvent.READ) { |
16 io.Datagram dg = server.receive(); | 16 io.Datagram dg = server.receive(); |
17 dg.data.forEach((x) => true); | 17 dg.data.forEach((x) => true); |
18 } | 18 } |
19 }); | 19 }); |
20 var client = await io.RawDatagramSocket.bind('127.0.0.1', 0); | 20 var client = await io.RawDatagramSocket.bind('127.0.0.1', 0); |
21 client.send(UTF8.encoder.convert('foobar'), | 21 client.send(UTF8.encoder.convert('foobar'), |
22 new io.InternetAddress('127.0.0.1'), server.port); | 22 new io.InternetAddress('127.0.0.1'), server.port); |
23 } | 23 } |
24 | 24 |
25 var udpTests = [ | 25 var udpTests = [ |
26 // Initial. | 26 // Initial. |
27 (Isolate isolate) async { | 27 (Isolate isolate) async { |
28 var result = | 28 var result = |
29 await isolate.invokeRpcNoUpgrade('ext.dart.io.getOpenSockets', {}); | 29 await isolate.invokeRpcNoUpgrade('ext.dart.io.getOpenSockets', {}); |
30 expect(result['type'], equals('_opensockets')); | 30 expect(result['type'], equals('_opensockets')); |
31 // We expect 2 sockets to be open (in this order): | 31 // We expect 2 sockets to be open (in this order): |
32 // The server socket accepting connections, on port X | 32 // The server socket accepting connections, on port X |
33 // The client socket on port Y | 33 // The client socket on port Y |
34 expect(result['data'].length, equals(2)); | 34 expect(result['data'].length, equals(2)); |
35 // The first socket will have a name like listening:127.0.0.1:X | 35 // The first socket will have a name like listening:127.0.0.1:X |
36 // The second will have a name like 127.0.0.1:Y | 36 // The second will have a name like 127.0.0.1:Y |
37 // The third will have a name like 127.0.0.1:X | 37 // The third will have a name like 127.0.0.1:X |
38 expect(result['data'][0]['name'].startsWith('127.0.0.1'), isTrue); | 38 expect(result['data'][0]['name'].startsWith('127.0.0.1'), isTrue); |
39 expect(result['data'][1]['name'].startsWith('127.0.0.1:'), isTrue); | 39 expect(result['data'][1]['name'].startsWith('127.0.0.1:'), isTrue); |
40 | 40 |
41 var server = await isolate.invokeRpcNoUpgrade( | 41 var server = await isolate.invokeRpcNoUpgrade( |
42 'ext.dart.io.getSocketByID', {'id': result['data'][0]['id']}); | 42 'ext.dart.io.getSocketByID', { 'id' : result['data'][0]['id'] }); |
43 expect(server['id'], equals(result['data'][0]['id'])); | 43 expect(server['id'], equals(result['data'][0]['id'])); |
44 expect(server['remotePort'], equals('NA')); | 44 expect(server['remotePort'], equals('NA')); |
45 expect(server['remoteHost'], equals('NA')); | 45 expect(server['remoteHost'], equals('NA')); |
46 expect(server['listening'], isFalse); | 46 expect(server['listening'], isFalse); |
47 expect(server['socketType'], equals('UDP')); | 47 expect(server['socketType'], equals('UDP')); |
48 expect(server['port'], greaterThanOrEqualTo(1024)); | 48 expect(server['port'], greaterThanOrEqualTo(1024)); |
49 // Stopwatch resolution on windows makes us sometimes report 0; | 49 // Stopwatch resolution on windows makes us sometimes report 0; |
50 if (io.Platform.isWindows) { | 50 if (io.Platform.isWindows) { |
51 expect(server['lastRead'], greaterThanOrEqualTo(0)); | 51 expect(server['lastRead'], greaterThanOrEqualTo(0)); |
52 } else { | 52 } else { |
53 expect(server['lastRead'], greaterThan(0)); | 53 expect(server['lastRead'], greaterThan(0)); |
54 } | 54 } |
55 expect(server['totalRead'], equals(6)); | 55 expect(server['totalRead'], equals(6)); |
56 expect(server['lastWrite'], equals(0)); | 56 expect(server['lastWrite'], equals(0)); |
57 expect(server['totalWritten'], equals(0)); | 57 expect(server['totalWritten'], equals(0)); |
58 expect(server['writeCount'], equals(0)); | 58 expect(server['writeCount'], equals(0)); |
59 expect(server['readCount'], greaterThanOrEqualTo(1)); | 59 expect(server['readCount'], greaterThanOrEqualTo(1)); |
60 | 60 |
61 var client = await isolate.invokeRpcNoUpgrade( | 61 var client = await isolate.invokeRpcNoUpgrade( |
62 'ext.dart.io.getSocketByID', {'id': result['data'][1]['id']}); | 62 'ext.dart.io.getSocketByID', { 'id' : result['data'][1]['id'] }); |
63 expect(client['id'], equals(result['data'][1]['id'])); | 63 expect(client['id'], equals(result['data'][1]['id'])); |
64 expect(client['remotePort'], equals('NA')); | 64 expect(client['remotePort'], equals('NA')); |
65 expect(client['remoteHost'], equals('NA')); | 65 expect(client['remoteHost'], equals('NA')); |
66 expect(client['listening'], isFalse); | 66 expect(client['listening'], isFalse); |
67 expect(client['socketType'], equals('UDP')); | 67 expect(client['socketType'], equals('UDP')); |
68 expect(client['port'], greaterThanOrEqualTo(1024)); | 68 expect(client['port'], greaterThanOrEqualTo(1024)); |
69 expect(client['lastRead'], equals(0)); | 69 expect(client['lastRead'], equals(0)); |
70 expect(client['totalRead'], equals(0)); | 70 expect(client['totalRead'], equals(0)); |
71 // Stopwatch resolution on windows makes us sometimes report 0; | 71 // Stopwatch resolution on windows makes us sometimes report 0; |
72 if (io.Platform.isWindows) { | 72 if (io.Platform.isWindows) { |
73 expect(client['lastWrite'], greaterThanOrEqualTo(0)); | 73 expect(client['lastWrite'], greaterThanOrEqualTo(0)); |
74 } else { | 74 } else { |
75 expect(client['lastWrite'], greaterThan(0)); | 75 expect(client['lastWrite'], greaterThan(0)); |
76 } | 76 } |
77 expect(client['totalWritten'], equals(6)); | 77 expect(client['totalWritten'], equals(6)); |
78 expect(client['writeCount'], greaterThanOrEqualTo(1)); | 78 expect(client['writeCount'], greaterThanOrEqualTo(1)); |
79 expect(client['readCount'], equals(0)); | 79 expect(client['readCount'], equals(0)); |
80 }, | 80 }, |
81 ]; | 81 ]; |
82 | 82 |
83 main(args) async => runIsolateTests(args, udpTests, testeeBefore: setupUDP); | 83 main(args) async => runIsolateTests(args, udpTests, testeeBefore:setupUDP); |
OLD | NEW |