| OLD | NEW |
| 1 library async_test; | 1 library async_test; |
| 2 | 2 |
| 3 import '../../pkg/unittest/lib/unittest.dart'; | 3 import '../../pkg/unittest/lib/unittest.dart'; |
| 4 import '../../pkg/unittest/lib/html_config.dart'; | 4 import '../../pkg/unittest/lib/html_config.dart'; |
| 5 | 5 |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
| 8 import 'dart:html'; | 8 import 'dart:html'; |
| 9 | 9 |
| 10 oneshotTimerIsolate() { | 10 oneshotTimerIsolate(message) { |
| 11 port.receive((msg, replyTo) { | 11 var command = message[0]; |
| 12 expect(msg, 'START'); | 12 var replyTo = message[1]; |
| 13 new Timer(const Duration(milliseconds: 10), () { | 13 expect(command, 'START'); |
| 14 new Timer(const Duration(milliseconds: 10), () { |
| 15 replyTo.send('DONE'); |
| 16 }); |
| 17 } |
| 18 |
| 19 periodicTimerIsolate(message) { |
| 20 var command = message[0]; |
| 21 var replyTo = message[1]; |
| 22 expect(command, 'START'); |
| 23 int counter = 0; |
| 24 new Timer.periodic(const Duration(milliseconds: 10), (timer) { |
| 25 if (counter == 3) { |
| 26 counter = 1024; |
| 27 timer.cancel(); |
| 28 // Wait some more time to be sure callback won't be invoked any |
| 29 // more. |
| 30 new Timer(const Duration(milliseconds: 30), () { |
| 31 replyTo.send('DONE'); |
| 32 }); |
| 33 return; |
| 34 } |
| 35 assert(counter < 3); |
| 36 counter++; |
| 37 }); |
| 38 } |
| 39 |
| 40 cancellingIsolate(message) { |
| 41 var command = message[0]; |
| 42 var replyTo = message[1]; |
| 43 expect(command, 'START'); |
| 44 bool shot = false; |
| 45 var oneshot; |
| 46 var periodic; |
| 47 periodic = new Timer.periodic(const Duration(milliseconds: 10), (timer) { |
| 48 expect(shot, isFalse); |
| 49 shot = true; |
| 50 expect(timer, same(periodic)); |
| 51 periodic.cancel(); |
| 52 oneshot.cancel(); |
| 53 // Wait some more time to be sure callbacks won't be invoked any |
| 54 // more. |
| 55 new Timer(const Duration(milliseconds: 50), () { |
| 14 replyTo.send('DONE'); | 56 replyTo.send('DONE'); |
| 15 }); | 57 }); |
| 16 }); | 58 }); |
| 17 } | 59 // We launch the oneshot timer after the periodic timer. Otherwise a |
| 18 | 60 // (very long) context switch could make this test flaky: assume the |
| 19 periodicTimerIsolate() { | 61 // oneshot timer is created first and then there is a 30ms context switch. |
| 20 port.receive((msg, replyTo) { | 62 // when the periodic timer is scheduled it would execute after the oneshot. |
| 21 expect(msg, 'START'); | 63 oneshot = new Timer(const Duration(milliseconds: 30), () { |
| 22 int counter = 0; | 64 fail('Should never be invoked'); |
| 23 new Timer.periodic(const Duration(milliseconds: 10), (timer) { | |
| 24 if (counter == 3) { | |
| 25 counter = 1024; | |
| 26 timer.cancel(); | |
| 27 // Wait some more time to be sure callback won't be invoked any | |
| 28 // more. | |
| 29 new Timer(const Duration(milliseconds: 30), () { | |
| 30 replyTo.send('DONE'); | |
| 31 }); | |
| 32 return; | |
| 33 } | |
| 34 assert(counter < 3); | |
| 35 counter++; | |
| 36 }); | |
| 37 }); | 65 }); |
| 38 } | 66 } |
| 39 | 67 |
| 40 cancellingIsolate() { | |
| 41 port.receive((msg, replyTo) { | |
| 42 expect(msg, 'START'); | |
| 43 bool shot = false; | |
| 44 var oneshot; | |
| 45 var periodic; | |
| 46 periodic = new Timer.periodic(const Duration(milliseconds: 10), (timer) { | |
| 47 expect(shot, isFalse); | |
| 48 shot = true; | |
| 49 expect(timer, same(periodic)); | |
| 50 periodic.cancel(); | |
| 51 oneshot.cancel(); | |
| 52 // Wait some more time to be sure callbacks won't be invoked any | |
| 53 // more. | |
| 54 new Timer(const Duration(milliseconds: 50), () { | |
| 55 replyTo.send('DONE'); | |
| 56 }); | |
| 57 }); | |
| 58 // We launch the oneshot timer after the periodic timer. Otherwise a | |
| 59 // (very long) context switch could make this test flaky: assume the | |
| 60 // oneshot timer is created first and then there is a 30ms context switch. | |
| 61 // when the periodic timer is scheduled it would execute after the oneshot. | |
| 62 oneshot = new Timer(const Duration(milliseconds: 30), () { | |
| 63 fail('Should never be invoked'); | |
| 64 }); | |
| 65 }); | |
| 66 } | |
| 67 | |
| 68 main() { | 68 main() { |
| 69 useHtmlConfiguration(); | 69 useHtmlConfiguration(); |
| 70 | 70 |
| 71 test('one shot timer in pure isolate', () { | 71 test('one shot timer in pure isolate', () { |
| 72 expect(spawnFunction(oneshotTimerIsolate).call('START'), | 72 var response = new ReceivePort(); |
| 73 completion('DONE')); | 73 var remote = Isolate.spawn(oneshotTimerIsolate, |
| 74 ['START', response.sendPort]); |
| 75 expect(remote.then((_) => response.first), completion('DONE')); |
| 74 }); | 76 }); |
| 75 test('periodic timer in pure isolate', () { | 77 test('periodic timer in pure isolate', () { |
| 76 expect(spawnFunction(periodicTimerIsolate).call('START'), | 78 var response = new ReceivePort(); |
| 77 completion('DONE')); | 79 var remote = Isolate.spawn(periodicTimerIsolate, |
| 80 ['START', response.sendPort]); |
| 81 expect(remote.then((_) => response.first), completion('DONE')); |
| 78 }); | 82 }); |
| 79 test('cancellation in pure isolate', () { | 83 test('cancellation in pure isolate', () { |
| 80 expect(spawnFunction(cancellingIsolate).call('START'), | 84 var response = new ReceivePort(); |
| 81 completion('DONE')); | 85 var remote = Isolate.spawn(cancellingIsolate, |
| 86 ['START', response.sendPort]); |
| 87 expect(remote.then((_) => response.first), completion('DONE')); |
| 82 }); | 88 }); |
| 83 } | 89 } |
| OLD | NEW |