use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class LogEntryV2CodecFactoryTest method createLearners.
private List<PeerId> createLearners(final String... peers) {
List<PeerId> ret = new ArrayList<>();
for (String s : peers) {
PeerId e = new PeerId();
e.parse(s);
ret.add(e);
}
return ret;
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class LogEntryV2CodecFactoryTest method testDecodeV1LogEntry.
@Test
public void testDecodeV1LogEntry() {
ByteBuffer buf = ByteBuffer.wrap("hello".getBytes());
LogEntry entry = new LogEntry(EnumOutter.EntryType.ENTRY_TYPE_NO_OP);
entry.setId(new LogId(100, 3));
entry.setData(buf);
entry.setPeers(Arrays.asList(new PeerId("localhost", 99, 1), new PeerId("localhost", 100, 2)));
assertEquals(buf, entry.getData());
byte[] content = V1Encoder.INSTANCE.encode(entry);
assertNotNull(content);
assertTrue(content.length > 0);
// Decode by auto detect decoder
LogEntry nentry = this.decoder.decode(content);
assertNotNull(nentry);
assertEquals(100, nentry.getId().getIndex());
assertEquals(3, nentry.getId().getTerm());
assertEquals(2, nentry.getPeers().size());
assertEquals("localhost:99:1", nentry.getPeers().get(0).toString());
assertEquals("localhost:100:2", nentry.getPeers().get(1).toString());
assertEquals(buf, nentry.getData());
assertEquals(0, nentry.getData().position());
assertEquals(5, nentry.getData().remaining());
assertNull(nentry.getOldPeers());
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class ConfigurationTest method testCopy.
@Test
public void testCopy() {
final Configuration conf = JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083");
final Configuration copied = conf.copy();
assertEquals(conf, copied);
assertNotSame(conf, copied);
assertEquals(copied.size(), 3);
assertEquals("localhost:8081,localhost:8082,localhost:8083", copied.toString());
final PeerId newPeer = new PeerId("localhost", 8084);
conf.addPeer(newPeer);
assertEquals(copied.size(), 3);
assertEquals(conf.size(), 4);
assertTrue(conf.contains(newPeer));
assertFalse(copied.contains(newPeer));
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class ConfigurationTest method testToStringParseStuffWithPriority.
@Test
public void testToStringParseStuffWithPriority() {
final String confStr = "localhost:8081:1:100,localhost:8082:1:100,localhost:8083:1:100";
final Configuration conf = JRaftUtils.getConfiguration(confStr);
assertEquals(3, conf.size());
for (final PeerId peer : conf) {
assertTrue(peer.toString().startsWith("localhost:80"));
assertEquals(100, peer.getPriority());
assertEquals(1, peer.getIdx());
}
assertFalse(conf.isEmpty());
assertEquals(confStr, conf.toString());
final Configuration newConf = new Configuration();
assertTrue(newConf.parse(conf.toString()));
assertEquals(3, newConf.getPeerSet().size());
assertTrue(newConf.contains(new PeerId("localhost", 8081, 1, 100)));
assertTrue(newConf.contains(new PeerId("localhost", 8082, 1, 100)));
assertTrue(newConf.contains(new PeerId("localhost", 8083, 1, 100)));
assertEquals(confStr, newConf.toString());
assertEquals(conf.hashCode(), newConf.hashCode());
assertEquals(conf, newConf);
}
use of com.alipay.sofa.jraft.entity.PeerId in project sofa-jraft by sofastack.
the class BallotBoxTest method testCommitAt.
@Test
public void testCommitAt() {
assertFalse(this.box.commitAt(1, 3, new PeerId("localhost", 8081)));
assertTrue(box.resetPendingIndex(1));
assertTrue(this.box.appendPendingTask(JRaftUtils.getConfiguration("localhost:8081,localhost:8082,localhost:8083"), JRaftUtils.getConfiguration("localhost:8081"), new Closure() {
@Override
public void run(Status status) {
}
}));
assertEquals(0, this.box.getLastCommittedIndex());
try {
this.box.commitAt(1, 3, new PeerId("localhost", 8081));
fail();
} catch (ArrayIndexOutOfBoundsException e) {
}
assertTrue(this.box.commitAt(1, 1, new PeerId("localhost", 8081)));
assertEquals(0, this.box.getLastCommittedIndex());
assertEquals(1, this.box.getPendingIndex());
assertTrue(this.box.commitAt(1, 1, new PeerId("localhost", 8082)));
assertEquals(1, this.box.getLastCommittedIndex());
assertEquals(2, this.box.getPendingIndex());
Mockito.verify(this.waiter, Mockito.only()).onCommitted(1);
}
Aggregations