pjfanning commented on code in PR #1802:
URL: https://github.com/apache/pekko/pull/1802#discussion_r2072167625


##########
stream-tests/src/test/java/org/apache/pekko/task/javadsl/TaskTest.java:
##########
@@ -0,0 +1,242 @@
+/*
+ * 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));

Review Comment:
   can you format the file? `sbt javafmtAll`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@pekko.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@pekko.apache.org
For additional commands, e-mail: notifications-h...@pekko.apache.org

Reply via email to