From f77cb3786c886afa67588f99acdaf985aae1f14b Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Thu, 3 Sep 2015 16:55:30 -0600 Subject: [PATCH] Terminate all directory searches if any fail MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit @bolinfest: I was seeing exceptions after the test terminated due to one of the searches still running. I think since the promise rejects with “all” semantics that we should cancel any other searches when one fails. I wouldn’t expect them to continue if the promise is no longer unresolved. --- spec/workspace-spec.coffee | 17 +++++++++++++++-- src/workspace.coffee | 7 ++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index 57fba6401..b42a882b8 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -1135,15 +1135,28 @@ describe "Workspace", -> expect(resultOfPromiseSearch).toBe('cancelled') it "will have the side-effect of failing the overall search if it fails", -> - cancelableSearch = atom.workspace.scan /aaaa/, -> - fakeSearch.hoistedReject() + # This provider's search should be cancelled when the first provider fails + fakeSearch2 = null + atom.packages.serviceHub.provide('atom.directory-searcher', '0.1.0', { + canSearchDirectory: (directory) -> directory.getPath() is dir2 + search: (directory, regex, options) -> fakeSearch2 = new FakeSearch(options) + }) didReject = false + promise = cancelableSearch = atom.workspace.scan /aaaa/, -> + waitsFor 'fakeSearch to be defined', -> fakeSearch? + + runs -> + fakeSearch.hoistedReject() + waitsForPromise -> cancelableSearch.catch -> didReject = true + waitsFor (done) -> promise.then(null, done) + runs -> expect(didReject).toBe(true) + expect(fakeSearch2.cancelled).toBe true # Cancels other ongoing searches describe "::replace(regex, replacementText, paths, iterator)", -> [filePath, commentFilePath, sampleContent, sampleCommentContent] = [] diff --git a/src/workspace.coffee b/src/workspace.coffee index 62fda5aed..c04162e34 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -904,7 +904,12 @@ class Workspace extends Model resolve('cancelled') else resolve(null) - searchPromise.then(onSuccess, reject) + + onFailure = -> + promise.cancel() for promise in allSearches + reject() + + searchPromise.then(onSuccess, onFailure) cancellablePromise.cancel = -> isCancelled = true # Note that cancelling all of the members of allSearches will cause all of the searches