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

Commit

Permalink
lib: add configurable timeout for all guru operations
Browse files Browse the repository at this point in the history
  • Loading branch information
robbawebba committed Oct 7, 2019
1 parent 0192d47 commit 50d2875
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/highlight/highlight-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion lib/implements/implements.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ class Implements implements PanelModel {

async runGuru(args: Array<string>) {
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
Expand Down
6 changes: 5 additions & 1 deletion lib/references/references-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
},
Expand Down
15 changes: 15 additions & 0 deletions spec/implements/implements-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,19 @@ 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(args1.startsWith('guru failed')).toBe(true)
expect(args1.endsWith(testTimeout + ' ms')).toBe(true)
})
})
40 changes: 39 additions & 1 deletion spec/references/references-provider-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
})
})

0 comments on commit 50d2875

Please sign in to comment.