[gjs/ewlsh/implicit-mainloop] Add test cases for promise and async ordering
- From: Evan Welsh <ewlsh src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/ewlsh/implicit-mainloop] Add test cases for promise and async ordering
- Date: Sat, 30 Oct 2021 06:21:57 +0000 (UTC)
commit b328e8b4486342f366ad94bb69d137310bcbe154
Author: Evan Welsh <contact evanwelsh com>
Date: Fri Oct 29 23:03:53 2021 -0700
Add test cases for promise and async ordering
installed-tests/js/.eslintrc.yml | 1 +
installed-tests/js/meson.build | 1 +
installed-tests/js/testAsync.js | 93 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 95 insertions(+)
---
diff --git a/installed-tests/js/.eslintrc.yml b/installed-tests/js/.eslintrc.yml
index abc9c527..bbf09ab2 100644
--- a/installed-tests/js/.eslintrc.yml
+++ b/installed-tests/js/.eslintrc.yml
@@ -32,6 +32,7 @@ globals:
overrides:
- files:
- matchers.js
+ - testAsync.js
- testCairoModule.js
- testConsole.js
- testESModules.js
diff --git a/installed-tests/js/meson.build b/installed-tests/js/meson.build
index 5ca37103..2f007351 100644
--- a/installed-tests/js/meson.build
+++ b/installed-tests/js/meson.build
@@ -231,6 +231,7 @@ endif
# minijasmine flag
modules_tests = [
+ 'Async',
'Console',
'ESModules',
'Encoding',
diff --git a/installed-tests/js/testAsync.js b/installed-tests/js/testAsync.js
new file mode 100644
index 00000000..f19a5678
--- /dev/null
+++ b/installed-tests/js/testAsync.js
@@ -0,0 +1,93 @@
+// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
+// SPDX-FileCopyrightText: 2021 Evan Welsh <contact evanwelsh com>
+
+import GLib from 'gi://GLib';
+import GObject from 'gi://GObject';
+
+const PRIORITIES = [
+ 'PRIORITY_LOW',
+ 'PRIORITY_HIGH',
+ 'PRIORITY_DEFAULT',
+ 'PRIORITY_HIGH_IDLE',
+ 'PRIORITY_DEFAULT_IDLE',
+];
+
+describe('Async microtasks resolves before', function () {
+ // Generate test suites with different types of Sources
+ const tests = [
+ {
+ description: 'idle task with',
+ createSource: () => GLib.idle_source_new(),
+ },
+ {
+ description: '0-second timeout task with',
+ // A timeout of 0 tests if microtasks (promises) run before
+ // non-idle tasks which would normally execute "next" in the loop
+ createSource: () => GLib.timeout_source_new(0),
+ },
+ ];
+
+ /* eslint-disable no-loop-func */
+
+ for (const {description, createSource} of tests) {
+ describe(description, function () {
+ /** @type {GLib.Source} */
+ let source;
+ /** @type {string[]} */
+ let tasks;
+
+ const CORRECT_TASKS = [
+ 'async 1',
+ 'async 2',
+ 'async 3',
+ 'source task',
+ ];
+
+ beforeEach(function () {
+ tasks = [];
+ source = createSource();
+ });
+
+ afterEach(function () {
+ source.destroy();
+ tasks = null;
+ source = null;
+ });
+
+ for (const priority of PRIORITIES) {
+ it(`priority set to GLib.${priority}`, function (done) {
+ source.set_priority(GLib[priority]);
+ GObject.source_set_closure(source, () => {
+ tasks.push('source task');
+
+ expect(tasks).toEqual(
+ jasmine.arrayWithExactContents(CORRECT_TASKS)
+ );
+
+ // Finish the test if all the expected tasks have been logged.
+ if (tasks.length === CORRECT_TASKS.length)
+ done();
+
+ return GLib.SOURCE_REMOVE;
+ });
+ source.attach(null);
+
+ // eslint-disable-next-line require-await
+ (async () => {
+ tasks.push('async 1');
+ })().then(() => {
+ tasks.push('async 2');
+ }).then(() => {
+ tasks.push('async 3');
+ }).finally(() => {
+ // Conclude if source task incorrectly ran before the async tasks
+ if (tasks.includes('source task'))
+ done();
+ });
+ });
+ }
+ });
+ }
+
+ /* eslint-enable */
+});
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]