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 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    @Test
    fun `callable wrap extension function `() {
        val call = Call("1")
        val ttlCallable = call.wrap()
        assertSame(call, ttlCallable.callable)
    }

    @Test
    fun `callable wrap extension function multiple times`() {
        val call = Call("1").wrap()
        try {
            call.wrap()
            fail()
        } catch (e: IllegalStateException) {
            assertThat<String>(e.message, CoreMatchers.containsString("Already TtlCallable"))
        }

    }

    @Test
    fun `list of callable wrap extension function`() {
        val callList = listOf(Call("1"), Call("2"), Call("3")).wrap()

        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)


        val ttlCallable = call.wrap()

        // 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))
    }

}