Skip to content

Commit

Permalink
[Code] fix lsp init unit test by using fake timers (#37201)
Browse files Browse the repository at this point in the history
* [Code] fix LSP init unit test by using fake timers
  • Loading branch information
Yulong authored May 29, 2019
1 parent 0ff0936 commit 575cdbe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions x-pack/plugins/code/server/lsp/request_expander.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { InitializingError, RequestExpander } from './request_expander';
const options: ServerOptions = {
workspacePath: '/tmp/test/workspace',
};

beforeEach(async () => {
sinon.reset();
if (!fs.existsSync(options.workspacePath)) {
Expand All @@ -31,6 +30,7 @@ afterEach(() => {
});

test('requests should be sequential', async () => {
const clock = sinon.useFakeTimers();
// @ts-ignore
const proxyStub = sinon.createStubInstance(LanguageServerProxy, {
handleRequest: sinon.stub().callsFake(() => {
Expand All @@ -53,22 +53,28 @@ test('requests should be sequential', async () => {
};
const response1Promise = expander.handleRequest(request1);
const response2Promise = expander.handleRequest(request2);
clock.tick(100);
process.nextTick(() => clock.runAll());
const response1 = await response1Promise;
const response2 = await response2Promise;
// response2 should not be started before response1 ends.
expect(response1.result.end).toBeLessThanOrEqual(response2.result.start);
clock.restore();
});

test('requests should throw error after lsp init timeout', async () => {
const clock = sinon.useFakeTimers();
// @ts-ignore
const proxyStub = sinon.createStubInstance(LanguageServerProxy, {
handleRequest: sinon.stub().callsFake(() => Promise.resolve('ok')),
handleRequest: sinon.stub().callsFake(() => {
Promise.resolve('ok');
}),
initialize: sinon.stub().callsFake(
() =>
new Promise(resolve => {
setTimeout(() => {
resolve();
}, 200);
}, 300);
})
),
});
Expand All @@ -89,8 +95,11 @@ test('requests should throw error after lsp init timeout', async () => {
mkdirp.sync(request2.workspacePath);
const response1Promise = expander.handleRequest(request1);
const response2Promise = expander.handleRequest(request2);
clock.tick(400);
process.nextTick(() => clock.runAll());
await expect(response1Promise).rejects.toEqual(InitializingError);
await expect(response2Promise).rejects.toEqual(InitializingError);
clock.restore();
});

test('be able to open multiple workspace', async () => {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/code/server/lsp/request_expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ export class RequestExpander implements ILanguageServerHandler {

if (timeout > 0 && ws.initPromise) {
try {
const elasped = Date.now() - startTime;
await promiseTimeout(timeout - elasped, ws.initPromise);
const elapsed = Date.now() - startTime;
await promiseTimeout(timeout - elapsed, ws.initPromise);
} catch (e) {
if (e.isTimeout) {
throw InitializingError;
Expand Down

0 comments on commit 575cdbe

Please sign in to comment.