|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +package org.swift.swiftkit.core; |
| 16 | + |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | + |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.concurrent.*; |
| 21 | +import static org.junit.jupiter.api.Assertions.*; |
| 22 | + |
| 23 | +public class SimpleCompletableFutureTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + void testCompleteAndGet() throws ExecutionException, InterruptedException { |
| 27 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 28 | + assertFalse(future.isDone()); |
| 29 | + assertTrue(future.complete("test")); |
| 30 | + assertTrue(future.isDone()); |
| 31 | + assertEquals("test", future.get()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + void testCompleteWithNullAndGet() throws ExecutionException, InterruptedException { |
| 36 | + SimpleCompletableFuture<Void> future = new SimpleCompletableFuture<>(); |
| 37 | + assertFalse(future.isDone()); |
| 38 | + assertTrue(future.complete(null)); |
| 39 | + assertTrue(future.isDone()); |
| 40 | + assertNull(future.get()); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testCompleteExceptionallyAndGet() { |
| 45 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 46 | + RuntimeException ex = new RuntimeException("Test Exception"); |
| 47 | + assertTrue(future.completeExceptionally(ex)); |
| 48 | + assertTrue(future.isDone()); |
| 49 | + |
| 50 | + ExecutionException thrown = assertThrows(ExecutionException.class, future::get); |
| 51 | + assertEquals(ex, thrown.getCause()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testGetWithTimeout_timesOut() { |
| 56 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 57 | + assertThrows(TimeoutException.class, () -> future.get(10, TimeUnit.MILLISECONDS)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testGetWithTimeout_completesInTime() throws ExecutionException, InterruptedException, TimeoutException { |
| 62 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 63 | + future.complete("fast"); |
| 64 | + assertEquals("fast", future.get(10, TimeUnit.MILLISECONDS)); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void testGetWithTimeout_completesInTimeAfterWait() throws Exception { |
| 69 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 70 | + Thread t = new Thread(() -> { |
| 71 | + try { |
| 72 | + Thread.sleep(50); |
| 73 | + } catch (InterruptedException e) { |
| 74 | + // ignore |
| 75 | + } |
| 76 | + future.complete("late"); |
| 77 | + }); |
| 78 | + t.start(); |
| 79 | + assertEquals("late", future.get(200, TimeUnit.MILLISECONDS)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void testThenApply() throws ExecutionException, InterruptedException { |
| 84 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 85 | + Future<Integer> mapped = future.thenApply(String::length); |
| 86 | + |
| 87 | + future.complete("hello"); |
| 88 | + |
| 89 | + assertEquals(5, mapped.get()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void testThenApplyOnCompletedFuture() throws ExecutionException, InterruptedException { |
| 94 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 95 | + future.complete("done"); |
| 96 | + |
| 97 | + Future<Integer> mapped = future.thenApply(String::length); |
| 98 | + |
| 99 | + assertEquals(4, mapped.get()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void testThenApplyWithNull() throws ExecutionException, InterruptedException { |
| 104 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 105 | + Future<Boolean> mapped = future.thenApply(Objects::isNull); |
| 106 | + |
| 107 | + future.complete(null); |
| 108 | + |
| 109 | + assertTrue(mapped.get()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void testThenApplyExceptionally() { |
| 114 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 115 | + RuntimeException ex = new RuntimeException("Initial Exception"); |
| 116 | + Future<Integer> mapped = future.thenApply(String::length); |
| 117 | + |
| 118 | + future.completeExceptionally(ex); |
| 119 | + |
| 120 | + ExecutionException thrown = assertThrows(ExecutionException.class, mapped::get); |
| 121 | + assertEquals(ex, thrown.getCause()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void testThenApplyTransformationThrows() { |
| 126 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 127 | + RuntimeException ex = new RuntimeException("Transformation Exception"); |
| 128 | + Future<Integer> mapped = future.thenApply(s -> { |
| 129 | + throw ex; |
| 130 | + }); |
| 131 | + |
| 132 | + future.complete("hello"); |
| 133 | + |
| 134 | + ExecutionException thrown = assertThrows(ExecutionException.class, mapped::get); |
| 135 | + assertEquals(ex, thrown.getCause()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void testCompleteTwice() throws ExecutionException, InterruptedException { |
| 140 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 141 | + |
| 142 | + assertTrue(future.complete("first")); |
| 143 | + assertFalse(future.complete("second")); |
| 144 | + |
| 145 | + assertEquals("first", future.get()); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void testCompleteThenCompleteExceptionally() throws ExecutionException, InterruptedException { |
| 150 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 151 | + |
| 152 | + assertTrue(future.complete("first")); |
| 153 | + assertFalse(future.completeExceptionally(new RuntimeException("second"))); |
| 154 | + |
| 155 | + assertEquals("first", future.get()); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + void testCompleteExceptionallyThenComplete() { |
| 160 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 161 | + RuntimeException ex = new RuntimeException("first"); |
| 162 | + |
| 163 | + assertTrue(future.completeExceptionally(ex)); |
| 164 | + assertFalse(future.complete("second")); |
| 165 | + |
| 166 | + ExecutionException thrown = assertThrows(ExecutionException.class, future::get); |
| 167 | + assertEquals(ex, thrown.getCause()); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + void testIsDone() { |
| 172 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 173 | + assertFalse(future.isDone()); |
| 174 | + future.complete("done"); |
| 175 | + assertTrue(future.isDone()); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + void testIsDoneExceptionally() { |
| 180 | + SimpleCompletableFuture<String> future = new SimpleCompletableFuture<>(); |
| 181 | + assertFalse(future.isDone()); |
| 182 | + future.completeExceptionally(new RuntimeException()); |
| 183 | + assertTrue(future.isDone()); |
| 184 | + } |
| 185 | +} |
0 commit comments