aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/kotlin/ca/allanwang/kau/kotlin/ZipTest.kt
blob: 1fbc38fe25f3a5a7598bc4f7b35b0a7ae715b0d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
77
78
79
80
81
82
83
/*
 * Copyright 2018 Allan Wang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ca.allanwang.kau.kotlin

import org.jetbrains.anko.doAsync
import org.junit.Test
import java.util.Random
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.test.assertTrue

/**
 * Created by Allan Wang on 2017-08-06.
 */
class ZipTest {

    val debug = false

    fun p(text: String) {
        if (debug) println(text)
    }

    @Test
    fun basic() {
        val start = System.currentTimeMillis()
        val latch = CountDownLatch(1)
        val rnd = Random()
        (0..10).map {
            { callback: ZipCallback<Int> ->
                doAsync {
                    val sleepTime = rnd.nextInt(100) + 200L
                    p("Task $it will sleep for ${sleepTime}ms")
                    Thread.sleep(sleepTime)
                    val finish = System.currentTimeMillis()
                    p("Task $it finished in ${finish - start}ms at $finish")
                    callback(it)
                }; Unit
            }
        }.zip(-1) { results ->
            val finish = System.currentTimeMillis()
            println("Results ${results.contentToString()} received in ${finish - start}ms at $finish")
            assertTrue((0..10).toList().toTypedArray().contentEquals(results), "Basic zip results do not match")
            assertTrue(finish - start < 1000L, "Basic zip does not seem to be running asynchronously")
            latch.countDown()
        }
        latch.await(1100, TimeUnit.MILLISECONDS)
    }

    @Test
    fun basicAsync() {
        val start = System.currentTimeMillis()
        val latch = CountDownLatch(1)
        val rnd = Random()
        (0..10).map {
            {
                val sleepTime = rnd.nextInt(100) + 200L
                p("Task $it will sleep for ${sleepTime}ms")
                Thread.sleep(sleepTime)
                val finish = System.currentTimeMillis()
                p("Task $it finished in ${finish - start}ms at $finish")
            }
        }.zipAsync {
            val finish = System.currentTimeMillis()
            println("Results received in ${finish - start}ms at $finish")
            assertTrue(finish - start < 1000L, "BasicAsync does not seem to be wrapping the tasks asynchronously")
            latch.countDown()
        }
        latch.await(1100, TimeUnit.MILLISECONDS)
    }
}