|
35 | 35 | import com.alipay.sofa.jraft.core.State; |
36 | 36 | import com.alipay.sofa.jraft.entity.LeaderChangeContext; |
37 | 37 | import com.alipay.sofa.jraft.entity.PeerId; |
| 38 | +import com.alipay.sofa.jraft.error.RaftException; |
38 | 39 |
|
39 | 40 | /** |
40 | 41 | * Pins the status a readiness probe actually receives from {@code GET /v1/ready}: 503 while |
@@ -120,6 +121,44 @@ private static LeaderChangeContext context() { |
120 | 121 | return new LeaderChangeContext(LEADER, 5, Status.OK()); |
121 | 122 | } |
122 | 123 |
|
| 124 | + @Test |
| 125 | + public void testFatalErrorSurvivesLeaderStopAndAnswers503() { |
| 126 | + Whitebox.setInternalState(RaftEngine.getInstance(), "raftNode", untouchableNode()); |
| 127 | + this.stateMachine.onLeaderStart(5); |
| 128 | + this.stateMachine.onError(new RaftException()); |
| 129 | + this.stateMachine.onLeaderStop(new Status(-1, "snapshot failed")); |
| 130 | + |
| 131 | + Assert.assertEquals(HttpStatus.SERVICE_UNAVAILABLE, |
| 132 | + this.api.checkHealthy().getStatusCode()); |
| 133 | + Assert.assertEquals("", this.api.checkHealthy().getBody()); |
| 134 | + ResponseEntity<Map<String, Object>> response = this.api.checkReady(); |
| 135 | + Assert.assertEquals(HttpStatus.SERVICE_UNAVAILABLE, response.getStatusCode()); |
| 136 | + Assert.assertEquals(State.STATE_ERROR.name(), response.getBody().get("state")); |
| 137 | + Assert.assertEquals(Boolean.FALSE, response.getBody().get("isLeader")); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testQuorumLossRemainsLive() { |
| 142 | + Whitebox.setInternalState(RaftEngine.getInstance(), "raftNode", untouchableNode()); |
| 143 | + this.stateMachine.onLeaderStart(5); |
| 144 | + Assert.assertEquals(HttpStatus.OK, this.api.checkHealthy().getStatusCode()); |
| 145 | + this.stateMachine.onLeaderStop(Status.OK()); |
| 146 | + Assert.assertEquals(HttpStatus.OK, this.api.checkHealthy().getStatusCode()); |
| 147 | + this.stateMachine.onStartFollowing(context()); |
| 148 | + Assert.assertEquals(HttpStatus.OK, this.api.checkHealthy().getStatusCode()); |
| 149 | + this.stateMachine.onStopFollowing(context()); |
| 150 | + Assert.assertEquals(HttpStatus.OK, this.api.checkHealthy().getStatusCode()); |
| 151 | + Assert.assertEquals(HttpStatus.SERVICE_UNAVAILABLE, |
| 152 | + this.api.checkReady().getStatusCode()); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + public void testShutdownIsNotLive() { |
| 157 | + this.stateMachine.onShutdown(); |
| 158 | + Assert.assertEquals(HttpStatus.SERVICE_UNAVAILABLE, |
| 159 | + this.api.checkHealthy().getStatusCode()); |
| 160 | + } |
| 161 | + |
123 | 162 | /** |
124 | 163 | * A started raft node that fails the test if the endpoint calls it. Readiness is served |
125 | 164 | * from the state machine callbacks precisely so that it never waits on the node lock. |
|
0 commit comments