|
| 1 | +package com.baeldung.kotlin |
| 2 | + |
| 3 | +import nl.komponents.kovenant.* |
| 4 | +import nl.komponents.kovenant.Kovenant.deferred |
| 5 | +import nl.komponents.kovenant.combine.and |
| 6 | +import nl.komponents.kovenant.combine.combine |
| 7 | +import org.junit.Assert |
| 8 | +import org.junit.Before |
| 9 | +import org.junit.Test |
| 10 | +import java.io.IOException |
| 11 | +import java.util.* |
| 12 | +import java.util.concurrent.TimeUnit |
| 13 | + |
| 14 | +class KovenantTest { |
| 15 | + @Before |
| 16 | + fun setupTestMode() { |
| 17 | + Kovenant.testMode { error -> |
| 18 | + println("An unexpected error occurred") |
| 19 | + Assert.fail(error.message) |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + fun testSuccessfulDeferred() { |
| 25 | + val def = deferred<Long, Exception>() |
| 26 | + Assert.assertFalse(def.promise.isDone()) |
| 27 | + |
| 28 | + def.resolve(1L) |
| 29 | + Assert.assertTrue(def.promise.isDone()) |
| 30 | + Assert.assertTrue(def.promise.isSuccess()) |
| 31 | + Assert.assertFalse(def.promise.isFailure()) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun testFailedDeferred() { |
| 36 | + val def = deferred<Long, Exception>() |
| 37 | + Assert.assertFalse(def.promise.isDone()) |
| 38 | + |
| 39 | + def.reject(RuntimeException()) |
| 40 | + Assert.assertTrue(def.promise.isDone()) |
| 41 | + Assert.assertFalse(def.promise.isSuccess()) |
| 42 | + Assert.assertTrue(def.promise.isFailure()) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun testResolveDeferredTwice() { |
| 47 | + val def = deferred<Long, Exception>() |
| 48 | + def.resolve(1L) |
| 49 | + try { |
| 50 | + def.resolve(1L) |
| 51 | + } catch (e: AssertionError) { |
| 52 | + // Expected. |
| 53 | + // This is slightly unusual. The AssertionError comes from Assert.fail() from setupTestMode() |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun testSuccessfulTask() { |
| 59 | + val promise = task { 1L } |
| 60 | + Assert.assertTrue(promise.isDone()) |
| 61 | + Assert.assertTrue(promise.isSuccess()) |
| 62 | + Assert.assertFalse(promise.isFailure()) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun testFailedTask() { |
| 67 | + val promise = task { throw RuntimeException() } |
| 68 | + Assert.assertTrue(promise.isDone()) |
| 69 | + Assert.assertFalse(promise.isSuccess()) |
| 70 | + Assert.assertTrue(promise.isFailure()) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun testCallbacks() { |
| 75 | + val promise = task { 1L } |
| 76 | + |
| 77 | + promise.success { |
| 78 | + println("This was a success") |
| 79 | + Assert.assertEquals(1L, it) |
| 80 | + } |
| 81 | + |
| 82 | + promise.fail { |
| 83 | + println(it) |
| 84 | + Assert.fail("This shouldn't happen") |
| 85 | + } |
| 86 | + |
| 87 | + promise.always { |
| 88 | + println("This always happens") |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun testGetValues() { |
| 94 | + val promise = task { 1L } |
| 95 | + Assert.assertEquals(1L, promise.get()) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun testAllSucceed() { |
| 100 | + val numbers = all( |
| 101 | + task { 1L }, |
| 102 | + task { 2L }, |
| 103 | + task { 3L } |
| 104 | + ) |
| 105 | + |
| 106 | + Assert.assertEquals(listOf(1L, 2L, 3L), numbers.get()) |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun testOneFails() { |
| 111 | + val runtimeException = RuntimeException() |
| 112 | + |
| 113 | + val numbers = all( |
| 114 | + task { 1L }, |
| 115 | + task { 2L }, |
| 116 | + task { throw runtimeException } |
| 117 | + ) |
| 118 | + |
| 119 | + Assert.assertEquals(runtimeException, numbers.getError()) |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun testAnySucceeds() { |
| 124 | + val promise = any( |
| 125 | + task { |
| 126 | + TimeUnit.SECONDS.sleep(3) |
| 127 | + 1L |
| 128 | + }, |
| 129 | + task { |
| 130 | + TimeUnit.SECONDS.sleep(2) |
| 131 | + 2L |
| 132 | + }, |
| 133 | + task { |
| 134 | + TimeUnit.SECONDS.sleep(1) |
| 135 | + 3L |
| 136 | + } |
| 137 | + ) |
| 138 | + |
| 139 | + Assert.assertTrue(promise.isDone()) |
| 140 | + Assert.assertTrue(promise.isSuccess()) |
| 141 | + Assert.assertFalse(promise.isFailure()) |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + fun testAllFails() { |
| 146 | + val runtimeException = RuntimeException() |
| 147 | + val ioException = IOException() |
| 148 | + val illegalStateException = IllegalStateException() |
| 149 | + val promise = any( |
| 150 | + task { |
| 151 | + TimeUnit.SECONDS.sleep(3) |
| 152 | + throw runtimeException |
| 153 | + }, |
| 154 | + task { |
| 155 | + TimeUnit.SECONDS.sleep(2) |
| 156 | + throw ioException |
| 157 | + }, |
| 158 | + task { |
| 159 | + TimeUnit.SECONDS.sleep(1) |
| 160 | + throw illegalStateException |
| 161 | + } |
| 162 | + ) |
| 163 | + |
| 164 | + Assert.assertTrue(promise.isDone()) |
| 165 | + Assert.assertFalse(promise.isSuccess()) |
| 166 | + Assert.assertTrue(promise.isFailure()) |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + fun testSimpleCombine() { |
| 171 | + val promise = task { 1L } and task { "Hello" } |
| 172 | + val result = promise.get() |
| 173 | + |
| 174 | + Assert.assertEquals(1L, result.first) |
| 175 | + Assert.assertEquals("Hello", result.second) |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + fun testLongerCombine() { |
| 180 | + val promise = combine( |
| 181 | + task { 1L }, |
| 182 | + task { "Hello" }, |
| 183 | + task { Currency.getInstance("USD") } |
| 184 | + ) |
| 185 | + val result = promise.get() |
| 186 | + |
| 187 | + Assert.assertEquals(1L, result.first) |
| 188 | + Assert.assertEquals("Hello", result.second) |
| 189 | + Assert.assertEquals(Currency.getInstance("USD"), result.third) |
| 190 | + } |
| 191 | +} |
0 commit comments