Skip to content
This repository was archived by the owner on May 7, 2021. It is now read-only.

Commit d97a3d5

Browse files
committed
lib: add configurable timeout for all guru operations
1 parent 0192d47 commit d97a3d5

File tree

6 files changed

+74
-4
lines changed

6 files changed

+74
-4
lines changed

lib/highlight/highlight-provider.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ class HighlightProvider {
4242
if (!args) return null
4343

4444
const options = {}
45-
options.timeout = 30000
45+
const t = atom.config.get('go-plus.guru.timeout')
46+
const timeout = typeof t === 'number' ? t : 30000
47+
options.timeout = timeout
48+
4649
const archive = buildGuruArchive(editor)
4750
if (archive && archive.length) {
4851
options.input = archive

lib/implements/implements.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ class Implements implements PanelModel {
5656

5757
async runGuru(args: Array<string>) {
5858
const options = {}
59-
options.timeout = 20000
59+
const t = atom.config.get('go-plus.guru.timeout')
60+
const timeout = typeof t === 'number' ? t : 30000
61+
options.timeout = timeout
62+
6063
const archive = buildGuruArchive()
6164
if (archive && archive.length) {
6265
options.input = archive

lib/references/references-provider.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ class ReferencesProvider {
7676

7777
const offset = utf8OffsetForBufferPosition(position, editor)
7878
const args = computeArgs('referrers', null, editor, offset) || []
79+
7980
const options = {}
80-
options.timeout = 30000
81+
const t = atom.config.get('go-plus.guru.timeout')
82+
const timeout = typeof t === 'number' ? t : 30000
83+
options.timeout = timeout
84+
8185
const archive = buildGuruArchive(editor)
8286
if (archive && archive.length) {
8387
options.input = archive

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,13 @@
384384
"type": "boolean",
385385
"default": true,
386386
"order": 1
387+
},
388+
"timeout": {
389+
"title": "Timeout",
390+
"description": "Stop Guru commands from runnning after this number of milliseconds",
391+
"type": "integer",
392+
"default": 30000,
393+
"order": 2
387394
}
388395
}
389396
},

spec/implements/implements-spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,19 @@ describe('implements', () => {
7272
expect(args1.from[0].name).toBe('implements.Fooer')
7373
expect(args1.from[1].name).toBe('io.Reader')
7474
})
75+
76+
it('times out according to the configured guru timeout', async () => {
77+
const testTimeout = 1
78+
atom.config.set('go-plus.guru.timeout', testTimeout)
79+
editor.setCursorBufferPosition([4, 9])
80+
await impl.handleCommand()
81+
expect(impl.view.update).toHaveBeenCalled()
82+
expect(impl.view.update.calls.length).toBe(2)
83+
84+
const args0 = impl.view.update.calls[0].args[0]
85+
const args1 = impl.view.update.calls[1].args[0]
86+
expect(args0.startsWith('running guru')).toBe(true)
87+
expect(args1.startsWith('guru failed')).toBe(true)
88+
expect(args1.endsWith(testTimeout + ' ms')).toBe(true)
89+
})
7590
})

spec/references/references-provider-spec.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import { lifecycle } from './../spec-helpers'
55
import { ReferencesProvider } from './../../lib/references/references-provider'
66
import path from 'path'
7-
import fs from 'fs'
7+
import fs from 'fs-extra'
8+
import { it, beforeEach } from '../async-spec-helpers' // eslint-disable-line
89

910
describe('References Provider', () => {
1011
let references
@@ -60,4 +61,41 @@ describe('References Provider', () => {
6061
)
6162
})
6263
})
64+
65+
describe('findReferences', () => {
66+
let editor
67+
let gopath = null
68+
let source = null
69+
let target = null
70+
71+
beforeEach(async () => {
72+
gopath = fs.realpathSync(lifecycle.temp.mkdirSync('gopath-'))
73+
process.env.GOPATH = gopath
74+
75+
await lifecycle.activatePackage()
76+
const { mainModule } = lifecycle
77+
references = mainModule.provideReferences()
78+
79+
source = path.join(__dirname, '..', 'fixtures')
80+
target = path.join(gopath, 'src', 'references')
81+
fs.copySync(source, target)
82+
editor = await atom.workspace.open(path.join(target || '.' , 'doc.go'))
83+
})
84+
afterEach(() => {
85+
lifecycle.teardown()
86+
})
87+
88+
it('times out according to the configured Guru timeout', async () => {
89+
const testTimeout = 1
90+
atom.config.set('go-plus.guru.timeout', testTimeout)
91+
editor.setCursorBufferPosition([22, 2])
92+
const refs = await references.findReferences(
93+
editor,
94+
editor.getCursorBufferPosition()
95+
)
96+
expect(typeof refs).toBe('object')
97+
expect(refs.type).toBe('error')
98+
expect(refs.message.endsWith(testTimeout + 'ms')).toBe(true)
99+
})
100+
})
63101
})

0 commit comments

Comments
 (0)