Repository navigation
Expand file tree
/
Copy pathmessage.test.ts
More file actions
47 lines (41 loc) · 2.35 KB
/
Copy pathmessage.test.ts
File metadata and controls
47 lines (41 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import assert from 'assert';
import { createSandbox } from 'sinon';
import { MessageHandler, vscode } from '../message';
describe('MessageHandler pending replies', () => {
const sandbox = createSandbox();
afterEach(() => sandbox.restore());
it('releases successful and rejected replies while keeping outstanding requests', async () => {
const requests: string[] = [];
sandbox.stub(vscode, 'postMessage').callsFake((message: { req: string }) => {
requests.push(message.req);
});
const commands = sandbox.spy();
const handler = new MessageHandler(commands);
const first = handler.postMessage({ command: 'first' });
const second = handler.postMessage({ command: 'second' });
const secondAssertion = assert.rejects(second, error => error === 'Request failed');
assert.strictEqual(handler['_pendingReplies'].size, 2);
window.dispatchEvent(new MessageEvent('message', { data: { seq: requests[0], res: 'Result' } }));
assert.strictEqual(await first, 'Result');
assert.strictEqual(handler['_pendingReplies'].size, 1);
window.dispatchEvent(new MessageEvent('message', { data: { seq: requests[1], err: 'Request failed' } }));
await secondAssertion;
assert.strictEqual(handler['_pendingReplies'].size, 0);
window.dispatchEvent(new MessageEvent('message', { data: { seq: requests[0], res: 'Duplicate' } }));
window.dispatchEvent(new MessageEvent('message', { data: { seq: 'unknown', res: 'Unknown' } }));
assert.strictEqual(commands.callCount, 0);
window.dispatchEvent(new MessageEvent('message', { data: { res: 'Command' } }));
assert(commands.calledOnceWithExactly('Command'));
});
it('releases callbacks when sending throws synchronously', async () => {
const failure = new Error('Transport failed');
sandbox.stub(vscode, 'postMessage').throws(failure);
const handler = new MessageHandler(null);
await assert.rejects(handler.postMessage({ command: 'failure' }), error => error === failure);
assert.strictEqual(handler['_pendingReplies'].size, 0);
});
});