aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/kotlin
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2017-08-07 22:20:57 -0700
committerGitHub <noreply@github.com>2017-08-07 22:20:57 -0700
commit02e1dbc84425b0ac7f771c82f70444f742397452 (patch)
tree05e978e7588e30ce653428671f3d9f5df5397385 /core/src/test/kotlin
parent187d8e64dc7189f63707d154166867084662dbe3 (diff)
downloadkau-02e1dbc84425b0ac7f771c82f70444f742397452.tar.gz
kau-02e1dbc84425b0ac7f771c82f70444f742397452.tar.bz2
kau-02e1dbc84425b0ac7f771c82f70444f742397452.zip
Release 3.3.0 (#32)3.3.0
* Rewrite Logger (#29) * Remove dependency on timber * Update logger * Reorder throwabl * Fix lint * Update readme * Blank target * Create Zip (#30) * Finish zips with tests * Finalize * Update changelog * Add log hooks * Open most logging functions * Remap kpref items (#31) * Update readme * Generate files and prepare release * Kpref -
Diffstat (limited to 'core/src/test/kotlin')
-rw-r--r--core/src/test/kotlin/ca/allanwang/kau/kotlin/ZipTest.kt72
1 files changed, 72 insertions, 0 deletions
diff --git a/core/src/test/kotlin/ca/allanwang/kau/kotlin/ZipTest.kt b/core/src/test/kotlin/ca/allanwang/kau/kotlin/ZipTest.kt
new file mode 100644
index 0000000..4a04142
--- /dev/null
+++ b/core/src/test/kotlin/ca/allanwang/kau/kotlin/ZipTest.kt
@@ -0,0 +1,72 @@
+package ca.allanwang.kau.kotlin
+
+import org.jetbrains.anko.doAsync
+import org.junit.Test
+import java.util.*
+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)
+ }
+
+} \ No newline at end of file