Skip to content

[Question] Testing with couroutine context #43

@moutyque

Description

@moutyque

Hello,

I wanted to use and test your wrapper inside a kotlin backend application.

I tried to test the ttl mechanisme with runTest and coroutine context but it failed:

    @Test
    fun `test test context to drop expired entry`() = runTest{

        val cache  =Caffeine
            .newBuilder()
            .expireAfterAccess(1.hours)
            .withEvictionListener { key, value, cause ->
                when (cause) {
                    RemovalCause.SIZE -> print("Removed $key due to size constraints")
                    RemovalCause.EXPIRED -> print("Removed $key due to time constraints")
                    RemovalCause.EXPLICIT -> print("Explicitly removed $key")
                    else -> ""
                }
            }.asCache<String, String>()

        cache.put("foo","bar")
        Assertions.assertThat(cache.getIfPresent("foo")).isEqualTo("bar")
        delay(2.hours)
        Assertions.assertThat(cache.getIfPresent("foo")).isNull() <-- return bar and not null
        
    }

I know I should inject my coroutine context somewhere to make it work and I saw the two parameters in Caffeine cache:

class Cache<K, V>(
   @Deprecated("This should be ignored - use overloaded methods") private val defaultScope: CoroutineScope,
   @Deprecated("This should be ignored - use overloaded methods") private val useCallingContext: Boolean,
   private val cache: AsyncCache<K, V>
)

How to inject coroutine context for testing ?
Is it even usefull as the underling lib is using completatble future ?

Thank you for any help / feedback and for this nice wrapper :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions