From 309785f8473d5e80fdab1cbe0c8ca283c8d17153 Mon Sep 17 00:00:00 2001 From: TriForMine Date: Sun, 11 Apr 2021 11:44:01 +0200 Subject: [PATCH] fix(test): delayUntil use setTimeout instead of while loop & addReaction test (#817) * fix: guild create tests * fix(test): use setTimeout instead of while loop and fix some tests * fix(test): fix some test and fix maxMs Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> --- tests/channels/category_children.ts | 4 ++-- tests/channels/create_channel.ts | 2 +- tests/channels/delete_channel.ts | 4 ++-- tests/guilds/create_guild.ts | 2 +- tests/guilds/delete_server.ts | 2 +- tests/messages/add_reaction.ts | 6 +++--- tests/messages/add_reactions.ts | 4 ++-- tests/messages/create_message.ts | 4 ++-- tests/messages/delete_message.ts | 4 ++-- tests/messages/delete_messages.ts | 6 +++--- tests/messages/edit_message.ts | 4 ++-- tests/messages/get_message.ts | 2 +- tests/messages/get_messages.ts | 2 +- tests/messages/get_reactions.ts | 2 +- tests/messages/pin_message.ts | 2 +- tests/messages/remove_all_reactions.ts | 6 +++--- tests/messages/remove_reaction.ts | 6 +++--- tests/messages/remove_reaction_emoji.ts | 6 +++--- tests/messages/remove_user_reaction.ts | 6 +++--- tests/messages/unpin_message.ts | 2 +- tests/util/delay_until.ts | 18 ++++++++++++++---- tests/ws/start_bot.ts | 1 + 22 files changed, 53 insertions(+), 42 deletions(-) diff --git a/tests/channels/category_children.ts b/tests/channels/category_children.ts index ef6110999..69bfc264b 100644 --- a/tests/channels/category_children.ts +++ b/tests/channels/category_children.ts @@ -16,7 +16,7 @@ Deno.test({ // Assertions assertExists(category); // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed - delayUntil(10000, () => cache.channels.has(category.id)); + await delayUntil(10000, () => cache.channels.has(category.id)); if (!cache.channels.has(category.id)) { throw new Error( @@ -34,7 +34,7 @@ Deno.test({ ), ); // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed - delayUntil(10000, () => channels.every((c) => cache.channels.has(c.id))); + await delayUntil(10000, () => channels.every((c) => cache.channels.has(c.id))); // If every channel is not present in the cache, error out if (!channels.every((c) => cache.channels.has(c.id))) { diff --git a/tests/channels/create_channel.ts b/tests/channels/create_channel.ts index f1da767a3..9c237556f 100644 --- a/tests/channels/create_channel.ts +++ b/tests/channels/create_channel.ts @@ -16,7 +16,7 @@ async function ifItFailsBlameWolf(options: CreateGuildChannel, save = false) { if (save) tempData.channelId = channel.id; // Delay the execution by 5 seconds to allow CHANNEL_CREATE event to be processed - delayUntil(10000, () => cache.channels.has(channel.id)); + await delayUntil(10000, () => cache.channels.has(channel.id)); if (!cache.channels.has(channel.id)) { throw new Error("The channel seemed to be created but it was not cached."); diff --git a/tests/channels/delete_channel.ts b/tests/channels/delete_channel.ts index aeb086210..3d3d95fad 100644 --- a/tests/channels/delete_channel.ts +++ b/tests/channels/delete_channel.ts @@ -42,7 +42,7 @@ Deno.test({ name: "delete-channel", }); // wait 5 seconds to give it time for CHANNEL_CREATE event - delayUntil(10000, () => cache.channels.has(channel.id)); + await delayUntil(10000, () => cache.channels.has(channel.id)); // Make sure the channel was created. if (!cache.channels.has(channel.id)) { throw new Error( @@ -53,7 +53,7 @@ Deno.test({ // Delete the channel now without a reason await deleteChannel(tempData.guildId, channel.id, "with a reason"); // wait 5 seconds to give it time for CHANNEL_DELETE event - delayUntil(10000, () => !cache.channels.has(channel.id)); + await delayUntil(10000, () => !cache.channels.has(channel.id)); // Make sure it is gone from cache if (cache.channels.has(channel.id)) { throw new Error( diff --git a/tests/guilds/create_guild.ts b/tests/guilds/create_guild.ts index b1af1ee9b..b2d1978c9 100644 --- a/tests/guilds/create_guild.ts +++ b/tests/guilds/create_guild.ts @@ -18,7 +18,7 @@ Deno.test({ tempData.guildId = guild.id; // Delay the execution by 5 seconds to allow GUILD_CREATE event to be processed - delayUntil(10000, () => cache.guilds.has(guild.id)); + await delayUntil(10000, () => cache.guilds.has(guild.id)); if (!cache.guilds.has(guild.id)) { throw new Error(`The guild seemed to be created but it was not cached. ${JSON.stringify(guild)}`); diff --git a/tests/guilds/delete_server.ts b/tests/guilds/delete_server.ts index 8867ea335..96dd306d9 100644 --- a/tests/guilds/delete_server.ts +++ b/tests/guilds/delete_server.ts @@ -14,7 +14,7 @@ Deno.test({ } await deleteServer(tempData.guildId); - delayUntil(10000, () => cache.guilds.has(tempData.guildId)); + await delayUntil(10000, () => !cache.guilds.has(tempData.guildId)); if (cache.guilds.has(tempData.guildId)) { throw new Error("The guild was not able to be deleted."); diff --git a/tests/messages/add_reaction.ts b/tests/messages/add_reaction.ts index ed550d693..391a6f085 100644 --- a/tests/messages/add_reaction.ts +++ b/tests/messages/add_reaction.ts @@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) { assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -45,9 +45,9 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", custom = false) { await message.addReaction(emojiId); } - delayUntil( + await delayUntil( 10000, - () => (cache.messages.get(message.id)?.reactions?.length || 0) > 0 + () => cache.messages.get(message.id)?.reactions?.length === 1 ); assertEquals( diff --git a/tests/messages/add_reactions.ts b/tests/messages/add_reactions.ts index 427f23f3a..8aaaa7ae7 100644 --- a/tests/messages/add_reactions.ts +++ b/tests/messages/add_reactions.ts @@ -14,7 +14,7 @@ async function ifItFailsBlameWolf( assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -61,7 +61,7 @@ async function ifItFailsBlameWolf( await message.addReactions(emojiIds, ordered); } - delayUntil( + await delayUntil( 10000, () => cache.messages.get(message.id)?.reactions?.length === 2, ); diff --git a/tests/messages/create_message.ts b/tests/messages/create_message.ts index 2f88c6e85..ad87e4e32 100644 --- a/tests/messages/create_message.ts +++ b/tests/messages/create_message.ts @@ -12,7 +12,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { assertExists(channel); // Wait few seconds for the channel create event to arrive and cache it - delayUntil(10000, () => cache.channels.has(channel.id)); + await delayUntil(10000, () => cache.channels.has(channel.id)); const message = type === "raw" ? await sendMessage(channel.id, "Hello World!") @@ -22,7 +22,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); diff --git a/tests/messages/delete_message.ts b/tests/messages/delete_message.ts index 3c1f963b8..47109ca28 100644 --- a/tests/messages/delete_message.ts +++ b/tests/messages/delete_message.ts @@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); // Make sure the message was created. if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -25,7 +25,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw", reason?: string) { } // Wait 5 seconds to give it time for MESSAGE_DELETE event - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => !cache.messages.has(message.id)); // Make sure it is gone from cache if (cache.messages.has(message.id)) { throw new Error( diff --git a/tests/messages/delete_messages.ts b/tests/messages/delete_messages.ts index 453129ab3..570360717 100644 --- a/tests/messages/delete_messages.ts +++ b/tests/messages/delete_messages.ts @@ -9,7 +9,7 @@ async function ifItFailsBlameWolf(reason?: string) { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); // Make sure the message was created. if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -20,7 +20,7 @@ async function ifItFailsBlameWolf(reason?: string) { // Assertions assertExists(secondMessage); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(secondMessage.id)); + await delayUntil(10000, () => cache.messages.has(secondMessage.id)); // Make sure the message was created. if (!cache.messages.has(secondMessage.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -34,7 +34,7 @@ async function ifItFailsBlameWolf(reason?: string) { ); // Wait 5 seconds to give it time for MESSAGE_DELETE event - delayUntil( + await delayUntil( 10000, () => !cache.messages.has(message.id) && !cache.messages.has(secondMessage.id), diff --git a/tests/messages/edit_message.ts b/tests/messages/edit_message.ts index c3a617034..0a3dc52d2 100644 --- a/tests/messages/edit_message.ts +++ b/tests/messages/edit_message.ts @@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); // Make sure the message was created. if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -24,7 +24,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { await message.edit("Goodbye World!"); } // Wait 5 seconds to give it time for MESSAGE_UPDATE event - delayUntil( + await delayUntil( 10000, () => cache.messages.get(message.id)?.content === "Goodbye World!", ); diff --git a/tests/messages/get_message.ts b/tests/messages/get_message.ts index 14c82deef..2faff1485 100644 --- a/tests/messages/get_message.ts +++ b/tests/messages/get_message.ts @@ -11,7 +11,7 @@ Deno.test({ // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); // Make sure the message was created. if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); diff --git a/tests/messages/get_messages.ts b/tests/messages/get_messages.ts index 8d67ab7eb..21f30c325 100644 --- a/tests/messages/get_messages.ts +++ b/tests/messages/get_messages.ts @@ -21,7 +21,7 @@ Deno.test({ assertExists(secondMessage); assertExists(thirdMessage); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil( + await delayUntil( 10000, () => cache.messages.has(message.id) && diff --git a/tests/messages/get_reactions.ts b/tests/messages/get_reactions.ts index 3e0e88b33..c54eb39a9 100644 --- a/tests/messages/get_reactions.ts +++ b/tests/messages/get_reactions.ts @@ -11,7 +11,7 @@ Deno.test({ // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); // Make sure the message was created. if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); diff --git a/tests/messages/pin_message.ts b/tests/messages/pin_message.ts index c88156c88..12eb91d4a 100644 --- a/tests/messages/pin_message.ts +++ b/tests/messages/pin_message.ts @@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); diff --git a/tests/messages/remove_all_reactions.ts b/tests/messages/remove_all_reactions.ts index 4c7c6abd5..b55b4b7ea 100644 --- a/tests/messages/remove_all_reactions.ts +++ b/tests/messages/remove_all_reactions.ts @@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); // Make sure the message was created. if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -23,7 +23,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Add reactions to the message await addReactions(message.channelId, message.id, ["❤", "😃", "🤫"]); // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed - delayUntil( + await delayUntil( 10000, () => cache.messages.get(message.id)?.reactions?.length === 3, ); @@ -38,7 +38,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { } // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed - delayUntil( + await delayUntil( 10000, () => cache.messages.get(message.id)?.reactions === undefined, ); diff --git a/tests/messages/remove_reaction.ts b/tests/messages/remove_reaction.ts index 7598cfd7d..3bf96826c 100644 --- a/tests/messages/remove_reaction.ts +++ b/tests/messages/remove_reaction.ts @@ -9,7 +9,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); // Make sure the message was created. if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -18,7 +18,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Add reactions to the message await addReaction(message.channelId, message.id, "❤"); // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed - delayUntil( + await delayUntil( 10000, () => cache.messages.get(message.id)?.reactions?.length === 1, ); @@ -33,7 +33,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { } // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed - delayUntil( + await delayUntil( 10000, () => cache.messages.get(message.id)?.reactions === undefined, ); diff --git a/tests/messages/remove_reaction_emoji.ts b/tests/messages/remove_reaction_emoji.ts index 28b513606..d7decb513 100644 --- a/tests/messages/remove_reaction_emoji.ts +++ b/tests/messages/remove_reaction_emoji.ts @@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); // Make sure the message was created. if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -23,7 +23,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Add reactions to the message await addReaction(message.channelId, message.id, "❤"); // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed - delayUntil( + await delayUntil( 10000, () => cache.messages.get(message.id)?.reactions?.length === 1, ); @@ -38,7 +38,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { } // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed - delayUntil( + await delayUntil( 10000, () => cache.messages.get(message.id)?.reactions === undefined, ); diff --git a/tests/messages/remove_user_reaction.ts b/tests/messages/remove_user_reaction.ts index a60318a05..0662738b4 100644 --- a/tests/messages/remove_user_reaction.ts +++ b/tests/messages/remove_user_reaction.ts @@ -14,7 +14,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Assertions assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); // Make sure the message was created. if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); @@ -23,7 +23,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { // Add reactions to the message await addReaction(message.channelId, message.id, "❤"); // Delay the execution by 5 seconds to allow MESSAGE_REACTION_ALL event to be processed - delayUntil( + await delayUntil( 10000, () => cache.messages.get(message.id)?.reactions?.length === 1, ); @@ -43,7 +43,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { } // Delay the execution by 5 seconds to allow MESSAGE_REACTION_REMOVE_ALL event to be processed - delayUntil( + await delayUntil( 10000, () => cache.messages.get(message.id)?.reactions === undefined, ); diff --git a/tests/messages/unpin_message.ts b/tests/messages/unpin_message.ts index 3dec8b9a0..00285772c 100644 --- a/tests/messages/unpin_message.ts +++ b/tests/messages/unpin_message.ts @@ -11,7 +11,7 @@ async function ifItFailsBlameWolf(type: "getter" | "raw") { assertExists(message); // Delay the execution by 5 seconds to allow MESSAGE_CREATE event to be processed - delayUntil(10000, () => cache.messages.has(message.id)); + await delayUntil(10000, () => cache.messages.has(message.id)); if (!cache.messages.has(message.id)) { throw new Error("The message seemed to be sent but it was not cached."); diff --git a/tests/util/delay_until.ts b/tests/util/delay_until.ts index 81bd94839..948d988b1 100644 --- a/tests/util/delay_until.ts +++ b/tests/util/delay_until.ts @@ -1,7 +1,17 @@ -export function delayUntil(maxMs: number, isReady: () => boolean) { - const maxed = Date.now() + maxMs; +export async function delayUntil(maxMs: number, isReady: () => boolean, timeoutTime= 100): Promise { + const maxTime = Date.now() + maxMs; - while (Date.now() < maxed) { - if (isReady()) return; + function hackyFix(resolve: () => void) { + if (isReady() || Date.now() >= maxTime) { + resolve(); + } + else { + setTimeout(async () => { + await hackyFix(resolve); + resolve(); + }, timeoutTime); + } } + + return new Promise(resolve => hackyFix(resolve)); } diff --git a/tests/ws/start_bot.ts b/tests/ws/start_bot.ts index 4db8378c0..0c623f437 100644 --- a/tests/ws/start_bot.ts +++ b/tests/ws/start_bot.ts @@ -4,6 +4,7 @@ import { deleteServer } from "../../src/helpers/guilds/delete_server.ts"; import { delay } from "../../src/util/utils.ts"; import { ws } from "../../src/ws/ws.ts"; import { assertExists } from "../deps.ts"; +import {delayUntil} from "../util/delay_until.ts"; // Set necessary settings // Disables the logger which logs everything