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 | |
13 /// Test that we correctly remove sockets that have been closed from the list | 12 /// Test that we correctly remove sockets that have been closed from the list |
14 /// of open sockets. We explictly leave one socket open. | 13 /// of open sockets. We explictly leave one socket open. |
15 | 14 |
16 Future setup() async { | 15 Future setup() async { |
17 var serverSocket = await io.ServerSocket.bind('127.0.0.1', 0); | 16 var serverSocket = await io.ServerSocket.bind('127.0.0.1', 0); |
18 serverSocket.listen((s) { | 17 serverSocket.listen((s) { |
19 s.drain(); | 18 s.drain(); |
20 s.close(); | 19 s.close(); |
21 }); | 20 }); |
22 var socket = await io.Socket.connect("127.0.0.1", serverSocket.port); | 21 var socket = await io.Socket.connect("127.0.0.1", serverSocket.port); |
23 socket.write("foobar"); | 22 socket.write("foobar"); |
24 socket.write("foobar"); | 23 socket.write("foobar"); |
25 | 24 |
26 await socket.flush(); | 25 await socket.flush(); |
27 await socket.close(); | 26 await socket.close(); |
28 await socket.drain(); | 27 await socket.drain(); |
29 | 28 |
30 var socket2 = await io.Socket.connect("127.0.0.1", serverSocket.port); | 29 var socket2 = await io.Socket.connect("127.0.0.1", serverSocket.port); |
31 socket2.write("foobarfoobar"); | 30 socket2.write("foobarfoobar"); |
32 await socket2.flush(); | 31 await socket2.flush(); |
33 await socket2.close(); | 32 await socket2.close(); |
34 await socket2.drain(); | 33 await socket2.drain(); |
35 await serverSocket.close(); | 34 await serverSocket.close(); |
36 | 35 |
37 var server = await io.RawDatagramSocket.bind('127.0.0.1', 0); | 36 var server = await io.RawDatagramSocket.bind('127.0.0.1', 0); |
38 server.listen((io.RawSocketEvent event) { | 37 server.listen((io.RawSocketEvent event) { |
39 if(event == io.RawSocketEvent.READ) { | 38 if (event == io.RawSocketEvent.READ) { |
40 io.Datagram dg = server.receive(); | 39 io.Datagram dg = server.receive(); |
41 dg.data.forEach((x) => true); | 40 dg.data.forEach((x) => true); |
42 server.close(); | 41 server.close(); |
43 } | 42 } |
44 }); | 43 }); |
45 var client = await io.RawDatagramSocket.bind('127.0.0.1', 0); | 44 var client = await io.RawDatagramSocket.bind('127.0.0.1', 0); |
46 client.send(UTF8.encoder.convert('foobar'), | 45 client.send(UTF8.encoder.convert('foobar'), |
47 new io.InternetAddress('127.0.0.1'), server.port); | 46 new io.InternetAddress('127.0.0.1'), server.port); |
48 client.close(); | 47 client.close(); |
49 | 48 |
50 // The one socket to expect. | 49 // The one socket to expect. |
51 await io.ServerSocket.bind('127.0.0.1', 0); | 50 await io.ServerSocket.bind('127.0.0.1', 0); |
52 } | 51 } |
53 | 52 |
54 var tests = [ | 53 var tests = [ |
55 // Initial. | 54 // Initial. |
56 (Isolate isolate) async { | 55 (Isolate isolate) async { |
57 var result = | 56 var result = |
58 await isolate.invokeRpcNoUpgrade('ext.dart.io.getOpenSockets', {}); | 57 await isolate.invokeRpcNoUpgrade('ext.dart.io.getOpenSockets', {}); |
59 expect(result['type'], equals('_opensockets')); | 58 expect(result['type'], equals('_opensockets')); |
60 // We expect only one socket to be open, the server socket create at the | 59 // We expect only one socket to be open, the server socket create at the |
61 // end of test. | 60 // end of test. |
62 expect(result['data'].length, equals(1)); | 61 expect(result['data'].length, equals(1)); |
63 var server = await isolate.invokeRpcNoUpgrade( | 62 var server = await isolate.invokeRpcNoUpgrade( |
64 'ext.dart.io.getSocketByID', { 'id' : result['data'][0]['id'] }); | 63 'ext.dart.io.getSocketByID', {'id': result['data'][0]['id']}); |
65 expect(server['listening'], isTrue); | 64 expect(server['listening'], isTrue); |
66 expect(server['lastRead'], equals(0)); | 65 expect(server['lastRead'], equals(0)); |
67 expect(server['totalRead'], equals(0)); | 66 expect(server['totalRead'], equals(0)); |
68 expect(server['lastWrite'], equals(0)); | 67 expect(server['lastWrite'], equals(0)); |
69 expect(server['totalWritten'], equals(0)); | 68 expect(server['totalWritten'], equals(0)); |
70 expect(server['writeCount'], equals(0)); | 69 expect(server['writeCount'], equals(0)); |
71 expect(server['readCount'], equals(0)); | 70 expect(server['readCount'], equals(0)); |
72 }, | 71 }, |
73 ]; | 72 ]; |
74 | 73 |
75 main(args) async => runIsolateTests(args, tests, testeeBefore:setup); | 74 main(args) async => runIsolateTests(args, tests, testeeBefore: setup); |
OLD | NEW |