Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
349 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apply plugin: 'kotlin' | ||
apply plugin: 'jacoco' | ||
apply from: "$rootDir/bintray.gradle" | ||
|
||
dependencies { | ||
compile project(':layercache') | ||
compile 'org.cache2k:cache2k-api:1.0.1.Final' | ||
runtime 'org.cache2k:cache2k-all:1.0.1.Final' | ||
// compileOnly | ||
|
||
testCompile project(':testutils') | ||
testCompile "org.slf4j:slf4j-nop:1.7.25" | ||
} | ||
|
||
kotlin { | ||
experimental { | ||
coroutines 'enable' | ||
} | ||
} | ||
|
||
test { finalizedBy jacocoTestReport } | ||
|
||
sourceCompatibility = "1.7" | ||
targetCompatibility = "1.7" |
69 changes: 69 additions & 0 deletions
69
layercache-cache2k/src/main/kotlin/com/appmattus/layercache/Cache2kWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright 2017 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.layercache | ||
|
||
import kotlinx.coroutines.experimental.CommonPool | ||
import kotlinx.coroutines.experimental.Deferred | ||
import kotlinx.coroutines.experimental.async | ||
import kotlinx.coroutines.experimental.runBlocking | ||
import org.cache2k.integration.FunctionalCacheLoader | ||
|
||
/** | ||
* Wrapper around Cache2k (https://cache2k.org/) | ||
*/ | ||
internal class Cache2kWrapper<Key : Any, Value : Any>(private val cache: org.cache2k.Cache<Key, Value>) : | ||
Cache<Key, Value> { | ||
override fun evict(key: Key): Deferred<Unit> { | ||
return async(CommonPool) { | ||
cache.remove(key) | ||
} | ||
} | ||
|
||
override fun get(key: Key): Deferred<Value?> { | ||
return async(CommonPool) { | ||
cache.get(key) | ||
} | ||
} | ||
|
||
override fun set(key: Key, value: Value): Deferred<Unit> { | ||
return async(CommonPool) { | ||
cache.put(key, value) | ||
} | ||
} | ||
|
||
override fun evictAll(): Deferred<Unit> { | ||
return async(CommonPool) { | ||
cache.clear() | ||
|
||
//FunctionalCacheLoader | ||
} | ||
} | ||
} | ||
|
||
@Suppress("unused", "USELESS_CAST") | ||
fun <Key : Any, Value : Any> Cache.Companion.fromCache2k(cache: org.cache2k.Cache<Key, Value>) = Cache2kWrapper(cache) as Cache<Key, Value> | ||
|
||
/** | ||
* TODO: Note: Fetcher should not return null | ||
*/ | ||
@Suppress("unused") | ||
fun <Key : Any, Value : Any> Fetcher<Key, Value>.toCache2kLoader(): FunctionalCacheLoader<Key, Value> { | ||
return FunctionalCacheLoader<Key, Value> { key -> | ||
// TODO What thread does a cache loader run on? | ||
runBlocking { get(key).await() } | ||
} | ||
} |
240 changes: 240 additions & 0 deletions
240
layercache-cache2k/src/test/kotlin/com/appmattus/layercache/Cache2kWrapperShould.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
/** | ||
* Copyright 2017 Appmattus Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.appmattus.layercache | ||
|
||
import kotlinx.coroutines.experimental.CommonPool | ||
import kotlinx.coroutines.experimental.async | ||
import kotlinx.coroutines.experimental.runBlocking | ||
import org.cache2k.Cache2kBuilder | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNull | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.mockito.Mock | ||
import org.mockito.Mockito | ||
import org.mockito.MockitoAnnotations | ||
import java.util.concurrent.TimeUnit | ||
|
||
|
||
class Cache2kWrapperShould { | ||
|
||
@Mock | ||
private lateinit var cache2k: org.cache2k.Cache<String, String> | ||
|
||
private lateinit var wrappedCache: Cache<String, String> | ||
|
||
private lateinit var integratedCache: Cache<String, String> | ||
|
||
@Mock | ||
private lateinit var loaderFetcher: AbstractFetcher<String, String> | ||
|
||
private lateinit var integratedCacheWithLoader: Cache<String, String> | ||
|
||
@Before | ||
fun before() { | ||
MockitoAnnotations.initMocks(this) | ||
wrappedCache = Cache2kWrapper(cache2k) | ||
|
||
val cache2k = object : Cache2kBuilder<String, String>() {} | ||
// expire/refresh after 5 minutes | ||
.expireAfterWrite(5, TimeUnit.MINUTES) | ||
// cope with at most 30 seconds outage before propagating exceptions | ||
.resilienceDuration(30, TimeUnit.SECONDS) | ||
.build() | ||
integratedCache = Cache.fromCache2k(cache2k) | ||
|
||
|
||
Mockito.`when`(loaderFetcher.get(Mockito.anyString())).then { async(CommonPool) { "hello" } } | ||
|
||
val cache2kWithLoader = object : Cache2kBuilder<String, String>() {} | ||
.expireAfterWrite(5, TimeUnit.MINUTES) // expire/refresh after 5 minutes | ||
.resilienceDuration(30, TimeUnit.SECONDS) // cope with at most 30 seconds outage before propagating | ||
// exceptions | ||
.refreshAhead(true) // keep fresh when expiring | ||
.loader(loaderFetcher.toCache2kLoader()) // auto populating function | ||
.build() | ||
integratedCacheWithLoader = Cache.fromCache2k(cache2kWithLoader) | ||
} | ||
|
||
// get | ||
@Test | ||
fun `get returns value from cache`() { | ||
runBlocking { | ||
// given value available in first cache only | ||
Mockito.`when`(cache2k.get("key")).thenReturn("value") | ||
|
||
// when we get the value | ||
val result = wrappedCache.get("key").await() | ||
|
||
// then we return the value | ||
assertEquals("value", result) | ||
} | ||
} | ||
|
||
@Test(expected = TestException::class) | ||
fun `get throws`() { | ||
runBlocking { | ||
// given value available in first cache only | ||
Mockito.`when`(cache2k.get("key")).then { throw TestException() } | ||
|
||
// when we get the value | ||
wrappedCache.get("key").await() | ||
|
||
// then we throw an exception | ||
} | ||
} | ||
|
||
// set | ||
@Test | ||
fun `set returns value from cache`() { | ||
runBlocking { | ||
// given | ||
|
||
// when we set the value | ||
wrappedCache.set("key", "value").await() | ||
|
||
// then put is called | ||
Mockito.verify(cache2k).put("key", "value") | ||
} | ||
} | ||
|
||
@Test(expected = TestException::class) | ||
fun `set throws`() { | ||
runBlocking { | ||
// given value available in first cache only | ||
Mockito.`when`(cache2k.put("key", "value")).then { throw TestException() } | ||
|
||
// when we get the value | ||
wrappedCache.set("key", "value").await() | ||
|
||
// then we throw an exception | ||
} | ||
} | ||
|
||
// evict | ||
@Test | ||
fun `evict returns value from cache`() { | ||
runBlocking { | ||
// given | ||
|
||
// when we get the value | ||
wrappedCache.evict("key").await() | ||
|
||
// then we return the value | ||
//assertEquals("value", result) | ||
Mockito.verify(cache2k).remove("key") | ||
} | ||
} | ||
|
||
@Test(expected = TestException::class) | ||
fun `evict throws`() { | ||
runBlocking { | ||
// given value available in first cache only | ||
Mockito.`when`(cache2k.remove("key")).then { throw TestException() } | ||
|
||
// when we get the value | ||
wrappedCache.evict("key").await() | ||
|
||
// then we throw an exception | ||
} | ||
} | ||
|
||
@Test | ||
fun `return null when the cache is empty`() { | ||
runBlocking { | ||
// given we have an empty cache | ||
// integratedCache | ||
|
||
// when we retrieve a value | ||
val result = integratedCache.get("key").await() | ||
|
||
// then it is null | ||
assertNull(result) | ||
} | ||
} | ||
|
||
@Test | ||
fun `return value when cache has value`() { | ||
runBlocking { | ||
// given we have a cache with a value | ||
integratedCache.set("key", "value").await() | ||
|
||
// when we retrieve a value | ||
val result = integratedCache.get("key").await() | ||
|
||
// then it is returned | ||
assertEquals("value", result) | ||
} | ||
} | ||
|
||
@Test | ||
fun `return null when the key has been evicted`() { | ||
runBlocking { | ||
// given we have a cache with a value | ||
integratedCache.set("key", "value").await() | ||
|
||
// when we evict the value | ||
integratedCache.evict("key").await() | ||
|
||
// then the value is null | ||
assertNull(integratedCache.get("key").await()) | ||
} | ||
} | ||
|
||
|
||
@Test | ||
fun `return from loader when the cache is empty`() { | ||
runBlocking { | ||
// given we have an empty cache | ||
// integratedCacheWithLoader | ||
|
||
// when we retrieve a value | ||
val result = integratedCacheWithLoader.get("key").await() | ||
|
||
// then the value comes from the loader | ||
assertEquals("hello", result) | ||
} | ||
} | ||
|
||
@Test | ||
fun `return value and not from loader when cache has value`() { | ||
runBlocking { | ||
// given we have a cache with a value | ||
integratedCacheWithLoader.set("key", "value").await() | ||
|
||
// when we retrieve a value | ||
val result = integratedCacheWithLoader.get("key").await() | ||
|
||
// then it is returned | ||
assertEquals("value", result) | ||
} | ||
} | ||
|
||
@Test | ||
fun `return value from loader when the key has been evicted`() { | ||
runBlocking { | ||
// given we have a cache with a value | ||
integratedCacheWithLoader.set("key", "value").await() | ||
|
||
// when we evict the value | ||
integratedCacheWithLoader.evict("key").await() | ||
|
||
// then the value comes from the loader | ||
assertEquals("hello", integratedCacheWithLoader.get("key").await()) | ||
} | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
layercache/src/test/kotlin/com/appmattus/layercache/AbstractCache.kt
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
layercache/src/test/kotlin/com/appmattus/layercache/AbstractFetcherCache.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.