|
| 1 | +package io.github.opencubicchunks.cubicchunks.test.server.level; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertSame; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | +import static org.mockito.Mockito.mock; |
| 9 | + |
| 10 | +import com.mojang.datafixers.util.Either; |
| 11 | +import io.github.opencubicchunks.cubicchunks.server.level.CloCollectorFuture; |
| 12 | +import io.github.opencubicchunks.cubicchunks.testutils.BaseTest; |
| 13 | +import io.github.opencubicchunks.cubicchunks.world.level.chunklike.CloAccess; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.TestInstance; |
| 16 | + |
| 17 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 18 | +public class TestCloCollectorFuture extends BaseTest { |
| 19 | + @Test public void testFutureCompletion() throws Exception { |
| 20 | + for (int i = 0; i < 9; i++) { // Run nine times, putting the center cube at each index |
| 21 | + var future = new CloCollectorFuture(9); |
| 22 | + CloAccess centerCube = mock(); |
| 23 | + CloAccess others = mock(); |
| 24 | + for (int j = 0; j < 9; j++) { |
| 25 | + assertFalse(future.isDone(), "future shouldn't complete until all Clos are added"); |
| 26 | + future.add(Either.left(j == i ? centerCube : others), null, j == i); |
| 27 | + } |
| 28 | + assertTrue(future.isDone(), "future should be complete once all Clos are added"); |
| 29 | + assertSame(future.get().get(4).left().get(), centerCube, "centerCube should be at center index"); |
| 30 | + for (int j = 0; j < 9; j++) { |
| 31 | + if (j == 4) continue; |
| 32 | + assertNotSame(future.get().get(j).left().get(), centerCube, "reference to centerCube should not be duplicated at any other index"); |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Test public void testMultipleCenterCubes() { |
| 38 | + var future = new CloCollectorFuture(9); |
| 39 | + CloAccess cloAccess = mock(); |
| 40 | + future.add(Either.left(cloAccess), null, true); |
| 41 | + assertThrows(IllegalStateException.class, () -> future.add(Either.left(cloAccess), null, true), "trying to set centerCube multiple times should throw an exception"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test public void testMissingCenterCube() { |
| 45 | + var future = new CloCollectorFuture(9); |
| 46 | + CloAccess cloAccess = mock(); |
| 47 | + for (int i = 0; i < 8; i++) { |
| 48 | + future.add(Either.left(cloAccess), null, false); |
| 49 | + } |
| 50 | + assertThrows(IllegalStateException.class, () -> future.add(Either.left(cloAccess), null, false), "completing without having set centerCube should throw an exception"); |
| 51 | + } |
| 52 | + |
| 53 | + @Test public void testFutureExceptionPropagation() { |
| 54 | + for (int i = 0; i < 9; i++) { // Run nine times, throwing an exception at each index |
| 55 | + var future = new CloCollectorFuture(9); |
| 56 | + var exception = new Exception("test exception"); |
| 57 | + CloAccess cloAccess = mock(); |
| 58 | + for (int j = 0; j < 9; j++) { |
| 59 | + future.add(Either.left(cloAccess), i == j ? exception : null, false); |
| 60 | + } |
| 61 | + assertTrue(future.isCompletedExceptionally(), "future should complete exceptionally upon receiving an Exception"); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments