From 3dee2d317c2ddd8485ddc207708d0fbadc353502 Mon Sep 17 00:00:00 2001 From: "taylor.smock" Date: Tue, 31 Oct 2023 20:27:34 +0000 Subject: [PATCH] Fix #23257: MVTTileTest has an infrequently hit race condition (patch by marcello, modified) The race condition is dependent upon calculation speed; if the layers get loaded prior to the image loading, it is ''possible'' for us to get to the point where we expect the image to be loaded, but it isn't yet. Modifications are as follows: * Reinitialize tileSource and loader before each test, but initialize the cache once and clear that between tests. git-svn-id: https://josm.openstreetmap.de/svn/trunk@18892 0c6e7542-c601-0410-84e7-c038aed88b3b --- .../vectortile/mapbox/MVTTileTest.java | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/test/unit/org/openstreetmap/josm/data/imagery/vectortile/mapbox/MVTTileTest.java b/test/unit/org/openstreetmap/josm/data/imagery/vectortile/mapbox/MVTTileTest.java index 67f0a378388..88950ca7889 100644 --- a/test/unit/org/openstreetmap/josm/data/imagery/vectortile/mapbox/MVTTileTest.java +++ b/test/unit/org/openstreetmap/josm/data/imagery/vectortile/mapbox/MVTTileTest.java @@ -8,8 +8,11 @@ import java.util.Collections; import java.util.stream.Stream; +import org.apache.commons.jcs3.access.behavior.ICacheAccess; import org.awaitility.Awaitility; import org.awaitility.Durations; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -17,6 +20,7 @@ import org.openstreetmap.gui.jmapviewer.Tile; import org.openstreetmap.gui.jmapviewer.interfaces.TileJob; import org.openstreetmap.josm.TestUtils; +import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry; import org.openstreetmap.josm.data.cache.JCSCacheManager; import org.openstreetmap.josm.data.imagery.ImageryInfo; import org.openstreetmap.josm.data.imagery.TileJobOptions; @@ -25,15 +29,28 @@ * Test class for {@link MVTTile} */ class MVTTileTest { + private static ICacheAccess cache; private MapboxVectorTileSource tileSource; private MapboxVectorCachedTileLoader loader; + + @BeforeAll + static void classSetup() { + cache = JCSCacheManager.getCache("testMapillaryCache"); + } + + @AfterAll + static void classTearDown() { + cache.clear(); + cache = null; + } + @BeforeEach void setup() { + cache.clear(); tileSource = new MapboxVectorTileSource(new ImageryInfo("Test Mapillary", "file:/" + TestUtils.getTestDataRoot() + "pbf/mapillary/{z}/{x}/{y}.mvt")); - loader = new MapboxVectorCachedTileLoader(null, - JCSCacheManager.getCache("testMapillaryCache"), new TileJobOptions(1, 1, Collections - .emptyMap(), 3600)); + final TileJobOptions options = new TileJobOptions(1, 1, Collections.emptyMap(), 3600); + loader = new MapboxVectorCachedTileLoader(null, cache, options); } /** @@ -57,10 +74,11 @@ void testMVTTile(BufferedImage image, Boolean isLoaded) { assertEquals(image, tile.getImage()); TileJob job = loader.createTileLoaderJob(tile); - job.submit(); + // Ensure that we are not getting a cached tile + job.submit(true); Awaitility.await().atMost(Durations.ONE_SECOND).until(tile::isLoaded); if (isLoaded) { - Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> tile.getLayers() != null && tile.getLayers().size() > 1); + Awaitility.await().atMost(Durations.ONE_SECOND).until(() -> tile.getImage() == MVTTile.CLEAR_LOADED); assertEquals(2, tile.getLayers().size()); assertEquals(4096, tile.getExtent()); // Ensure that we have the clear image set, such that the tile doesn't add to the dataset again