Skip to content

Commit

Permalink
Remove pollUntil, add unit test debug target
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyriar committed Aug 8, 2019
1 parent 377e6c4 commit b2d1e89
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 802 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
22 changes: 13 additions & 9 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 @@ -37,6 +37,11 @@ describe('Terminal', () => {
assert.equal((pty as any)._flowControlResume, '123');
});
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 = '';
Expand All @@ -48,14 +53,13 @@ describe('Terminal', () => {
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);
await pollUntil(() => {
return stripEscapeSequences(read).endsWith('1pausedresumed23');
}, 100, 10);
});
});
});

function stripEscapeSequences(data: string): string {
return data.replace(/\u001b\[0K/, '');
}
1 change: 1 addition & 0 deletions src/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export abstract class Terminal implements ITerminal {

public write(data: string): void {
if (this.handleFlowControl) {
console.log('handle flow control');
// PAUSE/RESUME messages are not forwarded to the pty
if (data === this._flowControlPause) {
this.pause();
Expand Down
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);
});
}
6 changes: 3 additions & 3 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 @@ -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
Loading

0 comments on commit b2d1e89

Please sign in to comment.