Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
revert: remove waitFor util
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwu1105 committed Aug 26, 2022
1 parent 2179cc9 commit 05dda25
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 45 deletions.
26 changes: 14 additions & 12 deletions src/test/suite/qmllint/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import * as assert from 'node:assert'
import * as path from 'node:path'
import type { TextDocument } from 'vscode'
import type { Diagnostic } from 'vscode'
import { commands, extensions, languages, window, workspace } from 'vscode'
import { URI } from 'vscode-uri'
import { notNil } from '../../../utils'
import { sleep, waitFor } from '../../utils'
import { sleep } from '../../utils'

const E2E_TIMEOUT = 1000000

suite('qmllint/e2e', () => {
let document: TextDocument

suiteSetup(async function () {
this.timeout(E2E_TIMEOUT)

Expand All @@ -37,29 +35,33 @@ suite('qmllint/e2e', () => {
})

suite('missing_import.qml', () => {
let diagnostics: readonly Diagnostic[]

suiteSetup(async function () {
this.timeout(E2E_TIMEOUT)

document = await openAndShowTestFile('missing_import.qml')
const document = await openAndShowTestFile('missing_import.qml')
await sleep()
diagnostics = languages.getDiagnostics(document.uri)
})

test('should contain diagnostics', async () =>
waitFor(() =>
assert.ok(languages.getDiagnostics(document.uri).length > 0),
)).timeout(E2E_TIMEOUT)
assert.ok(diagnostics.length > 0)).timeout(E2E_TIMEOUT)
}).timeout(E2E_TIMEOUT)

suite('pass.qml', () => {
let diagnostics: readonly Diagnostic[]

suiteSetup(async function () {
this.timeout(E2E_TIMEOUT)

document = await openAndShowTestFile('pass.qml')
const document = await openAndShowTestFile('pass.qml')
await sleep()
diagnostics = languages.getDiagnostics(document.uri)
})

test('should not contain diagnostic', async () =>
waitFor(() =>
assert.ok(languages.getDiagnostics(document.uri).length === 0),
)).timeout(E2E_TIMEOUT)
assert.ok(diagnostics.length === 0)).timeout(E2E_TIMEOUT)
}).timeout(E2E_TIMEOUT)
}).timeout(E2E_TIMEOUT)

Expand Down
34 changes: 1 addition & 33 deletions src/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const DEFAULT_SLEEP_TIME = process.env['CI'] === 'true' ? 2000 : 1000
const DEFAULT_TIMEOUT_FACTOR = 20

export async function waitFor<R>(
f: () => R,
{
timeout = DEFAULT_SLEEP_TIME * DEFAULT_TIMEOUT_FACTOR,
interval = DEFAULT_SLEEP_TIME,
}: WaitForOptions = {
timeout: DEFAULT_SLEEP_TIME * DEFAULT_TIMEOUT_FACTOR,
interval: DEFAULT_SLEEP_TIME,
},
) {
let hasTimeout = false

setTimeout(() => (hasTimeout = true), timeout)

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
while (!hasTimeout) {
try {
return f()
} catch (e) {
await sleep(interval)
}
}

return f()
}

type WaitForOptions = {
readonly timeout?: number
readonly interval?: number
}
const DEFAULT_SLEEP_TIME = process.env['CI'] === 'true' ? 30000 : 1000

export async function sleep(ms = DEFAULT_SLEEP_TIME) {
return new Promise(resolve => setTimeout(resolve, ms))
Expand Down

0 comments on commit 05dda25

Please sign in to comment.