From f5d2ec2b7e31b47c8d7eff4e5995a8430ca860a6 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Sat, 8 Oct 2022 00:27:29 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20Add=20`delay`=20global=20functio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Common/Core/Helpers.swift | 8 ++++++++ phpmon/Common/Shell/SystemShell.swift | 3 +-- phpmon/Common/Testables/TestableShell.swift | 6 ++---- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/phpmon/Common/Core/Helpers.swift b/phpmon/Common/Core/Helpers.swift index ba20a29..56082a1 100644 --- a/phpmon/Common/Core/Helpers.swift +++ b/phpmon/Common/Core/Helpers.swift @@ -49,3 +49,11 @@ func grepContains(file: String, query: String) async -> Bool { .trimmingCharacters(in: .whitespacesAndNewlines) .contains("YES") } + +/** + Attempts to introduce sleep for a particular duration. Use with caution. + Only intended for testing purposes. + */ +func delay(seconds: Double) async { + try! await Task.sleep(nanoseconds: UInt64(seconds * 1_000_000_000)) +} diff --git a/phpmon/Common/Shell/SystemShell.swift b/phpmon/Common/Shell/SystemShell.swift index e5a4ed2..652b78c 100644 --- a/phpmon/Common/Shell/SystemShell.swift +++ b/phpmon/Common/Shell/SystemShell.swift @@ -92,8 +92,7 @@ class SystemShell: Shellable { // (in order to debug or identify async issues) if ProcessInfo.processInfo.environment["SLOW_SHELL_MODE"] != nil { print("[SLOW SHELL] \(command)") - let delayInSeconds = 3 - try! await Task.sleep(nanoseconds: UInt64(delayInSeconds * 1_000_000_000)) + await delay(seconds: 3.0) } task.standardOutput = outputPipe diff --git a/phpmon/Common/Testables/TestableShell.swift b/phpmon/Common/Testables/TestableShell.swift index 05a4ee3..578c871 100644 --- a/phpmon/Common/Testables/TestableShell.swift +++ b/phpmon/Common/Testables/TestableShell.swift @@ -37,8 +37,7 @@ public class TestableShell: Shellable { // Seriously slow down the shell's return rate in order to debug or identify async issues if ProcessInfo.processInfo.environment["SLOW_SHELL_MODE"] != nil { print("[SLOW SHELL] \(command)") - let delayInSeconds = 3 - try! await Task.sleep(nanoseconds: UInt64(delayInSeconds * 1_000_000_000)) + await delay(seconds: 3.0) } // This assertion will only fire during test builds @@ -100,8 +99,7 @@ struct BatchFakeShellOutput { for item in items { if !ignoreDelay { - let delay = UInt64(item.delay * 1_000_000_000) - try! await Task.sleep(nanoseconds: delay) + await delay(seconds: item.delay) } if item.stream == .stdErr {