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