[gjs/ewlsh/implicit-mainloop] Add test cases for promise and async ordering




commit a0a4dba76ebadc40b5e120b68028212f97e0ee25
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  | 67 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 69 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..dbd6979c
--- /dev/null
+++ b/installed-tests/js/testAsync.js
@@ -0,0 +1,67 @@
+// 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),
+        },
+    ];
+
+    for (const {description, createSource} of tests) {
+        describe(description, function () {
+            const CORRECT_TASKS = [
+                'async 1',
+                'async 2',
+                'source task',
+            ];
+
+            for (const priority of PRIORITIES) {
+                it(`priority set to GLib.${priority}`, function (done) {
+                    const tasks = [];
+
+                    const source = createSource();
+                    source.set_priority(GLib[priority]);
+                    GObject.source_set_closure(source, () => {
+                        tasks.push('source task');
+
+                        expect(tasks).toEqual(jasmine.arrayWithExactContents(CORRECT_TASKS));
+
+                        done();
+                        source.destroy();
+
+                        return GLib.SOURCE_REMOVE;
+                    });
+                    source.attach(null);
+
+                    (async () => {
+                        // Without an await async functions execute
+                        // synchronously
+                        tasks.push(await 'async 1');
+                    })().then(() => {
+                        tasks.push('async 2');
+                    });
+                });
+            }
+        });
+    }
+});


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]