From e2724719bdf860edc67781045237eb20c55876e7 Mon Sep 17 00:00:00 2001 From: Alekseeva Yana Date: Thu, 28 Dec 2023 14:46:58 +0300 Subject: [PATCH] feat(#2746):added new tests in OptCachedTest and todo --- .../eolang/maven/optimization/OptCached.java | 9 ++- .../maven/optimization/OptCachedTest.java | 57 +++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) 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..3d72490de0 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 The tests + * {@link org.eolang.maven.optimization.OptCachedTest#returnsFromCacheCorrectProgram(Path path)} + * and + * {@link org.eolang.maven.optimization.OptCachedTest#returnsFromCacheCorrectProgram(Path path)} + * show that getting from cache don't work correctly. + * Need to fix file validation from cache: using checksum, but not time. */ 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..90572d93e0 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 @@ -74,6 +74,44 @@ void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path tmp) throws IOExcep ); } + @Disabled + @Test + void returnsFromCacheButTimesSaveAndExecuteDifferent(@TempDir final Path tmp) + throws IOException, InterruptedException { + final XML program = OptCachedTest.program(ZonedDateTime.now()); + Thread.sleep(70_000); + OptCachedTest.save(tmp, program); + MatcherAssert.assertThat( + "We expected that the 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, InterruptedException { + XML program = OptCachedTest.programWithSomethings(ZonedDateTime.now(), "first program"); + OptCachedTest.save(tmp, program); + program = OptCachedTest.programWithSomethings(ZonedDateTime.now(), "second program"); + MatcherAssert.assertThat( + "We expected that the correct 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) + ); + } + @Test void optimizesIfXmlIsAbsentInCache(@TempDir final Path tmp) { final XML program = OptCachedTest.program(); @@ -158,4 +196,23 @@ private static XML program(final ZonedDateTime time) { ).xmlQuietly() ); } + + /** + * Generates EO program for tests with specified time and content. + * @param time Time. + * @param something String. + * @return XML representation of program. + */ + private static XML programWithSomethings(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() + ); + } }