-
Notifications
You must be signed in to change notification settings - Fork 38
/
async_test.dart
115 lines (102 loc) · 3.29 KB
/
async_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import 'package:dart_eval/dart_eval.dart';
import 'package:dart_eval/stdlib/core.dart';
import 'package:test/test.dart';
void main() {
group('Async tests', () {
late Compiler compiler;
setUp(() {
compiler = Compiler();
});
test('Simple async/await', () async {
final runtime = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
Future main(int milliseconds) async {
await Future.delayed(Duration(milliseconds: milliseconds));
return 3;
}
'''
}
});
final startTime = DateTime.now().millisecondsSinceEpoch;
final future = runtime
.executeLib('package:example/main.dart', 'main', [150]) as Future;
await expectLater(future, completion($int(3)));
final endTime = DateTime.now().millisecondsSinceEpoch;
expect(endTime - startTime, greaterThan(80));
expect(endTime - startTime, lessThan(350));
});
test('Chained async/await', () async {
final runtime = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
Future main() async {
return await fun();
}
Future fun() async {
return 3;
}
'''
}
});
final future =
runtime.executeLib('package:example/main.dart', 'main') as Future;
await expectLater(future, completion($int(3)));
});
test('Auto await future return value', () async {
final runtime = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
Future main() async {
return fun();
}
Future fun() async {
return 3;
}
'''
}
});
final future =
runtime.executeLib('package:example/main.dart', 'main') as Future;
await expectLater(future, completion($int(3)));
});
test('Future.delayed()', () async {
final runtime = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
Future main(int milliseconds) {
return Future.delayed(Duration(milliseconds: milliseconds));
}
'''
}
});
final startTime = DateTime.now().millisecondsSinceEpoch;
final future = runtime
.executeLib('package:example/main.dart', 'main', [150]) as Future;
await expectLater(future, completion(null));
final endTime = DateTime.now().millisecondsSinceEpoch;
expect(endTime - startTime, greaterThan(100));
expect(endTime - startTime, lessThan(200));
});
test('Using a Future result', () async {
final runtime = compiler.compileWriteAndLoad({
'example': {
'main.dart': '''
import 'dart:math';
Future<num> main(int milliseconds) async {
final result = await getPoint(milliseconds).then((result) => result.x + result.y);
return result + 1;
}
Future<Point> getPoint(int milliseconds) async {
await Future.delayed(Duration(milliseconds: milliseconds));
return Point(5, 5);
}
'''
}
});
final value = await (runtime
.executeLib('package:example/main.dart', 'main', [150]) as Future);
expect(value, $int(11));
});
});
}