Skip to content

Commit

Permalink
fix: only collect test result once
Browse files Browse the repository at this point in the history
  • Loading branch information
zxch3n committed Nov 19, 2022
1 parent 744032c commit f330530
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 28 deletions.
14 changes: 12 additions & 2 deletions src/pure/ApiProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class ApiProcess {
port,
handlers: this.handlers,
reconnectInterval: 100,
reconnectTries: 100,
reconnectTries: 10,
})

this.vitestState.loadingPromise.then((isRunning) => {
Expand All @@ -144,7 +144,17 @@ export class ApiProcess {
debouncedLog,
).child

this.process.on('exit', () => {
this.process.on('error', (err) => {
log.error(`Process Error: ${err}`)
})

this.process.on('exit', (code) => {
if (code !== 0) {
this.dispose()
log.error(`Process exited with code ${code}`)
return
}

log.info('API PROCESS EXIT')
this.handlers.onFinished?.()
this.dispose()
Expand Down
44 changes: 24 additions & 20 deletions src/pure/watch/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,35 @@ export function buildWatchClient(
const status = ref<WebSocketStatus>('CONNECTING')
const files = computed(() => client.state.getFiles())

const handled = new WeakSet()
effect(() => {
const ws = client.ws
status.value = 'CONNECTING'
ws.addEventListener('open', () => {
log.info('WS Opened')
status.value = 'OPEN'
client.state.filesMap.clear()
client.rpc.getFiles().then((files) => {
client.state.collectFiles(files)
handlers?.onCollected?.(files)
if (!handled.has(ws)) {
handled.add(ws)
status.value = 'CONNECTING'
ws.addEventListener('open', () => {
log.info('WS Opened')
status.value = 'OPEN'
client.state.filesMap.clear()
client.rpc.getFiles().then((files) => {
client.state.collectFiles(files)
handlers?.onCollected?.(files)
})
client.rpc.getConfig().then(_config => config.value = _config)
})
client.rpc.getConfig().then(_config => config.value = _config)
})

ws.addEventListener('error', (e) => {
console.error('WS ERROR', e)
})
ws.addEventListener('error', (e) => {
console.error('WS ERROR', e)
})

ws.addEventListener('close', () => {
log.info('WS Close')
setTimeout(() => {
if (status.value === 'CONNECTING')
status.value = 'CLOSED'
}, 1000)
})
ws.addEventListener('close', () => {
log.info('WS Close')
setTimeout(() => {
if (status.value === 'CONNECTING')
status.value = 'CLOSED'
}, 1000)
})
}
})

// load result from first run manually
Expand Down
10 changes: 9 additions & 1 deletion src/pure/watch/ws-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
TaskResultPack,
UserConsoleLog, WebSocketEvents, WebSocketHandlers,
} from 'vitest'
import { log } from '../../log'

class StateManager {
filesMap = new Map<string, File>()
Expand Down Expand Up @@ -159,13 +160,20 @@ export function createClient(url: string, options: VitestClientOptions = {}) {
openResolve = resolve
})

let opened = false
function reconnect(reset = false) {
if (reset) {
opened = false
tries = reconnectTries
openPromise = new Promise((resolve) => {
openResolve = resolve
})
}

if (opened)
return

log.info('RECONNECT')
ctx.ws = shallowReactive(new WebSocketConstructor(url))
registerWS()
}
Expand All @@ -180,7 +188,7 @@ export function createClient(url: string, options: VitestClientOptions = {}) {
})
ctx.ws.addEventListener('close', () => {
tries -= 1
if (autoReconnect && tries > 0)
if (!opened && autoReconnect && tries > 0)
setTimeout(reconnect, reconnectInterval)
})
}
Expand Down
5 changes: 3 additions & 2 deletions src/runHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ async function runTest(
dispose2.dispose()
})
}
const finishedTests: Set<vscode.TestItem> = new Set()
const { output, testResultFiles } = await runner!.scheduleRun(
fileItems.map(x => x.uri!.fsPath),
items.length === 1
Expand All @@ -322,12 +323,12 @@ async function runTest(
command,
mode === 'update',
(files: File[]) => {
syncFilesTestStatus(files, discover, ctrl, run, false, false)
syncFilesTestStatus(files, discover, ctrl, run, false, false, finishedTests)
},
mode === 'debug' ? startDebugProcess : undefined,
)

const finishedTests = syncFilesTestStatus(testResultFiles, discover, ctrl, run, true, false)
syncFilesTestStatus(testResultFiles, discover, ctrl, run, true, false, finishedTests)
if (mode !== 'debug') {
for (const item of testCaseSet) {
if (!finishedTests.has(item)) {
Expand Down
13 changes: 10 additions & 3 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ export class TestWatcher extends Disposable {
}
},
},
reconnectInterval: 200,
reconnectTries: 10,
})

effect(() => {
Expand Down Expand Up @@ -320,8 +322,8 @@ export function syncFilesTestStatus(
run: TestRun | undefined,
finished: boolean,
isFirstUpdate: boolean,
finishedTest: Set<TestItem> = new Set(),
) {
const finishedTest: Set<TestItem> = new Set()
for (const file of files) {
const data = discover.discoverTestFromPath(ctrl, file.filepath)
run && syncTestStatusToVsCode(run, data, file, finished, isFirstUpdate, finishedTest)
Expand Down Expand Up @@ -356,23 +358,28 @@ export function syncTestStatusToVsCode(
else if (isFirstUpdate) { run.started(data.item) }
}
else {
if (finishedTest)
finishedTest.add(data.item)
if (finishedTest) {
if (finishedTest.has(data.item))
continue
}

switch (task.result?.state) {
case 'pass':
run.passed(data.item, task.result.duration)
finishedTest && finishedTest.add(data.item)
break
case 'fail':
run.failed(
data.item,
testMessageForTestError(data.item, task.result.error),
task.result.duration,
)
finishedTest && finishedTest.add(data.item)
break
case 'skip':
case 'todo':
run.skipped(data.item)
finishedTest && finishedTest.add(data.item)
break
case 'run':
run.started(data.item)
Expand Down

0 comments on commit f330530

Please sign in to comment.