Skip to content

Commit

Permalink
restore BaseTestUtils.delay
Browse files Browse the repository at this point in the history
  • Loading branch information
stbrody committed Jul 29, 2024
1 parent b21b7e8 commit 47aae6e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
22 changes: 17 additions & 5 deletions packages/base-test-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type { CID } from 'multiformats/cid'
import { randomCID, StreamID } from '@ceramicnetwork/streamid'

const delay = async function (ms: number) {
return new Promise<void>((resolve) => setTimeout(() => resolve(), ms))
}

export class BaseTestUtils {
static randomCID(version: 0 | 1 = 1, codec = 0x71, hasher = 0x12): CID {
return randomCID(version, codec, hasher)
Expand Down Expand Up @@ -36,7 +32,7 @@ export class BaseTestUtils {
const deadline = new Date(startTime.getTime() + timeoutMs)
let now = startTime
while (now < deadline) {
await delay(100)
await BaseTestUtils.delay(100)
now = new Date()

try {
Expand All @@ -57,4 +53,20 @@ export class BaseTestUtils {
const customMsg = typeof errMsgGenerator == 'string' ? errMsgGenerator : errMsgGenerator()
throw new Error(baseErrMsg + ': ' + customMsg)
}

// TODO: De-dupe this with `delayOrAbort` in abort-signal-utils.ts
static async delay(ms: number, signal?: AbortSignal): Promise<void> {
return new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => resolve(), ms)
if (signal) {
const handleAbort = () => {
clearTimeout(timeout)
signal.removeEventListener('abort', handleAbort)
reject(signal.reason)
}
if (signal.aborted) handleAbort()
signal.addEventListener('abort', handleAbort)
}
})
}
}
3 changes: 1 addition & 2 deletions packages/common-test-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { StreamState, Stream } from '@ceramicnetwork/common'
import {
AdminApi,
AnchorStatus,
delayOrAbort,
EnvironmentUtils,
EventType,
RunningStateLike,
Expand Down Expand Up @@ -46,7 +45,7 @@ export class CommonTestUtils {
}

static async delay(ms: number, signal?: AbortSignal): Promise<void> {
return delayOrAbort(ms, signal)
return BaseTestUtils.delay(ms, signal)
}

/**
Expand Down

0 comments on commit 47aae6e

Please sign in to comment.