Skip to content

Commit

Permalink
JENKINS-48716 Prevent Guava InvalidCacheLoadException (#136)
Browse files Browse the repository at this point in the history
JENKINS-48716 JENKINS-35998 Prevent Guava InvalidCacheLoadException
  • Loading branch information
gmshake authored and warden committed Dec 27, 2017
1 parent 678f540 commit a1909b0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/main/java/hudson/plugins/jira/JiraSite.java
Expand Up @@ -7,6 +7,7 @@
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import com.cloudbees.hudson.plugins.folder.AbstractFolder;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import hudson.Extension;
Expand Down Expand Up @@ -163,7 +164,7 @@ public class JiraSite extends AbstractDescribableImpl<JiraSite> {
// should we implement to invalidate this (say every hour)?
private transient volatile Set<String> projects;

private transient Cache<String, Issue> issueCache = makeIssueCache();
private transient Cache<String, Optional<Issue>> issueCache = makeIssueCache();

/**
* Used to guard the computation of {@link #projects}
Expand Down Expand Up @@ -251,7 +252,7 @@ protected Object readResolve() {
return this;
}

private static Cache<String, Issue> makeIssueCache() {
private static Cache<String, Optional<Issue>> makeIssueCache() {
return CacheBuilder.newBuilder().concurrencyLevel(2).expireAfterAccess(2, TimeUnit.MINUTES).build();
}

Expand Down Expand Up @@ -464,22 +465,22 @@ public boolean existsIssue(String id) {
public JiraIssue getIssue(final String id) throws IOException {
try {

Issue issue = issueCache.get(id, new Callable<Issue>() {
public Issue call() throws Exception {
Optional<Issue> issue = issueCache.get(id, new Callable<Optional<Issue>>() {
public Optional<Issue> call() throws Exception {
JiraSession session = getSession();
Issue issue = null;
if (session != null) {
issue = session.getIssue(id);
}
return issue;
return Optional.fromNullable(issue);
}
});

if (issue == null) {
if (!issue.isPresent()) {
return null;
}

return new JiraIssue(issue);
return new JiraIssue(issue.get());
} catch (ExecutionException e) {
throw new IOException(e);
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/groovy/hudson/plugins/jira/JiraSiteTest.groovy
@@ -1,5 +1,6 @@
package hudson.plugins.jira

import com.atlassian.jira.rest.client.api.domain.Issue
import com.cloudbees.hudson.plugins.folder.AbstractFolder
import com.cloudbees.hudson.plugins.folder.AbstractFolderProperty
import hudson.model.ItemGroup
Expand Down Expand Up @@ -106,4 +107,33 @@ class JiraSiteTest extends Specification {
0 * _._
result==site2
}


def "getIssue"() {
given:
// FIXME We need JENKINS-47500 to be backported into JENKINS 1.642 to use null password here.
def site = new JiraSite(null, null, null, "{*** HACK INVALID BASE64 PASSWORD ***}", false, false, null, false, null, null, false)
def session = Mock(JiraSession)
def issue = Mock(Issue)
site.jiraSession = session

when: "issue exists"
def result = site.getIssue("FOO-1")

then:
1 * issue.getKey() >> "FOO-1"
1 * issue.getSummary() >> "commit message"
1 * session.getIssue("FOO-1") >> issue
0 * _._
result != null
result.getKey() == "FOO-1"

when: "no issue found"
result = site.getIssue("BAR-2")

then:
1 * session.getIssue("BAR-2") >> null
0 * _._
result == null
}
}

0 comments on commit a1909b0

Please sign in to comment.