diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java index c4835add9e..f808fbfc94 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java @@ -41,9 +41,12 @@ * Returns already optimized XML if it's found in the cache. * * @since 0.28.11 - * @todo #2674:30min The function {@code OptCached.contains(final XML xml)} - * isn't work properly. This function compares caching and compilation times, - * which may lead to erroneous results. We need to fix this. + * @todo #2746:30min Use checksum, not time. + * The following tests show that fetching from the cache doesn't work correctly: + * - {@link OptCachedTest#returnsFromCacheCorrectProgram(Path path)}, + * - {@link OptCachedTest#returnsFromCacheButTimesSaveAndExecuteDifferent(Path path)}. + * Need to fix the file validation from cache: using checksum, but not time. + * Don't forget to enable the tests. */ public final class OptCached implements Optimization { diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java index 17f684ad45..7098317237 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/optimization/OptCachedTest.java @@ -63,7 +63,7 @@ void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path tmp) throws IOExcep final XML program = OptCachedTest.program(ZonedDateTime.now()); OptCachedTest.save(tmp, program); MatcherAssert.assertThat( - "We expected that the program will be returned from the cache", + "We expected that the program will be returned from the cache.", new OptCached( path -> { throw new IllegalStateException("This code shouldn't be executed"); @@ -74,6 +74,41 @@ void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path tmp) throws IOExcep ); } + @Disabled + @Test + void returnsFromCacheButTimesSaveAndExecuteDifferent(@TempDir final Path tmp) + throws IOException { + final XML program = OptCachedTest.program(ZonedDateTime.now().minusMinutes(2)); + OptCachedTest.save(tmp, program); + MatcherAssert.assertThat( + "We expected that the not immediately saved program will be returned from the cache.", + new OptCached( + path -> { + throw new IllegalStateException("This code shouldn't be executed"); + }, + tmp + ).apply(program), + Matchers.equalTo(program) + ); + } + + @Disabled + @Test + void returnsFromCacheCorrectProgram(@TempDir final Path tmp) + throws IOException { + final XML prev = OptCachedTest.program(ZonedDateTime.now(), "first program"); + OptCachedTest.save(tmp, prev); + final XML current = OptCachedTest.program(ZonedDateTime.now(), "second program"); + MatcherAssert.assertThat( + "Expecting current program to be compiled, but prev program was returned from cache.", + new OptCached( + path -> current, + tmp + ).apply(current), + Matchers.equalTo(current) + ); + } + @Test void optimizesIfXmlIsAbsentInCache(@TempDir final Path tmp) { final XML program = OptCachedTest.program(); @@ -148,12 +183,23 @@ private static XML program() { * @return XML representation of program. */ private static XML program(final ZonedDateTime time) { + return OptCachedTest.program(time, "same"); + } + + /** + * Generates EO program for tests with specified time and context. + * @param time Time. + * @param something String. + * @return XML representation of program. + */ + private static XML program(final ZonedDateTime time, final String something) { return new XMLDocument( new Xembler( new Directives() .add("program") .attr("name", "main") .attr("time", time.format(DateTimeFormatter.ISO_INSTANT)) + .attr("something", something) .up() ).xmlQuietly() );