|
| 1 | +package io.jenkins.plugins.pipelinegraphview.utils; |
| 2 | + |
| 3 | +import static org.awaitility.Awaitility.await; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | +import static org.hamcrest.Matchers.equalTo; |
| 6 | +import static org.hamcrest.Matchers.greaterThan; |
| 7 | +import static org.hamcrest.Matchers.is; |
| 8 | +import static org.hamcrest.Matchers.not; |
| 9 | +import static org.hamcrest.Matchers.nullValue; |
| 10 | +import static org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep.*; |
| 11 | + |
| 12 | +import hudson.XmlFile; |
| 13 | +import hudson.model.Result; |
| 14 | +import hudson.util.XStream2; |
| 15 | +import io.jenkins.plugins.pipelinegraphview.utils.PipelineGraphViewCache.CachedPayload; |
| 16 | +import java.io.File; |
| 17 | +import java.nio.file.Files; |
| 18 | +import java.time.Duration; |
| 19 | +import java.util.concurrent.atomic.AtomicInteger; |
| 20 | +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; |
| 21 | +import org.jenkinsci.plugins.workflow.job.WorkflowJob; |
| 22 | +import org.jenkinsci.plugins.workflow.job.WorkflowRun; |
| 23 | +import org.junit.jupiter.api.BeforeEach; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.jvnet.hudson.test.JenkinsRule; |
| 26 | +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; |
| 27 | + |
| 28 | +@WithJenkins |
| 29 | +class PipelineGraphViewCacheTest { |
| 30 | + |
| 31 | + private static final String CACHE_FILE_NAME = "pipeline-graph-view-cache.xml"; |
| 32 | + |
| 33 | + private JenkinsRule j; |
| 34 | + private PipelineGraphViewCache cache; |
| 35 | + |
| 36 | + @BeforeEach |
| 37 | + void setUp(JenkinsRule j) { |
| 38 | + this.j = j; |
| 39 | + // Use a fresh per-test instance so state doesn't leak between cases. |
| 40 | + this.cache = new PipelineGraphViewCache(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void coldCache_computesAndWritesFile() throws Exception { |
| 45 | + WorkflowRun run = TestUtils.createAndRunJob(j, "cold", "smokeTest.jenkinsfile", Result.FAILURE); |
| 46 | + File cacheFile = new File(run.getRootDir(), CACHE_FILE_NAME); |
| 47 | + assertThat("no cache file before first call", cacheFile.exists(), is(false)); |
| 48 | + |
| 49 | + AtomicInteger computes = new AtomicInteger(); |
| 50 | + PipelineGraph graph = cache.getGraph(run, () -> { |
| 51 | + computes.incrementAndGet(); |
| 52 | + return new PipelineGraphApi(run).computeTree(); |
| 53 | + }); |
| 54 | + |
| 55 | + assertThat(graph, is(not(nullValue()))); |
| 56 | + assertThat(graph.stages.isEmpty(), is(false)); |
| 57 | + assertThat("supplier ran exactly once", computes.get(), equalTo(1)); |
| 58 | + assertThat("cache file exists after first call", cacheFile.exists(), is(true)); |
| 59 | + assertThat("cache file is non-empty", Files.size(cacheFile.toPath()), greaterThan(0L)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void warmCache_returnsPayloadWithoutComputing() throws Exception { |
| 64 | + WorkflowRun run = TestUtils.createAndRunJob(j, "warm", "smokeTest.jenkinsfile", Result.FAILURE); |
| 65 | + cache.getGraph(run, () -> new PipelineGraphApi(run).computeTree()); |
| 66 | + |
| 67 | + // Drop in-memory cache to force re-read from disk on next call. |
| 68 | + cache.invalidateMemory(); |
| 69 | + |
| 70 | + AtomicInteger computes = new AtomicInteger(); |
| 71 | + PipelineGraph again = cache.getGraph(run, () -> { |
| 72 | + computes.incrementAndGet(); |
| 73 | + return new PipelineGraphApi(run).computeTree(); |
| 74 | + }); |
| 75 | + |
| 76 | + assertThat(again, is(not(nullValue()))); |
| 77 | + assertThat("supplier did not run — served from disk", computes.get(), equalTo(0)); |
| 78 | + assertThat(again.stages.isEmpty(), is(false)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void inProgressRun_doesNotPersist() throws Exception { |
| 83 | + WorkflowRun run = startLongRunningJob(); |
| 84 | + try { |
| 85 | + AtomicInteger computes = new AtomicInteger(); |
| 86 | + cache.getGraph(run, () -> { |
| 87 | + computes.incrementAndGet(); |
| 88 | + return new PipelineGraphApi(run).computeTree(); |
| 89 | + }); |
| 90 | + cache.getGraph(run, () -> { |
| 91 | + computes.incrementAndGet(); |
| 92 | + return new PipelineGraphApi(run).computeTree(); |
| 93 | + }); |
| 94 | + |
| 95 | + File cacheFile = new File(run.getRootDir(), CACHE_FILE_NAME); |
| 96 | + assertThat("no cache file while run is in-progress", cacheFile.exists(), is(false)); |
| 97 | + assertThat("both calls recomputed", computes.get(), equalTo(2)); |
| 98 | + } finally { |
| 99 | + run.getExecutor().interrupt(); |
| 100 | + j.waitForCompletion(run); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void schemaVersionMismatch_isIgnoredAndRecomputed() throws Exception { |
| 106 | + WorkflowRun run = TestUtils.createAndRunJob(j, "schema", "smokeTest.jenkinsfile", Result.FAILURE); |
| 107 | + File cacheFile = new File(run.getRootDir(), CACHE_FILE_NAME); |
| 108 | + |
| 109 | + // Write a stale-version payload directly to disk. |
| 110 | + CachedPayload stale = new CachedPayload(); |
| 111 | + stale.schemaVersion = Integer.MIN_VALUE; |
| 112 | + XStream2 xstream = new XStream2(); |
| 113 | + xstream.alias("pipeline-graph-view-cache", CachedPayload.class); |
| 114 | + new XmlFile(xstream, cacheFile).write(stale); |
| 115 | + assertThat(cacheFile.exists(), is(true)); |
| 116 | + |
| 117 | + AtomicInteger computes = new AtomicInteger(); |
| 118 | + PipelineGraph graph = cache.getGraph(run, () -> { |
| 119 | + computes.incrementAndGet(); |
| 120 | + return new PipelineGraphApi(run).computeTree(); |
| 121 | + }); |
| 122 | + |
| 123 | + assertThat("stale file ignored; supplier ran", computes.get(), equalTo(1)); |
| 124 | + assertThat(graph.stages.isEmpty(), is(false)); |
| 125 | + } |
| 126 | + |
| 127 | + private WorkflowRun startLongRunningJob() throws Exception { |
| 128 | + String jenkinsfile = "node { echo 'hi'; semaphore 'wait' }"; |
| 129 | + var job = j.createProject(WorkflowJob.class, "running"); |
| 130 | + job.setDefinition(new CpsFlowDefinition(jenkinsfile, true)); |
| 131 | + var future = job.scheduleBuild2(0); |
| 132 | + WorkflowRun run = future.waitForStart(); |
| 133 | + // Wait until the semaphore step is actually blocking so the run is reliably "building". |
| 134 | + await().atMost(Duration.ofSeconds(30)).until(run::isBuilding); |
| 135 | + waitForStart("wait/1", run); |
| 136 | + return run; |
| 137 | + } |
| 138 | +} |
0 commit comments