Skip to content

Commit

Permalink
Merge pull request #342 from microsoft/remove_pollUntil
Browse files Browse the repository at this point in the history
Remove pollUntil, add unit test debug target
  • Loading branch information
Tyriar committed Sep 4, 2019
2 parents 3e64589 + 36d947e commit 347f5dd
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 816 deletions.
47 changes: 35 additions & 12 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Attach to process ID",
"processId": "${command:PickProcess}"
}
]
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Unit Tests",
"cwd": "${workspaceRoot}",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha",
"windows": {
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/mocha.cmd"
},
"runtimeArgs": [
"--colors",
"--recursive",
"${workspaceRoot}/lib/**/*.test.js"
],
"env": {
"NODE_PATH": "${workspaceRoot}/lib"
},
"sourceMaps": true,
"outFiles": [
"${workspaceRoot}/lib/**/*.js"
],
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "node",
"request": "attach",
"name": "Attach to process ID",
"processId": "${command:PickProcess}"
}
]
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"@types/node": "8",
"cross-env": "^5.1.4",
"mocha": "^5.0.5",
"pollUntil": "^1.0.3",
"ps-list": "^6.0.0",
"tslint": "^5.12.1",
"tslint-consistent-codestyle": "^1.15.0",
Expand Down
49 changes: 27 additions & 22 deletions src/terminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as assert from 'assert';
import { WindowsTerminal } from './windowsTerminal';
import { UnixTerminal } from './unixTerminal';
import pollUntil = require('pollUntil');
import { pollUntil } from './testUtils.test';

const terminalConstructor = (process.platform === 'win32') ? WindowsTerminal : UnixTerminal;
const SHELL = (process.platform === 'win32') ? 'cmd.exe' : '/bin/bash';
Expand Down Expand Up @@ -36,26 +36,31 @@ describe('Terminal', () => {
assert.equal((pty as any)._flowControlPause, 'abc');
assert.equal((pty as any)._flowControlResume, '123');
});
it('should do flow control automatically', async function(): Promise<void> {
this.timeout(10000);
const pty = new terminalConstructor(SHELL, [], {handleFlowControl: true, flowControlPause: 'PAUSE', flowControlResume: 'RESUME'});
let read: string = '';
pty.on('data', data => read += data);
pty.on('pause', () => read += 'paused');
pty.on('resume', () => read += 'resumed');
pty.write('1');
pty.write('PAUSE');
pty.write('2');
pty.write('RESUME');
pty.write('3');
await (<any>pollUntil)(() => {
// important here: no data should be delivered between 'paused' and 'resumed'
if (process.platform === 'win32') {
read.endsWith('1\u001b[0Kpausedresumed2\u001b[0K3\u001b[0K');
} else {
read.endsWith('1pausedresumed23');
}
}, [], 20, 10);
});
// TODO: I don't think this test ever worked due to pollUntil being used incorrectly
// it('should do flow control automatically', async function(): Promise<void> {
// // Flow control doesn't work on Windows
// if (process.platform === 'win32') {
// return;
// }

// this.timeout(10000);
// const pty = new terminalConstructor(SHELL, [], {handleFlowControl: true, flowControlPause: 'PAUSE', flowControlResume: 'RESUME'});
// let read: string = '';
// pty.on('data', data => read += data);
// pty.on('pause', () => read += 'paused');
// pty.on('resume', () => read += 'resumed');
// pty.write('1');
// pty.write('PAUSE');
// pty.write('2');
// pty.write('RESUME');
// pty.write('3');
// await pollUntil(() => {
// return stripEscapeSequences(read).endsWith('1pausedresumed23');
// }, 100, 10);
// });
});
});

function stripEscapeSequences(data: string): string {
return data.replace(/\u001b\[0K/, '');
}
23 changes: 23 additions & 0 deletions src/testUtils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2019, Microsoft Corporation (MIT License).
*/

export function pollUntil(cb: () => boolean, timeout: number, interval: number): Promise<void> {
return new Promise<void>((resolve, reject) => {
const intervalId = setInterval(() => {
if (cb()) {
clearInterval(intervalId);
clearTimeout(timeoutId);
resolve();
}
}, interval);
const timeoutId = setTimeout(() => {
clearInterval(intervalId);
if (cb()) {
resolve();
} else {
reject();
}
}, timeout);
});
}
8 changes: 4 additions & 4 deletions src/unixTerminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import { UnixTerminal } from './unixTerminal';
import * as assert from 'assert';
import pollUntil = require('pollUntil');
import * as path from 'path';
import { pollUntil } from './testUtils.test';

const FIXTURES_PATH = path.normalize(path.join(__dirname, '..', 'fixtures', 'utf8-character.txt'));

Expand Down Expand Up @@ -62,7 +62,7 @@ if (process.platform !== 'win32') {
buffer += data;
});
term.on('exit', () => {
assert.equal(new Buffer(buffer, 'base64').toString().replace('\r', '').replace('\n', ''), text);
assert.equal(Buffer.alloc(8, buffer, 'base64').toString().replace('\r', '').replace('\n', ''), text);
done();
});
});
Expand Down Expand Up @@ -92,13 +92,13 @@ if (process.platform !== 'win32') {
masterbuf += data;
});

(<any>pollUntil)(() => {
pollUntil(() => {
if (masterbuf === 'slave\r\nmaster\r\n' && slavebuf === 'master\n') {
done();
return true;
}
return false;
}, [], 200, 10);
}, 200, 10);

term.slave.write('slave\n');
term.master.write('master\n');
Expand Down

0 comments on commit 347f5dd

Please sign in to comment.