Ticket #24893: no-tile-304.patch

File no-tile-304.patch, 5.8 KB (added by wangi, 4 days ago)
  • src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java

    diff --git src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java src/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJob.java
    index 7cc1a98e42..260fb01269 100644
    public abstract class JCSCachedTileLoaderJob<K, V extends CacheEntry> implements  
    366366                // If isModifiedSince or If-None-Match has been set
    367367                // and the server answers with a HTTP 304 = "Not Modified"
    368368                Logging.debug("JCS - If-Modified-Since/ETag test: local version is up to date: {0}", getUrl());
    369                 // update cache attributes
    370                 attributes = parseHeaders(urlConn);
    371                 cache.put(getCacheKey(), cacheData, attributes);
     369                updateAttributes(urlConn);
    372370                return true;
    373371            } else if (isObjectLoadable() // we have an object in cache, but we haven't received 304 response code
    374372                    && (
    … … public abstract class JCSCachedTileLoaderJob<K, V extends CacheEntry> implements  
    558556        boolean ret = (attributes.getEtag() != null && attributes.getEtag().equals(urlConn.getHeaderField("ETag"))) ||
    559557                (lastModified != 0 && lastModified <= attributes.getLastModification());
    560558        if (ret) {
    561             // update attributes
    562             attributes = parseHeaders(urlConn);
    563             cache.put(getCacheKey(), cacheData, attributes);
     559            updateAttributes(urlConn);
    564560        }
    565561        return ret;
    566562    }
    567563
     564    /**
     565     * Updates the cache attributes after the server confirmed that the cached object is still valid.
     566     * <p>
     567     * The "no tile at this zoom level" marker is kept: it was found from the response which delivered the object,
     568     * and a confirmation does not repeat that. Without it, an empty no-tile entry would look like a loaded tile
     569     * without an image (see #24893).
     570     * @param urlConn the response which confirmed the cached object
     571     */
     572    private void updateAttributes(HttpClient.Response urlConn) {
     573        final boolean noTileAtZoom = attributes.isNoTileAtZoom();
     574        attributes = parseHeaders(urlConn);
     575        attributes.setNoTileAtZoom(noTileAtZoom);
     576        cache.put(getCacheKey(), cacheData, attributes);
     577    }
     578
    568579    /**
    569580     * TODO: move to JobFactory
    570581     * cancels all outstanding tasks in the queue.
  • test/unit/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJobTest.java

    diff --git test/unit/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJobTest.java test/unit/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJobTest.java
    index 791ae6d36a..ff31eca64e 100644
    package org.openstreetmap.josm.data.imagery;  
    33
    44import static org.junit.jupiter.api.Assertions.assertArrayEquals;
    55import static org.junit.jupiter.api.Assertions.assertEquals;
     6import static org.junit.jupiter.api.Assertions.assertNotSame;
    67import static org.junit.jupiter.api.Assertions.assertTrue;
    78
    89import java.io.IOException;
    … … class TMSCachedTileLoaderJobTest {  
    202203        Listener listener = new Listener();
    203204        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(listener, tile, key, minimumExpiry);
    204205        job.submit(force);
     206        waitFor(listener);
     207        return job;
     208    }
     209
     210    private static void waitFor(Listener listener) {
    205211        synchronized (listener) {
    206212            while (!listener.ready) {
    207213                try {
    … … class TMSCachedTileLoaderJobTest {  
    212218                }
    213219            }
    214220        }
    215         return job;
    216221    }
    217222
    218223    /**
    … … class TMSCachedTileLoaderJobTest {  
    332337        wireMockRuntimeInfo.getWireMock().verifyThat(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test")));
    333338        assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent());
    334339    }
     340
     341    /**
     342     * A cached "no tile at this zoom level" entry which the server confirms with a 304 stays a no-tile.
     343     * <p>
     344     * Non-regression test for #24893: the 304 replaced the cache attributes and dropped the marker, so the empty
     345     * entry finished as a loaded tile without an image, which kept showing the hourglass a tile starts with.
     346     * @throws IOException exception
     347     */
     348    @Test
     349    void testNoTileAtZoomKeptOn304() throws IOException {
     350        final String eTag = "\"no-tile\"";
     351        long testStart = System.currentTimeMillis();
     352        wireMockRuntimeInfo.getWireMock().register(
     353                WireMock.get(WireMock.urlEqualTo("/no-tile"))
     354                .willReturn(WireMock.status(304).withHeader("ETag", eTag))
     355                );
     356        MockTile tile = new MockTile(wireMockRuntimeInfo.getHttpBaseUrl() + "/no-tile");
     357        Listener listener = new Listener();
     358        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(listener, tile, "no-tile");
     359        // the entry as cached for a no-tile response, expired since
     360        CacheEntryAttributes attributes = new CacheEntryAttributes();
     361        attributes.setNoTileAtZoom(true);
     362        attributes.setEtag(eTag);
     363        attributes.setLastModification(testStart - TimeUnit.DAYS.toMillis(2));
     364        attributes.setExpirationTime(testStart - TimeUnit.DAYS.toMillis(1));
     365        getCache().put(job.getCacheKey(), new BufferedImageCacheEntry(new byte[0]), attributes);
     366
     367        job.submit(false);
     368        waitFor(listener);
     369
     370        wireMockRuntimeInfo.getWireMock().verifyThat(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/no-tile")));
     371        assertTrue(job.getAttributes().isNoTileAtZoom(), "the no-tile marker was dropped");
     372        assertEquals("no-tile", tile.getValue("tile-info"));
     373        assertNotSame(Tile.LOADING_IMAGE, tile.getImage(), "the tile still shows the hourglass");
     374    }
    335375}