use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class LogManagerTest method testGetConfiguration.
@Test
public void testGetConfiguration() throws Exception {
assertTrue(this.logManager.getConfiguration(1).isEmpty());
final List<LogEntry> entries = new ArrayList<>(2);
final LogEntry confEntry1 = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
confEntry1.setId(new LogId(0, 1));
confEntry1.setPeers(JRaftUtils.getConfiguration("localhost:8081,localhost:8082").listPeers());
final LogEntry confEntry2 = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_CONFIGURATION);
confEntry2.setId(new LogId(0, 2));
confEntry2.setPeers(JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083").listPeers());
confEntry2.setOldPeers(confEntry1.getPeers());
entries.add(confEntry1);
entries.add(confEntry2);
final CountDownLatch latch = new CountDownLatch(1);
this.logManager.appendEntries(new ArrayList<>(entries), new LogManager.StableClosure() {
@Override
public void run(final Status status) {
assertTrue(status.isOk());
latch.countDown();
}
});
latch.await();
ConfigurationEntry entry = this.logManager.getConfiguration(1);
assertEquals("localhost:8081,localhost:8082", entry.getConf().toString());
assertTrue(entry.getOldConf().isEmpty());
entry = this.logManager.getConfiguration(2);
assertEquals("localhost:8081,localhost:8082,localhost:8083", entry.getConf().toString());
assertEquals("localhost:8081,localhost:8082", entry.getOldConf().toString());
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class LogManagerTest method mockAddEntries.
private List<LogEntry> mockAddEntries() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final List<LogEntry> mockEntries = TestUtils.mockEntries(10);
this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {
@Override
public void run(final Status status) {
assertTrue(status.isOk());
latch.countDown();
}
});
latch.await();
return mockEntries;
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class ProtobufSerializerTest method testEncodeDecodeResponseContent.
@Test
public void testEncodeDecodeResponseContent() throws Exception {
final PingRequest reqObject = TestUtils.createPingRequest();
final RpcRequestCommand request = cmdFactory.createRequestCommand(reqObject);
final ErrorResponse respObject = (ErrorResponse) RpcFactoryHelper.responseFactory().newResponse(null, new Status(-1, "test"));
final RpcResponseCommand response = cmdFactory.createResponse(respObject, request);
response.setResponseClass(ErrorResponse.class.getName());
assertTrue(serializer.serializeContent(response));
response.setResponseObject(null);
assertTrue(serializer.deserializeContent(response, null));
assertNotNull(response.getResponseObject());
assertEquals(respObject, response.getResponseObject());
assertNotSame(respObject, response.getResponseObject());
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class RpcResponseFactoryTest method testNewResponseWithErrorStatus.
@Test
public void testNewResponseWithErrorStatus() {
ErrorResponse response = (ErrorResponse) RpcFactoryHelper.responseFactory().newResponse(null, new Status(300, "test"));
assertEquals(response.getErrorCode(), 300);
assertEquals(response.getErrorMsg(), "test");
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class LogManagerTest method testAppendEntresConflicts.
@Test
public void testAppendEntresConflicts() throws Exception {
// Append 0-10
List<LogEntry> mockEntries = TestUtils.mockEntries(10);
for (int i = 0; i < 10; i++) {
mockEntries.get(i).getId().setTerm(1);
}
final CountDownLatch latch1 = new CountDownLatch(1);
this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {
@Override
public void run(final Status status) {
assertTrue(status.isOk());
latch1.countDown();
}
});
latch1.await();
assertEquals(1, this.logManager.getFirstLogIndex());
assertEquals(10, this.logManager.getLastLogIndex());
// Append 11-20
final CountDownLatch latch2 = new CountDownLatch(1);
mockEntries = TestUtils.mockEntries(10);
for (int i = 0; i < 10; i++) {
mockEntries.get(i).getId().setIndex(11 + i);
mockEntries.get(i).getId().setTerm(1);
}
this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {
@Override
public void run(final Status status) {
assertTrue(status.isOk());
latch2.countDown();
}
});
latch2.await();
assertEquals(1, this.logManager.getFirstLogIndex());
assertEquals(20, this.logManager.getLastLogIndex());
// Re-adds 11-30, but 15 has different term, it will truncate [14,lastIndex] logs
mockEntries = TestUtils.mockEntries(20);
for (int i = 0; i < 20; i++) {
if (11 + i >= 15) {
mockEntries.get(i).getId().setTerm(2);
} else {
mockEntries.get(i).getId().setTerm(1);
}
mockEntries.get(i).getId().setIndex(11 + i);
}
final CountDownLatch latch3 = new CountDownLatch(1);
this.logManager.appendEntries(new ArrayList<>(mockEntries), new LogManager.StableClosure() {
@Override
public void run(final Status status) {
assertTrue(status.isOk());
latch3.countDown();
}
});
latch3.await();
assertEquals(1, this.logManager.getFirstLogIndex());
assertEquals(30, this.logManager.getLastLogIndex());
for (int i = 0; i < 30; i++) {
final LogEntry entry = (this.logManager.getEntry(i + 1));
assertEquals(i + 1, entry.getId().getIndex());
if (i + 1 >= 15) {
assertEquals(2, entry.getId().getTerm());
} else {
assertEquals(1, entry.getId().getTerm());
}
}
}
Aggregations