|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.pekko.task.javadsl; |
| 19 | + |
| 20 | +import org.apache.pekko.stream.StreamTest; |
| 21 | +import org.apache.pekko.testkit.PekkoJUnitActorSystemResource; |
| 22 | +import org.apache.pekko.testkit.PekkoSpec; |
| 23 | +import org.apache.pekko.stream.Materializer; |
| 24 | +import org.apache.pekko.Done; |
| 25 | + |
| 26 | +import org.junit.ClassRule; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import org.apache.pekko.japi.function.Creator; |
| 30 | +import org.apache.pekko.stream.javadsl.Sink; |
| 31 | +import org.apache.pekko.stream.javadsl.Source; |
| 32 | + |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.concurrent.ExecutionException; |
| 35 | +import java.util.concurrent.atomic.AtomicLong; |
| 36 | + |
| 37 | +import java.util.Optional; |
| 38 | +import java.time.Duration; |
| 39 | + |
| 40 | +import static org.junit.Assert.assertEquals; |
| 41 | +import static org.junit.Assert.assertTrue; |
| 42 | + |
| 43 | +public class TaskTest extends StreamTest { |
| 44 | + private final Runtime runtime = Runtime.create(Materializer.createMaterializer(system)); |
| 45 | + |
| 46 | + public TaskTest() { |
| 47 | + super(actorSystemResource); |
| 48 | + } |
| 49 | + |
| 50 | + @ClassRule |
| 51 | + public static PekkoJUnitActorSystemResource actorSystemResource = |
| 52 | + new PekkoJUnitActorSystemResource("TaskTest", PekkoSpec.testConf()); |
| 53 | + |
| 54 | + private <T> T run(Task<T> task) throws Exception { |
| 55 | + return runtime.runAsync(task).get(2, TimeUnit.SECONDS); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void can_run_task_from_lambda() throws Exception { |
| 60 | + assertEquals("Hello", run(Task.run(() -> "Hello"))); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void can_map() throws Exception { |
| 65 | + assertEquals(25, run(Task.run(() -> "25").map(Integer::parseInt)).intValue()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void can_flatMap_to_run() throws Exception { |
| 70 | + assertEquals( |
| 71 | + 25, run(Task.run(() -> "25").flatMap(s -> Task.run(() -> Integer.parseInt(s)))).intValue()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + public void can_zipPar_two_tasks() throws Exception { |
| 76 | + Task<String> task = |
| 77 | + Task.run( |
| 78 | + () -> { |
| 79 | + return "Hello"; |
| 80 | + }); |
| 81 | + assertEquals("HelloHello", run(task.zipPar(task, (s1, s2) -> s1 + s2))); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void can_interrupt_forked_task() throws Exception { |
| 86 | + AtomicLong check = new AtomicLong(); |
| 87 | + Task<Long> task = Task.run(() -> check.incrementAndGet()).delayed(Duration.ofMillis(100)); |
| 88 | + run(task.forkDaemon().flatMap(fiber -> fiber.interrupt().map(cancelled -> "cancelled"))); |
| 89 | + assertEquals(0, check.get()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test(expected = ExecutionException.class) |
| 93 | + public void joining_interrupted_fiber_yields_exception() throws Exception { |
| 94 | + Task<Long> task = Task.succeed(42L).delayed(Duration.ofMillis(100)); |
| 95 | + run(task.forkDaemon().flatMap(fiber -> fiber.interrupt().flatMap(cancelled -> fiber.join()))); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void can_run_graph() throws Exception { |
| 100 | + assertEquals( |
| 101 | + Optional.of("hello"), run(Task.connect(Source.single("hello"), Sink.headOption()))); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void can_interrupt_graph() throws Exception { |
| 106 | + AtomicLong check = new AtomicLong(); |
| 107 | + assertEquals( |
| 108 | + Done.getInstance(), |
| 109 | + run( |
| 110 | + Task.connect( |
| 111 | + Source.tick(Duration.ofMillis(1), Duration.ofMillis(1), ""), |
| 112 | + Sink.foreach(s -> check.incrementAndGet())) |
| 113 | + .forkDaemon() |
| 114 | + .flatMap(fiber -> fiber.interrupt()))); |
| 115 | + Thread.sleep(100); |
| 116 | + assertTrue(check.get() < 10); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void resource_is_acquired_and_released() throws Exception { |
| 121 | + AtomicLong check = new AtomicLong(); |
| 122 | + Resource<Long> res = |
| 123 | + Resource.acquireRelease( |
| 124 | + Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet())); |
| 125 | + Task<Long> task = res.use(i -> Task.succeed(i)); |
| 126 | + assertEquals(1L, run(task).longValue()); |
| 127 | + assertEquals(0L, check.get()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void resource_is_released_on_failure() throws Exception { |
| 132 | + AtomicLong check = new AtomicLong(); |
| 133 | + Resource<Long> res = |
| 134 | + Resource.acquireRelease( |
| 135 | + Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet())); |
| 136 | + Task<Long> task = res.use(i -> Task.fail(new RuntimeException("Simulated failure"))); |
| 137 | + try { |
| 138 | + run(task); |
| 139 | + } catch (Exception ignored) { |
| 140 | + } |
| 141 | + assertEquals(0L, check.get()); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void resource_is_released_when_interrupted() throws Exception { |
| 146 | + AtomicLong check = new AtomicLong(); |
| 147 | + AtomicLong started = new AtomicLong(); |
| 148 | + |
| 149 | + Resource<Long> res = |
| 150 | + Resource.acquireRelease( |
| 151 | + Task.run( |
| 152 | + () -> { |
| 153 | + return check.incrementAndGet(); |
| 154 | + }), |
| 155 | + i -> |
| 156 | + Task.run( |
| 157 | + () -> { |
| 158 | + return check.decrementAndGet(); |
| 159 | + })); |
| 160 | + |
| 161 | + Task<Long> task = |
| 162 | + res.use( |
| 163 | + i -> |
| 164 | + Task.run(() -> started.incrementAndGet()) |
| 165 | + .before(Clock.sleep(Duration.ofMillis(100)))); |
| 166 | + run(task.forkDaemon().flatMap(fiber -> fiber.interrupt().delayed(Duration.ofMillis(50)))); |
| 167 | + |
| 168 | + assertEquals(0L, check.get()); |
| 169 | + assertEquals(1L, started.get()); |
| 170 | + } |
| 171 | + |
| 172 | + @Test |
| 173 | + public void resource_can_fork() throws Exception { |
| 174 | + AtomicLong check = new AtomicLong(); |
| 175 | + Resource<Long> res = |
| 176 | + Resource.acquireRelease(Task.run(() -> check.incrementAndGet()), i -> Task.done); |
| 177 | + Task<Long> task = res.fork().use(fiber -> fiber.join()); |
| 178 | + run(task); |
| 179 | + assertEquals(1L, check.get()); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + public void resource_is_released_when_fork_is_interrupted() throws Exception { |
| 184 | + AtomicLong check = new AtomicLong(); |
| 185 | + Resource<Long> res = |
| 186 | + Resource.acquireRelease( |
| 187 | + Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet())); |
| 188 | + Task<Done> task = res.fork().use(fiber -> fiber.interrupt()); |
| 189 | + run(task); |
| 190 | + assertEquals(0L, check.get()); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void resource_is_released_when_fork_is_completed() throws Exception { |
| 195 | + AtomicLong check = new AtomicLong(); |
| 196 | + Resource<Long> res = |
| 197 | + Resource.acquireRelease( |
| 198 | + Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet())); |
| 199 | + Task<Long> task = res.fork().use(fiber -> fiber.join()); |
| 200 | + run(task); |
| 201 | + assertEquals(0L, check.get()); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + public void can_create_and_complete_promise() throws Exception { |
| 206 | + Task<Integer> task = |
| 207 | + Promise.<Integer>make() |
| 208 | + .flatMap( |
| 209 | + promise -> |
| 210 | + promise |
| 211 | + .await() |
| 212 | + .forkDaemon() |
| 213 | + .flatMap(fiber -> promise.succeed(42).andThen(fiber.join()))); |
| 214 | + assertEquals(42, run(task).intValue()); |
| 215 | + } |
| 216 | + |
| 217 | + @Test |
| 218 | + public void can_race_two_tasks() throws Exception { |
| 219 | + Task<Integer> task1 = Task.succeed(0).delayed(Duration.ofMillis(100)); |
| 220 | + Task<Integer> task2 = Task.succeed(42); |
| 221 | + Task<Integer> task = Task.raceAll(task1, task2); |
| 222 | + assertEquals(42, run(task).intValue()); |
| 223 | + } |
| 224 | +} |
0 commit comments