Adding more tests for cached response

This commit is contained in:
Matt Bierner
2018-12-10 14:12:25 -08:00
parent 2b7278c712
commit 7683decdb5

View File

@@ -12,40 +12,34 @@ import { ServerResponse, CancelledResponse } from '../typescriptService';
suite('CachedResponse', () => {
test('should cache simple response for same document', async () => {
const doc = await vscode.workspace.openTextDocument({ language: 'javascript', content: '' });
const doc = await createTextDocument();
const response = new CachedResponse();
const responder = createSequentialResponder('test');
assertResult(await response.execute(doc, responder), 'test-0');
assertResult(await response.execute(doc, responder), 'test-0');
assertResult(await response.execute(doc, respondWith('test-0')), 'test-0');
assertResult(await response.execute(doc, respondWith('test-1')), 'test-0');
});
test('should invalidate cache for new document', async () => {
const doc1 = await vscode.workspace.openTextDocument({ language: 'javascript', content: '' });
const doc2 = await vscode.workspace.openTextDocument({ language: 'javascript', content: '' });
const doc1 = await createTextDocument();
const doc2 = await createTextDocument();
const response = new CachedResponse();
const responder = createSequentialResponder('test');
assertResult(await response.execute(doc1, responder), 'test-0');
assertResult(await response.execute(doc1, responder), 'test-0');
assertResult(await response.execute(doc2, responder), 'test-1');
assertResult(await response.execute(doc2, responder), 'test-1');
assertResult(await response.execute(doc1, responder), 'test-2');
assertResult(await response.execute(doc1, responder), 'test-2');
assertResult(await response.execute(doc1, respondWith('test-0')), 'test-0');
assertResult(await response.execute(doc1, respondWith('test-1')), 'test-0');
assertResult(await response.execute(doc2, respondWith('test-2')), 'test-2');
assertResult(await response.execute(doc2, respondWith('test-3')), 'test-2');
assertResult(await response.execute(doc1, respondWith('test-4')), 'test-4');
assertResult(await response.execute(doc1, respondWith('test-5')), 'test-4');
});
test('should not cache cancelled responses', async () => {
const doc = await vscode.workspace.openTextDocument({ language: 'javascript', content: '' });
const doc = await createTextDocument();
const response = new CachedResponse();
const responder = createSequentialResponder('test');
const cancelledResponder = createEventualResponder<CancelledResponse>();
const result1 = response.execute(doc, () => cancelledResponder.promise);
const result2 = response.execute(doc, responder);
const result3 = response.execute(doc, responder);
const result2 = response.execute(doc, respondWith('test-0'));
const result3 = response.execute(doc, respondWith('test-1'));
cancelledResponder.resolve(new CancelledResponse('cancelled'));
@@ -53,8 +47,58 @@ suite('CachedResponse', () => {
assertResult(await result2, 'test-0');
assertResult(await result3, 'test-0');
});
test('should not care if subsequent requests are cancelled if first request is resolved ok', async () => {
const doc = await createTextDocument();
const response = new CachedResponse();
const cancelledResponder = createEventualResponder<CancelledResponse>();
const result1 = response.execute(doc, respondWith('test-0'));
const result2 = response.execute(doc, () => cancelledResponder.promise);
const result3 = response.execute(doc, respondWith('test-1'));
cancelledResponder.resolve(new CancelledResponse('cancelled'));
assertResult(await result1, 'test-0');
assertResult(await result2, 'test-0');
assertResult(await result3, 'test-0');
});
test('should not cache cancelled responses with document changes', async () => {
const doc1 = await createTextDocument();
const doc2 = await createTextDocument();
const response = new CachedResponse();
const cancelledResponder = createEventualResponder<CancelledResponse>();
const cancelledResponder2 = createEventualResponder<CancelledResponse>();
const result1 = response.execute(doc1, () => cancelledResponder.promise);
const result2 = response.execute(doc1, respondWith('test-0'));
const result3 = response.execute(doc1, respondWith('test-1'));
const result4 = response.execute(doc2, () => cancelledResponder2.promise);
const result5 = response.execute(doc2, respondWith('test-2'));
const result6 = response.execute(doc1, respondWith('test-3'));
cancelledResponder.resolve(new CancelledResponse('cancelled'));
cancelledResponder2.resolve(new CancelledResponse('cancelled'));
assert.strictEqual((await result1).type, 'cancelled');
assertResult(await result2, 'test-0');
assertResult(await result3, 'test-0');
assert.strictEqual((await result4).type, 'cancelled');
assertResult(await result5, 'test-2');
assertResult(await result6, 'test-3');
});
});
function respondWith(command: string) {
return async () => createResponse(command);
}
function createTextDocument() {
return vscode.workspace.openTextDocument({ language: 'javascript', content: '' });
}
function assertResult(result: ServerResponse<Proto.Response>, command: string) {
if (result.type === 'response') {
assert.strictEqual(result.command, command);
@@ -74,12 +118,7 @@ function createResponse(command: string): Proto.Response {
};
}
function createSequentialResponder(prefix: string) {
let count = 0;
return async () => createResponse(`${prefix}-${count++}`);
}
function createEventualResponder<T>(): {promise: Promise<T>, resolve: (x: T) => void } {
function createEventualResponder<T>(): { promise: Promise<T>, resolve: (x: T) => void } {
let resolve: (value: T) => void;
const promise = new Promise<T>(r => { resolve = r; });
return { promise, resolve: resolve! };