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