Changeset 10558 in josm for trunk/test


Ignore:
Timestamp:
2016-07-17T20:43:22+02:00 (4 years ago)
Author:
wiktorn
Message:

Do not cache IOException in JCS, they should be cached only in memory.

Caching IOException in JCS Cache leads to the situation, where errors
are cached for DEFAULT_EXPIRE_TIME which causes the error message to
stay too long on user screen.

Fix tests for JCSCachedTileLoaderJob

See: #13128

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java

    r10557 r10558  
    33
    44import static org.junit.Assert.assertEquals;
     5import static org.junit.Assert.assertFalse;
    56
    67import java.io.IOException;
     
    1314import org.junit.Test;
    1415import org.openstreetmap.josm.JOSMFixture;
     16import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult;
    1517
    1618import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
     
    2325    private static class TestCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, CacheEntry> {
    2426        private String url;
     27        private String key;
    2528
    26         TestCachedTileLoaderJob(String url) throws IOException {
     29        TestCachedTileLoaderJob(String url, String key) throws IOException {
    2730            super(getCache(), 30000, 30000, null);
     31
    2832            this.url = url;
    29         }
    30 
    31         private static ICacheAccess<String, CacheEntry> getCache() throws IOException {
    32          return JCSCacheManager.getCache("test");
     33            this.key = key;
    3334        }
    3435
    3536        @Override
    3637        public String getCacheKey() {
    37             return "cachekey" + url;
     38            return key;
    3839        }
    3940
     
    5657        private CacheEntryAttributes attributes;
    5758        private boolean ready;
     59        private LoadResult result;
    5860
    5961        @Override
     
    6163            this.attributes = attributes;
    6264            this.ready = true;
     65            this.result = result;
    6366            this.notifyAll();
    6467        }
     
    100103    @SuppressFBWarnings(value = "WA_NOT_IN_LOOP")
    101104    public void testUnknownHost() throws IOException, InterruptedException {
    102         TestCachedTileLoaderJob job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown");
     105        String key = "key_unknown_host";
     106        TestCachedTileLoaderJob job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown", key);
    103107        Listener listener = new Listener();
    104108        job.submit(listener, true);
     
    109113        }
    110114        assertEquals("java.net.UnknownHostException: unkownhost.unkownhost", listener.attributes.getErrorMessage());
     115        assertEquals(LoadResult.FAILURE, listener.result); // because response will be cached, and that is checked below
     116
     117        ICacheAccess<String, CacheEntry> cache = getCache();
     118        CacheEntry e = new CacheEntry(new byte[]{0,1,2,3});
     119        CacheEntryAttributes attributes = new CacheEntryAttributes();
     120        attributes.setExpirationTime(2);
     121        cache.put(key, e, attributes);
     122
     123        job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown", key);
     124        listener = new Listener();
     125        job.submit(listener, true);
     126        synchronized (listener) {
     127            if (!listener.ready) {
     128                listener.wait();
     129            }
     130        }
     131        assertEquals(LoadResult.SUCCESS, listener.result);
     132        assertFalse(job.isCacheElementValid());
    111133    }
    112134
     
    125147
    126148    private static TestCachedTileLoaderJob getStatusLoaderJob(int responseCode) throws IOException {
    127         return new TestCachedTileLoaderJob("http://httpstat.us/" + responseCode);
     149        return new TestCachedTileLoaderJob("http://httpstat.us/" + responseCode, "key_" + responseCode);
     150    }
     151
     152    private static ICacheAccess<String, CacheEntry> getCache() throws IOException {
     153        return JCSCacheManager.getCache("test");
    128154    }
    129155}
Note: See TracChangeset for help on using the changeset viewer.