TtlCallableExtKtTest.kt 2.0 KB
Newer Older
R
rybalkinsd 已提交
1 2 3 4 5 6 7
package com.alibaba.ttl

import com.alibaba.assertChildTtlValues
import com.alibaba.assertParentTtlValues
import com.alibaba.copyTtlValues
import com.alibaba.createParentTtlInstances
import com.alibaba.createParentTtlInstancesAfterCreateChild
R
rybalkinsd 已提交
8
import com.alibaba.support.junit.conditional.ConditionalIgnoreRule
R
rybalkinsd 已提交
9 10 11 12 13 14
import com.alibaba.ttl.testmodel.Call
import org.hamcrest.CoreMatchers
import org.junit.Assert.assertEquals
import org.junit.Assert.assertSame
import org.junit.Assert.assertThat
import org.junit.Assert.fail
R
rybalkinsd 已提交
15
import org.junit.Rule
R
rybalkinsd 已提交
16 17 18 19
import org.junit.Test

class TtlCallableExtKtTest {

R
rybalkinsd 已提交
20 21 22 23
    @Rule
    @JvmField
    val rule = ConditionalIgnoreRule()

R
rybalkinsd 已提交
24
    @Test
25
    fun `callable wrapTtl extension function `() {
R
rybalkinsd 已提交
26
        val call = Call("1")
27
        val ttlCallable = call.wrapTtl()
R
rybalkinsd 已提交
28 29 30 31
        assertSame(call, ttlCallable.callable)
    }

    @Test
32 33
    fun `callable wrapTtl extension function multiple times`() {
        val call = Call("1").wrapTtl()
R
rybalkinsd 已提交
34
        try {
35
            call.wrapTtl()
R
rybalkinsd 已提交
36 37 38 39 40 41 42 43
            fail()
        } catch (e: IllegalStateException) {
            assertThat<String>(e.message, CoreMatchers.containsString("Already TtlCallable"))
        }

    }

    @Test
44 45
    fun `list of callable wrapTtl extension function`() {
        val callList = listOf(Call("1"), Call("2"), Call("3")).wrapTtl()
R
rybalkinsd 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59

        assertEquals(3, callList.size)
        callList.forEach {
            assertThat(it, CoreMatchers.instanceOf(TtlCallable::class.java))
        }
    }

    @Test
    fun `TtlCallable invoke operator`() {
        val ttlInstances = createParentTtlInstances()

        val call = Call("1", ttlInstances)


60
        val ttlCallable = call.wrapTtl()
R
rybalkinsd 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

        // create after new Task, won't see parent value in in task!
        createParentTtlInstancesAfterCreateChild(ttlInstances)

        // run in the *current* thread
        assertEquals("ok", ttlCallable())


        // child Inheritable
        assertChildTtlValues("1", call.copied)

        // child do not effect parent
        assertParentTtlValues(copyTtlValues(ttlInstances))
    }

}