Skip to content

Commit 5f09598

Browse files
committed
test: bound vim harness cleanup
1 parent 317f554 commit 5f09598

3 files changed

Lines changed: 42 additions & 12 deletions

File tree

tests/formatting.test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ const expectNoPluginFlags = flags => {
251251
const countPluginFlags = flags => (flags.match(/--plugin=/g) || []).length;
252252

253253
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
254+
const timeoutAfter = (promise, ms) => {
255+
let timeout;
256+
const timeoutPromise = new Promise((_, reject) => {
257+
timeout = setTimeout(() => reject(new Error(`Timed out after ${ms}ms`)), ms);
258+
});
259+
260+
return Promise.race([promise, timeoutPromise]).finally(() => clearTimeout(timeout));
261+
};
254262
const waitUntil = (condition, timeout = 2000) => {
255263
return new Promise(resolve => {
256264
let isTimedOut = false;
@@ -346,18 +354,17 @@ afterEach(async () => {
346354
try {
347355
if (remote.isConnected()) {
348356
try {
349-
const filename = await remote.call('expand', ['%:p']);
357+
const filename = await timeoutAfter(remote.call('expand', ['%:p']), 5000);
350358

351359
if (filename) {
352360
// restore the file
353-
await remote.execute('earlier 1d | noautocmd | write');
361+
await timeoutAfter(remote.execute('earlier 1d | noautocmd | write'), 5000);
354362
}
355363
} catch (e) {
356-
} finally {
357-
await remote.close();
358364
}
359365
}
360366
} finally {
367+
await timeoutAfter(remote.close(), 5000).catch(() => {});
361368
cleanupTempProjects();
362369
}
363370
});

tests/vim-driver/HeadlessRemoteClient.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,17 @@ class HeadlessRemoteClient {
121121
}
122122

123123
async close() {
124-
this._assertConnected('close');
125-
await this._remote.close();
126-
if (this._vimProcess != null) {
127-
await this._vimProcess.kill();
124+
try {
125+
if (this._remote != null) {
126+
await this._remote.close();
127+
}
128+
} finally {
129+
if (this._vimProcess != null) {
130+
await this._vimProcess.kill();
131+
this._vimProcess = null;
132+
}
133+
this._unassignRemote();
128134
}
129-
this._unassignRemote();
130135
return this;
131136
}
132137

tests/vim-driver/VimProcess.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,36 @@ class VimProcess {
7373
resolve(this);
7474
return;
7575
}
76-
const onClose = () => {
77-
child.removeListener('close', onClose);
78-
child.removeListener('exit', onClose);
76+
let resolved = false;
77+
let killTimeout;
78+
const finish = () => {
79+
if (resolved) {
80+
return;
81+
}
82+
resolved = true;
83+
clearTimeout(killTimeout);
84+
child.removeListener('close', finish);
85+
child.removeListener('exit', finish);
7986
resolve(this);
8087
};
88+
const onClose = () => {
89+
finish();
90+
};
8191
child.addListener('close', onClose);
8292
child.addListener('exit', onClose);
8393
try {
8494
global.process.kill(-child.pid, 'SIGTERM');
8595
} catch (error) {
8696
child.kill();
8797
}
98+
killTimeout = setTimeout(() => {
99+
try {
100+
global.process.kill(-child.pid, 'SIGKILL');
101+
} catch (error) {
102+
child.kill('SIGKILL');
103+
}
104+
finish();
105+
}, 2000);
88106
});
89107
}
90108
}

0 commit comments

Comments
 (0)