| OLD | NEW |
| 1 library async_test; | 1 library async_test; |
| 2 | 2 |
| 3 import 'package:unittest/unittest.dart'; | 3 import 'package:unittest/unittest.dart'; |
| 4 import 'package:unittest/html_config.dart'; | 4 import 'package:unittest/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(message) { | 10 import 'async_oneshot.dart' as oneshot_test show main; |
| 11 var command = message[0]; | 11 import 'async_periodictimer.dart' as periodictimer_test show main; |
| 12 var replyTo = message[1]; | 12 import 'async_cancellingisolate.dart' as cancelling_test show main; |
| 13 expect(command, 'START'); | |
| 14 new Timer(const Duration(milliseconds: 10), () { | |
| 15 replyTo.send('DONE'); | |
| 16 }); | |
| 17 } | |
| 18 | 13 |
| 19 periodicTimerIsolate(message) { | 14 oneshot(message) => oneshot_test.main(message.first, message.last); |
| 20 var command = message[0]; | 15 periodicTimerIsolate(message) => |
| 21 var replyTo = message[1]; | 16 periodictimer_test.main(message.first, message.last); |
| 22 expect(command, 'START'); | 17 cancellingIsolate(message) => cancelling_test.main(message.first, message.last); |
| 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), () { | |
| 56 replyTo.send('DONE'); | |
| 57 }); | |
| 58 }); | |
| 59 // We launch the oneshot timer after the periodic timer. Otherwise a | |
| 60 // (very long) context switch could make this test flaky: assume the | |
| 61 // oneshot timer is created first and then there is a 30ms context switch. | |
| 62 // when the periodic timer is scheduled it would execute after the oneshot. | |
| 63 oneshot = new Timer(const Duration(milliseconds: 30), () { | |
| 64 fail('Should never be invoked'); | |
| 65 }); | |
| 66 } | |
| 67 | 18 |
| 68 main() { | 19 main() { |
| 69 useHtmlConfiguration(); | 20 useHtmlConfiguration(); |
| 70 | 21 |
| 71 test('one shot timer in pure isolate', () { | 22 test('one shot timer in pure isolate', () { |
| 72 var response = new ReceivePort(); | 23 var response = new ReceivePort(); |
| 73 var remote = Isolate.spawn(oneshotTimerIsolate, | 24 var remote = Isolate.spawn(oneshot, |
| 74 ['START', response.sendPort]); | 25 [['START'], response.sendPort]); |
| 75 expect(remote.then((_) => response.first), completion('DONE')); | 26 expect(remote.then((_) => response.first), completion('DONE')); |
| 76 }); | 27 }); |
| 28 |
| 77 test('periodic timer in pure isolate', () { | 29 test('periodic timer in pure isolate', () { |
| 78 var response = new ReceivePort(); | 30 var response = new ReceivePort(); |
| 79 var remote = Isolate.spawn(periodicTimerIsolate, | 31 var remote = Isolate.spawn(periodicTimerIsolate, |
| 80 ['START', response.sendPort]); | 32 [['START'], response.sendPort]); |
| 81 expect(remote.then((_) => response.first), completion('DONE')); | 33 expect(remote.then((_) => response.first), completion('DONE')); |
| 82 }); | 34 }); |
| 35 |
| 83 test('cancellation in pure isolate', () { | 36 test('cancellation in pure isolate', () { |
| 84 var response = new ReceivePort(); | 37 var response = new ReceivePort(); |
| 85 var remote = Isolate.spawn(cancellingIsolate, | 38 var remote = Isolate.spawn(cancellingIsolate, |
| 86 ['START', response.sendPort]); | 39 [['START'], response.sendPort]); |
| 87 expect(remote.then((_) => response.first), completion('DONE')); | 40 expect(remote.then((_) => response.first), completion('DONE')); |
| 88 }); | 41 }); |
| 89 } | 42 } |
| OLD | NEW |