Skip to content

Commit 41018d5

Browse files
committed
Merge pull request 'fibers' (dart-lang#20) from fibers into main
Reviewed-on: http://git.local/dependencies/dart/pulls/20
2 parents 29185f9 + 8011517 commit 41018d5

27 files changed

+2121
-3514
lines changed

.clang-format

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ AllowShortIfStatementsOnASingleLine: 'true'
1111

1212
# Put escaped newlines into the rightmost column.
1313
AlignEscapedNewlinesLeft: false
14+
15+
ColumnLimit: 0
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import 'dart:fiber';
2+
import 'dart:async';
3+
import 'package:expect/expect.dart';
4+
5+
final tests = [
6+
testEmpty,
7+
testIdle,
8+
testTerminated,
9+
testFunction,
10+
testClosure,
11+
testFork,
12+
testForks,
13+
];
14+
15+
void testEmpty() {
16+
Fiber.launch(() {});
17+
}
18+
19+
void testIdle() {
20+
Expect.throws(
21+
() => Fiber.launch(() => Fiber.spawn(() => Fiber.reschedule())),
22+
(error) => error is StateError && error.message == "There are no scheduled fibers and FiberProcessor idle function is not defined",
23+
);
24+
}
25+
26+
void testTerminated() {
27+
Fiber.launch(() => Fiber.spawn(() => Fiber.reschedule()), terminate: true);
28+
}
29+
30+
void testFunction() {
31+
void entry() {
32+
Expect.equals("argument", Fiber.current().argument.positioned(0));
33+
}
34+
35+
Fiber.launch(entry, argument: ["argument"], terminate: true);
36+
}
37+
38+
void testClosure() {
39+
Fiber.launch(
40+
() => Expect.equals("argument", Fiber.current().argument.positioned(0)),
41+
argument: ["argument"],
42+
terminate: true,
43+
);
44+
}
45+
46+
void testFork() {
47+
void child() {
48+
Expect.equals("child", Fiber.current().name);
49+
Expect.equals("child", Fiber.current().argument.positioned(0));
50+
}
51+
52+
void main() {
53+
Expect.equals("main", Fiber.current().argument.positioned(0));
54+
Fiber.spawn(child, name: "child", argument: ["child"]);
55+
}
56+
57+
Fiber.launch(main, argument: ["main"], terminate: true);
58+
}
59+
60+
void testForks() {
61+
void child3() {
62+
Expect.equals("child3", Fiber.current().name);
63+
Expect.equals("child3", Fiber.current().argument.positioned(0));
64+
}
65+
66+
void child2() {
67+
Expect.equals("child2", Fiber.current().name);
68+
Expect.equals("child2", Fiber.current().argument.positioned(0));
69+
Fiber.spawn(child3, name: "child3", argument: ["child3"]);
70+
}
71+
72+
void child1() {
73+
Expect.equals("child1", Fiber.current().name);
74+
Expect.equals("child1", Fiber.current().argument.positioned(0));
75+
Fiber.spawn(child2, name: "child2", argument: ["child2"]);
76+
}
77+
78+
void main() {
79+
Expect.equals("main", Fiber.current().argument.positioned(0));
80+
Fiber.spawn(child1, name: "child1", argument: ["child1"]);
81+
}
82+
83+
Fiber.launch(main, argument: ["main"], terminate: true);
84+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'dart:fiber';
2+
import 'dart:async';
3+
import 'package:expect/expect.dart';
4+
5+
final tests = [
6+
testRecycle,
7+
];
8+
9+
void testRecycle() {
10+
Fiber.launch(
11+
() {
12+
var localState = "main";
13+
final child = Fiber.child(
14+
() => localState = "$localState -> child",
15+
persistent: true,
16+
);
17+
Fiber.fork(child);
18+
Expect.isTrue(child.state.finished);
19+
Fiber.fork(child);
20+
Expect.equals("main -> child -> child", localState);
21+
},
22+
terminate: true,
23+
);
24+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import 'dart:fiber';
2+
import 'dart:async';
3+
import 'package:expect/expect.dart';
4+
5+
final tests = [
6+
testGlobalState,
7+
testClosureState,
8+
];
9+
10+
var globalStateValue = "";
11+
void testGlobalState() {
12+
void child() {
13+
globalStateValue += "child -> ";
14+
Fiber.reschedule();
15+
globalStateValue += "child";
16+
}
17+
18+
void main() {
19+
globalStateValue = "";
20+
globalStateValue += "main -> ";
21+
Fiber.schedule(Fiber.current());
22+
Fiber.spawn(child);
23+
globalStateValue += "main -> ";
24+
Fiber.reschedule();
25+
Expect.equals("main -> child -> main -> child", globalStateValue);
26+
}
27+
28+
Fiber.launch(main, terminate: true);
29+
}
30+
31+
void testClosureState() {
32+
var localState = "localState";
33+
Fiber.launch(
34+
() {
35+
Expect.equals("localState", localState);
36+
localState = "after fiber";
37+
},
38+
terminate: true,
39+
);
40+
Expect.equals("after fiber", localState);
41+
42+
localState = "localState";
43+
Fiber.launch(
44+
() {
45+
Expect.equals("localState", localState);
46+
localState = "after main fiber";
47+
Fiber.schedule(Fiber.current());
48+
Fiber.spawn(
49+
() {
50+
Expect.equals("after main fiber", localState);
51+
localState = "after child fiber";
52+
Fiber.reschedule();
53+
Expect.equals("after child fiber after main fiber", localState);
54+
localState = "finish";
55+
},
56+
name: "child",
57+
);
58+
Expect.equals("after child fiber", localState);
59+
localState = "after child fiber after main fiber";
60+
Fiber.suspend();
61+
},
62+
terminate: true,
63+
);
64+
Expect.equals("finish", localState);
65+
66+
localState = "level 1";
67+
Fiber.launch(
68+
() {
69+
Expect.equals("level 1", localState);
70+
localState = "level 2";
71+
Fiber.spawn(
72+
() {
73+
Expect.equals("level 2", localState);
74+
localState = "level 3";
75+
Fiber.spawn(
76+
() {
77+
Expect.equals("level 3", localState);
78+
localState = "level 4";
79+
},
80+
name: "child",
81+
);
82+
},
83+
name: "child",
84+
);
85+
},
86+
terminate: true,
87+
);
88+
Expect.equals("level 4", localState);
89+
}
Lines changed: 36 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,40 @@
11
import 'dart:fiber';
22
import 'dart:async';
33
import 'package:expect/expect.dart';
4-
5-
var globalState = "";
6-
7-
void main() {
8-
testBase();
9-
testClosures();
10-
testRecycle();
11-
}
12-
13-
void testBase() {
14-
Fiber.launch(mainEntry, terminate: true);
15-
}
16-
17-
void testRecycle() {
18-
Fiber.launch(
19-
() {
20-
var localState = "main";
21-
final child = Fiber.child(
22-
() => localState = "$localState -> child",
23-
persistent: true,
24-
);
25-
Fiber.fork(child);
26-
Fiber.fork(child);
27-
Expect.equals("main -> child -> child", localState);
28-
},
29-
terminate: true,
30-
);
31-
}
32-
33-
void mainEntry() {
34-
globalState = "";
35-
globalState += "main -> ";
36-
Fiber.schedule(Fiber.current());
37-
Fiber.spawn(childEntry);
38-
globalState += "main -> ";
39-
Fiber.reschedule();
40-
Expect.equals("main -> child -> main -> child", globalState);
41-
}
42-
43-
void childEntry() {
44-
globalState += "child -> ";
45-
Fiber.reschedule();
46-
globalState += "child";
47-
}
48-
49-
void testClosures() {
50-
var localState = "localState";
51-
Fiber.launch(
52-
() {
53-
Expect.equals("localState", localState);
54-
localState = "after fiber";
55-
},
56-
terminate: true,
57-
);
58-
Expect.equals("after fiber", localState);
59-
60-
localState = "localState";
61-
Fiber.launch(
62-
() {
63-
Expect.equals("localState", localState);
64-
localState = "after main fiber";
65-
Fiber.schedule(Fiber.current());
66-
Fiber.spawn(
67-
() {
68-
Expect.equals("after main fiber", localState);
69-
localState = "after child fiber";
70-
Fiber.reschedule();
71-
Expect.equals("after child fiber after main fiber", localState);
72-
localState = "finish";
73-
},
74-
name: "child",
75-
);
76-
Expect.equals("after child fiber", localState);
77-
localState = "after child fiber after main fiber";
78-
Fiber.suspend();
79-
},
80-
terminate: true,
81-
);
82-
Expect.equals("finish", localState);
83-
84-
localState = "level 1";
85-
Fiber.launch(
86-
() {
87-
Expect.equals("level 1", localState);
88-
localState = "level 2";
89-
Fiber.spawn(
90-
() {
91-
Expect.equals("level 2", localState);
92-
localState = "level 3";
93-
Fiber.spawn(
94-
() {
95-
Expect.equals("level 3", localState);
96-
localState = "level 4";
97-
},
98-
name: "child",
99-
);
100-
},
101-
name: "child",
102-
);
103-
},
104-
terminate: true,
105-
);
106-
Expect.equals("level 4", localState);
4+
import 'fiber_lifecycle_suite.dart' as lifecycle;
5+
import 'fiber_launch_suite.dart' as launch;
6+
import 'fiber_state_suite.dart' as state;
7+
8+
final suites = {
9+
"launch": launch.tests,
10+
"state": state.tests,
11+
"lifecycle": lifecycle.tests,
12+
};
13+
14+
void main(List<String> arguments) {
15+
if (arguments.isEmpty) {
16+
for (var suite in suites.entries) {
17+
print("Processing suite: ${suite.key}");
18+
for (var test in suite.value) {
19+
final function = RegExp(r"Function 'test(.+)'").firstMatch(test.toString())!.group(1);
20+
print("Processing test: test${function}");
21+
test();
22+
print("Test: test${function} finished");
23+
}
24+
print("Suite: ${suite.key} finished\n");
25+
}
26+
return;
27+
}
28+
final suite = suites[arguments[0]];
29+
if (suite == null) return;
30+
print("Processing suite: ${arguments[0]}");
31+
for (var test in suite!) {
32+
final function = RegExp(r"Function 'test(.+)'").firstMatch(test.toString())!.group(1);
33+
if (arguments.length == 1 || function == arguments[1].toLowerCase()) {
34+
print("Processing test: test${function}");
35+
test();
36+
print("Test: test${function} finished");
37+
}
38+
}
39+
print("Suite: ${arguments[0]} finished");
10740
}

0 commit comments

Comments
 (0)