| 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 library isolate.test.isolaterunner_test; |
| 6 |
| 7 import 'dart:async' show Future; |
| 8 import 'dart:isolate' show Capability; |
| 9 |
| 10 import 'package:isolate/isolate_runner.dart'; |
| 11 import 'package:test/test.dart'; |
| 12 |
| 13 const MS = const Duration(milliseconds: 1); |
| 14 |
| 15 void main() { |
| 16 test("create-close", testCreateClose); |
| 17 test("create-run-close", testCreateRunClose); |
| 18 test("separate-isolates", testSeparateIsolates); |
| 19 group('isolate functions', testIsolateFunctions); |
| 20 } |
| 21 |
| 22 Future testCreateClose() { |
| 23 return IsolateRunner.spawn().then((IsolateRunner runner) { |
| 24 return runner.close(); |
| 25 }); |
| 26 } |
| 27 |
| 28 Future testCreateRunClose() { |
| 29 return IsolateRunner.spawn().then((IsolateRunner runner) { |
| 30 return runner.run(id, "testCreateRunClose").then((v) { |
| 31 expect(v, "testCreateRunClose"); |
| 32 return runner.close().then((_) => runner.onExit); |
| 33 }); |
| 34 }); |
| 35 } |
| 36 |
| 37 Future testSeparateIsolates() { |
| 38 // Check that each isolate has its own _global variable. |
| 39 return Future.wait(new Iterable.generate(2, (_) => IsolateRunner.spawn())) |
| 40 .then((runners) { |
| 41 Future runAll(action(IsolateRunner runner, int index)) { |
| 42 var indices = new Iterable.generate(runners.length); |
| 43 return Future.wait(indices.map((i) => action(runners[i], i))); |
| 44 } |
| 45 |
| 46 return runAll((runner, i) => runner.run(setGlobal, i + 1)) |
| 47 .then((values) { |
| 48 expect(values, [1, 2]); |
| 49 expect(_global, null); |
| 50 return runAll((runner, _) => runner.run(getGlobal, null)); |
| 51 }) |
| 52 .then((values) { |
| 53 expect(values, [1, 2]); |
| 54 expect(_global, null); |
| 55 return runAll((runner, _) => runner.close()); |
| 56 }); |
| 57 }); |
| 58 } |
| 59 |
| 60 void testIsolateFunctions() { |
| 61 test("pause", () { |
| 62 bool mayComplete = false; |
| 63 return IsolateRunner.spawn().then((isolate) { |
| 64 isolate.pause(); |
| 65 new Future.delayed(MS * 500, () { |
| 66 mayComplete = true; |
| 67 isolate.resume(); |
| 68 }); |
| 69 isolate.run(id, 42).then((v) { |
| 70 expect(v, 42); |
| 71 expect(mayComplete, isTrue); |
| 72 }).whenComplete(isolate.close); |
| 73 }); |
| 74 }); |
| 75 test("pause2", () { |
| 76 Capability c1 = new Capability(); |
| 77 Capability c2 = new Capability(); |
| 78 int mayCompleteCount = 2; |
| 79 return IsolateRunner.spawn().then((isolate) { |
| 80 isolate.pause(c1); |
| 81 isolate.pause(c2); |
| 82 new Future.delayed(MS * 500, () { |
| 83 mayCompleteCount--; |
| 84 isolate.resume(c1); |
| 85 }); |
| 86 new Future.delayed(MS * 500, () { |
| 87 mayCompleteCount--; |
| 88 isolate.resume(c2); |
| 89 }); |
| 90 isolate.run(id, 42).then((v) { |
| 91 expect(v, 42); |
| 92 expect(mayCompleteCount, 0); |
| 93 }).whenComplete(isolate.close); |
| 94 }); |
| 95 }); |
| 96 test("ping", () { |
| 97 return IsolateRunner.spawn().then((isolate) { |
| 98 return isolate.ping().then((v) { |
| 99 expect(v, isTrue); |
| 100 return isolate.close(); |
| 101 }); |
| 102 }); |
| 103 }); |
| 104 test("kill", () { |
| 105 return IsolateRunner.spawn().then((isolate) { |
| 106 return isolate.kill(); |
| 107 }); |
| 108 }); |
| 109 } |
| 110 |
| 111 id(x) => x; |
| 112 |
| 113 var _global; |
| 114 getGlobal(_) => _global; |
| 115 setGlobal(v) => _global = v; |
| OLD | NEW |