Skip to content

Commit 52e5d1a

Browse files
authored
test(transport): high-traffic RPC #412
Problem: In vscode-neovim/vscode-neovim#2184 (comment) it was reported that "Github Copilot Chat opens MANY buffers while writing code, and that somehow breaks the vscode-neovim extension". Solution: - Add test coverage for high amounts of RPC traffic. - TODO: this doesn't cover complex scenarios such as a looping `OptionSet` handler.
1 parent 3775c8d commit 52e5d1a

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

packages/neovim/src/api/Base.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export class BaseApi extends EventEmitter {
118118
});
119119
}
120120

121+
/** Sends a request to Nvim (the peer). */
121122
request(name: string, args: any[] = []): Promise<any> {
122123
return this.asyncRequest(name, args);
123124
}

packages/neovim/src/api/client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export class NeovimClient extends Neovim {
6363
return this.attachedBuffers.has(key);
6464
}
6565

66+
/** Handles incoming request (from the peer). */
6667
handleRequest(
6768
method: string,
6869
args: VimValue[],
@@ -85,6 +86,7 @@ export class NeovimClient extends Neovim {
8586
}
8687
}
8788

89+
/** Publishes to (local) subscribers of this `EventEmitter`. */
8890
emitNotification(method: string, args: any[]) {
8991
if (method.endsWith('_event')) {
9092
if (!method.startsWith('nvim_buf_')) {
@@ -114,6 +116,7 @@ export class NeovimClient extends Neovim {
114116
}
115117
}
116118

119+
/** Handles incoming notification (from the peer). */
117120
handleNotification(method: string, args: VimValue[], ...restArgs: any[]) {
118121
this.logger.info('handleNotification: %s', method);
119122
// If neovim API is not generated yet then queue up requests

packages/neovim/src/attach/attach.test.ts

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,33 @@ describe('Nvim API', () => {
1111
let proc: ReturnType<typeof testUtil.startNvim>[0];
1212
let nvim: ReturnType<typeof testUtil.startNvim>[1];
1313

14+
/** Incoming requests (from Nvim). */
15+
const requests: { method: string; args: number[] }[] = [];
16+
/** Incoming notifications (from Nvim). */
17+
const notifications: { method: string; args: number[] }[] = [];
18+
1419
before(async () => {
1520
[proc, nvim] = testUtil.startNvim();
16-
});
17-
18-
after(() => {
19-
testUtil.stopNvim();
20-
});
2121

22-
let requests: { method: string; args: number[] }[];
23-
let notifications: { method: string; args: number[] }[];
24-
25-
before(async () => {
22+
// Incoming requests (from Nvim).
2623
nvim.on('request', (method, args, resp) => {
2724
requests.push({ method, args });
2825
resp.send(`received ${method}(${args})`);
2926
});
27+
28+
// Incoming notifications (from Nvim).
3029
nvim.on('notification', (method, args) => {
3130
notifications.push({ method, args });
3231
});
3332
});
3433

34+
after(() => {
35+
testUtil.stopNvim();
36+
});
37+
3538
beforeEach(() => {
36-
requests = [];
37-
notifications = [];
39+
requests.length = 0;
40+
notifications.length = 0;
3841
});
3942

4043
it('failure modes', async () => {
@@ -101,6 +104,30 @@ describe('Nvim API', () => {
101104
testUtil.stopNvim(nvim2);
102105
});
103106

107+
it('noisy RPC traffic', async () => {
108+
let requestCount = 0;
109+
const oldRequest = nvim.request;
110+
nvim.request = function (
111+
this: any,
112+
name: string,
113+
args: any[] = []
114+
): Promise<any> {
115+
requestCount = requestCount + 1;
116+
return oldRequest.call(this, name, args);
117+
};
118+
119+
for (let i = 0; i < 99; i = i + 1) {
120+
nvim.command('noswapfile edit test-node-client.lua');
121+
nvim.command('bwipeout!');
122+
}
123+
124+
expect(requestCount).toEqual(99 * 2);
125+
// Still alive?
126+
expect(await nvim.eval('1+1')).toEqual(2);
127+
128+
nvim.request = oldRequest;
129+
});
130+
104131
it('can send requests and receive response', async () => {
105132
const result = await nvim.eval('{"k1": "v1", "k2": 2}');
106133
expect(result).toEqual({ k1: 'v1', k2: 2 });
@@ -149,7 +176,7 @@ describe('Nvim API', () => {
149176
end: -1,
150177
strictIndexing: true,
151178
});
152-
expect(lines).toEqual([]);
179+
expect(lines).toEqual(['']);
153180

154181
buf.setLines(['line1', 'line2'], { start: 0, end: 1 });
155182
const newLines = await buf.getLines({

0 commit comments

Comments
 (0)