Skip to content
This repository was archived by the owner on Dec 21, 2021. It is now read-only.

Commit c98b044

Browse files
committed
fix: Don't use waitForCondition outside test, doesn't wait for async, uses browser-incompatible setImmediate.
1 parent 17e64ac commit c98b044

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/stream/index.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fetch from 'node-fetch'
22
import { getAddress } from '@ethersproject/address'
3-
import { waitForCondition } from 'streamr-test-utils'
4-
import { getEndpointUrl } from '../utils'
3+
import { getEndpointUrl, until } from '../utils'
54
import authFetch from '../rest/authFetch'
65

76
import { StorageNode } from './StorageNode'
@@ -224,7 +223,7 @@ export class Stream {
224223
await this.update()
225224
}
226225

227-
async addToStorageNode(address: string) {
226+
async addToStorageNode(address: string, { timeout = 30000 }: { timeout: number } = { timeout: 30000 }) {
228227
// currently we support only one storage node
229228
// -> we can validate that the given address is that address
230229
// -> remove this comparison when we start to support multiple storage nodes
@@ -233,8 +232,7 @@ export class Stream {
233232
}
234233
await authFetch(
235234
getEndpointUrl(this._client.options.restUrl, 'streams', this.id, 'storageNodes'),
236-
this._client.session,
237-
{
235+
this._client.session, {
238236
method: 'POST',
239237
body: JSON.stringify({
240238
address
@@ -243,9 +241,8 @@ export class Stream {
243241
)
244242
// wait for propagation: the storage node sees the database change in E&E and
245243
// is ready to store the any stream data which we publish
246-
const TIMEOUT = 30 * 1000
247244
const POLL_INTERVAL = 500
248-
await waitForCondition(() => this.isStreamStoredInStorageNode(this.id), TIMEOUT, POLL_INTERVAL,
245+
await until(() => this.isStreamStoredInStorageNode(this.id), timeout, POLL_INTERVAL,
249246
() => `Propagation timeout when adding stream to a storage node: ${this.id}`)
250247
}
251248

src/utils/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ export async function sleep(ms: number = 0) {
494494
* @param timeOutMs - stop waiting after that many milliseconds, -1 for disable
495495
* @param pollingIntervalMs - check condition between so many milliseconds
496496
*/
497-
export async function until(condition: MaybeAsync<() => boolean>, timeOutMs = 10000, pollingIntervalMs = 100) {
497+
export async function until(condition: MaybeAsync<() => boolean>, timeOutMs = 10000, pollingIntervalMs = 100, failedMsgFn?: () => string) {
498+
const err = new Error(`Timeout after ${timeOutMs} milliseconds`)
498499
let timeout = false
499500
if (timeOutMs > 0) {
500501
setTimeout(() => { timeout = true }, timeOutMs)
@@ -503,8 +504,12 @@ export async function until(condition: MaybeAsync<() => boolean>, timeOutMs = 10
503504
// Promise wrapped condition function works for normal functions just the same as Promises
504505
while (!await Promise.resolve().then(condition)) { // eslint-disable-line no-await-in-loop
505506
if (timeout) {
506-
throw new Error(`Timeout after ${timeOutMs} milliseconds`)
507+
if (failedMsgFn) {
508+
err.message += ` ${failedMsgFn()}`
509+
}
510+
throw err
507511
}
512+
508513
await sleep(pollingIntervalMs) // eslint-disable-line no-await-in-loop
509514
}
510515
return condition()

0 commit comments

Comments
 (0)