-
Notifications
You must be signed in to change notification settings - Fork 193
Introduce Task monad #1802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jypma
wants to merge
1
commit into
apache:main
Choose a base branch
from
jypma:jyp-task
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Introduce Task monad #1802
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
270 changes: 270 additions & 0 deletions
270
stream-tests/src/test/java/org/apache/pekko/task/javadsl/TaskTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,270 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.pekko.task.javadsl; | ||
|
|
||
| import org.apache.pekko.stream.StreamTest; | ||
| import org.apache.pekko.testkit.PekkoJUnitActorSystemResource; | ||
| import org.apache.pekko.testkit.PekkoSpec; | ||
| import org.apache.pekko.stream.Materializer; | ||
| import org.apache.pekko.Done; | ||
|
|
||
| import org.junit.ClassRule; | ||
| import org.junit.Test; | ||
|
|
||
| import org.apache.pekko.japi.function.Creator; | ||
| import org.apache.pekko.stream.javadsl.Sink; | ||
| import org.apache.pekko.stream.javadsl.Source; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| import java.util.Optional; | ||
| import java.time.Duration; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| public class TaskTest extends StreamTest { | ||
| private final Runtime runtime = Runtime.create(Materializer.createMaterializer(system)); | ||
|
|
||
| public TaskTest() { | ||
| super(actorSystemResource); | ||
| } | ||
|
|
||
| @ClassRule | ||
| public static PekkoJUnitActorSystemResource actorSystemResource = | ||
| new PekkoJUnitActorSystemResource("TaskTest", PekkoSpec.testConf()); | ||
|
|
||
| private <T> T run(Task<T> task) throws Throwable { | ||
| return runtime.run(task.timeout(Duration.ofSeconds(2))); | ||
| } | ||
|
|
||
| @Test | ||
| public void can_run_task_from_lambda() throws Throwable { | ||
| assertEquals("Hello", run(Task.run(() -> "Hello"))); | ||
| } | ||
|
|
||
| @Test | ||
| public void can_map() throws Throwable { | ||
| assertEquals(25, run(Task.run(() -> "25").map(Integer::parseInt)).intValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void can_flatMap_to_run() throws Throwable { | ||
| assertEquals( | ||
| 25, run(Task.run(() -> "25").flatMap(s -> Task.run(() -> Integer.parseInt(s)))).intValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void can_zipPar_two_tasks() throws Throwable { | ||
| Task<String> task = | ||
| Task.run( | ||
| () -> { | ||
| return "Hello"; | ||
| }); | ||
| assertEquals("HelloHello", run(task.zipPar(task, (s1, s2) -> s1 + s2))); | ||
| } | ||
|
|
||
| @Test | ||
| public void zipPar_interrupts_first_on_error_in_second() throws Throwable { | ||
| AtomicLong check = new AtomicLong(); | ||
| Task<String> task1 = | ||
| Task.succeed("A").delayed(Duration.ofMillis(100)).before(Task.run(check::incrementAndGet)); | ||
| Task<String> task2 = Task.fail(new RuntimeException("simulated failure")); | ||
| org.junit.Assert.assertThrows( | ||
| RuntimeException.class, () -> run(task1.zipPar(task2, (a, b) -> a + b))); | ||
| assertEquals(0, check.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void zipPar_interrupts_second_on_error_in_first() throws Throwable { | ||
| AtomicLong check = new AtomicLong(); | ||
| Task<String> task1 = | ||
| Task.succeed("A").delayed(Duration.ofMillis(100)).before(Task.run(check::incrementAndGet)); | ||
| Task<String> task2 = Task.fail(new RuntimeException("simulated failure")); | ||
| org.junit.Assert.assertThrows( | ||
| RuntimeException.class, () -> run(task2.zipPar(task1, (a, b) -> a + b))); | ||
| assertEquals(0, check.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void can_interrupt_forked_task() throws Throwable { | ||
| AtomicLong check = new AtomicLong(); | ||
| Task<Long> task = Task.run(() -> check.incrementAndGet()).delayed(Duration.ofMillis(100)); | ||
| run(task.forkDaemon().flatMap(fiber -> fiber.interrupt().map(cancelled -> "cancelled"))); | ||
| assertEquals(0, check.get()); | ||
| } | ||
|
|
||
| @Test(expected = InterruptedException.class) | ||
| public void joining_interrupted_fiber_yields_exception() throws Throwable { | ||
| Task<Long> task = Task.succeed(42L).delayed(Duration.ofMillis(100)); | ||
| run(task.forkDaemon().flatMap(fiber -> fiber.interrupt().flatMap(cancelled -> fiber.join()))); | ||
| } | ||
|
|
||
| @Test | ||
| public void can_run_graph() throws Throwable { | ||
| assertEquals( | ||
| Optional.of("hello"), run(Task.connect(Source.single("hello"), Sink.headOption()))); | ||
| } | ||
|
|
||
| @Test | ||
| public void can_interrupt_graph() throws Throwable { | ||
| AtomicLong check = new AtomicLong(); | ||
| assertEquals( | ||
| Done.getInstance(), | ||
| run( | ||
| Task.connect( | ||
| Source.tick(Duration.ofMillis(1), Duration.ofMillis(1), ""), | ||
| Sink.foreach(s -> check.incrementAndGet())) | ||
| .forkDaemon() | ||
| .flatMap(fiber -> fiber.interrupt()))); | ||
| Thread.sleep(100); | ||
| assertTrue(check.get() < 10); | ||
| } | ||
|
|
||
| @Test | ||
| public void resource_is_acquired_and_released() throws Throwable { | ||
| AtomicLong check = new AtomicLong(); | ||
| Resource<Long> res = | ||
| Resource.acquireRelease( | ||
| Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet())); | ||
| Task<Long> task = res.use(i -> Task.succeed(i)); | ||
| assertEquals(1L, run(task).longValue()); | ||
| assertEquals(0L, check.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void resource_is_released_on_failure() throws Throwable { | ||
| AtomicLong check = new AtomicLong(); | ||
| Resource<Long> res = | ||
| Resource.acquireRelease( | ||
| Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet())); | ||
| Task<Long> task = res.use(i -> Task.fail(new RuntimeException("Simulated failure"))); | ||
| try { | ||
| run(task); | ||
| } catch (Exception ignored) { | ||
| } | ||
| assertEquals(0L, check.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void resource_closes_AutoCloseable() throws Throwable { | ||
| AtomicLong created = new AtomicLong(); | ||
| AtomicLong closed = new AtomicLong(); | ||
| Resource<AutoCloseable> res = | ||
| Resource.autoCloseable( | ||
| Task.run( | ||
| () -> { | ||
| created.incrementAndGet(); | ||
| return () -> closed.incrementAndGet(); | ||
| })); | ||
| run(res.use(ac -> Task.done)); | ||
| assertEquals(1L, created.get()); | ||
| assertEquals(1L, closed.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void resource_is_released_when_interrupted() throws Throwable { | ||
| AtomicLong check = new AtomicLong(); | ||
| AtomicLong started = new AtomicLong(); | ||
|
|
||
| Resource<Long> res = | ||
| Resource.acquireRelease( | ||
| Task.run( | ||
| () -> { | ||
| return check.incrementAndGet(); | ||
| }), | ||
| i -> | ||
| Task.run( | ||
| () -> { | ||
| return check.decrementAndGet(); | ||
| })); | ||
|
|
||
| Task<Long> task = | ||
| res.use( | ||
| i -> | ||
| Task.run(() -> started.incrementAndGet()) | ||
| .before(Clock.sleep(Duration.ofMillis(100)))); | ||
| run(task.forkDaemon().flatMap(fiber -> fiber.interrupt().delayed(Duration.ofMillis(50)))); | ||
|
|
||
| assertEquals(0L, check.get()); | ||
| assertEquals(1L, started.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void resource_can_fork() throws Throwable { | ||
| AtomicLong check = new AtomicLong(); | ||
| Resource<Long> res = | ||
| Resource.acquireRelease(Task.run(() -> check.incrementAndGet()), i -> Task.done); | ||
| Task<Long> task = res.fork().use(fiber -> fiber.join()); | ||
| run(task); | ||
| assertEquals(1L, check.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void resource_is_released_when_fork_is_interrupted() throws Throwable { | ||
| AtomicLong check = new AtomicLong(); | ||
| Resource<Long> res = | ||
| Resource.acquireRelease( | ||
| Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet())); | ||
| Task<Done> task = res.fork().use(fiber -> fiber.interrupt()); | ||
| run(task); | ||
| assertEquals(0L, check.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void resource_is_released_when_fork_is_completed() throws Throwable { | ||
| AtomicLong check = new AtomicLong(); | ||
| Resource<Long> res = | ||
| Resource.acquireRelease( | ||
| Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet())); | ||
| Task<Long> task = res.fork().use(fiber -> fiber.join()); | ||
| run(task); | ||
| assertEquals(0L, check.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void can_create_and_complete_promise() throws Throwable { | ||
| Task<Integer> task = | ||
| Promise.<Integer>make() | ||
| .flatMap( | ||
| promise -> | ||
| promise | ||
| .await() | ||
| .forkDaemon() | ||
| .flatMap(fiber -> promise.succeed(42).andThen(fiber.join()))); | ||
| assertEquals(42, run(task).intValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void can_race_two_tasks() throws Throwable { | ||
| Task<Integer> task1 = Task.succeed(0).delayed(Duration.ofMillis(100)); | ||
| Task<Integer> task2 = Task.succeed(42); | ||
| Task<Integer> task = Task.raceAll(task1, task2); | ||
| assertEquals(42, run(task).intValue()); | ||
| } | ||
|
|
||
| @Test | ||
| public void can_race_task_with_never() throws Throwable { | ||
| Task<Integer> task1 = Task.succeed(42).delayed(Duration.ofMillis(100)); | ||
| Task<Integer> task2 = Task.never(); | ||
| Task<Integer> task = Task.raceAll(task1, task2); | ||
| assertEquals(42, run(task).intValue()); | ||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
stream/src/main/java/org/apache/pekko/task/javadsl/Clock.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.pekko.task.javadsl; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
|
|
||
| import org.apache.pekko.task.GetRuntimeDef$; | ||
| import org.apache.pekko.task.ClockDef$; | ||
| import org.apache.pekko.task.TaskDef; | ||
| import org.apache.pekko.Done; | ||
|
|
||
| public class Clock { | ||
| public static final Task<Long> nanoTime = new Task<>(ClockDef$.MODULE$.nanoTime()); | ||
| public static final Task<Instant> now = new Task<>(ClockDef$.MODULE$.now()); | ||
|
|
||
| public static Task<Done> sleep(Duration duration) { | ||
| return new Task<>(ClockDef$.MODULE$.sleep(duration)).asDone(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.