Skip to content

Commit

Permalink
Extend LruCache unit tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Kenneth J. Shackleton <[email protected]>
  • Loading branch information
kennethshackleton committed Jun 1, 2024
1 parent 311c091 commit d30ec8e
Showing 1 changed file with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* limitations under the License.
*/

package com.bloomberg.selekt
package com.bloomberg.selekt.cache

import com.bloomberg.selekt.cache.LruCache
import org.junit.jupiter.api.Test
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.doReturn
Expand All @@ -30,8 +29,42 @@ import org.mockito.kotlin.whenever
import kotlin.test.assertFalse
import kotlin.test.assertSame
import kotlin.test.assertTrue
import kotlin.test.fail

internal class LruCacheTest {
@Test
fun get() {
val first = Any()
val disposal: (Any) -> Unit = mock { onGeneric { invoke(it) } doReturn Unit }
val cache = LruCache(1, disposal)
cache["1", { first }]
assertSame(first, cache["1", { fail() }])
}

@Test
fun getTwo() {
val first = Any()
val second = Any()
val disposal: (Any) -> Unit = mock { onGeneric { invoke(it) } doReturn Unit }
val cache = LruCache(2, disposal)
cache["1", { first }]
cache["2", { second }]
assertSame(first, cache["1", { fail() }])
assertSame(second, cache["2", { fail() }])
}

@Test
fun getAfterEvict() {
val first = Any()
val second = Any()
val disposal: (Any) -> Unit = mock { onGeneric { invoke(it) } doReturn Unit }
val cache = LruCache(1, disposal)
cache["1", { first }]
cache["2", { second }]
assertFalse(cache.containsKey("1"))
assertSame(second, cache["2", { fail() }])
}

@Test
fun evict() {
val first = Any()
Expand All @@ -44,6 +77,7 @@ internal class LruCacheTest {
inOrder(disposal) {
verify(disposal, times(1)).invoke(same(first))
}
assertSame(second, cache["2", { fail() }])
}

@Test
Expand Down

0 comments on commit d30ec8e

Please sign in to comment.