From 0a4eab1aa553b24e2505cd2734590ce786a8bf1e Mon Sep 17 00:00:00 2001 From: Milly Date: Thu, 11 Jul 2024 03:35:44 +0900 Subject: [PATCH] WIP: fix `denops#plugin#wait()` tests --- tests/denops/runtime/functions/plugin_test.ts | 120 ++++++++++++------ 1 file changed, 83 insertions(+), 37 deletions(-) diff --git a/tests/denops/runtime/functions/plugin_test.ts b/tests/denops/runtime/functions/plugin_test.ts index e42a8303..96c20e45 100644 --- a/tests/denops/runtime/functions/plugin_test.ts +++ b/tests/denops/runtime/functions/plugin_test.ts @@ -1,6 +1,8 @@ import { assertArrayIncludes, assertEquals, + assertGreater, + assertLess, assertMatch, assertRejects, } from "jsr:@std/assert@0.225.2"; @@ -9,7 +11,6 @@ import { join } from "jsr:@std/path@0.225.0/join"; import { AsyncDisposableStack } from "jsr:@nick/dispose@1.1.0/async-disposable-stack"; import { testHost } from "/denops-testutil/host.ts"; import { wait } from "/denops-testutil/wait.ts"; -import { promiseState } from "jsr:@lambdalisue/async@2.1.1"; const MESSAGE_DELAY = 200; @@ -1733,25 +1734,35 @@ testHost({ // FIXME: This test will run infinitely on Mac. await t.step({ name: "denops#plugin#wait()", - ignore: Deno.build.os === "darwin", + ignore: false, // Deno.build.os === "darwin", fn: async (t) => { + console.log("NOTE: Testing 'wait()' may stop, so workaround delay..."); + await delay(1000); + await t.step("if the plugin is loading", async (t) => { await host.call("execute", [ "let g:__test_denops_events = []", `call denops#plugin#load('dummyWaitLoading', '${scriptValidWait}')`, - ], ""); - - const resultPromise = host.call( - "denops#plugin#wait", - "dummyWaitLoading", - ); + "let g:__test_denops_wait_start = reltime()", + "let g:__test_denops_wait_result = denops#plugin#wait('dummyWaitLoading', {'timeout': 5000})", + "let g:__test_denops_wait_elapsed = g:__test_denops_wait_start->reltime()->reltimefloat()", + ]); await t.step("waits the plugin is loaded", async () => { - assertEquals(await promiseState(resultPromise), "pending"); + const elapsed_sec = await host.call( + "eval", + "g:__test_denops_wait_elapsed", + ) as number; + assertGreater(elapsed_sec, 1.0); + assertLess(elapsed_sec, 5.0); }); await t.step("returns 0", async () => { - assertEquals(await resultPromise, 0); + const actual = await host.call( + "eval", + "g:__test_denops_wait_result", + ); + assertEquals(actual, 0); }); await t.step("the plugin is loaded after returns", async () => { @@ -1773,17 +1784,26 @@ testHost({ .includes("DenopsPluginPost:dummyWaitLoaded") ); - const resultPromise = host.call( - "denops#plugin#wait", - "dummyWaitLoaded", - ); + await host.call("execute", [ + "let g:__test_denops_wait_start = reltime()", + "let g:__test_denops_wait_result = denops#plugin#wait('dummyWaitLoaded', {'timeout': 5000})", + "let g:__test_denops_wait_elapsed = g:__test_denops_wait_start->reltime()->reltimefloat()", + ]); await t.step("returns immediately", async () => { - await deadline(resultPromise, 100); + const elapsed_sec = await host.call( + "eval", + "g:__test_denops_wait_elapsed", + ) as number; + assertLess(elapsed_sec, 0.1); }); await t.step("returns 0", async () => { - assertEquals(await resultPromise, 0); + const actual = await host.call( + "eval", + "g:__test_denops_wait_result", + ); + assertEquals(actual, 0); }); }); @@ -1797,23 +1817,30 @@ testHost({ (await host.call("eval", "g:__test_denops_events") as string[]) .includes("DenopsPluginPost:dummyWaitReloading") ); - // Reload plugin. + await host.call("execute", [ "let g:__test_denops_events = []", `call denops#plugin#reload('dummyWaitReloading')`, - ], ""); - - const resultPromise = host.call( - "denops#plugin#wait", - "dummyWaitReloading", - ); + "let g:__test_denops_wait_start = reltime()", + "let g:__test_denops_wait_result = denops#plugin#wait('dummyWaitReloading', {'timeout': 5000})", + "let g:__test_denops_wait_elapsed = g:__test_denops_wait_start->reltime()->reltimefloat()", + ]); await t.step("waits the plugin is loaded", async () => { - assertEquals(await promiseState(resultPromise), "pending"); + const elapsed_sec = await host.call( + "eval", + "g:__test_denops_wait_elapsed", + ) as number; + assertGreater(elapsed_sec, 1.0); + assertLess(elapsed_sec, 5.0); }); await t.step("returns 0", async () => { - assertEquals(await resultPromise, 0); + const actual = await host.call( + "eval", + "g:__test_denops_wait_result", + ); + assertEquals(actual, 0); }); await t.step("the plugin is loaded after returns", async () => { @@ -1844,17 +1871,26 @@ testHost({ .includes("DenopsPluginPost:dummyWaitReloaded") ); - const resultPromise = host.call( - "denops#plugin#wait", - "dummyWaitReloaded", - ); + await host.call("execute", [ + "let g:__test_denops_wait_start = reltime()", + "let g:__test_denops_wait_result = denops#plugin#wait('dummyWaitReloaded', {'timeout': 5000})", + "let g:__test_denops_wait_elapsed = g:__test_denops_wait_start->reltime()->reltimefloat()", + ]); await t.step("returns immediately", async () => { - await deadline(resultPromise, 100); + const elapsed_sec = await host.call( + "eval", + "g:__test_denops_wait_elapsed", + ) as number; + assertLess(elapsed_sec, 0.1); }); await t.step("returns 0", async () => { - assertEquals(await resultPromise, 0); + const actual = await host.call( + "eval", + "g:__test_denops_wait_result", + ); + assertEquals(actual, 0); }); }); @@ -1864,17 +1900,27 @@ testHost({ `call denops#plugin#load('dummyWaitInvalid', '${scriptInvalidWait}')`, ], ""); - const resultPromise = host.call( - "denops#plugin#wait", - "dummyWaitInvalid", - ); + await host.call("execute", [ + "let g:__test_denops_wait_start = reltime()", + "let g:__test_denops_wait_result = denops#plugin#wait('dummyWaitInvalid', {'timeout': 5000})", + "let g:__test_denops_wait_elapsed = g:__test_denops_wait_start->reltime()->reltimefloat()", + ]); await t.step("waits the plugin is failed", async () => { - assertEquals(await promiseState(resultPromise), "pending"); + const elapsed_sec = await host.call( + "eval", + "g:__test_denops_wait_elapsed", + ) as number; + assertGreater(elapsed_sec, 1.0); + assertLess(elapsed_sec, 5.0); }); await t.step("returns -3", async () => { - assertEquals(await resultPromise, -3); + const actual = await host.call( + "eval", + "g:__test_denops_wait_result", + ); + assertEquals(actual, -3); }); await t.step("the plugin is failed after returns", async () => {