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



commit 26d3ba20e95f1570a6e8640c7a6d344921ec6e17
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/meson.build  |  1 +
 installed-tests/js/testAsync.js | 83 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 84 insertions(+)
---
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..702cc8a9
--- /dev/null
+++ b/installed-tests/js/testAsync.js
@@ -0,0 +1,83 @@
+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 () {
+    const tests = [
+        {
+            description: 'idle task with',
+            createSource: () => GLib.idle_source_new(),
+        },
+        {
+            description: '0-second timeout task with',
+            createSource: () => GLib.timeout_source_new(0),
+        },
+    ];
+
+    for (const { description, createSource } of tests) {
+        describe(description, function () {
+            /** @type {GLib.Source} */
+            let source;
+            let tasks;
+
+            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([
+                                'async 1',
+                                'async 2',
+                                'async 3',
+                                'source task',
+                            ])
+                        );
+
+                        // Finish the test if all 4 tasks have been logged.
+                        if (tasks.length === 4) {
+                            done();
+                        }
+                        return GLib.SOURCE_REMOVE;
+                    });
+                    source.attach(null);
+
+                    (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();
+                        }
+                    });
+                });
+
+            }
+
+
+        });
+    }
+});
\ No newline at end of file


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