| 1 |
/* |
| 2 |
* Copyright (C) 2015 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.ide.common.caching; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertEquals; |
| 20 |
import static org.junit.Assert.assertTrue; |
| 21 |
|
| 22 |
import com.android.annotations.NonNull; |
| 23 |
import com.android.annotations.Nullable; |
| 24 |
|
| 25 |
import org.junit.Test; |
| 26 |
|
| 27 |
import java.util.concurrent.CountDownLatch; |
| 28 |
|
| 29 |
/** |
| 30 |
*/ |
| 31 |
public class CreatingCacheTest { |
| 32 |
|
| 33 |
private static class FakeFactory implements CreatingCache.ValueFactory<String, String> { |
| 34 |
@Override |
| 35 |
@NonNull |
| 36 |
public String create(@NonNull String key) { |
| 37 |
return key; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
private static class DelayedFactory implements CreatingCache.ValueFactory<String, String> { |
| 42 |
|
| 43 |
@NonNull |
| 44 |
private final CountDownLatch mLatch; |
| 45 |
|
| 46 |
public DelayedFactory(@NonNull CountDownLatch latch) { |
| 47 |
mLatch = latch; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
@NonNull |
| 52 |
public String create(@NonNull String key) { |
| 53 |
try { |
| 54 |
mLatch.await(); |
| 55 |
} catch (InterruptedException ignored) { |
| 56 |
} |
| 57 |
return key; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void testSingleThread() throws Exception { |
| 63 |
CreatingCache<String, String> cache = new CreatingCache<String, String>(new FakeFactory()); |
| 64 |
|
| 65 |
String value1 = cache.get("key"); |
| 66 |
assertEquals("key", value1); |
| 67 |
String value2 = cache.get("key"); |
| 68 |
assertEquals("key", value2); |
| 69 |
//noinspection StringEquality |
| 70 |
assertTrue("repetitive calls give same instance", value1 == value2); |
| 71 |
} |
| 72 |
|
| 73 |
private static class CacheRunnable implements Runnable { |
| 74 |
|
| 75 |
@NonNull |
| 76 |
private final CreatingCache<String, String> mCache; |
| 77 |
@Nullable |
| 78 |
private final CountDownLatch mLatch; |
| 79 |
|
| 80 |
private String mResult; |
| 81 |
private InterruptedException mException; |
| 82 |
|
| 83 |
CacheRunnable(@NonNull CreatingCache<String, String> cache) { |
| 84 |
this(cache, null); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Creates a runnable, that will notify when it's pending on a query. |
| 89 |
* |
| 90 |
* @param cache the cache to query |
| 91 |
* @param latch the latch to countdown when the query is being processed. |
| 92 |
*/ |
| 93 |
CacheRunnable( |
| 94 |
@NonNull CreatingCache<String, String> cache, |
| 95 |
@Nullable CountDownLatch latch) { |
| 96 |
mCache = cache; |
| 97 |
mLatch = latch; |
| 98 |
} |
| 99 |
|
| 100 |
@Override |
| 101 |
public void run() { |
| 102 |
if (mLatch != null) { |
| 103 |
mResult = mCache.get("foo", new CreatingCache.QueryListener() { |
| 104 |
@Override |
| 105 |
public void onQueryState(@NonNull CreatingCache.State state) { |
| 106 |
mLatch.countDown(); |
| 107 |
} |
| 108 |
}); |
| 109 |
} else { |
| 110 |
mResult = mCache.get("foo"); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
public String getResult() { |
| 115 |
return mResult; |
| 116 |
} |
| 117 |
|
| 118 |
public InterruptedException getException() { |
| 119 |
return mException; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
@Test |
| 124 |
public void testMultiThread() throws Exception { |
| 125 |
// the latch that controls whether the factory will "create" an item. |
| 126 |
CountDownLatch factoryLatch = new CountDownLatch(1); |
| 127 |
|
| 128 |
CreatingCache<String, String> |
| 129 |
cache = new CreatingCache<String, String>(new DelayedFactory(factoryLatch)); |
| 130 |
|
| 131 |
// the latch that will be released when the runnable1 is pending its query. |
| 132 |
CountDownLatch latch1 = new CountDownLatch(1); |
| 133 |
|
| 134 |
CacheRunnable runnable1 = new CacheRunnable(cache, latch1); |
| 135 |
Thread t1 = new Thread(runnable1); |
| 136 |
t1.start(); |
| 137 |
|
| 138 |
// wait on thread1 being waiting on the query, before creating thread2 |
| 139 |
latch1.await(); |
| 140 |
|
| 141 |
// the latch that will be released when the runnable1 is pending its query. |
| 142 |
CountDownLatch latch2 = new CountDownLatch(1); |
| 143 |
|
| 144 |
CacheRunnable runnable2 = new CacheRunnable(cache,latch2); |
| 145 |
Thread t2 = new Thread(runnable2); |
| 146 |
t2.start(); |
| 147 |
|
| 148 |
// wait on thread2 being waiting on the query, before releasing the factory |
| 149 |
latch2.await(); |
| 150 |
|
| 151 |
factoryLatch.countDown(); |
| 152 |
|
| 153 |
// wait on threads being done. |
| 154 |
t1.join(); |
| 155 |
t2.join(); |
| 156 |
|
| 157 |
assertEquals("foo", runnable1.getResult()); |
| 158 |
assertEquals("foo", runnable2.getResult()); |
| 159 |
//noinspection StringEquality |
| 160 |
assertTrue("repetitive calls give same instance", runnable1.getResult() == runnable2.getResult()); |
| 161 |
} |
| 162 |
|
| 163 |
@Test(expected = IllegalStateException.class) |
| 164 |
public void testClear() throws Exception { |
| 165 |
// the latch that controls whether the factory will "create" an item. |
| 166 |
// this is never released in this test since we want to try clearing the cache while an |
| 167 |
// item is pending creation. |
| 168 |
CountDownLatch factoryLatch = new CountDownLatch(1); |
| 169 |
|
| 170 |
final CreatingCache<String, String> |
| 171 |
cache = new CreatingCache<String, String>(new DelayedFactory(factoryLatch)); |
| 172 |
|
| 173 |
// the latch that will be released when the thread is pending its query. |
| 174 |
CountDownLatch latch = new CountDownLatch(1); |
| 175 |
|
| 176 |
new Thread(new CacheRunnable(cache, latch)).start(); |
| 177 |
|
| 178 |
// wait on thread to be waiting, before trying to clear the cache. |
| 179 |
latch.await(); |
| 180 |
|
| 181 |
cache.clear(); |
| 182 |
} |
| 183 |
} |