| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 // Script used by the file_lock_test.dart test. | 5 // Script used by the file_lock_test.dart test. |
| 6 | 6 |
| 7 import "dart:async"; | 7 import "dart:async"; |
| 8 import "dart:io"; | 8 import "dart:io"; |
| 9 | 9 |
| 10 Future<int> testLockWholeFile(File file, int len) async { | 10 Future<int> testLockWholeFile(File file, int len) async { |
| 11 var raf = await file.open(mode: APPEND); | 11 var raf = await file.open(mode: APPEND); |
| 12 await raf.setPosition(0); | 12 await raf.setPosition(0); |
| 13 int nextToWrite = 1; | 13 int nextToWrite = 1; |
| 14 while (nextToWrite <= len) { | 14 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, len); |
| 15 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, len); | |
| 16 | 15 |
| 17 int at; | 16 // Make sure the peer fails a non-blocking lock at some point. |
| 18 int p; | 17 await new Future.delayed(const Duration(seconds: 1)); |
| 19 while (true) { | |
| 20 p = await raf.position(); | |
| 21 at = await raf.readByte(); | |
| 22 if (at == 0 || at == -1) break; | |
| 23 nextToWrite++; | |
| 24 } | |
| 25 await raf.setPosition(p); | |
| 26 await raf.writeByte(nextToWrite); | |
| 27 await raf.flush(); | |
| 28 nextToWrite++; | |
| 29 await raf.unlock(0, len); | |
| 30 } | |
| 31 | 18 |
| 32 await raf.lock(FileLock.BLOCKING_EXCLUSIVE, 0, len); | 19 int p = 0; |
| 33 await raf.setPosition(0); | 20 while (p < len) { |
| 34 for (int i = 1; i <= len; i++) { | 21 await raf.writeByte(1); |
| 35 if ((await raf.readByte()) != i) { | 22 p++; |
| 36 await raf.unlock(0, len); | |
| 37 await raf.close(); | |
| 38 return 1; | |
| 39 } | |
| 40 } | 23 } |
| 41 await raf.unlock(0, len); | 24 await raf.unlock(0, len); |
| 42 await raf.close(); | 25 await raf.close(); |
| 43 return 0; | 26 return 0; |
| 44 } | 27 } |
| 45 | 28 |
| 46 main(List<String> args) async { | 29 main(List<String> args) async { |
| 47 File file = new File(args[0]); | 30 File file = new File(args[0]); |
| 48 int len = int.parse(args[1]); | 31 int len = int.parse(args[1]); |
| 49 exit(await testLockWholeFile(file, len)); | 32 exit(await testLockWholeFile(file, len)); |
| 50 } | 33 } |
| OLD | NEW |