|
1 | | -/** |
| 1 | +/* |
2 | 2 | * Licensed to the Apache Software Foundation (ASF) under one |
3 | 3 | * or more contributor license agreements. See the NOTICE file |
4 | 4 | * distributed with this work for additional information |
|
25 | 25 | import java.util.EnumMap; |
26 | 26 | import java.util.List; |
27 | 27 | import java.util.Map; |
| 28 | +import java.util.concurrent.CompletableFuture; |
| 29 | +import java.util.concurrent.ExecutionException; |
28 | 30 |
|
29 | 31 | import static org.apache.ratis.util.LifeCycle.State.*; |
30 | 32 | import static org.junit.jupiter.api.Assertions.assertEquals; |
31 | 33 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 34 | +import static org.junit.jupiter.api.Assertions.assertSame; |
32 | 35 | import static org.junit.jupiter.api.Assertions.assertTrue; |
33 | 36 |
|
34 | 37 |
|
@@ -101,4 +104,68 @@ private static void testInvalidTransition(TriConsumer<LifeCycle.State, LifeCycle |
101 | 104 | } |
102 | 105 | } |
103 | 106 |
|
| 107 | + @Test |
| 108 | + public void testStartAndTransition() throws Exception { |
| 109 | + final SimulatedServer simulatedServer = new SimulatedServer(); |
| 110 | + assertEquals(NEW, simulatedServer.getLifeCycleState()); |
| 111 | + |
| 112 | + final CompletableFuture<Throwable> f = CompletableFuture.supplyAsync(() -> { |
| 113 | + try { |
| 114 | + simulatedServer.start(); |
| 115 | + throw new AssertionError("start() should fail"); |
| 116 | + } catch (Exception e) { |
| 117 | + return e.getCause(); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + Thread.sleep(100); |
| 122 | + assertEquals(STARTING, simulatedServer.getLifeCycleState()); |
| 123 | + |
| 124 | + // call close() during STARTING, start() should throw the simulated exception |
| 125 | + CompletableFuture.supplyAsync(simulatedServer::close); |
| 126 | + assertSame(simulatedServer.getSimulatedException(), f.get()); |
| 127 | + |
| 128 | + assertEquals(CLOSING, simulatedServer.getLifeCycleState()); |
| 129 | + simulatedServer.getCloseFuture().complete(null); |
| 130 | + Thread.sleep(100); |
| 131 | + assertEquals(CLOSED, simulatedServer.getLifeCycleState()); |
| 132 | + } |
| 133 | + |
| 134 | + private static final class SimulatedServer { |
| 135 | + private final LifeCycle lifeCycle = new LifeCycle(getClass().getSimpleName()); |
| 136 | + private final Exception simulatedException = new Exception("Simulated exception"); |
| 137 | + private final CompletableFuture<Void> startFuture = new CompletableFuture<>(); |
| 138 | + private final CompletableFuture<Void> closeFuture = new CompletableFuture<>(); |
| 139 | + |
| 140 | + LifeCycle.State getLifeCycleState() { |
| 141 | + return lifeCycle.getCurrentState(); |
| 142 | + } |
| 143 | + |
| 144 | + Exception getSimulatedException() { |
| 145 | + return simulatedException; |
| 146 | + } |
| 147 | + |
| 148 | + CompletableFuture<Void> getCloseFuture() { |
| 149 | + return closeFuture; |
| 150 | + } |
| 151 | + |
| 152 | + void start() throws Exception { |
| 153 | + lifeCycle.startAndTransition(this::startImpl); |
| 154 | + } |
| 155 | + |
| 156 | + void startImpl() throws Exception { |
| 157 | + startFuture.get(); |
| 158 | + } |
| 159 | + |
| 160 | + Void close() { |
| 161 | + // simulate close and then cause start() to fail. |
| 162 | + lifeCycle.checkStateAndClose(this::closeImpl); |
| 163 | + return null; |
| 164 | + } |
| 165 | + |
| 166 | + void closeImpl() { |
| 167 | + startFuture.completeExceptionally(simulatedException); |
| 168 | + closeFuture.join(); |
| 169 | + } |
| 170 | + } |
104 | 171 | } |
0 commit comments