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:developer'; | 7 import 'dart:developer'; |
8 import 'dart:io' as io; | 8 import 'dart:io' as io; |
9 import 'package:observatory/service_io.dart'; | 9 import 'package:observatory/service_io.dart'; |
10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
11 import 'test_helper.dart'; | 11 import 'test_helper.dart'; |
12 | 12 |
13 Future setupFiles() async { | 13 Future setupFiles() async { |
14 var dir = await io.Directory.systemTemp.createTemp('file_service'); | 14 var dir = await io.Directory.systemTemp.createTemp('file_service'); |
15 var writingFile; | 15 var writingFile; |
16 var readingFile; | 16 var readingFile; |
17 | 17 |
18 void closeDown() { | 18 void closeDown() { |
19 if (writingFile != null) { | 19 if (writingFile != null) { |
20 writingFile.closeSync(); | 20 writingFile.closeSync(); |
21 } | 21 } |
22 if (readingFile != null) { | 22 if (readingFile != null) { |
23 readingFile.closeSync(); | 23 readingFile.closeSync(); |
24 } | 24 } |
25 dir.deleteSync(recursive: true); | 25 dir.deleteSync(recursive: true); |
26 } | 26 } |
27 | 27 |
28 Future<ServiceExtensionResponse> cleanup(ignored_a, ignored_b) { | 28 Future<ServiceExtensionResponse> cleanup(ignored_a, ignored_b) { |
29 closeDown(); | 29 closeDown(); |
30 var result = JSON.encode({'type' : 'foobar'}); | 30 var result = JSON.encode({'type': 'foobar'}); |
31 return new Future.value(new ServiceExtensionResponse.result(result)); | 31 return new Future.value(new ServiceExtensionResponse.result(result)); |
32 } | 32 } |
33 | 33 |
34 Future<ServiceExtensionResponse> setup(ignored_a, ignored_b) async { | 34 Future<ServiceExtensionResponse> setup(ignored_a, ignored_b) async { |
35 try { | 35 try { |
36 var filePath = dir.path + io.Platform.pathSeparator + "file"; | 36 var filePath = dir.path + io.Platform.pathSeparator + "file"; |
37 var f = new io.File(filePath); | 37 var f = new io.File(filePath); |
38 writingFile = await f.open(mode: io.FileMode.WRITE); | 38 writingFile = await f.open(mode: io.FileMode.WRITE); |
39 await writingFile.writeByte(42); | 39 await writingFile.writeByte(42); |
40 await writingFile.writeByte(42); | 40 await writingFile.writeByte(42); |
41 await writingFile.writeByte(42); | 41 await writingFile.writeByte(42); |
42 | 42 |
43 var file = new io.File.fromUri(io.Platform.script); | 43 var file = new io.File.fromUri(io.Platform.script); |
44 readingFile = await file.open(); | 44 readingFile = await file.open(); |
45 await readingFile.readByte(); | 45 await readingFile.readByte(); |
46 await readingFile.readByte(); | 46 await readingFile.readByte(); |
47 await readingFile.readByte(); | 47 await readingFile.readByte(); |
48 await readingFile.readByte(); | 48 await readingFile.readByte(); |
49 await readingFile.readByte(); | 49 await readingFile.readByte(); |
50 | 50 |
51 // The utility functions should close the files after them, so we | 51 // The utility functions should close the files after them, so we |
52 // don't expect the calls below to result in open files. | 52 // don't expect the calls below to result in open files. |
53 var writeTemp = dir.path + io.Platform.pathSeparator + "other_file"; | 53 var writeTemp = dir.path + io.Platform.pathSeparator + "other_file"; |
54 var utilFile = new io.File(writeTemp); | 54 var utilFile = new io.File(writeTemp); |
55 await utilFile.writeAsString('foobar'); | 55 await utilFile.writeAsString('foobar'); |
56 var readTemp = new io.File(writeTemp); | 56 var readTemp = new io.File(writeTemp); |
57 var result = await readTemp.readAsString(); | 57 var result = await readTemp.readAsString(); |
58 expect(result, equals('foobar')); | 58 expect(result, equals('foobar')); |
59 | |
60 } catch (e) { | 59 } catch (e) { |
61 closeDown(); | 60 closeDown(); |
62 throw e; | 61 throw e; |
63 } | 62 } |
64 var result = JSON.encode({'type' : 'foobar'}); | 63 var result = JSON.encode({'type': 'foobar'}); |
65 return new Future.value(new ServiceExtensionResponse.result(result)); | 64 return new Future.value(new ServiceExtensionResponse.result(result)); |
66 } | 65 } |
| 66 |
67 registerExtension('ext.dart.io.cleanup', cleanup); | 67 registerExtension('ext.dart.io.cleanup', cleanup); |
68 registerExtension('ext.dart.io.setup', setup); | 68 registerExtension('ext.dart.io.setup', setup); |
69 } | 69 } |
70 | 70 |
71 var fileTests = [ | 71 var fileTests = [ |
72 (Isolate isolate) async { | 72 (Isolate isolate) async { |
73 await isolate.invokeRpcNoUpgrade('ext.dart.io.setup', {}); | 73 await isolate.invokeRpcNoUpgrade('ext.dart.io.setup', {}); |
74 try { | 74 try { |
75 var result = | 75 var result = |
76 await isolate.invokeRpcNoUpgrade('ext.dart.io.getOpenFiles', {}); | 76 await isolate.invokeRpcNoUpgrade('ext.dart.io.getOpenFiles', {}); |
77 expect(result['type'], equals('_openfiles')); | 77 expect(result['type'], equals('_openfiles')); |
78 | 78 |
79 expect(result['data'].length, equals(2)); | 79 expect(result['data'].length, equals(2)); |
80 var writing = await isolate.invokeRpcNoUpgrade( | 80 var writing = await isolate.invokeRpcNoUpgrade( |
81 'ext.dart.io.getFileByID', { 'id' : result['data'][0]['id'] }); | 81 'ext.dart.io.getFileByID', {'id': result['data'][0]['id']}); |
82 | 82 |
83 expect(writing['totalRead'], equals(0)); | 83 expect(writing['totalRead'], equals(0)); |
84 expect(writing['readCount'], equals(0)); | 84 expect(writing['readCount'], equals(0)); |
85 expect(writing['writeCount'], equals(3)); | 85 expect(writing['writeCount'], equals(3)); |
86 expect(writing['totalWritten'], equals(3)); | 86 expect(writing['totalWritten'], equals(3)); |
87 expect(writing['lastWrite'], greaterThan(0)); | 87 expect(writing['lastWrite'], greaterThan(0)); |
88 expect(writing['lastRead'], equals(0)); | 88 expect(writing['lastRead'], equals(0)); |
89 | 89 |
90 var reading = await isolate.invokeRpcNoUpgrade( | 90 var reading = await isolate.invokeRpcNoUpgrade( |
91 'ext.dart.io.getFileByID', { 'id' : result['data'][1]['id'] }); | 91 'ext.dart.io.getFileByID', {'id': result['data'][1]['id']}); |
92 | 92 |
93 expect(reading['totalRead'], equals(5)); | 93 expect(reading['totalRead'], equals(5)); |
94 expect(reading['readCount'], equals(5)); | 94 expect(reading['readCount'], equals(5)); |
95 expect(reading['writeCount'], equals(0)); | 95 expect(reading['writeCount'], equals(0)); |
96 expect(reading['totalWritten'], equals(0)); | 96 expect(reading['totalWritten'], equals(0)); |
97 expect(reading['lastWrite'], equals(0)); | 97 expect(reading['lastWrite'], equals(0)); |
98 expect(reading['lastRead'], greaterThan(0)); | 98 expect(reading['lastRead'], greaterThan(0)); |
99 | |
100 } finally { | 99 } finally { |
101 await isolate.invokeRpcNoUpgrade('ext.dart.io.cleanup', {}); | 100 await isolate.invokeRpcNoUpgrade('ext.dart.io.cleanup', {}); |
102 } | 101 } |
103 }, | 102 }, |
104 ]; | 103 ]; |
105 | 104 |
106 main(args) async => runIsolateTests(args, fileTests, testeeBefore:setupFiles); | 105 main(args) async => runIsolateTests(args, fileTests, testeeBefore: setupFiles); |
OLD | NEW |