From 5ecd342586115877e2e44c387c8bb2fb1293b3a7 Mon Sep 17 00:00:00 2001 From: Robert Weber Date: Sun, 6 Oct 2019 21:58:58 -0700 Subject: [PATCH] lib: add configurable timeout for all guru operations --- lib/highlight/highlight-provider.js | 5 ++- lib/implements/implements.js | 5 ++- lib/references/references-provider.js | 6 +++- package.json | 7 ++++ spec/implements/implements-spec.js | 16 +++++++++ spec/references/references-provider-spec.js | 40 ++++++++++++++++++++- 6 files changed, 75 insertions(+), 4 deletions(-) diff --git a/lib/highlight/highlight-provider.js b/lib/highlight/highlight-provider.js index afd673a8..975ee442 100644 --- a/lib/highlight/highlight-provider.js +++ b/lib/highlight/highlight-provider.js @@ -42,7 +42,10 @@ class HighlightProvider { if (!args) return null const options = {} - options.timeout = 30000 + const t = atom.config.get('go-plus.guru.timeout') + const timeout = typeof t === 'number' ? t : 30000 + options.timeout = timeout + const archive = buildGuruArchive(editor) if (archive && archive.length) { options.input = archive diff --git a/lib/implements/implements.js b/lib/implements/implements.js index be362b8b..d3696f1a 100644 --- a/lib/implements/implements.js +++ b/lib/implements/implements.js @@ -56,7 +56,10 @@ class Implements implements PanelModel { async runGuru(args: Array) { const options = {} - options.timeout = 20000 + const t = atom.config.get('go-plus.guru.timeout') + const timeout = typeof t === 'number' ? t : 30000 + options.timeout = timeout + const archive = buildGuruArchive() if (archive && archive.length) { options.input = archive diff --git a/lib/references/references-provider.js b/lib/references/references-provider.js index 29c1c5f3..18a68017 100644 --- a/lib/references/references-provider.js +++ b/lib/references/references-provider.js @@ -76,8 +76,12 @@ class ReferencesProvider { const offset = utf8OffsetForBufferPosition(position, editor) const args = computeArgs('referrers', null, editor, offset) || [] + const options = {} - options.timeout = 30000 + const t = atom.config.get('go-plus.guru.timeout') + const timeout = typeof t === 'number' ? t : 30000 + options.timeout = timeout + const archive = buildGuruArchive(editor) if (archive && archive.length) { options.input = archive diff --git a/package.json b/package.json index cd1ffb66..4e9988cc 100644 --- a/package.json +++ b/package.json @@ -384,6 +384,13 @@ "type": "boolean", "default": true, "order": 1 + }, + "timeout": { + "title": "Timeout", + "description": "Stop Guru commands from runnning after this number of milliseconds", + "type": "integer", + "default": 30000, + "order": 2 } } }, diff --git a/spec/implements/implements-spec.js b/spec/implements/implements-spec.js index 173141f5..12db745b 100644 --- a/spec/implements/implements-spec.js +++ b/spec/implements/implements-spec.js @@ -72,4 +72,20 @@ describe('implements', () => { expect(args1.from[0].name).toBe('implements.Fooer') expect(args1.from[1].name).toBe('io.Reader') }) + + it('times out according to the configured guru timeout', async () => { + const testTimeout = 1 + atom.config.set('go-plus.guru.timeout', testTimeout) + editor.setCursorBufferPosition([4, 9]) + await impl.handleCommand() + expect(impl.view.update).toHaveBeenCalled() + expect(impl.view.update.calls.length).toBe(2) + + const args0 = impl.view.update.calls[0].args[0] + const args1 = impl.view.update.calls[1].args[0] + expect(args0.startsWith('running guru')).toBe(true) + expect(typeof args1).toBe('string') + expect(args1.startsWith('guru failed')).toBe(true) + expect(args1.endsWith(testTimeout + ' ms')).toBe(true) + }) }) diff --git a/spec/references/references-provider-spec.js b/spec/references/references-provider-spec.js index 511a5f5c..1d53a43e 100644 --- a/spec/references/references-provider-spec.js +++ b/spec/references/references-provider-spec.js @@ -4,7 +4,8 @@ import { lifecycle } from './../spec-helpers' import { ReferencesProvider } from './../../lib/references/references-provider' import path from 'path' -import fs from 'fs' +import fs from 'fs-extra' +import { it, beforeEach } from '../async-spec-helpers' // eslint-disable-line describe('References Provider', () => { let references @@ -60,4 +61,41 @@ describe('References Provider', () => { ) }) }) + + describe('findReferences', () => { + let editor + let gopath = null + let source = null + let target = null + + beforeEach(async () => { + gopath = fs.realpathSync(lifecycle.temp.mkdirSync('gopath-')) + process.env.GOPATH = gopath + + await lifecycle.activatePackage() + const { mainModule } = lifecycle + references = mainModule.provideReferences() + + source = path.join(__dirname, '..', 'fixtures') + target = path.join(gopath, 'src', 'references') + fs.copySync(source, target) + editor = await atom.workspace.open(path.join(target || '.', 'doc.go')) + }) + afterEach(() => { + lifecycle.teardown() + }) + + it('times out according to the configured Guru timeout', async () => { + const testTimeout = 1 + atom.config.set('go-plus.guru.timeout', testTimeout) + editor.setCursorBufferPosition([22, 2]) + const refs = await references.findReferences( + editor, + editor.getCursorBufferPosition() + ) + expect(typeof refs).toBe('object') + expect(refs.type).toBe('error') + expect(refs.message.endsWith(testTimeout + 'ms')).toBe(true) + }) + }) })