Skip to content

Commit

Permalink
Merge pull request #489 from scireum/feature/smo/managedcachetest
Browse files Browse the repository at this point in the history
Feature/smo/managedcachetest
  • Loading branch information
MOOOOOSER authored Sep 8, 2023
2 parents 8ad973c + 0c2610a commit 851f0ea
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 112 deletions.
112 changes: 0 additions & 112 deletions src/test/java/sirius/kernel/cache/ManagedCacheSpec.groovy

This file was deleted.

156 changes: 156 additions & 0 deletions src/test/kotlin/sirius/kernel/cache/ManagedCacheTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - info@scireum.de
*/

package sirius.kernel.cache

import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import sirius.kernel.NightlyTest
import sirius.kernel.SiriusExtension
import sirius.kernel.commons.Strings
import sirius.kernel.commons.Tuple
import sirius.kernel.commons.Wait
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

/**
* Tests the [AdvancedDateParser] class.
*/

@ExtendWith(SiriusExtension::class)
@NightlyTest
class ManagedCacheTest {

@Test
fun `test run eviction removes old entries`() {
val cache: ManagedCache<String, String> = ManagedCache("test-cache", null, null)
cache.put("key1", "value1")
cache.put("key2", "value2")
Wait.millis(1001)
cache.put("key3", "value3")
cache.put("key4", "value4")
assertEquals(4, cache.getSize())
cache.runEviction()
assertEquals(2, cache.getSize())
assertEquals("value3", cache.get("key3"))
assertEquals("value4", cache.get("key4"))
}

@Test
fun `optional value computer works`() {
val valueComputer = { key: String ->
if (key.isEmpty() || key.startsWith("empty")) {
Optional.empty<String>()
} else {
Optional.of(key.toUpperCase())
}
}
val cache = ManagedCache("test-cache", OptionalValueComputer.of(valueComputer), null)
assertEquals(null, cache.get(""))
assertEquals(Optional.of("KEY"), cache.getOptional("key"))
assertEquals(Optional.empty(), cache.getOptional("empty_key"))

}

@Test
fun `removeAll works as expected`() {
val cache: ManagedCache<String, Tuple<String, String>> = ManagedCache("test-cache", null, null)
cache.addRemover("FIRST",
{ key, entry ->
Strings.areEqual(key, entry.getValue()?.getFirst())
})
cache.addRemover("SECOND",
{ key, entry ->
Strings.areEqual(key, entry.getValue()?.getSecond())
})
cache.put("A", Tuple.create("0", "0"))
cache.put("B", Tuple.create("1", "2"))
cache.put("C", Tuple.create("2", "1"))
cache.put("D", Tuple.create("3", "3"))

//Remove all entries where the first is a '1' and then all where the second is a '1'
cache.removeAll("FIRST", "1")
cache.removeAll("SECOND", "1")

//Ensure that the correct entries were removed and others remained in cache
assertNotEquals(null, cache.get("A"))
assertEquals(null, cache.get("B"))
assertEquals(null, cache.get("C"))
assertNotEquals(null, cache.get("D"))
}

@Test
fun `remover builder works as expected`() {
val cache: ManagedCache<String, String> = ManagedCache("test-cache", null, null)
cache.put("A", "1")
cache.put("B", "12")
cache.put("C", "123")
cache.put("D", "1234")
cache.put("E", "12345")

// defines a remover, that allows to define a key value, which should not be removed
cache.addRemover("FILTER")
.filter(
{ selector, entry ->
entry.getKey() != selector
}
)
.map(
// get value of managed cache
{ entry ->
entry.getValue()
}
)
.map(
// add key + value of the cache Entry, for example: "E" + 12345
{ selector, value ->
value + selector
}
).removeIf(
// if the resulting string, f.E. "E12345" is larger than 5, remove entry
{ x ->
x.length > 5
}
)

cache.removeAll("FILTER", "A")

assertNotEquals(null, cache.get("A"))
assertNotEquals(null, cache.get("B"))
assertNotEquals(null, cache.get("C"))
assertNotEquals(null, cache.get("D"))
assertEquals(null, cache.get("E"))
}

@Test
fun `valueBasedRemover works as expected`() {
val cache: ManagedCache<String, Tuple<String, String>> = ManagedCache("test-cache", null, null)
cache.put("Key1", Tuple.create("1, ", "A"))
cache.put("Key2", Tuple.create("2 ", "B"))
cache.put("Key3", Tuple.create("3", "C"))
cache.put("Key4", Tuple.create("4", "D"))
cache.put("Key5", Tuple.create("5", "E"))

cache.addValueBasedRemover("REMOVE_ALWAYS")
.removeAlways({ selector, tuple ->
tuple.getSecond() == selector
}).removeIf({ tuple ->
false
})

cache.removeAll("REMOVE_ALWAYS", "C")
cache.removeAll("REMOVE_ALWAYS", "B")

assertNotEquals(null, cache.get("Key1"))
assertEquals(null, cache.get("Key2"))
assertEquals(null, cache.get("Key3"))
assertNotEquals(null, cache.get("Key4"))
assertNotEquals(null, cache.get("Key5"))
}
}

0 comments on commit 851f0ea

Please sign in to comment.